未验证 提交 9bb0427e 编写于 作者: S Stephen Toub 提交者: GitHub

Add internal String.{Try}CopyTo and use in a few places in corelib (#51062)

* Add internal String.{Try}CopyTo and use in a few places in corelib

We can use in more if it's made public.

* Apply suggestions from code review
Co-authored-by: NLevi Broderick <GrabYourPitchforks@users.noreply.github.com>
Co-authored-by: NLevi Broderick <GrabYourPitchforks@users.noreply.github.com>
上级 160fd45f
......@@ -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;
}
......
......@@ -230,7 +230,7 @@ private static string ValueToHexString(object value)
Span<char> resultSpan = new Span<char>(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);
......
......@@ -176,7 +176,7 @@ public override string ToString()
static void Write(string source, ref Span<char> dest)
{
source.AsSpan().CopyTo(dest);
source.CopyTo(dest);
dest = dest.Slice(source.Length);
}
}
......
......@@ -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));
......
......@@ -758,7 +758,7 @@ private static bool TryCopyTo(string source, Span<char> destination, out int cha
{
Debug.Assert(source != null);
if (source.AsSpan().TryCopyTo(destination))
if (source.TryCopyTo(destination))
{
charsWritten = source.Length;
return true;
......
......@@ -960,7 +960,7 @@ public static unsafe IntPtr StringToHGlobalUni(string? s)
IntPtr ptr = AllocHGlobal((IntPtr)nb);
s.AsSpan().CopyTo(new Span<char>((char*)ptr, s.Length));
s.CopyTo(new Span<char>((char*)ptr, s.Length));
((char*)ptr)[s.Length] = '\0';
return ptr;
......@@ -1007,7 +1007,7 @@ public static unsafe IntPtr StringToCoTaskMemUni(string? s)
IntPtr ptr = AllocCoTaskMem(nb);
s.AsSpan().CopyTo(new Span<char>((char*)ptr, s.Length));
s.CopyTo(new Span<char>((char*)ptr, s.Length));
((char*)ptr)[s.Length] = '\0';
return ptr;
......@@ -1233,7 +1233,7 @@ public static unsafe IntPtr StringToBSTR(string? s)
IntPtr bstr = AllocBSTR(s.Length);
s.AsSpan().CopyTo(new Span<char>((char*)bstr, s.Length)); // AllocBSTR already included the null terminator
s.CopyTo(new Span<char>((char*)bstr, s.Length)); // AllocBSTR already included the null terminator
return bstr;
}
......
......@@ -1141,7 +1141,7 @@ private string ReplaceHelper(int oldValueLength, string newValue, ReadOnlySpan<i
thisIdx = replacementIdx + oldValueLength;
// Copy over newValue to replace the oldValue.
newValue.AsSpan().CopyTo(dstSpan.Slice(dstIdx));
newValue.CopyTo(dstSpan.Slice(dstIdx));
dstIdx += newValue.Length;
}
......
......@@ -447,6 +447,40 @@ public unsafe void CopyTo(int sourceIndex, char[] destination, int destinationIn
elementCount: (uint)count);
}
// TODO: https://github.com/dotnet/runtime/issues/51061
// Make these {Try}CopyTo methods public and use throughout dotnet/runtime.
/// <summary>Copies the contents of this string into the destination span.</summary>
/// <param name="destination">The span into which to copy this string's contents.</param>
/// <exception cref="System.ArgumentException">The destination span is shorter than the source string.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal void CopyTo(Span<char> destination)
{
if ((uint)Length <= (uint)destination.Length)
{
Buffer.Memmove(ref destination._pointer.Value, ref _firstChar, (uint)Length);
}
else
{
ThrowHelper.ThrowArgumentException_DestinationTooShort();
}
}
/// <summary>Copies the contents of this string into the destination span.</summary>
/// <param name="destination">The span into which to copy this string's contents.</param>
/// <returns>true if the data was copied; false if the destination was too short to fit the contents of the string.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal bool TryCopyTo(Span<char> 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()
{
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册