Skip to content

Commit 40e80eb

Browse files
committed
Added Trim overloads
1 parent fc65575 commit 40e80eb

File tree

3 files changed

+103
-0
lines changed

3 files changed

+103
-0
lines changed

CHANGELOG.md

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

77
## [Unreleased]
88

9+
### Added
10+
11+
- New overloads for `Trim`, `TrimStart` and `TrimEnd` that accept a character as parameter
12+
913
## [1.15.0] - 2023-03-26
1014

1115
### Added

src/LinkDotNet.StringBuilder/ValueStringBuilder.Trim.cs

+66
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,35 @@ public void Trim()
3232
}
3333
}
3434

35+
/// <summary>
36+
/// Removes the specified character from the beginning and end of this string.
37+
/// </summary>
38+
/// <param name="c">The character to remove.</param>
39+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
40+
public void Trim(char c)
41+
{
42+
// Remove character from the beginning
43+
var start = 0;
44+
while (start < bufferPosition && buffer[start] == c)
45+
{
46+
start++;
47+
}
48+
49+
// Remove character from the end
50+
var end = bufferPosition - 1;
51+
while (end >= start && buffer[end] == c)
52+
{
53+
end--;
54+
}
55+
56+
var newLength = end - start + 1;
57+
if (newLength < bufferPosition)
58+
{
59+
bufferPosition = newLength;
60+
buffer.Slice(start, start + newLength).CopyTo(buffer);
61+
}
62+
}
63+
3564
/// <summary>
3665
/// Removes a set of whitespace characters from the beginning of this string.
3766
/// </summary>
@@ -52,6 +81,27 @@ public void TrimStart()
5281
}
5382
}
5483

84+
/// <summary>
85+
/// Removes the specified character from the beginning of this string.
86+
/// </summary>
87+
/// <param name="c">The character to remove.</param>
88+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
89+
public void TrimStart(char c)
90+
{
91+
var start = 0;
92+
while (start < bufferPosition && buffer[start] == c)
93+
{
94+
start++;
95+
}
96+
97+
if (start > 0)
98+
{
99+
var newLength = bufferPosition - start;
100+
buffer.Slice(start, bufferPosition).CopyTo(buffer);
101+
bufferPosition = newLength;
102+
}
103+
}
104+
55105
/// <summary>
56106
/// Removes a set of whitespace characters from the ending of this string.
57107
/// </summary>
@@ -66,4 +116,20 @@ public void TrimEnd()
66116

67117
bufferPosition = end + 1;
68118
}
119+
120+
/// <summary>
121+
/// Removes the specified character from the end of this string.
122+
/// </summary>
123+
/// <param name="c">The character to remove.</param>
124+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
125+
public void TrimEnd(char c)
126+
{
127+
var end = bufferPosition - 1;
128+
while (end >= 0 && buffer[end] == c)
129+
{
130+
end--;
131+
}
132+
133+
bufferPosition = end + 1;
134+
}
69135
}

tests/LinkDotNet.StringBuilder.UnitTests/ValueStringBuilder.Trim.Tests.cs

+33
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,37 @@ public void GivenStringWithWhitespaces_WhenTrim_ThenShouldRemoveWhitespaces(stri
4646

4747
valueStringBuilder.ToString().Should().Be(expected);
4848
}
49+
50+
[Fact]
51+
public void GivenString_WhenTrimStartCharacter_ThenShouldRemoveCharacter()
52+
{
53+
using var valueStringBuilder = new ValueStringBuilder();
54+
valueStringBuilder.Append("HHeeHH");
55+
56+
valueStringBuilder.TrimStart('H');
57+
58+
valueStringBuilder.ToString().Should().Be("eeHH");
59+
}
60+
61+
[Fact]
62+
public void GivenString_WhenTrimEndCharacter_ThenShouldRemoveCharacter()
63+
{
64+
using var valueStringBuilder = new ValueStringBuilder();
65+
valueStringBuilder.Append("HHeeHH");
66+
67+
valueStringBuilder.TrimEnd('H');
68+
69+
valueStringBuilder.ToString().Should().Be("HHee");
70+
}
71+
72+
[Fact]
73+
public void GivenString_WhenTrimCharacter_ThenShouldRemoveCharacter()
74+
{
75+
using var valueStringBuilder = new ValueStringBuilder();
76+
valueStringBuilder.Append("HHeeHH");
77+
78+
valueStringBuilder.Trim('H');
79+
80+
valueStringBuilder.ToString().Should().Be("ee");
81+
}
4982
}

0 commit comments

Comments
 (0)