Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion src/libraries/Common/src/System/Net/SocketAddress.cs
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ public override string ToString()
stackalloc char[256] :
new char[maxLength];

familyString.AsSpan().CopyTo(result);
familyString.CopyTo(result);
int length = familyString.Length;

result[length++] = ':';
Expand Down
10 changes: 4 additions & 6 deletions src/libraries/Common/src/System/Text/ValueStringBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,9 @@ public void Insert(int index, string? s)

int remaining = _pos - index;
_chars.Slice(index, remaining).CopyTo(_chars.Slice(index + count));
#if SYSTEM_PRIVATE_CORELIB
s
#else
s.AsSpan()
#if !NET6_0_OR_GREATER
.AsSpan()
#endif
.CopyTo(_chars.Slice(index));
_pos += count;
Expand Down Expand Up @@ -210,10 +209,9 @@ private void AppendSlow(string s)
Grow(s.Length);
}

#if SYSTEM_PRIVATE_CORELIB
s
#else
s.AsSpan()
#if !NET6_0_OR_GREATER
.AsSpan()
#endif
.CopyTo(_chars.Slice(pos));
_pos += s.Length;
Expand Down
36 changes: 36 additions & 0 deletions src/libraries/Common/tests/Tests/System/StringTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,42 @@ public static void CopyTo_Invalid()
AssertExtensions.Throws<ArgumentOutOfRangeException>("sourceIndex", () => s.CopyTo(0, dst, 0, 6));
}

[Theory]
[InlineData("", 0)]
[InlineData("", 1)]
[InlineData("a", 1)]
[InlineData("a", 0)]
[InlineData("a", 2)]
[InlineData("abc", 2)]
[InlineData("abc", 3)]
[InlineData("abc", 4)]
[InlineData("Hello world", 20)]
public static void CopyTo_Span(string s, int destinationLength)
{
char[] destination = new char[destinationLength];

if (s.Length > destinationLength)
{
AssertExtensions.Throws<ArgumentException>("destination", () => s.CopyTo(destination));
Assert.All(destination, c => Assert.Equal(0, c));

Assert.False(s.TryCopyTo(destination));
Assert.All(destination, c => Assert.Equal(0, c));
}
else
{
s.CopyTo(destination);
Assert.Equal(s, new Span<char>(destination, 0, s.Length).ToString());
Assert.All(destination.AsSpan(s.Length).ToArray(), c => Assert.Equal(0, c));

Array.Clear(destination, 0, destination.Length);

Assert.True(s.TryCopyTo(destination));
Assert.Equal(s, new Span<char>(destination, 0, s.Length).ToString());
Assert.All(destination.AsSpan(s.Length).ToArray(), c => Assert.Equal(0, c));
}
}

