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

Remove most of string.Trim() usages in System.Private.Xml solution. #75452

Merged
merged 5 commits into from Sep 26, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/libraries/System.Private.Xml/src/System/Xml/XmlConvert.cs
Expand Up @@ -963,6 +963,8 @@ public static ulong ToUInt64(string s)

public static float ToSingle(string s)
{
ArgumentNullException.ThrowIfNull(s);

ReadOnlySpan<char> value = s.AsSpan().Trim(WhitespaceChars);
switch (value)
{
Expand Down Expand Up @@ -1008,6 +1010,8 @@ public static float ToSingle(string s)

public static double ToDouble(string s)
{
ArgumentNullException.ThrowIfNull(s);
Copy link
Member

Choose a reason for hiding this comment

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

interestingly this currently throws NRE

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@krwq This is to mimic current behavior. Check upper comment which shows difference when using double/float.Parse(string...) and double/float.Parse(ReadOnlySpan...) and passing null.

public static float Parse(string s, NumberStyles style, IFormatProvider? provider)
{
NumberFormatInfo.ValidateParseStyleFloatingPoint(style);
if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
return Number.ParseSingle(s, style, NumberFormatInfo.GetInstance(provider));
}
public static float Parse(ReadOnlySpan<char> s, NumberStyles style = NumberStyles.Float | NumberStyles.AllowThousands, IFormatProvider? provider = null)
{
NumberFormatInfo.ValidateParseStyleFloatingPoint(style);
return Number.ParseSingle(s, style, NumberFormatInfo.GetInstance(provider));
}


ReadOnlySpan<char> value = s.AsSpan().Trim(WhitespaceChars);
switch (value)
{
Expand Down Expand Up @@ -1058,6 +1062,8 @@ internal static double ToXPathDouble(object? o)
{
case string str:
{
ArgumentNullException.ThrowIfNull(str);

ReadOnlySpan<char> value = str.AsSpan().Trim(WhitespaceChars);
if (value.Length != 0 && value[0] != '+')
{
Expand Down