Skip to content

Commit b71ea72

Browse files
committed
feat: PadLeft and PadRight
1 parent 0a14ea3 commit b71ea72

File tree

3 files changed

+89
-0
lines changed

3 files changed

+89
-0
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ All notable changes to **ValueStringBuilder** will be documented in this file. T
66

77
## [Unreleased]
88

9+
### Added
10+
- `PadLeft` and `PadRight` methods
11+
912
## [1.20.0] - 2024-05-02
1013

1114
### Added
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
namespace LinkDotNet.StringBuilder;
2+
3+
public ref partial struct ValueStringBuilder
4+
{
5+
/// <summary>
6+
/// Pads the left side of the string with the given character.
7+
/// </summary>
8+
/// <param name="totalWidth">Total width of the string after padding.</param>
9+
/// <param name="paddingChar">Character to pad the string with.</param>
10+
public void PadLeft(int totalWidth, char paddingChar)
11+
{
12+
if (totalWidth <= bufferPosition)
13+
{
14+
return;
15+
}
16+
17+
EnsureCapacity(totalWidth);
18+
19+
var padding = totalWidth - bufferPosition;
20+
buffer[..bufferPosition].CopyTo(buffer[padding..]);
21+
buffer[..padding].Fill(paddingChar);
22+
bufferPosition = totalWidth;
23+
}
24+
25+
/// <summary>
26+
/// Pads the right side of the string with the given character.
27+
/// </summary>
28+
/// <param name="totalWidth">Total width of the string after padding.</param>
29+
/// <param name="paddingChar">Character to pad the string with.</param>
30+
public void PadRight(int totalWidth, char paddingChar)
31+
{
32+
if (totalWidth <= bufferPosition)
33+
{
34+
return;
35+
}
36+
37+
EnsureCapacity(totalWidth);
38+
39+
buffer[bufferPosition..totalWidth].Fill(paddingChar);
40+
bufferPosition = totalWidth;
41+
}
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
namespace LinkDotNet.StringBuilder.UnitTests;
2+
3+
public class ValueStringBuilderPadTests
4+
{
5+
[Fact]
6+
public void ShouldPadLeft()
7+
{
8+
using var stringBuilder = new ValueStringBuilder("Hello");
9+
10+
stringBuilder.PadLeft(10, ' ');
11+
12+
stringBuilder.ToString().Should().Be(" Hello");
13+
}
14+
15+
[Fact]
16+
public void ShouldPadRight()
17+
{
18+
using var stringBuilder = new ValueStringBuilder("Hello");
19+
20+
stringBuilder.PadRight(10, ' ');
21+
22+
stringBuilder.ToString().Should().Be("Hello ");
23+
}
24+
25+
[Fact]
26+
public void GivenTotalWidthIsSmallerThanCurrentLength_WhenPadLeft_ThenShouldNotChange()
27+
{
28+
using var stringBuilder = new ValueStringBuilder("Hello");
29+
30+
stringBuilder.PadLeft(3, ' ');
31+
32+
stringBuilder.ToString().Should().Be("Hello");
33+
}
34+
35+
[Fact]
36+
public void GivenTotalWidthIsSmallerThanCurrentLength_WhenPadRight_ThenShouldNotChange()
37+
{
38+
using var stringBuilder = new ValueStringBuilder("Hello");
39+
40+
stringBuilder.PadRight(3, ' ');
41+
42+
stringBuilder.ToString().Should().Be("Hello");
43+
}
44+
}

0 commit comments

Comments
 (0)