public static IEnumerable<object[]> Compare_TestData()
{
// CurrentCulture
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,14 @@ public override void Fail(string? message, string? detailMessage)
TraceEvent(null, SR.TraceAsTraceSource, TraceEventType.Error, 0, string.Create(length, (message, detailMessage),
(dst, v) =>
{
ReadOnlySpan<char> prefix = v.message;
string prefix = v.message;
prefix.CopyTo(dst);

if (v.detailMessage != null)
{
dst[prefix.Length] = ' ';

ReadOnlySpan<char> detail = v.detailMessage;
string detail = v.detailMessage;
detail.CopyTo(dst.Slice(prefix.Length + 1, detail.Length));
}
}));
Expand Down
7 changes: 2 additions & 5 deletions src/libraries/System.Private.CoreLib/src/System/String.cs
Original file line number Diff line number Diff line change
Expand Up @@ -461,14 +461,11 @@ 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)
public void CopyTo(Span<char> destination)
{
if ((uint)Length <= (uint)destination.Length)
{
Expand All @@ -484,7 +481,7 @@ internal void CopyTo(Span<char> destination)
/// <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)
public bool TryCopyTo(Span<char> destination)
{
bool retVal = false;
if ((uint)Length <= (uint)destination.Length)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public static string MakePascal(string identifier)
{
return string.Create(identifier.Length, identifier, static (buffer, identifier) =>
{
identifier.AsSpan().CopyTo(buffer);
identifier.CopyTo(buffer);
buffer[0] = char.ToUpperInvariant(buffer[0]); // convert only first char to uppercase; leave all else as-is
});
}
Expand All @@ -59,7 +59,7 @@ public static string MakeCamel(string identifier)
{
return string.Create(identifier.Length, identifier, static (buffer, identifier) =>
{
identifier.AsSpan().CopyTo(buffer);
identifier.CopyTo(buffer);
buffer[0] = char.ToLowerInvariant(buffer[0]); // convert only first char to lowercase; leave all else as-is
});
}
Expand Down
2 changes: 2 additions & 0 deletions src/libraries/System.Runtime/ref/System.Runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3632,6 +3632,7 @@ public unsafe String(sbyte* value, int startIndex, int length, System.Text.Encod
[System.ObsoleteAttribute("This API should not be used to create mutable strings. See https://go.microsoft.com/fwlink/?linkid=2084035 for alternatives.")]
public static System.String Copy(System.String str) { throw null; }
public void CopyTo(int sourceIndex, char[] destination, int destinationIndex, int count) { }
public void CopyTo(System.Span<char> destination) { }
public static System.String Create<TState>(int length, TState state, System.Buffers.SpanAction<char, TState> action) { throw null; }
public bool EndsWith(char value) { throw null; }
public bool EndsWith(System.String value) { throw null; }
Expand Down Expand Up @@ -3767,6 +3768,7 @@ public void CopyTo(int sourceIndex, char[] destination, int destinationIndex, in
public System.String TrimStart() { throw null; }
public System.String TrimStart(char trimChar) { throw null; }
public System.String TrimStart(params char[]? trimChars) { throw null; }
public bool TryCopyTo(System.Span<char> destination) { throw null; }
}
public abstract partial class StringComparer : System.Collections.Generic.IComparer<string?>, System.Collections.Generic.IEqualityComparer<string?>, System.Collections.IComparer, System.Collections.IEqualityComparer
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,9 @@ static ReadOnlySpan<char> WritePostEB(ReadOnlySpan<char> label, Span<char> desti
{
int size = PostEBPrefix.Length + label.Length + Ending.Length;
Debug.Assert(destination.Length >= size);
PostEBPrefix.AsSpan().CopyTo(destination);
PostEBPrefix.CopyTo(destination);
label.CopyTo(destination.Slice(PostEBPrefix.Length));
Ending.AsSpan().CopyTo(destination.Slice(PostEBPrefix.Length + label.Length));
Ending.CopyTo(destination.Slice(PostEBPrefix.Length + label.Length));
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this is a netcoreapp2.1 assembly? Surprised CI isn't complaining.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, then it looks like I should stop paying attention to source.dot.net. :)

https://source.dot.net/System.Security.Cryptography.Encoding/System.Security.Cryptography.Encoding.csproj.html

<TargetFramework>netcoreapp2.1</TargetFramework>

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alexperovich, where is that file coming from? I can't find anything in the repo or in the history of the csproj that looks like that?

return destination.Slice(0, size);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -740,7 +740,7 @@ private static string UrlPathAppend(string baseUri, ReadOnlyMemory<char> resourc
(baseUri, resource),
(buf, st) =>
{
st.baseUri.AsSpan().CopyTo(buf);
st.baseUri.CopyTo(buf);
st.resource.Span.CopyTo(buf.Slice(st.baseUri.Length));
});
}
Expand All @@ -750,7 +750,7 @@ private static string UrlPathAppend(string baseUri, ReadOnlyMemory<char> resourc
(baseUri, resource),
(buf, st) =>
{
st.baseUri.AsSpan().CopyTo(buf);
st.baseUri.CopyTo(buf);
buf[st.baseUri.Length] = '/';
st.resource.Span.CopyTo(buf.Slice(st.baseUri.Length + 1));
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ public override string ConvertName(string name)
#if BUILDING_INBOX_LIBRARY
return string.Create(name.Length, name, (chars, name) =>
{
name.AsSpan().CopyTo(chars);
name
#if !NET6_0_OR_GREATER
.AsSpan()
#endif
.CopyTo(chars);
FixCasing(chars);
});
#else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -576,11 +576,11 @@ public static string ConvertOldStringsToClass(string set, string category)
span[FlagsIndex] = '\0';
span[SetLengthIndex] = (char)state.set.Length;
span[CategoryLengthIndex] = (char)state.category.Length;
state.set.AsSpan().CopyTo(span.Slice(SetStartIndex));
state.set.CopyTo(span.Slice(SetStartIndex));
index = SetStartIndex + state.set.Length;
}

state.category.AsSpan().CopyTo(span.Slice(index));
state.category.CopyTo(span.Slice(index));
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ internal static string ValidateString(string input)
// slow case: surrogates exist, so we need to validate them
return string.Create(input.Length, (input, idxOfFirstSurrogate), (chars, state) =>
{
state.input.AsSpan().CopyTo(chars);
state.input.CopyTo(chars);
for (int i = state.idxOfFirstSurrogate; i < chars.Length; i++)
{
char thisChar = chars[i];
Expand Down