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
20 changes: 17 additions & 3 deletions src/libraries/Common/src/System/HexConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ public static void EncodeToUtf16(ReadOnlySpan<byte> bytes, Span<char> chars, Cas
}
}

public static unsafe string ToString(ReadOnlySpan<byte> bytes, Casing casing = Casing.Upper)
public static string ToString(ReadOnlySpan<byte> bytes, Casing casing = Casing.Upper)
Comment thread
EgorBo marked this conversation as resolved.
{
#if NETFRAMEWORK || NETSTANDARD2_0
Span<char> result = bytes.Length > 16 ?
Expand All @@ -192,12 +192,26 @@ public static unsafe string ToString(ReadOnlySpan<byte> bytes, Casing casing = C
pos += 2;
}
return result.ToString();
#elif NET9_0_OR_GREATER
SpanCasingPair args = new() { Bytes = bytes, Casing = casing };
return string.Create(bytes.Length * 2, args, static (chars, args) =>
EncodeToUtf16(args.Bytes, chars, args.Casing));
#else
return string.Create(bytes.Length * 2, (RosPtr: (IntPtr)(&bytes), casing), static (chars, args) =>
EncodeToUtf16(*(ReadOnlySpan<byte>*)args.RosPtr, chars, args.casing));
// .NET 8.0 path (doesn't support 'allow ref struct' feature)
unsafe
{
return string.Create(bytes.Length * 2, (RosPtr: (IntPtr)(&bytes), casing), static (chars, args) =>
EncodeToUtf16(*(ReadOnlySpan<byte>*)args.RosPtr, chars, args.casing));
}

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.

This will be removed once we stop building for net8.0 (this november?)

#endif
}

private ref struct SpanCasingPair
Comment thread
EgorBo marked this conversation as resolved.
{
public ReadOnlySpan<byte> Bytes { get; set; }
public Casing Casing { get; set; }
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static char ToCharUpper(int value)
{
Expand Down
16 changes: 0 additions & 16 deletions src/libraries/Common/src/System/Text/ValueStringBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -235,22 +235,6 @@ public void Append(char c, int count)
_pos += count;
}

public unsafe void Append(char* value, int length)
{
int pos = _pos;
if (pos > _chars.Length - length)
{
Grow(length);
}

Span<char> dst = _chars.Slice(_pos, length);
for (int i = 0; i < dst.Length; i++)
{
dst[i] = *value++;
}
_pos += length;
}

public void Append(scoped ReadOnlySpan<char> value)
{
int pos = _pos;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,25 +103,6 @@ public void Append_CharInt_MatchesStringBuilder()
Assert.Equal(sb.ToString(), vsb.ToString());
}

[Fact]
public unsafe void Append_PtrInt_MatchesStringBuilder()
{
var sb = new StringBuilder();
var vsb = new ValueStringBuilder();
for (int i = 1; i <= 100; i++)
{
string s = i.ToString();
fixed (char* p = s)
{
sb.Append(p, s.Length);
vsb.Append(p, s.Length);
}
}

Assert.Equal(sb.Length, vsb.Length);
Assert.Equal(sb.ToString(), vsb.ToString());
}

[Fact]
public void AppendSpan_DataAppendedCorrectly()
{
Expand Down Expand Up @@ -266,7 +247,7 @@ public void Dispose_ClearsBuilder_ThenReusable()
}

[Fact]
public unsafe void Indexer()
public void Indexer()
{
const string Text1 = "foobar";
var vsb = new ValueStringBuilder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public static unsafe int UnescapePercentEncodedUTF8Sequence(char* input, int len
{
if (charsToCopy != 0)
{
dest.Append(input + totalCharsConsumed - charsToCopy, charsToCopy);
dest.Append(new ReadOnlySpan<char>(input + totalCharsConsumed - charsToCopy, charsToCopy));
Comment thread
EgorBo marked this conversation as resolved.
charsToCopy = 0;
}

Expand Down Expand Up @@ -167,7 +167,7 @@ public static unsafe int UnescapePercentEncodedUTF8Sequence(char* input, int len
return totalCharsConsumed;

bytesLeftInBuffer *= 3;
dest.Append(input + totalCharsConsumed - charsToCopy, charsToCopy + bytesLeftInBuffer);
dest.Append(new ReadOnlySpan<char>(input + totalCharsConsumed - charsToCopy, charsToCopy + bytesLeftInBuffer));
Comment thread
EgorBo marked this conversation as resolved.
return totalCharsConsumed + bytesLeftInBuffer;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/libraries/System.Private.Uri/src/System/UriHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ internal static unsafe void UnescapeString(char* pStr, int start, int end, ref V
{
if ((unescapeMode & UnescapeMode.EscapeUnescape) == UnescapeMode.CopyOnly)
{
dest.Append(pStr + start, end - start);
dest.Append(new ReadOnlySpan<char>(pStr + start, end - start));
Comment thread
EgorBo marked this conversation as resolved.
return;
}

Expand Down