diff --git a/src/libraries/Common/src/System/Text/ValueStringBuilder.cs b/src/libraries/Common/src/System/Text/ValueStringBuilder.cs index c4c4ea1ba478d..fbba96711347e 100644 --- a/src/libraries/Common/src/System/Text/ValueStringBuilder.cs +++ b/src/libraries/Common/src/System/Text/ValueStringBuilder.cs @@ -158,7 +158,12 @@ public void Insert(int index, string? s) int remaining = _pos - index; _chars.Slice(index, remaining).CopyTo(_chars.Slice(index + count)); - s.AsSpan().CopyTo(_chars.Slice(index)); +#if SYSTEM_PRIVATE_CORELIB + s +#else + s.AsSpan() +#endif + .CopyTo(_chars.Slice(index)); _pos += count; } @@ -205,7 +210,12 @@ private void AppendSlow(string s) Grow(s.Length); } - s.AsSpan().CopyTo(_chars.Slice(pos)); +#if SYSTEM_PRIVATE_CORELIB + s +#else + s.AsSpan() +#endif + .CopyTo(_chars.Slice(pos)); _pos += s.Length; } diff --git a/src/libraries/System.Private.CoreLib/src/System/Enum.cs b/src/libraries/System.Private.CoreLib/src/System/Enum.cs index bd2b4f71f1833..cb86c8353c268 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Enum.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Enum.cs @@ -230,7 +230,7 @@ private static string ValueToHexString(object value) Span resultSpan = new Span(ref result.GetRawStringData(), result.Length); string name = names[foundItems[--foundItemsCount]]; - name.AsSpan().CopyTo(resultSpan); + name.CopyTo(resultSpan); resultSpan = resultSpan.Slice(name.Length); while (--foundItemsCount >= 0) { @@ -239,7 +239,7 @@ private static string ValueToHexString(object value) resultSpan = resultSpan.Slice(2); name = names[foundItems[foundItemsCount]]; - name.AsSpan().CopyTo(resultSpan); + name.CopyTo(resultSpan); resultSpan = resultSpan.Slice(name.Length); } Debug.Assert(resultSpan.IsEmpty); diff --git a/src/libraries/System.Private.CoreLib/src/System/Exception.cs b/src/libraries/System.Private.CoreLib/src/System/Exception.cs index cef527ee50d15..a36983489dca0 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Exception.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Exception.cs @@ -176,7 +176,7 @@ public override string ToString() static void Write(string source, ref Span dest) { - source.AsSpan().CopyTo(dest); + source.CopyTo(dest); dest = dest.Slice(source.Length); } } diff --git a/src/libraries/System.Private.CoreLib/src/System/Globalization/TimeSpanFormat.cs b/src/libraries/System.Private.CoreLib/src/System/Globalization/TimeSpanFormat.cs index 1ed63ae31a7b5..01162580b7876 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Globalization/TimeSpanFormat.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Globalization/TimeSpanFormat.cs @@ -269,7 +269,7 @@ private static bool TryFormatStandard(TimeSpan value, StandardFormat format, str } else { - decimalSeparator.AsSpan().CopyTo(destination); + decimalSeparator.CopyTo(destination); idx += decimalSeparator.Length; } WriteDigits(fraction, destination.Slice(idx, fractionDigits)); diff --git a/src/libraries/System.Private.CoreLib/src/System/Number.Formatting.cs b/src/libraries/System.Private.CoreLib/src/System/Number.Formatting.cs index 7cf4fc606097b..be13d58792365 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Number.Formatting.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Number.Formatting.cs @@ -758,7 +758,7 @@ private static bool TryCopyTo(string source, Span destination, out int cha { Debug.Assert(source != null); - if (source.AsSpan().TryCopyTo(destination)) + if (source.TryCopyTo(destination)) { charsWritten = source.Length; return true; diff --git a/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.cs b/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.cs index 81c87972f7842..e49e4c2d7604f 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.cs @@ -932,7 +932,7 @@ public static unsafe IntPtr StringToHGlobalUni(string? s) IntPtr ptr = AllocHGlobal((IntPtr)nb); - s.AsSpan().CopyTo(new Span((char*)ptr, s.Length)); + s.CopyTo(new Span((char*)ptr, s.Length)); ((char*)ptr)[s.Length] = '\0'; return ptr; @@ -979,7 +979,7 @@ public static unsafe IntPtr StringToCoTaskMemUni(string? s) IntPtr ptr = AllocCoTaskMem(nb); - s.AsSpan().CopyTo(new Span((char*)ptr, s.Length)); + s.CopyTo(new Span((char*)ptr, s.Length)); ((char*)ptr)[s.Length] = '\0'; return ptr; @@ -1205,7 +1205,7 @@ public static unsafe IntPtr StringToBSTR(string? s) IntPtr bstr = AllocBSTR(s.Length); - s.AsSpan().CopyTo(new Span((char*)bstr, s.Length)); // AllocBSTR already included the null terminator + s.CopyTo(new Span((char*)bstr, s.Length)); // AllocBSTR already included the null terminator return bstr; } diff --git a/src/libraries/System.Private.CoreLib/src/System/String.Manipulation.cs b/src/libraries/System.Private.CoreLib/src/System/String.Manipulation.cs index 8c6a7f1420d43..558d0a399af12 100644 --- a/src/libraries/System.Private.CoreLib/src/System/String.Manipulation.cs +++ b/src/libraries/System.Private.CoreLib/src/System/String.Manipulation.cs @@ -1141,7 +1141,7 @@ private string ReplaceHelper(int oldValueLength, string newValue, ReadOnlySpanCopies the contents of this string into the destination span. + /// The span into which to copy this string's contents. + /// The destination span is shorter than the source string. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + internal void CopyTo(Span destination) + { + if ((uint)Length <= (uint)destination.Length) + { + Buffer.Memmove(ref destination._pointer.Value, ref _firstChar, (uint)Length); + } + else + { + ThrowHelper.ThrowArgumentException_DestinationTooShort(); + } + } + + /// Copies the contents of this string into the destination span. + /// The span into which to copy this string's contents. + /// true if the data was copied; false if the destination was too short to fit the contents of the string. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + internal bool TryCopyTo(Span destination) + { + bool retVal = false; + if ((uint)Length <= (uint)destination.Length) + { + Buffer.Memmove(ref destination._pointer.Value, ref _firstChar, (uint)Length); + retVal = true; + } + return retVal; + } + // Returns the entire string as an array of characters. public char[] ToCharArray() {