-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathValueStringBuilder.Pad.cs
46 lines (39 loc) · 1.4 KB
/
ValueStringBuilder.Pad.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
using System.Runtime.CompilerServices;
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>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
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>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void PadRight(int totalWidth, char paddingChar)
{
if (totalWidth <= bufferPosition)
{
return;
}
EnsureCapacity(totalWidth);
buffer[bufferPosition..totalWidth].Fill(paddingChar);
bufferPosition = totalWidth;
}
}