-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0a14ea3
commit b71ea72
Showing
3 changed files
with
89 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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 |
---|---|---|
@@ -0,0 +1,42 @@ | ||
namespace LinkDotNet.StringBuilder; | ||
|
||
public ref partial struct ValueStringBuilder | ||
{ | ||
/// <summary> | ||
/// Pads the left side of the string with the given character. | ||
/// </summary> | ||
/// <param name="totalWidth">Total width of the string after padding.</param> | ||
/// <param name="paddingChar">Character to pad the string with.</param> | ||
public void PadLeft(int totalWidth, char paddingChar) | ||
{ | ||
if (totalWidth <= bufferPosition) | ||
{ | ||
return; | ||
} | ||
|
||
EnsureCapacity(totalWidth); | ||
|
||
var padding = totalWidth - bufferPosition; | ||
buffer[..bufferPosition].CopyTo(buffer[padding..]); | ||
buffer[..padding].Fill(paddingChar); | ||
bufferPosition = totalWidth; | ||
} | ||
|
||
/// <summary> | ||
/// Pads the right side of the string with the given character. | ||
/// </summary> | ||
/// <param name="totalWidth">Total width of the string after padding.</param> | ||
/// <param name="paddingChar">Character to pad the string with.</param> | ||
public void PadRight(int totalWidth, char paddingChar) | ||
{ | ||
if (totalWidth <= bufferPosition) | ||
{ | ||
return; | ||
} | ||
|
||
EnsureCapacity(totalWidth); | ||
|
||
buffer[bufferPosition..totalWidth].Fill(paddingChar); | ||
bufferPosition = totalWidth; | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
tests/LinkDotNet.StringBuilder.UnitTests/ValueStringBuilder.Pad.Tests.cs
This file contains 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 |
---|---|---|
@@ -0,0 +1,44 @@ | ||
namespace LinkDotNet.StringBuilder.UnitTests; | ||
|
||
public class ValueStringBuilderPadTests | ||
{ | ||
[Fact] | ||
public void ShouldPadLeft() | ||
{ | ||
using var stringBuilder = new ValueStringBuilder("Hello"); | ||
|
||
stringBuilder.PadLeft(10, ' '); | ||
|
||
stringBuilder.ToString().Should().Be(" Hello"); | ||
} | ||
|
||
[Fact] | ||
public void ShouldPadRight() | ||
{ | ||
using var stringBuilder = new ValueStringBuilder("Hello"); | ||
|
||
stringBuilder.PadRight(10, ' '); | ||
|
||
stringBuilder.ToString().Should().Be("Hello "); | ||
} | ||
|
||
[Fact] | ||
public void GivenTotalWidthIsSmallerThanCurrentLength_WhenPadLeft_ThenShouldNotChange() | ||
{ | ||
using var stringBuilder = new ValueStringBuilder("Hello"); | ||
|
||
stringBuilder.PadLeft(3, ' '); | ||
|
||
stringBuilder.ToString().Should().Be("Hello"); | ||
} | ||
|
||
[Fact] | ||
public void GivenTotalWidthIsSmallerThanCurrentLength_WhenPadRight_ThenShouldNotChange() | ||
{ | ||
using var stringBuilder = new ValueStringBuilder("Hello"); | ||
|
||
stringBuilder.PadRight(3, ' '); | ||
|
||
stringBuilder.ToString().Should().Be("Hello"); | ||
} | ||
} |