diff --git a/src/ByteSizeLib.Tests/ArithmeticMethods.cs b/src/ByteSizeLib.Tests/ArithmeticMethods.cs index fe301b5..3317cea 100644 --- a/src/ByteSizeLib.Tests/ArithmeticMethods.cs +++ b/src/ByteSizeLib.Tests/ArithmeticMethods.cs @@ -1,5 +1,4 @@ -using System.Linq; -using Xunit; +using Xunit; namespace ByteSizeLib.Tests { @@ -42,17 +41,6 @@ public void SubtractMethod() Assert.Equal(2, size.Bytes); } - [Fact] - public void IncrementOperator() - { - var size = ByteSize.FromBytes(2); - size++; - - Assert.Equal(24, size.Bits); - Assert.Equal(3, size.Bytes); - } - - [Fact] public void MinusOperatorUnary() { @@ -73,16 +61,6 @@ public void MinusOperatorBinary() Assert.Equal(2, size.Bytes); } - [Fact] - public void DecrementOperator() - { - var size = ByteSize.FromBytes(2); - size--; - - Assert.Equal(8, size.Bits); - Assert.Equal(1, size.Bytes); - } - [Fact] public void MultiplyOperator() { diff --git a/src/ByteSizeLib.Tests/ByteSizeLib.Tests.csproj b/src/ByteSizeLib.Tests/ByteSizeLib.Tests.csproj index 8bdf5b0..a69c7f1 100755 --- a/src/ByteSizeLib.Tests/ByteSizeLib.Tests.csproj +++ b/src/ByteSizeLib.Tests/ByteSizeLib.Tests.csproj @@ -1,9 +1,13 @@  - net5.0 + net6.0 false + + True + + bytetests.snk @@ -11,7 +15,7 @@ - + all diff --git a/src/ByteSizeLib.Tests/ByteSizeTypeConverterTests.cs b/src/ByteSizeLib.Tests/ByteSizeTypeConverterTests.cs index 874e538..a3f0c31 100644 --- a/src/ByteSizeLib.Tests/ByteSizeTypeConverterTests.cs +++ b/src/ByteSizeLib.Tests/ByteSizeTypeConverterTests.cs @@ -1,10 +1,6 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using Xunit; +using System.Collections.Generic; using System.ComponentModel; +using Xunit; namespace ByteSizeLib.Tests { @@ -16,7 +12,7 @@ public void ConvertsToString() var converter = TypeDescriptor.GetConverter(typeof(ByteSize)); Assert.NotNull(converter); var bs = ByteSize.FromBytes(1024); - var actual =converter.ConvertToString(bs); + var actual = converter.ConvertToString(bs); var expected = "1 KiB"; Assert.Equal(expected, actual); } diff --git a/src/ByteSizeLib.Tests/Decimal/ParsingMethods.cs b/src/ByteSizeLib.Tests/Decimal/ParsingMethods.cs index 7447491..73974ba 100644 --- a/src/ByteSizeLib.Tests/Decimal/ParsingMethods.cs +++ b/src/ByteSizeLib.Tests/Decimal/ParsingMethods.cs @@ -70,7 +70,7 @@ public void ParseCultureNumberSeparator() var result = ByteSize.Parse(val); Assert.Equal(expected, result); - + CultureInfo.CurrentCulture = new CultureInfo("en-US"); } } diff --git a/src/ByteSizeLib.Tests/ToStringMethod.cs b/src/ByteSizeLib.Tests/ToStringMethod.cs index d1bd6ea..61ab455 100644 --- a/src/ByteSizeLib.Tests/ToStringMethod.cs +++ b/src/ByteSizeLib.Tests/ToStringMethod.cs @@ -177,7 +177,7 @@ public void StringInterpolationWithFormat() var result = $"{b:0.0}"; // Assert - Assert.Equal("12.0 B", result); + Assert.Equal($"{12.0:0.0} B", result); } [Fact] @@ -203,7 +203,7 @@ public void StringInterpolationDefaultFormat() var result = $"{b}"; // Assert - Assert.Equal("1.12 KB", result); + Assert.Equal($"{1.12} KB", result); } } } diff --git a/src/ByteSizeLib.Tests/bytetests.snk b/src/ByteSizeLib.Tests/bytetests.snk new file mode 100644 index 0000000..153848d Binary files /dev/null and b/src/ByteSizeLib.Tests/bytetests.snk differ diff --git a/src/ByteSizeLib/AssemblyInfo.cs b/src/ByteSizeLib/AssemblyInfo.cs new file mode 100644 index 0000000..649a488 --- /dev/null +++ b/src/ByteSizeLib/AssemblyInfo.cs @@ -0,0 +1,19 @@ +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// In SDK-style projects such as this one, several assembly attributes that were historically +// defined in this file are now automatically added during build and populated with +// values defined in project properties. For details of which attributes are included +// and how to customise this process see: https://aka.ms/assembly-info-properties + + +// Setting ComVisible to false makes the types in this assembly not visible to COM +// components. If you need to access a type in this assembly from COM, set the ComVisible +// attribute to true on that type. + +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM. + +[assembly: Guid("c5d64482-f2bc-41b0-b6c1-0ed7a24f338d")] +[assembly: InternalsVisibleTo("ByteSizeLib.Tests, PublicKey=00240000048000009400000006020000002400005253413100040000010001009dc5b9fd3ac54829a66893b2e69fa3bed6a91b4cc788c6a8a87f48e94210eb6728bac2cf673cc29f5e0d422c45970064e571af4b2b4f6c564fded8ab671f3a3c40c9667caa04c659c4d2aeef867eb75528db421ad5741b363cb03bba3bd28785a1a68edd70d86b653917c476b84145c4690da184fb58f2419f82f51e1ac14fcc")] \ No newline at end of file diff --git a/src/ByteSizeLib/BinaryByteSize.cs b/src/ByteSizeLib/BinaryByteSize.cs index f37f92a..4187fae 100644 --- a/src/ByteSizeLib/BinaryByteSize.cs +++ b/src/ByteSizeLib/BinaryByteSize.cs @@ -10,43 +10,43 @@ public partial struct ByteSize /// Number of bytes in 1 mebibyte. public const long BytesInMebiByte = 1_048_576; - + /// Number of bytes in 1 gibibyte. public const long BytesInGibiByte = 1_073_741_824; - + /// Number of bytes in 1 tebibyte. public const long BytesInTebiByte = 1_099_511_627_776; - + /// Number of bytes in 1 pebibyte. public const long BytesInPebiByte = 1_125_899_906_842_624; /// Kibibyte symbol. public const string KibiByteSymbol = "KiB"; - + /// Mebibyte symbol. public const string MebiByteSymbol = "MiB"; - + /// Gibibyte symbol. public const string GibiByteSymbol = "GiB"; - + /// Tebibyte symbol. public const string TebiByteSymbol = "TiB"; - + /// Pebibyte symbol. public const string PebiByteSymbol = "PiB"; /// Gets the number of kibibytes represented by this object. public double KibiBytes => Bytes / BytesInKibiByte; - + /// Gets the number of mebibytes represented by this object. public double MebiBytes => Bytes / BytesInMebiByte; - + /// Gets the number of gibibytes represented by this object. public double GibiBytes => Bytes / BytesInGibiByte; - + /// Gets the number of tebibytes represented by this object. public double TebiBytes => Bytes / BytesInTebiByte; - + /// Gets the number of pebibytes represented by this object. public double PebiBytes => Bytes / BytesInPebiByte; diff --git a/src/ByteSizeLib/ByteSize.cs b/src/ByteSizeLib/ByteSize.cs index 647e158..b8ae3e9 100644 --- a/src/ByteSizeLib/ByteSize.cs +++ b/src/ByteSizeLib/ByteSize.cs @@ -212,7 +212,7 @@ public static ByteSize FromBytes(double value) /// public override string ToString() { - return this.ToString("0.##", CultureInfo.CurrentCulture); + return this.ToString("0.##", null); } /// @@ -225,7 +225,7 @@ public override string ToString() /// A numeric format string. public string ToString(string format) { - return this.ToString(format, CultureInfo.CurrentCulture); + return this.ToString(format, null); } /// @@ -236,10 +236,10 @@ public string ToString(string format) /// Use for binary string representation. /// /// A numeric format string. - /// An object that supplies culture-specific formatting information. - public string ToString(string? format, IFormatProvider? provider) + /// An object that supplies culture-specific formatting information. + public string ToString(string format, IFormatProvider formatProvider) { - return this.ToString(format, provider, useBinaryByte: false); + return this.ToString(format, formatProvider, useBinaryByte: false); } /// @@ -253,11 +253,11 @@ public string ToString(string? format, IFormatProvider? provider) /// An object that supplies culture-specific formatting information. /// to use binary byte values (1 KiB = 1024) instead of decimal values (1 KB = 1000 B). [System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0046:Convert to conditional expression")] - public string ToString(string? format, IFormatProvider? provider, bool useBinaryByte) + public string ToString(string format, IFormatProvider provider, bool useBinaryByte) { if (format != null && !format.Contains("#") && !format.Contains("0")) format = "0.## " + format; - else + else format ??= "0.##"; provider ??= CultureInfo.CurrentCulture; @@ -304,12 +304,12 @@ public string ToString(string? format, IFormatProvider? provider, bool useBinary (0, _) => "0 B", (not 0, true) => string.Format( - "{0} {1}", - this.LargestWholeNumberBinaryValue.ToString(format, provider), + "{0} {1}", + this.LargestWholeNumberBinaryValue.ToString(format, provider), this.LargestWholeNumberBinarySymbol), - (not 0, false) => string.Format("{0} {1}", - this.LargestWholeNumberDecimalValue.ToString(format, provider), + (not 0, false) => string.Format("{0} {1}", + this.LargestWholeNumberDecimalValue.ToString(format, provider), this.LargestWholeNumberDecimalSymbol), }; } @@ -317,15 +317,15 @@ public string ToString(string? format, IFormatProvider? provider, bool useBinary /// /// Indicates whether an instance has equal number of bits to this instance. /// - /// The instance to compare. - public override bool Equals(object? value) + /// The instance to compare. + public override bool Equals(object obj) { - if (value == null) + if (obj == null) return false; ByteSize other; - if (value is ByteSize) - other = (ByteSize)value; + if (obj is ByteSize size) + other = size; else return false; @@ -404,16 +404,6 @@ public ByteSize Subtract(ByteSize bs) return new ByteSize(b1.Bytes + b2.Bytes); } - /// - /// Returns an instance whose value is 1 byte greater than this instance. - /// - /// The instance to increment. - public static ByteSize operator ++(ByteSize b) - { - // TODO: I can't imagine this method being useful. Might want to get rid of. - return new ByteSize(b.Bytes + 1); - } - /// /// Returns a new instance whose value is the negative value of the specified instance. /// @@ -431,22 +421,12 @@ public ByteSize Subtract(ByteSize bs) return new ByteSize(b1.Bytes - b2.Bytes); } - /// - /// Returns a new instance whose value is 1 byte less than this instance. - /// - /// The instance to decrement. - public static ByteSize operator --(ByteSize b) - { - // TODO: I can't imagine this method being useful. Might want to get rid of. - return new ByteSize(b.Bytes - 1); - } - /// /// Multiply two instances. /// /// First instance to multiply. /// Second instance to multiply. - public static ByteSize operator *(ByteSize a, ByteSize b) + public static ByteSize operator *(ByteSize a, ByteSize b) { return new ByteSize(a.Bytes * b.Bytes); } @@ -553,7 +533,7 @@ public static ByteSize Parse(string s, IFormatProvider formatProvider) /// An object that supplies culture-specific formatting information. public static ByteSize Parse(string s, NumberStyles numberStyles, IFormatProvider formatProvider) { - + // Arg checking if (string.IsNullOrWhiteSpace(s)) throw new ArgumentNullException("s", "String is null or whitespace"); @@ -561,13 +541,14 @@ public static ByteSize Parse(string s, NumberStyles numberStyles, IFormatProvide // Get the index of the first non-digit character s = s.TrimStart(); // Protect against leading spaces - var num = 0; var found = false; var numberFormatInfo = NumberFormatInfo.GetInstance(formatProvider); var decimalSeparator = Convert.ToChar(numberFormatInfo.NumberDecimalSeparator); var groupSeparator = Convert.ToChar(numberFormatInfo.NumberGroupSeparator); + + int num; // Pick first non-digit number for (num = 0; num < s.Length; num++) if (!(char.IsDigit(s[num]) || s[num] == decimalSeparator || s[num] == groupSeparator)) @@ -576,7 +557,7 @@ public static ByteSize Parse(string s, NumberStyles numberStyles, IFormatProvide break; } - if (found == false) + if (!found) throw new FormatException($"No byte indicator found in value '{s}'."); int lastNumber = num; @@ -586,8 +567,7 @@ public static ByteSize Parse(string s, NumberStyles numberStyles, IFormatProvide string sizePart = s.Substring(lastNumber, s.Length - lastNumber).Trim(); // Get the numeric part - double number; - if (!double.TryParse(numberPart, numberStyles, formatProvider, out number)) + if (!double.TryParse(numberPart, numberStyles, formatProvider, out double number)) throw new FormatException($"No number found in value '{s}'."); // Get the magnitude part @@ -597,49 +577,28 @@ public static ByteSize Parse(string s, NumberStyles numberStyles, IFormatProvide if (number % 1 != 0) // Can't have partial bits throw new FormatException($"Can't have partial bits for value '{s}'."); - return FromBits((long) number); + return FromBits((long)number); case "B": return FromBytes(number); } - switch (sizePart.ToLowerInvariant()) + return sizePart.ToLowerInvariant() switch { // Binary - case "kib": - return FromKibiBytes(number); - - case "mib": - return FromMebiBytes(number); - - case "gib": - return FromGibiBytes(number); - - case "tib": - return FromTebiBytes(number); - - case "pib": - return FromPebiBytes(number); - + "kib" => FromKibiBytes(number), + "mib" => FromMebiBytes(number), + "gib" => FromGibiBytes(number), + "tib" => FromTebiBytes(number), + "pib" => FromPebiBytes(number), // Decimal - case "kb": - return FromKiloBytes(number); - - case "mb": - return FromMegaBytes(number); - - case "gb": - return FromGigaBytes(number); - - case "tb": - return FromTeraBytes(number); - - case "pb": - return FromPetaBytes(number); - - default: - throw new FormatException($"Bytes of magnitude '{sizePart}' is not supported."); - } + "kb" => FromKiloBytes(number), + "mb" => FromMegaBytes(number), + "gb" => FromGigaBytes(number), + "tb" => FromTeraBytes(number), + "pb" => FromPetaBytes(number), + _ => throw new FormatException($"Bytes of magnitude '{sizePart}' is not supported."), + }; } /// @@ -650,7 +609,7 @@ public static ByteSize Parse(string s, NumberStyles numberStyles, IFormatProvide /// Object reference to store the result if successful. public static bool TryParse(string s, out ByteSize result) { - try + try { result = Parse(s); return true; diff --git a/src/ByteSizeLib/ByteSizeLib.csproj b/src/ByteSizeLib/ByteSizeLib.csproj index 99410e7..13a4e65 100755 --- a/src/ByteSizeLib/ByteSizeLib.csproj +++ b/src/ByteSizeLib/ByteSizeLib.csproj @@ -4,12 +4,12 @@ ByteSize ByteSize is a utility class that makes byte size representation in code easier by removing ambiguity of the value being represented. ByteSize is to bytes what System.TimeSpan is to time. Copyright © Omar Khudeira 2013-2022 - 2.1.1 + 2.1.2 ByteSize Omar Khudeira - netstandard1.0;netstandard2.1;netstandard2.0;net45;net5.0; - 9.0 - enable + net472;net6.0 + latest + disable ByteSize ByteSize bytes @@ -24,11 +24,18 @@ View all release notes at https://github.com/omar/ByteSize/blob/master/CHANGELOG en MIT true - true snupkg - true + disable + True + bytesize.snk + + true + true + true + + true true @@ -39,13 +46,6 @@ View all release notes at https://github.com/omar/ByteSize/blob/master/CHANGELOG - - - - - false diff --git a/src/ByteSizeLib/DecimalByteSize.cs b/src/ByteSizeLib/DecimalByteSize.cs index d3fcc86..bb8f696 100644 --- a/src/ByteSizeLib/DecimalByteSize.cs +++ b/src/ByteSizeLib/DecimalByteSize.cs @@ -4,31 +4,31 @@ public partial struct ByteSize { /// Number of bytes in 1 kilobyte. public const long BytesInKiloByte = 1_000; - + /// Number of bytes in 1 megabyte. public const long BytesInMegaByte = 1_000_000; - + /// Number of bytes in 1 gigabyte. public const long BytesInGigaByte = 1_000_000_000; - + /// Number of bytes in 1 terabyte. public const long BytesInTeraByte = 1_000_000_000_000; - + /// Number of bytes in 1 petabyte. public const long BytesInPetaByte = 1_000_000_000_000_000; /// Kilobyte symbol. public const string KiloByteSymbol = "KB"; - + /// Megabyte symbol. public const string MegaByteSymbol = "MB"; - + /// Gigabyte symbol. public const string GigaByteSymbol = "GB"; - + /// Terabyte symbol. public const string TeraByteSymbol = "TB"; - + /// Petabyte symbol. public const string PetaByteSymbol = "PB"; diff --git a/src/ByteSizeLib/bytesize.snk b/src/ByteSizeLib/bytesize.snk new file mode 100644 index 0000000..5d063ff Binary files /dev/null and b/src/ByteSizeLib/bytesize.snk differ