From b71ea72de9f5640a59cd693ea8169d7741c46006 Mon Sep 17 00:00:00 2001 From: Steven Giesel Date: Fri, 20 Sep 2024 09:17:03 +0200 Subject: [PATCH] feat: PadLeft and PadRight --- CHANGELOG.md | 3 ++ .../ValueStringBuilder.Pad.cs | 42 ++++++++++++++++++ .../ValueStringBuilder.Pad.Tests.cs | 44 +++++++++++++++++++ 3 files changed, 89 insertions(+) create mode 100644 src/LinkDotNet.StringBuilder/ValueStringBuilder.Pad.cs create mode 100644 tests/LinkDotNet.StringBuilder.UnitTests/ValueStringBuilder.Pad.Tests.cs diff --git a/CHANGELOG.md b/CHANGELOG.md index f75c59e..eaaec3c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,9 @@ All notable changes to **ValueStringBuilder** will be documented in this file. T ## [Unreleased] +### Added +- `PadLeft` and `PadRight` methods + ## [1.20.0] - 2024-05-02 ### Added diff --git a/src/LinkDotNet.StringBuilder/ValueStringBuilder.Pad.cs b/src/LinkDotNet.StringBuilder/ValueStringBuilder.Pad.cs new file mode 100644 index 0000000..2a63b7b --- /dev/null +++ b/src/LinkDotNet.StringBuilder/ValueStringBuilder.Pad.cs @@ -0,0 +1,42 @@ +namespace LinkDotNet.StringBuilder; + +public ref partial struct ValueStringBuilder +{ + /// + /// Pads the left side of the string with the given character. + /// + /// Total width of the string after padding. + /// Character to pad the string with. + 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; + } + + /// + /// Pads the right side of the string with the given character. + /// + /// Total width of the string after padding. + /// Character to pad the string with. + public void PadRight(int totalWidth, char paddingChar) + { + if (totalWidth <= bufferPosition) + { + return; + } + + EnsureCapacity(totalWidth); + + buffer[bufferPosition..totalWidth].Fill(paddingChar); + bufferPosition = totalWidth; + } +} \ No newline at end of file diff --git a/tests/LinkDotNet.StringBuilder.UnitTests/ValueStringBuilder.Pad.Tests.cs b/tests/LinkDotNet.StringBuilder.UnitTests/ValueStringBuilder.Pad.Tests.cs new file mode 100644 index 0000000..5c494a5 --- /dev/null +++ b/tests/LinkDotNet.StringBuilder.UnitTests/ValueStringBuilder.Pad.Tests.cs @@ -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"); + } +} \ No newline at end of file