Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: Optimize number parse logics #1007

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions YamlDotNet.Test/Serialization/DeserializerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,27 @@ public void UnquotedStringTypeDeserialization_RegularNumbers(object expected)
Assert.Equal(expected, resultDict["Value"]);
}

[Theory]
[InlineData(System.Byte.MinValue)]
[InlineData(System.Byte.MaxValue)]
[InlineData(System.Int16.MinValue)]
[InlineData(System.Int16.MaxValue)]
[InlineData(System.Int32.MinValue)]
[InlineData(System.Int32.MaxValue)]
[InlineData(System.Int64.MinValue)]
[InlineData(System.Int64.MaxValue)]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

UnitTest for ulong is not included.
Because ulong code path is not seems not be used.
Hex/Octal number representation is same for long/ulong and these number is always parsed as long

public void UnquotedStringTypeDeserialization_HexNumbers(object expected)
{
var deserializer = new DeserializerBuilder()
.WithAttemptingUnquotedStringTypeDeserialization().Build();

var yaml = $"Value: 0x{expected:X2}";

var resultDict = deserializer.Deserialize<IDictionary<string, object>>(yaml);
Assert.True(resultDict.ContainsKey("Value"));
Assert.Equal(expected, resultDict["Value"]);
}

[Theory]
[InlineData(".nan", System.Single.NaN)]
[InlineData(".NaN", System.Single.NaN)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -354,20 +354,21 @@ private static object CastInteger(ulong number, TypeCode typeCode)
case "FALSE":
return false;
default:
if (Regex.IsMatch(v, "0x[0-9a-fA-F]+")) //base16 number
if (Regex.IsMatch(v, "^0x[0-9a-fA-F]+$")) //base16 number
{
if (TryAndSwallow(() => Convert.ToByte(v, 16), out result)) { }
else if (TryAndSwallow(() => Convert.ToInt16(v, 16), out result)) { }
else if (TryAndSwallow(() => Convert.ToInt32(v, 16), out result)) { }
else if (TryAndSwallow(() => Convert.ToInt64(v, 16), out result)) { }
else if (TryAndSwallow(() => Convert.ToUInt64(v, 16), out result)) { }
v = v.Substring(2);
if (byte.TryParse(v, NumberStyles.AllowHexSpecifier, formatter.NumberFormat, out var byteValue)) { result = byteValue; }
else if (short.TryParse(v, NumberStyles.AllowHexSpecifier, formatter.NumberFormat, out var shortValue)) { result = shortValue; }
else if (int.TryParse(v, NumberStyles.AllowHexSpecifier, formatter.NumberFormat, out var intValue)) { result = intValue; }
else if (long.TryParse(v, NumberStyles.AllowHexSpecifier, formatter.NumberFormat, out var longValue)) { result = longValue; }
else if (ulong.TryParse(v, NumberStyles.AllowHexSpecifier, formatter.NumberFormat, out var ulongValue)) { result = ulongValue; }
else
{
//we couldn't parse it, default to a string. It's probably to big.
result = v;
}
}
else if (Regex.IsMatch(v, "0o[0-9a-fA-F]+")) //base8 number
else if (Regex.IsMatch(v, "^0o[0-9a-fA-F]+$")) //base8 number
{
if (TryAndSwallow(() => Convert.ToByte(v, 8), out result)) { }
else if (TryAndSwallow(() => Convert.ToInt16(v, 8), out result)) { }
Expand All @@ -380,7 +381,7 @@ private static object CastInteger(ulong number, TypeCode typeCode)
result = v;
}
}
else if (Regex.IsMatch(v, @"[-+]?(\.[0-9]+|[0-9]+(\.[0-9]*)?)([eE][-+]?[0-9]+)?")) //regular number
else if (Regex.IsMatch(v, @"^[-+]?(\.[0-9]+|[0-9]+(\.[0-9]*)?)([eE][-+]?[0-9]+)?$")) //regular number
{
if (byte.TryParse(v, NumberStyles.Integer, formatter.NumberFormat, out var byteValue)) { result = byteValue; }
else if (short.TryParse(v, NumberStyles.Integer, formatter.NumberFormat, out var shortValue)) { result = shortValue; }
Expand Down