-
Notifications
You must be signed in to change notification settings - Fork 5.2k
StringBuilder: use Span.Fill in Append repeating char #86287
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
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
0945de5
StringBuilder: use Span.Fill when adding repeating characters
yesmey 6acfd23
Update test
yesmey 4143418
Merge branch 'main' into fill-append
yesmey 7ee3ffa
Use Span<T>.Slice check
yesmey 8222f57
Apply suggestions from code review
adamsitnik File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -659,36 +659,58 @@ public StringBuilder Append(char value, int repeatCount) | |
| return this; | ||
| } | ||
|
|
||
| // this is where we can check if the repeatCount will put us over m_MaxCapacity | ||
| // We are doing the check here to prevent the corruption of the StringBuilder. | ||
| int newLength = Length + repeatCount; | ||
| if (newLength > m_MaxCapacity || newLength < repeatCount) | ||
| char[] chunkChars = m_ChunkChars; | ||
| int chunkLength = m_ChunkLength; | ||
|
|
||
| // Try to fit the whole repeatCount in the current chunk | ||
| // Use the same check as Span<T>.Slice for 64-bit so it can be folded | ||
| // Since repeatCount can't be negative, there's no risk for it to overflow on 32 bit | ||
| if (((nuint)(uint)chunkLength + (nuint)(uint)repeatCount) <= (nuint)(uint)chunkChars.Length) | ||
| { | ||
| throw new ArgumentOutOfRangeException(nameof(repeatCount), SR.ArgumentOutOfRange_LengthGreaterThanCapacity); | ||
| chunkChars.AsSpan(chunkLength, repeatCount).Fill(value); | ||
| m_ChunkLength += repeatCount; | ||
| } | ||
|
|
||
| int index = m_ChunkLength; | ||
| while (repeatCount > 0) | ||
| else | ||
| { | ||
| if (index < m_ChunkChars.Length) | ||
| { | ||
| m_ChunkChars[index++] = value; | ||
| --repeatCount; | ||
| } | ||
| else | ||
| { | ||
| m_ChunkLength = index; | ||
| ExpandByABlock(repeatCount); | ||
| Debug.Assert(m_ChunkLength == 0); | ||
| index = 0; | ||
| } | ||
| AppendWithExpansion(value, repeatCount); | ||
| } | ||
|
|
||
| m_ChunkLength = index; | ||
| AssertInvariants(); | ||
| return this; | ||
| } | ||
|
|
||
| [MethodImpl(MethodImplOptions.NoInlining)] | ||
| private void AppendWithExpansion(char value, int repeatCount) | ||
| { | ||
| Debug.Assert(repeatCount > 0, "Invalid length; should have been validated by caller."); | ||
yesmey marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // Check if the repeatCount will put us over m_MaxCapacity | ||
| if ((uint)(repeatCount + Length) > (uint)m_MaxCapacity) | ||
yesmey marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| throw new ArgumentOutOfRangeException(nameof(repeatCount), SR.ArgumentOutOfRange_LengthGreaterThanCapacity); | ||
| } | ||
|
|
||
| char[] chunkChars = m_ChunkChars; | ||
| int chunkLength = m_ChunkLength; | ||
|
|
||
| // Fill the rest of the current chunk | ||
| int firstLength = chunkChars.Length - chunkLength; | ||
| if (firstLength > 0) | ||
| { | ||
| chunkChars.AsSpan(chunkLength, firstLength).Fill(value); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Insert Debug.Assert(firstLength < repeatCount, "We shouldn't be called if there was enough space for the entire run."); |
||
| m_ChunkLength = chunkChars.Length; | ||
| } | ||
yesmey marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // Expand the builder to add another chunk | ||
| int restLength = repeatCount - firstLength; | ||
yesmey marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ExpandByABlock(restLength); | ||
| Debug.Assert(m_ChunkLength == 0, "A new block was not created."); | ||
|
|
||
| // Fill the new chunk with the remaining part of repeatCount | ||
| m_ChunkChars.AsSpan(0, restLength).Fill(value); | ||
| m_ChunkLength = restLength; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Appends a range of characters to the end of this builder. | ||
| /// </summary> | ||
|
|
@@ -990,12 +1012,21 @@ public StringBuilder Append(char value) | |
| } | ||
| else | ||
| { | ||
| Append(value, 1); | ||
| AppendWithExpansion(value); | ||
| } | ||
|
|
||
| return this; | ||
| } | ||
|
|
||
| [MethodImpl(MethodImplOptions.NoInlining)] | ||
yesmey marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| private void AppendWithExpansion(char value) | ||
| { | ||
| ExpandByABlock(1); | ||
| Debug.Assert(m_ChunkLength == 0, "A new block was not created."); | ||
| m_ChunkChars[0] = value; | ||
| m_ChunkLength++; | ||
| } | ||
|
|
||
| [CLSCompliant(false)] | ||
| public StringBuilder Append(sbyte value) => AppendSpanFormattable(value); | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.