Skip to content

Commit b70c420

Browse files
Pankratyandersnm
authored andcommitted
Replace all usages of Convert.ToString with CompatibleConvert.ToString for uniformity. Fix NRE for null formatString
1 parent 89a051a commit b70c420

File tree

5 files changed

+44
-27
lines changed

5 files changed

+44
-27
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System;
2+
3+
namespace ExcelNumberFormat
4+
{
5+
internal static class CompatibleConvert
6+
{
7+
/// <summary>
8+
/// A backward-compatible version of <see cref="Convert.ToString(object, IFormatProvider)"/>.
9+
/// Starting from .net Core 3.0 the default precision used for formatting floating point number has changed.
10+
/// To always format numbers the same way, no matter what version of runtime is used, we specify the precision explicitly.
11+
/// </summary>
12+
public static string ToString(object value, IFormatProvider provider)
13+
{
14+
switch (value)
15+
{
16+
case double d:
17+
return d.ToString("G15", provider);
18+
case float f:
19+
return f.ToString("G7", provider);
20+
default:
21+
return Convert.ToString(value, provider);
22+
}
23+
}
24+
}
25+
}

src/ExcelNumberFormat/Formatter.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ static public string Format(object value, string formatString, CultureInfo cultu
1111
{
1212
var format = new NumberFormat(formatString);
1313
if (!format.IsValid)
14-
return Convert.ToString(value, culture);
14+
return CompatibleConvert.ToString(value, culture);
1515

1616
var section = Evaluator.GetSection(format.Sections, value);
1717
if (section == null)
18-
return Convert.ToString(value, culture);
18+
return CompatibleConvert.ToString(value, culture);
1919

2020
return Format(value, section, culture);
2121
}
@@ -40,7 +40,7 @@ static public string Format(object value, Section node, CultureInfo culture)
4040

4141
case SectionType.General:
4242
case SectionType.Text:
43-
return FormatGeneralText(Convert.ToString(value, culture), node.GeneralTextDateDurationParts);
43+
return FormatGeneralText(CompatibleConvert.ToString(value, culture), node.GeneralTextDateDurationParts);
4444

4545
case SectionType.Exponential:
4646
return FormatExponential(Convert.ToDouble(value, culture), node, culture);

src/ExcelNumberFormat/NumberFormat.cs

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -63,21 +63,11 @@ public NumberFormat(string formatString)
6363
public string Format(object value, CultureInfo culture)
6464
{
6565
if (!IsValid || string.IsNullOrEmpty(FormatString))
66-
{
67-
switch (value)
68-
{
69-
case double d:
70-
return d.ToString("G15", culture);
71-
case Single s:
72-
return s.ToString("G7", culture);
73-
default:
74-
return Convert.ToString(value, culture);
75-
}
76-
}
66+
CompatibleConvert.ToString(value, culture);
7767

7868
var section = Evaluator.GetSection(Sections, value);
7969
if (section == null)
80-
return Convert.ToString(value, culture);
70+
return CompatibleConvert.ToString(value, culture);
8171

8272
try
8373
{
@@ -86,12 +76,12 @@ public string Format(object value, CultureInfo culture)
8676
catch (InvalidCastException)
8777
{
8878
// TimeSpan cast exception
89-
return Convert.ToString(value, culture);
79+
return CompatibleConvert.ToString(value, culture);
9080
}
9181
catch (FormatException)
9282
{
9383
// Convert.ToDouble/ToDateTime exceptions
94-
return Convert.ToString(value, culture);
84+
return CompatibleConvert.ToString(value, culture);
9585
}
9686
}
9787
}

src/ExcelNumberFormat/Tokenizer.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public Tokenizer(string fmt)
1414

1515
public int Position => formatStringPosition;
1616

17-
public int Length => formatString.Length;
17+
public int Length => formatString?.Length ?? 0;
1818

1919
public string Substring(int startIndex, int length)
2020
{
@@ -23,7 +23,7 @@ public string Substring(int startIndex, int length)
2323

2424
public int Peek(int offset = 0)
2525
{
26-
if (formatStringPosition + offset >= formatString.Length)
26+
if (formatStringPosition + offset >= Length)
2727
return -1;
2828
return formatString[formatStringPosition + offset];
2929
}
@@ -82,7 +82,7 @@ public bool ReadOneOf(string s)
8282

8383
public bool ReadString(string s, bool ignoreCase = false)
8484
{
85-
if (formatStringPosition + s.Length > formatString.Length)
85+
if (formatStringPosition + s.Length > Length)
8686
return false;
8787

8888
for (var i = 0; i < s.Length; i++)

test/ExcelNumberFormat.Tests/Class1.cs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -911,24 +911,26 @@ public void TestValid()
911911
TestValid("yyyy\\-mm\\-dd\\Thhmmss.000");
912912
}
913913

914-
[TestMethod]
915-
public void TestEmptyFormatString()
914+
[TestCase(null)]
915+
[TestCase("")]
916+
[TestCase("General")]
917+
public void TestDefaultFormatString(string formatString)
916918
{
917919
string result;
918920

919-
result = Format(1234.56, string.Empty, CultureInfo.InvariantCulture);
921+
result = Format(1234.56, formatString, CultureInfo.InvariantCulture);
920922
Assert.AreEqual("1234.56", result);
921923

922-
result = Format(Double.MaxValue, string.Empty, CultureInfo.InvariantCulture);
924+
result = Format(Double.MaxValue, formatString, CultureInfo.InvariantCulture);
923925
Assert.AreEqual("1.79769313486232E+308", result);
924926

925-
result = Format(float.MaxValue, string.Empty, CultureInfo.InvariantCulture);
927+
result = Format(float.MaxValue, formatString, CultureInfo.InvariantCulture);
926928
Assert.AreEqual("3.402823E+38", result);
927929

928-
result = Format(new DateTime(2017, 10, 28), string.Empty, new CultureInfo("sv-se"));
930+
result = Format(new DateTime(2017, 10, 28), formatString, new CultureInfo("sv-se"));
929931
Assert.AreEqual("2017-10-28 00:00:00", result);
930932

931-
result = Format(new DateTime(2017, 10, 28), string.Empty, CultureInfo.InvariantCulture);
933+
result = Format(new DateTime(2017, 10, 28), formatString, CultureInfo.InvariantCulture);
932934
Assert.AreEqual("10/28/2017 00:00:00", result);
933935
}
934936

0 commit comments

Comments
 (0)