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

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

Merged
merged 2 commits into from
Apr 11, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions src/libraries/Common/src/System/Text/ValueStringBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
stephentoub marked this conversation as resolved.
Show resolved Hide resolved
s
#else
s.AsSpan()
#endif
.CopyTo(_chars.Slice(index));
_pos += count;
}

Expand Down Expand Up @@ -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;
}

Expand Down
4 changes: 2 additions & 2 deletions src/libraries/System.Private.CoreLib/src/System/Enum.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -932,7 +932,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;
Expand Down Expand Up @@ -979,7 +979,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;
Expand Down Expand Up @@ -1205,7 +1205,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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
34 changes: 34 additions & 0 deletions src/libraries/System.Private.CoreLib/src/System/String.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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, (nuint)Length);
stephentoub marked this conversation as resolved.
Show resolved Hide resolved
}
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, (nuint)Length);
stephentoub marked this conversation as resolved.
Show resolved Hide resolved
retVal = true;
}
return retVal;
}

// Returns the entire string as an array of characters.
public char[] ToCharArray()
{
Expand Down