Skip to content

[Blazor] Fix ExpressionFormatter sometimes generating malformed expression strings #58799

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

Merged
merged 1 commit into from
Nov 5, 2024
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
18 changes: 18 additions & 0 deletions src/Components/Forms/test/ReverseStringBuilderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,24 @@ public void ToString_Works_AfterExceedingStackAllocatedBuffer()
Assert.Equal(1, builder.SequenceSegmentCount);
}

[Fact]
public void ToString_Works_AfterExceedingStackAllocatedBuffer_WithUnusedSpaceInInitialBuffer()
{
const int InitialBufferSize = 64;
var builder = new ReverseStringBuilder(stackalloc char[InitialBufferSize]);

var firstChunk = new string('A', InitialBufferSize - 10);
var secondChunk = new string('B', 20);

builder.InsertFront(firstChunk);
builder.InsertFront(secondChunk);

var expected = secondChunk + firstChunk;
var actual = builder.ToString();

Assert.Equal(expected, actual);
}

[Fact]
public void ToString_Works_AfterExpandingIntoMultipleBuffersFromEstimatedStringSize()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ internal ref struct ReverseStringBuilder
private SequenceSegment? _fallbackSequenceSegment;

// For testing.
internal int SequenceSegmentCount => _fallbackSequenceSegment?.Count() ?? 0;
internal readonly int SequenceSegmentCount => _fallbackSequenceSegment?.Count() ?? 0;

public ReverseStringBuilder(int conservativeEstimatedStringLength)
{
Expand All @@ -33,7 +33,7 @@ public ReverseStringBuilder(Span<char> initialBuffer)
_nextEndIndex = _currentBuffer.Length;
}

public bool Empty => _nextEndIndex == _currentBuffer.Length;
public readonly bool Empty => _nextEndIndex == _currentBuffer.Length;

public void InsertFront(scoped ReadOnlySpan<char> span)
{
Expand All @@ -60,13 +60,13 @@ public void InsertFront(scoped ReadOnlySpan<char> span)
var newBuffer = s_arrayPool.Rent(sizeToRent);
_fallbackSequenceSegment = new(newBuffer);

_nextEndIndex = newBuffer.Length - _currentBuffer.Length;
_currentBuffer.CopyTo(newBuffer.AsSpan()[_nextEndIndex..]);
_currentBuffer = newBuffer;
var newEndIndex = newBuffer.Length - _currentBuffer.Length + _nextEndIndex;
_currentBuffer[_nextEndIndex..].CopyTo(newBuffer.AsSpan(newEndIndex));
newEndIndex -= span.Length;
span.CopyTo(newBuffer.AsSpan(newEndIndex));

startIndex = _nextEndIndex - span.Length;
span.CopyTo(_currentBuffer[startIndex..]);
_nextEndIndex = startIndex;
_currentBuffer = newBuffer;
_nextEndIndex = newEndIndex;
}
else
{
Expand Down
Loading