From 02f0853126d690910fc378a672dd008c69f5bbbe Mon Sep 17 00:00:00 2001 From: Adrian Godong Date: Sun, 27 Aug 2017 20:24:02 -0700 Subject: [PATCH] Refactor AsSpan to AsReadOnlySpan (#13608) * Refactor AsSpan to AsReadOnlySpan * Updated usages. * Renamed test methods. * Bring back AsSpan as obsolete method. --- src/mscorlib/shared/System/Boolean.cs | 8 ++++---- src/mscorlib/shared/System/Byte.cs | 12 ++++++------ src/mscorlib/shared/System/Double.cs | 12 ++++++------ .../shared/System/Globalization/HijriCalendar.Win32.cs | 2 +- .../shared/System/Globalization/JapaneseCalendar.Win32.cs | 2 +- src/mscorlib/shared/System/Int16.cs | 12 ++++++------ src/mscorlib/shared/System/Int32.cs | 12 ++++++------ src/mscorlib/shared/System/Int64.cs | 12 ++++++------ src/mscorlib/shared/System/SByte.cs | 14 +++++++------- src/mscorlib/shared/System/Single.cs | 12 ++++++------ src/mscorlib/shared/System/Span.NonGeneric.cs | 6 +++++- src/mscorlib/shared/System/StringSpanHelpers.cs | 2 +- src/mscorlib/shared/System/UInt16.cs | 12 ++++++------ src/mscorlib/shared/System/UInt32.cs | 12 ++++++------ src/mscorlib/shared/System/UInt64.cs | 12 ++++++------ src/mscorlib/shared/System/Version.cs | 4 ++-- src/mscorlib/src/System/Decimal.cs | 12 ++++++------ src/mscorlib/src/System/Number.cs | 2 +- src/mscorlib/src/System/TimeZoneInfo.Unix.cs | 6 +++--- tests/src/JIT/Performance/CodeQuality/Span/SpanBench.cs | 10 +++++----- 20 files changed, 90 insertions(+), 86 deletions(-) diff --git a/src/mscorlib/shared/System/Boolean.cs b/src/mscorlib/shared/System/Boolean.cs index 458c01b..a6ffb6d 100644 --- a/src/mscorlib/shared/System/Boolean.cs +++ b/src/mscorlib/shared/System/Boolean.cs @@ -166,7 +166,7 @@ namespace System public static Boolean Parse(String value) { if (value == null) throw new ArgumentNullException(nameof(value)); - return Parse(value.AsSpan()); + return Parse(value.AsReadOnlySpan()); } public static bool Parse(ReadOnlySpan value) => @@ -182,19 +182,19 @@ namespace System return false; } - return TryParse(value.AsSpan(), out result); + return TryParse(value.AsReadOnlySpan(), out result); } public static bool TryParse(ReadOnlySpan value, out bool result) { - ReadOnlySpan trueSpan = TrueLiteral.AsSpan(); + ReadOnlySpan trueSpan = TrueLiteral.AsReadOnlySpan(); if (StringSpanHelpers.Equals(trueSpan, value, StringComparison.OrdinalIgnoreCase)) { result = true; return true; } - ReadOnlySpan falseSpan = FalseLiteral.AsSpan(); + ReadOnlySpan falseSpan = FalseLiteral.AsReadOnlySpan(); if (StringSpanHelpers.Equals(falseSpan, value, StringComparison.OrdinalIgnoreCase)) { result = false; diff --git a/src/mscorlib/shared/System/Byte.cs b/src/mscorlib/shared/System/Byte.cs index 61ca221..a0f8ff8 100644 --- a/src/mscorlib/shared/System/Byte.cs +++ b/src/mscorlib/shared/System/Byte.cs @@ -85,7 +85,7 @@ namespace System public static byte Parse(String s) { if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s); - return Parse(s.AsSpan(), NumberStyles.Integer, NumberFormatInfo.CurrentInfo); + return Parse(s.AsReadOnlySpan(), NumberStyles.Integer, NumberFormatInfo.CurrentInfo); } [Pure] @@ -93,14 +93,14 @@ namespace System { NumberFormatInfo.ValidateParseStyleInteger(style); if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s); - return Parse(s.AsSpan(), style, NumberFormatInfo.CurrentInfo); + return Parse(s.AsReadOnlySpan(), style, NumberFormatInfo.CurrentInfo); } [Pure] public static byte Parse(String s, IFormatProvider provider) { if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s); - return Parse(s.AsSpan(), NumberStyles.Integer, NumberFormatInfo.GetInstance(provider)); + return Parse(s.AsReadOnlySpan(), NumberStyles.Integer, NumberFormatInfo.GetInstance(provider)); } // Parses an unsigned byte from a String in the given style. If @@ -111,7 +111,7 @@ namespace System { NumberFormatInfo.ValidateParseStyleInteger(style); if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s); - return Parse(s.AsSpan(), style, NumberFormatInfo.GetInstance(provider)); + return Parse(s.AsReadOnlySpan(), style, NumberFormatInfo.GetInstance(provider)); } public static byte Parse(ReadOnlySpan s, NumberStyles style = NumberStyles.Integer, IFormatProvider provider = null) @@ -144,7 +144,7 @@ namespace System return false; } - return TryParse(s.AsSpan(), NumberStyles.Integer, NumberFormatInfo.CurrentInfo, out result); + return TryParse(s.AsReadOnlySpan(), NumberStyles.Integer, NumberFormatInfo.CurrentInfo, out result); } public static bool TryParse(String s, NumberStyles style, IFormatProvider provider, out Byte result) @@ -157,7 +157,7 @@ namespace System return false; } - return TryParse(s.AsSpan(), style, NumberFormatInfo.GetInstance(provider), out result); + return TryParse(s.AsReadOnlySpan(), style, NumberFormatInfo.GetInstance(provider), out result); } public static bool TryParse(ReadOnlySpan s, out byte result, NumberStyles style = NumberStyles.Integer, IFormatProvider provider = null) diff --git a/src/mscorlib/shared/System/Double.cs b/src/mscorlib/shared/System/Double.cs index 285f98a..7ee5202 100644 --- a/src/mscorlib/shared/System/Double.cs +++ b/src/mscorlib/shared/System/Double.cs @@ -269,27 +269,27 @@ namespace System public static double Parse(String s) { if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s); - return Number.ParseDouble(s.AsSpan(), NumberStyles.Float | NumberStyles.AllowThousands, NumberFormatInfo.CurrentInfo); + return Number.ParseDouble(s.AsReadOnlySpan(), NumberStyles.Float | NumberStyles.AllowThousands, NumberFormatInfo.CurrentInfo); } public static double Parse(String s, NumberStyles style) { NumberFormatInfo.ValidateParseStyleFloatingPoint(style); if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s); - return Number.ParseDouble(s.AsSpan(), style, NumberFormatInfo.CurrentInfo); + return Number.ParseDouble(s.AsReadOnlySpan(), style, NumberFormatInfo.CurrentInfo); } public static double Parse(String s, IFormatProvider provider) { if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s); - return Number.ParseDouble(s.AsSpan(), NumberStyles.Float | NumberStyles.AllowThousands, NumberFormatInfo.GetInstance(provider)); + return Number.ParseDouble(s.AsReadOnlySpan(), NumberStyles.Float | NumberStyles.AllowThousands, NumberFormatInfo.GetInstance(provider)); } public static double Parse(String s, NumberStyles style, IFormatProvider provider) { NumberFormatInfo.ValidateParseStyleFloatingPoint(style); if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s); - return Number.ParseDouble(s.AsSpan(), style, NumberFormatInfo.GetInstance(provider)); + return Number.ParseDouble(s.AsReadOnlySpan(), style, NumberFormatInfo.GetInstance(provider)); } // Parses a double from a String in the given style. If @@ -316,7 +316,7 @@ namespace System return false; } - return TryParse(s.AsSpan(), NumberStyles.Float | NumberStyles.AllowThousands, NumberFormatInfo.CurrentInfo, out result); + return TryParse(s.AsReadOnlySpan(), NumberStyles.Float | NumberStyles.AllowThousands, NumberFormatInfo.CurrentInfo, out result); } public static bool TryParse(String s, NumberStyles style, IFormatProvider provider, out double result) @@ -329,7 +329,7 @@ namespace System return false; } - return TryParse(s.AsSpan(), style, NumberFormatInfo.GetInstance(provider), out result); + return TryParse(s.AsReadOnlySpan(), style, NumberFormatInfo.GetInstance(provider), out result); } public static bool TryParse(ReadOnlySpan s, out double result, NumberStyles style = NumberStyles.Integer, IFormatProvider provider = null) diff --git a/src/mscorlib/shared/System/Globalization/HijriCalendar.Win32.cs b/src/mscorlib/shared/System/Globalization/HijriCalendar.Win32.cs index 365942c..09b1f20 100644 --- a/src/mscorlib/shared/System/Globalization/HijriCalendar.Win32.cs +++ b/src/mscorlib/shared/System/Globalization/HijriCalendar.Win32.cs @@ -69,7 +69,7 @@ namespace System.Globalization { try { - int advance = Int32.Parse(str.AsSpan().Slice(HijriAdvanceRegKeyEntry.Length), provider:CultureInfo.InvariantCulture); + int advance = Int32.Parse(str.AsReadOnlySpan().Slice(HijriAdvanceRegKeyEntry.Length), provider:CultureInfo.InvariantCulture); if ((advance >= MinAdvancedHijri) && (advance <= MaxAdvancedHijri)) { hijriAdvance = advance; diff --git a/src/mscorlib/shared/System/Globalization/JapaneseCalendar.Win32.cs b/src/mscorlib/shared/System/Globalization/JapaneseCalendar.Win32.cs index 7a524e1..94e0668 100644 --- a/src/mscorlib/shared/System/Globalization/JapaneseCalendar.Win32.cs +++ b/src/mscorlib/shared/System/Globalization/JapaneseCalendar.Win32.cs @@ -159,7 +159,7 @@ namespace System.Globalization int month; int day; - ReadOnlySpan valueSpan = value.AsSpan(); + ReadOnlySpan valueSpan = value.AsReadOnlySpan(); if (!Int32.TryParse(valueSpan.Slice(0, 4), out year, style:NumberStyles.None, provider: NumberFormatInfo.InvariantInfo) || !Int32.TryParse(valueSpan.Slice(5, 2), out month, style:NumberStyles.None, provider: NumberFormatInfo.InvariantInfo) || !Int32.TryParse(valueSpan.Slice(8, 2), out day, style:NumberStyles.None, provider: NumberFormatInfo.InvariantInfo)) diff --git a/src/mscorlib/shared/System/Int16.cs b/src/mscorlib/shared/System/Int16.cs index bb20f91..6aaf428 100644 --- a/src/mscorlib/shared/System/Int16.cs +++ b/src/mscorlib/shared/System/Int16.cs @@ -117,27 +117,27 @@ namespace System public static short Parse(String s) { if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s); - return Parse(s.AsSpan(), NumberStyles.Integer, NumberFormatInfo.CurrentInfo); + return Parse(s.AsReadOnlySpan(), NumberStyles.Integer, NumberFormatInfo.CurrentInfo); } public static short Parse(String s, NumberStyles style) { NumberFormatInfo.ValidateParseStyleInteger(style); if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s); - return Parse(s.AsSpan(), style, NumberFormatInfo.CurrentInfo); + return Parse(s.AsReadOnlySpan(), style, NumberFormatInfo.CurrentInfo); } public static short Parse(String s, IFormatProvider provider) { if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s); - return Parse(s.AsSpan(), NumberStyles.Integer, NumberFormatInfo.GetInstance(provider)); + return Parse(s.AsReadOnlySpan(), NumberStyles.Integer, NumberFormatInfo.GetInstance(provider)); } public static short Parse(String s, NumberStyles style, IFormatProvider provider) { NumberFormatInfo.ValidateParseStyleInteger(style); if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s); - return Parse(s.AsSpan(), style, NumberFormatInfo.GetInstance(provider)); + return Parse(s.AsReadOnlySpan(), style, NumberFormatInfo.GetInstance(provider)); } public static short Parse(ReadOnlySpan s, NumberStyles style = NumberStyles.Integer, IFormatProvider provider = null) @@ -181,7 +181,7 @@ namespace System return false; } - return TryParse(s.AsSpan(), NumberStyles.Integer, NumberFormatInfo.CurrentInfo, out result); + return TryParse(s.AsReadOnlySpan(), NumberStyles.Integer, NumberFormatInfo.CurrentInfo, out result); } public static bool TryParse(String s, NumberStyles style, IFormatProvider provider, out Int16 result) @@ -194,7 +194,7 @@ namespace System return false; } - return TryParse(s.AsSpan(), style, NumberFormatInfo.GetInstance(provider), out result); + return TryParse(s.AsReadOnlySpan(), style, NumberFormatInfo.GetInstance(provider), out result); } public static bool TryParse(ReadOnlySpan s, out Int16 result, NumberStyles style = NumberStyles.Integer, IFormatProvider provider = null) diff --git a/src/mscorlib/shared/System/Int32.cs b/src/mscorlib/shared/System/Int32.cs index d2ee9b3..f5e832a 100644 --- a/src/mscorlib/shared/System/Int32.cs +++ b/src/mscorlib/shared/System/Int32.cs @@ -119,7 +119,7 @@ namespace System public static int Parse(String s) { if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s); - return Number.ParseInt32(s.AsSpan(), NumberStyles.Integer, NumberFormatInfo.CurrentInfo); + return Number.ParseInt32(s.AsReadOnlySpan(), NumberStyles.Integer, NumberFormatInfo.CurrentInfo); } [Pure] @@ -127,7 +127,7 @@ namespace System { NumberFormatInfo.ValidateParseStyleInteger(style); if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s); - return Number.ParseInt32(s.AsSpan(), style, NumberFormatInfo.CurrentInfo); + return Number.ParseInt32(s.AsReadOnlySpan(), style, NumberFormatInfo.CurrentInfo); } // Parses an integer from a String in the given style. If @@ -138,7 +138,7 @@ namespace System public static int Parse(String s, IFormatProvider provider) { if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s); - return Number.ParseInt32(s.AsSpan(), NumberStyles.Integer, NumberFormatInfo.GetInstance(provider)); + return Number.ParseInt32(s.AsReadOnlySpan(), NumberStyles.Integer, NumberFormatInfo.GetInstance(provider)); } // Parses an integer from a String in the given style. If @@ -150,7 +150,7 @@ namespace System { NumberFormatInfo.ValidateParseStyleInteger(style); if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s); - return Number.ParseInt32(s.AsSpan(), style, NumberFormatInfo.GetInstance(provider)); + return Number.ParseInt32(s.AsReadOnlySpan(), style, NumberFormatInfo.GetInstance(provider)); } public static int Parse(ReadOnlySpan s, NumberStyles style = NumberStyles.Integer, IFormatProvider provider = null) @@ -171,7 +171,7 @@ namespace System return false; } - return Number.TryParseInt32(s.AsSpan(), NumberStyles.Integer, NumberFormatInfo.CurrentInfo, out result); + return Number.TryParseInt32(s.AsReadOnlySpan(), NumberStyles.Integer, NumberFormatInfo.CurrentInfo, out result); } // Parses an integer from a String in the given style. Returns false rather @@ -188,7 +188,7 @@ namespace System return false; } - return Number.TryParseInt32(s.AsSpan(), style, NumberFormatInfo.GetInstance(provider), out result); + return Number.TryParseInt32(s.AsReadOnlySpan(), style, NumberFormatInfo.GetInstance(provider), out result); } public static bool TryParse(ReadOnlySpan s, out int result, NumberStyles style = NumberStyles.Integer, IFormatProvider provider = null) diff --git a/src/mscorlib/shared/System/Int64.cs b/src/mscorlib/shared/System/Int64.cs index 7ee4198..785fa39 100644 --- a/src/mscorlib/shared/System/Int64.cs +++ b/src/mscorlib/shared/System/Int64.cs @@ -111,20 +111,20 @@ namespace System public static long Parse(String s) { if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s); - return Number.ParseInt64(s.AsSpan(), NumberStyles.Integer, NumberFormatInfo.CurrentInfo); + return Number.ParseInt64(s.AsReadOnlySpan(), NumberStyles.Integer, NumberFormatInfo.CurrentInfo); } public static long Parse(String s, NumberStyles style) { NumberFormatInfo.ValidateParseStyleInteger(style); if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s); - return Number.ParseInt64(s.AsSpan(), style, NumberFormatInfo.CurrentInfo); + return Number.ParseInt64(s.AsReadOnlySpan(), style, NumberFormatInfo.CurrentInfo); } public static long Parse(String s, IFormatProvider provider) { if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s); - return Number.ParseInt64(s.AsSpan(), NumberStyles.Integer, NumberFormatInfo.GetInstance(provider)); + return Number.ParseInt64(s.AsReadOnlySpan(), NumberStyles.Integer, NumberFormatInfo.GetInstance(provider)); } @@ -136,7 +136,7 @@ namespace System { NumberFormatInfo.ValidateParseStyleInteger(style); if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s); - return Number.ParseInt64(s.AsSpan(), style, NumberFormatInfo.GetInstance(provider)); + return Number.ParseInt64(s.AsReadOnlySpan(), style, NumberFormatInfo.GetInstance(provider)); } public static long Parse(ReadOnlySpan s, NumberStyles style = NumberStyles.Integer, IFormatProvider provider = null) @@ -153,7 +153,7 @@ namespace System return false; } - return Number.TryParseInt64(s.AsSpan(), NumberStyles.Integer, NumberFormatInfo.CurrentInfo, out result); + return Number.TryParseInt64(s.AsReadOnlySpan(), NumberStyles.Integer, NumberFormatInfo.CurrentInfo, out result); } public static Boolean TryParse(String s, NumberStyles style, IFormatProvider provider, out Int64 result) @@ -166,7 +166,7 @@ namespace System return false; } - return Number.TryParseInt64(s.AsSpan(), style, NumberFormatInfo.GetInstance(provider), out result); + return Number.TryParseInt64(s.AsReadOnlySpan(), style, NumberFormatInfo.GetInstance(provider), out result); } public static bool TryParse(ReadOnlySpan s, out long result, NumberStyles style = NumberStyles.Integer, IFormatProvider provider = null) diff --git a/src/mscorlib/shared/System/SByte.cs b/src/mscorlib/shared/System/SByte.cs index 375f6bb..c984767 100644 --- a/src/mscorlib/shared/System/SByte.cs +++ b/src/mscorlib/shared/System/SByte.cs @@ -112,7 +112,7 @@ namespace System public static sbyte Parse(String s) { if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s); - return Parse(s.AsSpan(), NumberStyles.Integer, NumberFormatInfo.CurrentInfo); + return Parse(s.AsReadOnlySpan(), NumberStyles.Integer, NumberFormatInfo.CurrentInfo); } [CLSCompliant(false)] @@ -120,14 +120,14 @@ namespace System { NumberFormatInfo.ValidateParseStyleInteger(style); if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s); - return Parse(s.AsSpan(), style, NumberFormatInfo.CurrentInfo); + return Parse(s.AsReadOnlySpan(), style, NumberFormatInfo.CurrentInfo); } [CLSCompliant(false)] public static sbyte Parse(String s, IFormatProvider provider) { if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s); - return Parse(s.AsSpan(), NumberStyles.Integer, NumberFormatInfo.GetInstance(provider)); + return Parse(s.AsReadOnlySpan(), NumberStyles.Integer, NumberFormatInfo.GetInstance(provider)); } // Parses a signed byte from a String in the given style. If @@ -139,7 +139,7 @@ namespace System { NumberFormatInfo.ValidateParseStyleInteger(style); if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s); - return Parse(s.AsSpan(), style, NumberFormatInfo.GetInstance(provider)); + return Parse(s.AsReadOnlySpan(), style, NumberFormatInfo.GetInstance(provider)); } [CLSCompliant(false)] @@ -152,7 +152,7 @@ namespace System private static sbyte Parse(String s, NumberStyles style, NumberFormatInfo info) { if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s); - return Parse(s.AsSpan(), style, info); + return Parse(s.AsReadOnlySpan(), style, info); } private static sbyte Parse(ReadOnlySpan s, NumberStyles style, NumberFormatInfo info) @@ -189,7 +189,7 @@ namespace System return false; } - return TryParse(s.AsSpan(), NumberStyles.Integer, NumberFormatInfo.CurrentInfo, out result); + return TryParse(s.AsReadOnlySpan(), NumberStyles.Integer, NumberFormatInfo.CurrentInfo, out result); } [CLSCompliant(false)] @@ -203,7 +203,7 @@ namespace System return false; } - return TryParse(s.AsSpan(), style, NumberFormatInfo.GetInstance(provider), out result); + return TryParse(s.AsReadOnlySpan(), style, NumberFormatInfo.GetInstance(provider), out result); } [CLSCompliant(false)] diff --git a/src/mscorlib/shared/System/Single.cs b/src/mscorlib/shared/System/Single.cs index bb110b5..0be8cfb 100644 --- a/src/mscorlib/shared/System/Single.cs +++ b/src/mscorlib/shared/System/Single.cs @@ -269,27 +269,27 @@ namespace System public static float Parse(String s) { if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s); - return Number.ParseSingle(s.AsSpan(), NumberStyles.Float | NumberStyles.AllowThousands, NumberFormatInfo.CurrentInfo); + return Number.ParseSingle(s.AsReadOnlySpan(), NumberStyles.Float | NumberStyles.AllowThousands, NumberFormatInfo.CurrentInfo); } public static float Parse(String s, NumberStyles style) { NumberFormatInfo.ValidateParseStyleFloatingPoint(style); if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s); - return Number.ParseSingle(s.AsSpan(), style, NumberFormatInfo.CurrentInfo); + return Number.ParseSingle(s.AsReadOnlySpan(), style, NumberFormatInfo.CurrentInfo); } public static float Parse(String s, IFormatProvider provider) { if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s); - return Number.ParseSingle(s.AsSpan(), NumberStyles.Float | NumberStyles.AllowThousands, NumberFormatInfo.GetInstance(provider)); + return Number.ParseSingle(s.AsReadOnlySpan(), NumberStyles.Float | NumberStyles.AllowThousands, NumberFormatInfo.GetInstance(provider)); } public static float Parse(String s, NumberStyles style, IFormatProvider provider) { NumberFormatInfo.ValidateParseStyleFloatingPoint(style); if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s); - return Number.ParseSingle(s.AsSpan(), style, NumberFormatInfo.GetInstance(provider)); + return Number.ParseSingle(s.AsReadOnlySpan(), style, NumberFormatInfo.GetInstance(provider)); } public static float Parse(ReadOnlySpan s, NumberStyles style = NumberStyles.Integer, IFormatProvider provider = null) @@ -306,7 +306,7 @@ namespace System return false; } - return TryParse(s.AsSpan(), NumberStyles.Float | NumberStyles.AllowThousands, NumberFormatInfo.CurrentInfo, out result); + return TryParse(s.AsReadOnlySpan(), NumberStyles.Float | NumberStyles.AllowThousands, NumberFormatInfo.CurrentInfo, out result); } public static Boolean TryParse(String s, NumberStyles style, IFormatProvider provider, out Single result) @@ -319,7 +319,7 @@ namespace System return false; } - return TryParse(s.AsSpan(), style, NumberFormatInfo.GetInstance(provider), out result); + return TryParse(s.AsReadOnlySpan(), style, NumberFormatInfo.GetInstance(provider), out result); } public static Boolean TryParse(ReadOnlySpan s, out Single result, NumberStyles style = NumberStyles.Integer, IFormatProvider provider = null) diff --git a/src/mscorlib/shared/System/Span.NonGeneric.cs b/src/mscorlib/shared/System/Span.NonGeneric.cs index 62b6faa..60deccb 100644 --- a/src/mscorlib/shared/System/Span.NonGeneric.cs +++ b/src/mscorlib/shared/System/Span.NonGeneric.cs @@ -111,6 +111,10 @@ namespace System checked((int)((long)source.Length * Unsafe.SizeOf() / Unsafe.SizeOf()))); } + [Obsolete("This method is obsolete. Use AsReadOnlySpan instead.", false)] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static ReadOnlySpan AsSpan(this string text) => AsReadOnlySpan(text); + /// /// Creates a new readonly span over the portion of the target string. /// @@ -118,7 +122,7 @@ namespace System /// Thrown when is a null /// reference (Nothing in Visual Basic). [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static ReadOnlySpan AsSpan(this string text) + public static ReadOnlySpan AsReadOnlySpan(this string text) { if (text == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.text); diff --git a/src/mscorlib/shared/System/StringSpanHelpers.cs b/src/mscorlib/shared/System/StringSpanHelpers.cs index dc285a2..bdfd965 100644 --- a/src/mscorlib/shared/System/StringSpanHelpers.cs +++ b/src/mscorlib/shared/System/StringSpanHelpers.cs @@ -17,7 +17,7 @@ namespace System throw new ArgumentOutOfRangeException(nameof(comparisonType)); public static bool Equals(this ReadOnlySpan left, string right) => - Equals(left, right.AsSpan()); + Equals(left, right.AsReadOnlySpan()); public static bool Equals(this ReadOnlySpan left, ReadOnlySpan right) { diff --git a/src/mscorlib/shared/System/UInt16.cs b/src/mscorlib/shared/System/UInt16.cs index 93e76f2..4cd290b 100644 --- a/src/mscorlib/shared/System/UInt16.cs +++ b/src/mscorlib/shared/System/UInt16.cs @@ -106,7 +106,7 @@ namespace System public static ushort Parse(String s) { if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s); - return Parse(s.AsSpan(), NumberStyles.Integer, NumberFormatInfo.CurrentInfo); + return Parse(s.AsReadOnlySpan(), NumberStyles.Integer, NumberFormatInfo.CurrentInfo); } [CLSCompliant(false)] @@ -114,7 +114,7 @@ namespace System { NumberFormatInfo.ValidateParseStyleInteger(style); if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s); - return Parse(s.AsSpan(), style, NumberFormatInfo.CurrentInfo); + return Parse(s.AsReadOnlySpan(), style, NumberFormatInfo.CurrentInfo); } @@ -122,7 +122,7 @@ namespace System public static ushort Parse(String s, IFormatProvider provider) { if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s); - return Parse(s.AsSpan(), NumberStyles.Integer, NumberFormatInfo.GetInstance(provider)); + return Parse(s.AsReadOnlySpan(), NumberStyles.Integer, NumberFormatInfo.GetInstance(provider)); } [CLSCompliant(false)] @@ -130,7 +130,7 @@ namespace System { NumberFormatInfo.ValidateParseStyleInteger(style); if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s); - return Parse(s.AsSpan(), style, NumberFormatInfo.GetInstance(provider)); + return Parse(s.AsReadOnlySpan(), style, NumberFormatInfo.GetInstance(provider)); } [CLSCompliant(false)] @@ -165,7 +165,7 @@ namespace System return false; } - return TryParse(s.AsSpan(), NumberStyles.Integer, NumberFormatInfo.CurrentInfo, out result); + return TryParse(s.AsReadOnlySpan(), NumberStyles.Integer, NumberFormatInfo.CurrentInfo, out result); } [CLSCompliant(false)] @@ -179,7 +179,7 @@ namespace System return false; } - return TryParse(s.AsSpan(), style, NumberFormatInfo.GetInstance(provider), out result); + return TryParse(s.AsReadOnlySpan(), style, NumberFormatInfo.GetInstance(provider), out result); } [CLSCompliant(false)] diff --git a/src/mscorlib/shared/System/UInt32.cs b/src/mscorlib/shared/System/UInt32.cs index d6cf943..4983561 100644 --- a/src/mscorlib/shared/System/UInt32.cs +++ b/src/mscorlib/shared/System/UInt32.cs @@ -115,7 +115,7 @@ namespace System public static uint Parse(String s) { if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s); - return Number.ParseUInt32(s.AsSpan(), NumberStyles.Integer, NumberFormatInfo.CurrentInfo); + return Number.ParseUInt32(s.AsReadOnlySpan(), NumberStyles.Integer, NumberFormatInfo.CurrentInfo); } [CLSCompliant(false)] @@ -123,7 +123,7 @@ namespace System { NumberFormatInfo.ValidateParseStyleInteger(style); if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s); - return Number.ParseUInt32(s.AsSpan(), style, NumberFormatInfo.CurrentInfo); + return Number.ParseUInt32(s.AsReadOnlySpan(), style, NumberFormatInfo.CurrentInfo); } @@ -131,7 +131,7 @@ namespace System public static uint Parse(String s, IFormatProvider provider) { if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s); - return Number.ParseUInt32(s.AsSpan(), NumberStyles.Integer, NumberFormatInfo.GetInstance(provider)); + return Number.ParseUInt32(s.AsReadOnlySpan(), NumberStyles.Integer, NumberFormatInfo.GetInstance(provider)); } [CLSCompliant(false)] @@ -139,7 +139,7 @@ namespace System { NumberFormatInfo.ValidateParseStyleInteger(style); if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s); - return Number.ParseUInt32(s.AsSpan(), style, NumberFormatInfo.GetInstance(provider)); + return Number.ParseUInt32(s.AsReadOnlySpan(), style, NumberFormatInfo.GetInstance(provider)); } [CLSCompliant(false)] @@ -158,7 +158,7 @@ namespace System return false; } - return Number.TryParseUInt32(s.AsSpan(), NumberStyles.Integer, NumberFormatInfo.CurrentInfo, out result); + return Number.TryParseUInt32(s.AsReadOnlySpan(), NumberStyles.Integer, NumberFormatInfo.CurrentInfo, out result); } [CLSCompliant(false)] @@ -172,7 +172,7 @@ namespace System return false; } - return Number.TryParseUInt32(s.AsSpan(), style, NumberFormatInfo.GetInstance(provider), out result); + return Number.TryParseUInt32(s.AsReadOnlySpan(), style, NumberFormatInfo.GetInstance(provider), out result); } [CLSCompliant(false)] diff --git a/src/mscorlib/shared/System/UInt64.cs b/src/mscorlib/shared/System/UInt64.cs index 586b811..424d48b 100644 --- a/src/mscorlib/shared/System/UInt64.cs +++ b/src/mscorlib/shared/System/UInt64.cs @@ -112,7 +112,7 @@ namespace System public static ulong Parse(String s) { if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s); - return Number.ParseUInt64(s.AsSpan(), NumberStyles.Integer, NumberFormatInfo.CurrentInfo); + return Number.ParseUInt64(s.AsReadOnlySpan(), NumberStyles.Integer, NumberFormatInfo.CurrentInfo); } [CLSCompliant(false)] @@ -120,14 +120,14 @@ namespace System { NumberFormatInfo.ValidateParseStyleInteger(style); if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s); - return Number.ParseUInt64(s.AsSpan(), style, NumberFormatInfo.CurrentInfo); + return Number.ParseUInt64(s.AsReadOnlySpan(), style, NumberFormatInfo.CurrentInfo); } [CLSCompliant(false)] public static ulong Parse(string s, IFormatProvider provider) { if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s); - return Number.ParseUInt64(s.AsSpan(), NumberStyles.Integer, NumberFormatInfo.GetInstance(provider)); + return Number.ParseUInt64(s.AsReadOnlySpan(), NumberStyles.Integer, NumberFormatInfo.GetInstance(provider)); } [CLSCompliant(false)] @@ -135,7 +135,7 @@ namespace System { NumberFormatInfo.ValidateParseStyleInteger(style); if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s); - return Number.ParseUInt64(s.AsSpan(), style, NumberFormatInfo.GetInstance(provider)); + return Number.ParseUInt64(s.AsReadOnlySpan(), style, NumberFormatInfo.GetInstance(provider)); } [CLSCompliant(false)] @@ -154,7 +154,7 @@ namespace System return false; } - return Number.TryParseUInt64(s.AsSpan(), NumberStyles.Integer, NumberFormatInfo.CurrentInfo, out result); + return Number.TryParseUInt64(s.AsReadOnlySpan(), NumberStyles.Integer, NumberFormatInfo.CurrentInfo, out result); } [CLSCompliant(false)] @@ -168,7 +168,7 @@ namespace System return false; } - return Number.TryParseUInt64(s.AsSpan(), style, NumberFormatInfo.GetInstance(provider), out result); + return Number.TryParseUInt64(s.AsReadOnlySpan(), style, NumberFormatInfo.GetInstance(provider), out result); } [CLSCompliant(false)] diff --git a/src/mscorlib/shared/System/Version.cs b/src/mscorlib/shared/System/Version.cs index 3d7158d..9c66d9b 100644 --- a/src/mscorlib/shared/System/Version.cs +++ b/src/mscorlib/shared/System/Version.cs @@ -319,7 +319,7 @@ namespace System throw new ArgumentNullException(nameof(input)); } - return ParseVersion(input.AsSpan(), throwOnFailure: true); + return ParseVersion(input.AsReadOnlySpan(), throwOnFailure: true); } public static Version Parse(ReadOnlySpan input) => @@ -333,7 +333,7 @@ namespace System return false; } - return (result = ParseVersion(input.AsSpan(), throwOnFailure: false)) != null; + return (result = ParseVersion(input.AsReadOnlySpan(), throwOnFailure: false)) != null; } public static bool TryParse(ReadOnlySpan input, out Version result) => diff --git a/src/mscorlib/src/System/Decimal.cs b/src/mscorlib/src/System/Decimal.cs index 6d7b953..5bb0446 100644 --- a/src/mscorlib/src/System/Decimal.cs +++ b/src/mscorlib/src/System/Decimal.cs @@ -511,27 +511,27 @@ namespace System public static Decimal Parse(String s) { if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s); - return Number.ParseDecimal(s.AsSpan(), NumberStyles.Number, NumberFormatInfo.CurrentInfo); + return Number.ParseDecimal(s.AsReadOnlySpan(), NumberStyles.Number, NumberFormatInfo.CurrentInfo); } public static Decimal Parse(String s, NumberStyles style) { NumberFormatInfo.ValidateParseStyleFloatingPoint(style); if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s); - return Number.ParseDecimal(s.AsSpan(), style, NumberFormatInfo.CurrentInfo); + return Number.ParseDecimal(s.AsReadOnlySpan(), style, NumberFormatInfo.CurrentInfo); } public static Decimal Parse(String s, IFormatProvider provider) { if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s); - return Number.ParseDecimal(s.AsSpan(), NumberStyles.Number, NumberFormatInfo.GetInstance(provider)); + return Number.ParseDecimal(s.AsReadOnlySpan(), NumberStyles.Number, NumberFormatInfo.GetInstance(provider)); } public static Decimal Parse(String s, NumberStyles style, IFormatProvider provider) { NumberFormatInfo.ValidateParseStyleFloatingPoint(style); if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s); - return Number.ParseDecimal(s.AsSpan(), style, NumberFormatInfo.GetInstance(provider)); + return Number.ParseDecimal(s.AsReadOnlySpan(), style, NumberFormatInfo.GetInstance(provider)); } public static decimal Parse(ReadOnlySpan s, NumberStyles style = NumberStyles.Integer, IFormatProvider provider = null) @@ -548,7 +548,7 @@ namespace System return false; } - return Number.TryParseDecimal(s.AsSpan(), NumberStyles.Number, NumberFormatInfo.CurrentInfo, out result); + return Number.TryParseDecimal(s.AsReadOnlySpan(), NumberStyles.Number, NumberFormatInfo.CurrentInfo, out result); } public static Boolean TryParse(String s, NumberStyles style, IFormatProvider provider, out Decimal result) @@ -561,7 +561,7 @@ namespace System return false; } - return Number.TryParseDecimal(s.AsSpan(), style, NumberFormatInfo.GetInstance(provider), out result); + return Number.TryParseDecimal(s.AsReadOnlySpan(), style, NumberFormatInfo.GetInstance(provider), out result); } public static bool TryParse(ReadOnlySpan s, out decimal result, NumberStyles style = NumberStyles.Integer, IFormatProvider provider = null) diff --git a/src/mscorlib/src/System/Number.cs b/src/mscorlib/src/System/Number.cs index 5c21c47..0eaa0b2 100644 --- a/src/mscorlib/src/System/Number.cs +++ b/src/mscorlib/src/System/Number.cs @@ -1268,7 +1268,7 @@ namespace System [System.Runtime.CompilerServices.FriendAccessAllowed] internal unsafe static Boolean TryStringToNumber(String str, NumberStyles options, ref NumberBuffer number, StringBuilder sb, NumberFormatInfo numfmt, Boolean parseDecimal) => str != null ? - TryStringToNumber(str.AsSpan(), options, ref number, numfmt, parseDecimal) : + TryStringToNumber(str.AsReadOnlySpan(), options, ref number, numfmt, parseDecimal) : false; internal static unsafe Boolean TryStringToNumber(ReadOnlySpan str, NumberStyles options, ref NumberBuffer number, NumberFormatInfo numfmt, Boolean parseDecimal) diff --git a/src/mscorlib/src/System/TimeZoneInfo.Unix.cs b/src/mscorlib/src/System/TimeZoneInfo.Unix.cs index 6f3448a..2c044bf 100644 --- a/src/mscorlib/src/System/TimeZoneInfo.Unix.cs +++ b/src/mscorlib/src/System/TimeZoneInfo.Unix.cs @@ -1123,9 +1123,9 @@ namespace System int secondDotIndex = dateRule.IndexOf('.', firstDotIndex + 1); if (secondDotIndex > 0) { - if (int.TryParse(dateRule.AsSpan().Slice(1, firstDotIndex - 1), out month) && - int.TryParse(dateRule.AsSpan().Slice(firstDotIndex + 1, secondDotIndex - firstDotIndex - 1), out week) && - int.TryParse(dateRule.AsSpan().Slice(secondDotIndex + 1), out int day)) + if (int.TryParse(dateRule.AsReadOnlySpan().Slice(1, firstDotIndex - 1), out month) && + int.TryParse(dateRule.AsReadOnlySpan().Slice(firstDotIndex + 1, secondDotIndex - firstDotIndex - 1), out week) && + int.TryParse(dateRule.AsReadOnlySpan().Slice(secondDotIndex + 1), out int day)) { dayOfWeek = (DayOfWeek)day; return true; diff --git a/tests/src/JIT/Performance/CodeQuality/Span/SpanBench.cs b/tests/src/JIT/Performance/CodeQuality/Span/SpanBench.cs index c9a4c7b..d0ccbc5 100644 --- a/tests/src/JIT/Performance/CodeQuality/Span/SpanBench.cs +++ b/tests/src/JIT/Performance/CodeQuality/Span/SpanBench.cs @@ -1011,11 +1011,11 @@ namespace Span } #endregion - #region TestSpanAsSpanStringChar + #region TestSpanAsReadOnlySpanStringChar [Benchmark(InnerIterationCount = BaseIterations)] [InlineData(100)] - public static void TestSpanAsSpanStringCharWrapper(int length) + public static void TestSpanAsReadOnlySpanStringCharWrapper(int length) { StringBuilder sb = new StringBuilder(); Random rand = new Random(42); @@ -1027,12 +1027,12 @@ namespace Span } string s = sb.ToString(); - Invoke((int innerIterationCount) => TestSpanAsSpanStringChar(s, innerIterationCount, false), - "TestSpanAsSpanStringChar({0})", length); + Invoke((int innerIterationCount) => TestSpanAsReadOnlySpanStringChar(s, innerIterationCount, false), + "TestSpanAsReadOnlySpanStringChar({0})", length); } [MethodImpl(MethodImplOptions.NoInlining)] - static void TestSpanAsSpanStringChar(string s, int iterationCount, bool untrue) + static void TestSpanAsReadOnlySpanStringChar(string s, int iterationCount, bool untrue) { var sink = Sink.Instance; -- 2.7.4