Skip to content
This repository has been archived by the owner on Aug 2, 2023. It is now read-only.

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
some more changes
Browse files Browse the repository at this point in the history
VSadov committed Sep 7, 2017

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 330a409 commit 1cc6053
Showing 3 changed files with 18 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -16,8 +16,11 @@ public static void Pipe(this Transformation transformation, ReadOnlyBytes source
{
int afterMergeSlice = 0;

// TODO: "ReadOnlySpan<byte> remainder = stackalloc byte[0]" would fit better here,
// but it emits substandard IL, see https://github.com/dotnet/roslyn/issues/21952
//
// make 'remainder' formally stack-referring or we won't be able to reference stack data later
ReadOnlySpan<byte> remainder = stackalloc byte[0];
var remainder = true ? new ReadOnlySpan<byte>() : stackalloc byte[0];
Span<byte> stackSpan = stackalloc byte[stackLength];

var poisition = Position.First;
Original file line number Diff line number Diff line change
@@ -210,7 +210,11 @@ public static string GetUtf8String(this ReadableBuffer buffer)
return null;
}

ReadOnlySpan<byte> textSpan = stackalloc byte[0];
// TODO: "ReadOnlySpan<byte> textSpan = stackalloc byte[0]" would fit better here,
// but it emits substandard IL, see https://github.com/dotnet/roslyn/issues/21952
//
// make 'textSpan' formally stack-referring or we won't be able to reference stack data later
var textSpan = true? new ReadOnlySpan<byte>() : stackalloc byte[0];

if (buffer.IsSingleSpan)
{
23 changes: 9 additions & 14 deletions src/System.Text.Formatting/System/Text/Parsing/SequenceParser.cs
Original file line number Diff line number Diff line change
@@ -37,9 +37,6 @@ public static bool TryParseUInt64<T>(this T bufferSequence, out ulong value, out
}

// Combine the first, the second, and potentially more segments into a stack allocated buffer
// make 'combinedSpan' formally stack-referring or we won't be able to reference stack data later
ReadOnlySpan<byte> combinedSpan = stackalloc byte[0];

if (first.Length < StackBufferSize)
{
Span<byte> destination = stackalloc byte[StackBufferSize];
@@ -66,12 +63,12 @@ public static bool TryParseUInt64<T>(this T bufferSequence, out ulong value, out
}
}

combinedSpan = destination.Slice(0, StackBufferSize - free.Length);
var combinedStackSpan = destination.Slice(0, StackBufferSize - free.Length);

// if the stack allocated buffer parsed succesfully (and for uint it should always do), then return success.
if (PrimitiveParser.InvariantUtf8.TryParseUInt64(combinedSpan, out value, out consumed))
if (PrimitiveParser.InvariantUtf8.TryParseUInt64(combinedStackSpan, out value, out consumed))
{
if (consumed < combinedSpan.Length || combinedSpan.Length < StackBufferSize)
if (consumed < combinedStackSpan.Length || combinedStackSpan.Length < StackBufferSize)
{
return true;
}
@@ -80,8 +77,8 @@ public static bool TryParseUInt64<T>(this T bufferSequence, out ulong value, out

// for invariant culture, we should never reach this point, as invariant uint text is never longer than 127 bytes.
// I left this code here, as we will need it for custom cultures and possibly when we shrink the stack allocated buffer.
combinedSpan = bufferSequence.ToSpan();
if (!PrimitiveParser.InvariantUtf8.TryParseUInt64(first.Span, out value, out consumed)) {
var combinedSpan = bufferSequence.ToSpan();
if (!PrimitiveParser.InvariantUtf8.TryParseUInt64(combinedSpan, out value, out consumed)) {
return false;
}
return true;
@@ -114,8 +111,6 @@ public static bool TryParseUInt32<T>(this T bufferSequence, out uint value, out
}

// Combine the first, the second, and potentially more segments into a stack allocated buffer
// make 'combinedSpan' formally stack-referring or we won't be able to reference stack data later
ReadOnlySpan<byte> combinedSpan = stackalloc byte[0];
if (first.Length < StackBufferSize) {

Span<byte> destination = stackalloc byte[StackBufferSize];
@@ -139,19 +134,19 @@ public static bool TryParseUInt32<T>(this T bufferSequence, out uint value, out
}
}

combinedSpan = destination.Slice(0, StackBufferSize - free.Length);
var combinedStackSpan = destination.Slice(0, StackBufferSize - free.Length);

// if the stack allocated buffer parsed succesfully (and for uint it should always do), then return success.
if (PrimitiveParser.InvariantUtf8.TryParseUInt32(combinedSpan, out value, out consumed)) {
if(consumed < combinedSpan.Length || combinedSpan.Length < StackBufferSize) {
if (PrimitiveParser.InvariantUtf8.TryParseUInt32(combinedStackSpan, out value, out consumed)) {
if(consumed < combinedStackSpan.Length || combinedStackSpan.Length < StackBufferSize) {
return true;
}
}
}

// for invariant culture, we should never reach this point, as invariant uint text is never longer than 127 bytes.
// I left this code here, as we will need it for custom cultures and possibly when we shrink the stack allocated buffer.
combinedSpan = bufferSequence.ToSpan();
var combinedSpan = bufferSequence.ToSpan();
if (!PrimitiveParser.InvariantUtf8.TryParseUInt32(combinedSpan, out value, out consumed)) {
return false;
}

0 comments on commit 1cc6053

Please sign in to comment.