File tree Expand file tree Collapse file tree 3 files changed +89
-0
lines changed
src/LinkDotNet.StringBuilder
tests/LinkDotNet.StringBuilder.UnitTests Expand file tree Collapse file tree 3 files changed +89
-0
lines changed Original file line number Diff line number Diff 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 number Diff line number Diff line change 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 number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments