Skip to content
Closed
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
6 changes: 5 additions & 1 deletion src/libraries/System.Private.CoreLib/src/System/Char.cs
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,9 @@ static char IUtf8SpanParsable<char>.Parse(ReadOnlySpan<byte> utf8Text, IFormatPr
}

/// <inheritdoc cref="IUtf8SpanParsable{TSelf}.TryParse(ReadOnlySpan{byte}, IFormatProvider?, out TSelf)" />
static bool IUtf8SpanParsable<char>.TryParse(ReadOnlySpan<byte> utf8Text, IFormatProvider? provider, out char result)
static bool IUtf8SpanParsable<char>.TryParse(ReadOnlySpan<byte> utf8Text, IFormatProvider? provider, out char result) => TryParseUtf8(utf8Text, out result);

private static bool TryParseUtf8(ReadOnlySpan<byte> utf8Text, out char result)
{
if (Rune.DecodeFromUtf8(utf8Text, out Rune rune, out int bytesConsumed) != Buffers.OperationStatus.Done ||
bytesConsumed != utf8Text.Length ||
Expand Down Expand Up @@ -1977,6 +1979,8 @@ static bool INumberBase<char>.TryConvertToTruncating<TOther>(char value, [MaybeN

static bool INumberBase<char>.TryParse(ReadOnlySpan<char> s, NumberStyles style, IFormatProvider? provider, out char result) => TryParse(s, out result);

static bool INumberBase<char>.TryParse(ReadOnlySpan<byte> utf8Text, NumberStyles style, IFormatProvider? provider, out char result) => TryParseUtf8(utf8Text, out result);

static bool INumberBase<char>.TryParsePartial([NotNullWhen(true)] string? s, NumberStyles style, IFormatProvider? provider, out char result, out int charsConsumed)
{
if (TryParse(s, out result))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -912,6 +912,11 @@ public unsafe string GetString(byte* bytes, int byteCount)

public unsafe string GetString(ReadOnlySpan<byte> bytes)
{
if (this is UTF8Encoding.UTF8EncodingSealed utf8Encoding && bytes.Length <= UTF8Encoding.UTF8EncodingSealed.MaxSmallInputElementCount)
{
return utf8Encoding.GetStringForSmallInput(bytes);
}

fixed (byte* bytesPtr = &MemoryMarshal.GetNonNullPinnableReference(bytes))
{
return string.CreateStringFromEncoding(bytesPtr, bytes.Length, this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ internal sealed class UTF8EncodingSealed : UTF8Encoding
/// <summary>
/// Maximum number of input elements we'll allow for going through the fast one-pass stackalloc code paths.
/// </summary>
private const int MaxSmallInputElementCount = 32;
internal const int MaxSmallInputElementCount = 32;

public UTF8EncodingSealed(bool encoderShouldEmitUTF8Identifier) : base(encoderShouldEmitUTF8Identifier) { }

Expand Down Expand Up @@ -130,9 +130,8 @@ public override string GetString(byte[] bytes)
}
}

private unsafe string GetStringForSmallInput(byte[] bytes)
internal unsafe string GetStringForSmallInput(ReadOnlySpan<byte> bytes)
{
Debug.Assert(bytes != null);
Debug.Assert(bytes.Length <= MaxSmallInputElementCount);

char* pDestination = stackalloc char[MaxSmallInputElementCount]; // each byte produces at most one char
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -697,6 +697,11 @@ public override unsafe string GetString(byte[] bytes, int index, int count)
if (count == 0)
return string.Empty;

if (this is UTF8EncodingSealed utf8Encoding && count <= UTF8EncodingSealed.MaxSmallInputElementCount)
{
return utf8Encoding.GetStringForSmallInput(bytes.AsSpan(index, count));
}

fixed (byte* pBytes = bytes)
{
return string.CreateStringFromEncoding(pBytes + index, count, this);
Expand Down
Loading