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
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -5119,7 +5119,7 @@ private void OnXmlReservedAttribute(NodeData attr)
PushXmlContext();
}

switch (XmlConvert.TrimString(attr.StringValue))
switch (attr.StringValue.AsSpan().Trim(XmlConvert.WhitespaceChars))
{
case "preserve":
_xmlContext.xmlSpace = XmlSpace.Preserve;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1758,18 +1758,16 @@ private void HandleSpecialAttribute()
break;
case SpecialAttr.XmlSpace:
// validate XmlSpace attribute
value = XmlConvert.TrimString(value);
if (value == "default")
switch (value.AsSpan().Trim(XmlConvert.WhitespaceChars))
{
_stack[_top].xmlSpace = XmlSpace.Default;
}
else if (value == "preserve")
{
_stack[_top].xmlSpace = XmlSpace.Preserve;
}
else
{
throw new ArgumentException(SR.Format(SR.Xml_InvalidXmlSpace, value));
case "default":
_stack[_top].xmlSpace = XmlSpace.Default;
break;
case "preserve":
_stack[_top].xmlSpace = XmlSpace.Preserve;
break;
default:
throw new ArgumentException(SR.Format(SR.Xml_InvalidXmlSpace, value));
}
break;
case SpecialAttr.XmlNs:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1349,7 +1349,7 @@ internal virtual XmlSpace XmlSpace
elem = node as XmlElement;
if (elem != null && elem.HasAttribute("xml:space"))
{
switch (XmlConvert.TrimString(elem.GetAttribute("xml:space")))
switch (elem.GetAttribute("xml:space").AsSpan().Trim(XmlConvert.WhitespaceChars))
{
case "default":
return XmlSpace.Default;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,7 @@ protected static byte[] StringToHexBinary(string value)
{
try
{
return XmlConvert.FromBinHexString(XmlConvert.TrimString(value), false);
krwq marked this conversation as resolved.
Show resolved Hide resolved
return XmlConvert.FromBinHexString(value.AsSpan().Trim(XmlConvert.WhitespaceChars), false);
}
catch (XmlException e)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -437,8 +437,7 @@ internal static char ToChar(string value)
internal static byte[]? ToByteArrayHex(string? value)
{
if (value == null) return null;
value = value.Trim();
return XmlConvert.FromBinHexString(value);
return XmlConvert.FromBinHexString(value.AsSpan().Trim(XmlConvert.WhitespaceChars), true);
TrayanZapryanov marked this conversation as resolved.
Show resolved Hide resolved
}

internal static long ToEnum(string val, Hashtable vals, string? typeName, bool validate)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ public override XmlSpace XmlSpace
{
if (tempNav.MoveToAttribute(XPathNavigatorReader.space, XmlReservedNs.NsXml))
{
switch (XmlConvert.TrimString(tempNav.Value))
switch (tempNav.Value.AsSpan().Trim(XmlConvert.WhitespaceChars))
{
case "default":
return XmlSpace.Default;
Expand Down
175 changes: 94 additions & 81 deletions src/libraries/System.Private.Xml/src/System/Xml/XmlConvert.cs
Original file line number Diff line number Diff line change
Expand Up @@ -291,16 +291,9 @@ private static int FromHex(char digit)
return HexConverter.FromChar(digit);
}

internal static byte[] FromBinHexString(string s)
internal static byte[] FromBinHexString(ReadOnlySpan<char> s, bool allowOddCount)
{
return FromBinHexString(s, true);
}

internal static byte[] FromBinHexString(string s, bool allowOddCount)
{
ArgumentNullException.ThrowIfNull(s);

return BinHexDecoder.Decode(s.AsSpan(), allowOddCount);
return BinHexDecoder.Decode(s, allowOddCount);
}

internal static string ToBinHexString(byte[] inArray)
Expand Down Expand Up @@ -761,28 +754,35 @@ public static string ToString(Guid value)

public static bool ToBoolean(string s)
{
s = TrimString(s);
TrayanZapryanov marked this conversation as resolved.
Show resolved Hide resolved
if (s == "1" || s == "true") return true;
if (s == "0" || s == "false") return false;
throw new FormatException(SR.Format(SR.XmlConvert_BadFormat, s, "Boolean"));
switch (s.AsSpan().Trim(WhitespaceChars))
{
case "1":
case "true":
return true;
case "0":
case "false":
return false;
default:
throw new FormatException(SR.Format(SR.XmlConvert_BadFormat, s, "Boolean"));
}
}

internal static Exception? TryToBoolean(string s, out bool result)
{
s = TrimString(s);
if (s == "0" || s == "false")
{
result = false;
return null;
}
else if (s == "1" || s == "true")
switch (s.AsSpan().Trim(WhitespaceChars))
{
result = true;
return null;
case "0":
case "false":
result = false;
return null;
case "1":
case "true":
result = true;
return null;
default:
result = false;
return new FormatException(SR.Format(SR.XmlConvert_BadFormat, s, "Boolean"));
}

result = false;
return new FormatException(SR.Format(SR.XmlConvert_BadFormat, s, "Boolean"));
}

public static char ToChar(string s)
Expand Down Expand Up @@ -963,11 +963,17 @@ public static ulong ToUInt64(string s)

public static float ToSingle(string s)
{
s = TrimString(s);
if (s == "-INF") return float.NegativeInfinity;
if (s == "INF") return float.PositiveInfinity;
float f = float.Parse(s, NumberStyles.AllowLeadingSign | NumberStyles.AllowDecimalPoint | NumberStyles.AllowExponent, NumberFormatInfo.InvariantInfo);
if (f == 0 && s[0] == '-')
ReadOnlySpan<char> roS = s.AsSpan().Trim(WhitespaceChars);
TrayanZapryanov marked this conversation as resolved.
Show resolved Hide resolved
switch (roS)
{
case "-INF":
return float.NegativeInfinity;
case "INF":
return float.PositiveInfinity;
}

float f = float.Parse(roS, NumberStyles.AllowLeadingSign | NumberStyles.AllowDecimalPoint | NumberStyles.AllowExponent, NumberFormatInfo.InvariantInfo);
TrayanZapryanov marked this conversation as resolved.
Show resolved Hide resolved
if (f == 0 && roS[0] == '-')
{
return -0f;
}
Expand All @@ -977,23 +983,27 @@ public static float ToSingle(string s)

internal static Exception? TryToSingle(string s, out float result)
{
s = TrimString(s);
if (s == "-INF")
{
result = float.NegativeInfinity;
return null;
}
else if (s == "INF")
ReadOnlySpan<char> roS = s.AsSpan().Trim(WhitespaceChars);
switch (roS)
{
result = float.PositiveInfinity;
return null;
}
else if (!float.TryParse(s, NumberStyles.AllowLeadingSign | NumberStyles.AllowDecimalPoint | NumberStyles.AllowExponent, NumberFormatInfo.InvariantInfo, out result))
{
return new FormatException(SR.Format(SR.XmlConvert_BadFormat, s, "Single"));
case "-INF":
result = float.NegativeInfinity;
return null;
case "INF":
result = float.PositiveInfinity;
return null;
default:
{
if (!float.TryParse(roS, NumberStyles.AllowLeadingSign | NumberStyles.AllowDecimalPoint | NumberStyles.AllowExponent, NumberFormatInfo.InvariantInfo, out result))
{
return new FormatException(SR.Format(SR.XmlConvert_BadFormat, s, "Single"));
}

break;
}
}

if (result == 0 && s[0] == '-')
if (result == 0 && roS[0] == '-')
{
result = -0f;
}
Expand All @@ -1003,12 +1013,17 @@ public static float ToSingle(string s)

public static double ToDouble(string s)
{
s = TrimString(s);
if (s == "-INF") return double.NegativeInfinity;
if (s == "INF") return double.PositiveInfinity;
ReadOnlySpan<char> roS = s.AsSpan().Trim(WhitespaceChars);
switch (roS)
{
case "-INF":
return double.NegativeInfinity;
case "INF":
return double.PositiveInfinity;
}

double dVal = double.Parse(s, NumberStyles.AllowLeadingSign | NumberStyles.AllowDecimalPoint | NumberStyles.AllowExponent | NumberStyles.AllowLeadingWhite | NumberStyles.AllowTrailingWhite, NumberFormatInfo.InvariantInfo);
if (dVal == 0 && s[0] == '-')
double dVal = double.Parse(roS, NumberStyles.AllowLeadingSign | NumberStyles.AllowDecimalPoint | NumberStyles.AllowExponent | NumberStyles.AllowLeadingWhite | NumberStyles.AllowTrailingWhite, NumberFormatInfo.InvariantInfo);
TrayanZapryanov marked this conversation as resolved.
Show resolved Hide resolved
if (dVal == 0 && roS[0] == '-')
{
return -0d;
}
Expand All @@ -1018,23 +1033,27 @@ public static double ToDouble(string s)

internal static Exception? TryToDouble(string s, out double result)
{
s = TrimString(s);
if (s == "-INF")
ReadOnlySpan<char> roS = s.AsSpan().Trim(WhitespaceChars);
switch (roS)
{
result = double.NegativeInfinity;
return null;
}
else if (s == "INF")
{
result = double.PositiveInfinity;
return null;
}
else if (!double.TryParse(s, NumberStyles.AllowLeadingSign | NumberStyles.AllowDecimalPoint | NumberStyles.AllowExponent, NumberFormatInfo.InvariantInfo, out result))
{
return new FormatException(SR.Format(SR.XmlConvert_BadFormat, s, "Double"));
case "-INF":
result = double.NegativeInfinity;
return null;
case "INF":
result = double.PositiveInfinity;
return null;
default:
{
if (!double.TryParse(roS, NumberStyles.AllowLeadingSign | NumberStyles.AllowDecimalPoint | NumberStyles.AllowExponent, NumberFormatInfo.InvariantInfo, out result))
{
return new FormatException(SR.Format(SR.XmlConvert_BadFormat, s, "Double"));
}

break;
}
}

if (result == 0 && s[0] == '-')
if (result == 0 && roS[0] == '-')
{
result = -0d;
}
Expand All @@ -1046,11 +1065,10 @@ internal static double ToXPathDouble(object? o)
{
if (o is string str)
{
str = TrimString(str);
if (str.Length != 0 && str[0] != '+')
ReadOnlySpan<char> roS = str.AsSpan().Trim(WhitespaceChars);
if (roS.Length != 0 && roS[0] != '+')
{
double d;
if (double.TryParse(str, NumberStyles.AllowLeadingSign | NumberStyles.AllowDecimalPoint | NumberStyles.AllowTrailingWhite, NumberFormatInfo.InvariantInfo, out d))
if (double.TryParse(roS, NumberStyles.AllowLeadingSign | NumberStyles.AllowDecimalPoint | NumberStyles.AllowTrailingWhite, NumberFormatInfo.InvariantInfo, out double d))
{
return d;
}
Expand Down Expand Up @@ -1085,21 +1103,16 @@ internal static double ToXPathDouble(object? o)

internal static string? ToXPathString(object? value)
{
if (value is string s)
{
return s;
}
else if (value is double)
switch (value)
{
return ((double)value).ToString("R", NumberFormatInfo.InvariantInfo);
}
else if (value is bool)
{
return (bool)value ? "true" : "false";
}
else
{
return Convert.ToString(value, NumberFormatInfo.InvariantInfo);
case string s:
return s;
case double d:
return d.ToString("R", NumberFormatInfo.InvariantInfo);
case bool b:
return b ? "true" : "false";
default:
return Convert.ToString(value, NumberFormatInfo.InvariantInfo);
}
}

Expand Down