Skip to content

Commit

Permalink
Add ByteSize Parse/TryParse with IFormatProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
dahlbyk committed May 3, 2021
1 parent 42ac2a1 commit 75f96e3
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 7 deletions.
31 changes: 31 additions & 0 deletions src/Humanizer.Tests.Shared/Bytes/ParsingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
//THE SOFTWARE.

using System;
using System.Globalization;
using Humanizer.Bytes;
using Xunit;

Expand All @@ -45,6 +46,36 @@ public void TryParse()
Assert.Equal(ByteSize.FromKilobytes(1020), resultByteSize);
}

[Theory]
[InlineData("2000.01KB", "")] // Invariant
[InlineData("2000.01KB", "en")]
[InlineData("2000,01KB", "de")]
public void TryParseWithCultureInfo(string value, string cultureName)
{
var culture = new CultureInfo(cultureName);

Assert.True(ByteSize.TryParse(value, culture, out var resultByteSize));
Assert.Equal(ByteSize.FromKilobytes(2000.01), resultByteSize);

Assert.Equal(resultByteSize, ByteSize.Parse(value, culture));
}

[Fact]
public void TryParseWithNumberFormatInfo()
{
var numberFormat = new NumberFormatInfo
{
NumberDecimalSeparator = "_",
};

var value = "2000_01KB";

Assert.True(ByteSize.TryParse(value, numberFormat, out var resultByteSize));
Assert.Equal(ByteSize.FromKilobytes(2000.01), resultByteSize);

Assert.Equal(resultByteSize, ByteSize.Parse(value, numberFormat));
}

[Fact]
public void ParseDecimalMegabytes()
{
Expand Down
42 changes: 35 additions & 7 deletions src/Humanizer/Bytes/ByteSize.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@

using System;
using System.Globalization;
using System.Linq;
using Humanizer.Configuration;
using Humanizer.Localisation;

using static System.Globalization.NumberStyles;

namespace Humanizer.Bytes
{
/// <summary>
Expand Down Expand Up @@ -466,13 +469,27 @@ public ByteSize Subtract(ByteSize bs)
}

public static bool TryParse(string s, out ByteSize result)
{
return TryParse(s, null, out result);
}

public static bool TryParse(string s, IFormatProvider formatProvider, out ByteSize result)
{
// Arg checking
if (string.IsNullOrWhiteSpace(s))
{
throw new ArgumentNullException(nameof(s), "String is null or whitespace");
}

// Acquiring culture-specific parsing info
var numberFormat = GetNumberFormatInfo(formatProvider);

const NumberStyles numberStyles = AllowDecimalPoint;
var numberSpecialChars = new[]
{
Convert.ToChar(numberFormat.NumberDecimalSeparator),
};

// Setup the result
result = new ByteSize();

Expand All @@ -482,14 +499,10 @@ public static bool TryParse(string s, out ByteSize result)
int num;
var found = false;

// Acquiring culture specific decimal separator

var decSep = Convert.ToChar(CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator);

// Pick first non-digit number
for (num = 0; num < s.Length; num++)
{
if (!(char.IsDigit(s[num]) || s[num] == decSep))
if (!(char.IsDigit(s[num]) || numberSpecialChars.Contains(s[num])))
{
found = true;
break;
Expand All @@ -508,7 +521,7 @@ public static bool TryParse(string s, out ByteSize result)
var sizePart = s.Substring(lastNumber, s.Length - lastNumber).Trim();

// Get the numeric part
if (!double.TryParse(numberPart, out var number))
if (!double.TryParse(numberPart, numberStyles, formatProvider, out var number))
{
return false;
}
Expand Down Expand Up @@ -552,9 +565,24 @@ public static bool TryParse(string s, out ByteSize result)
return true;
}

private static NumberFormatInfo GetNumberFormatInfo(IFormatProvider formatProvider)
{
if (formatProvider is NumberFormatInfo numberFormat)
return numberFormat;

var culture = formatProvider as CultureInfo ?? CultureInfo.CurrentCulture;

return culture.NumberFormat;
}

public static ByteSize Parse(string s)
{
if (TryParse(s, out var result))
return Parse(s, null);
}

public static ByteSize Parse(string s, IFormatProvider formatProvider)
{
if (TryParse(s, formatProvider, out var result))
{
return result;
}
Expand Down

0 comments on commit 75f96e3

Please sign in to comment.