Skip to content

Commit db796aa

Browse files
committed
added Remove method
1 parent 75b0986 commit db796aa

File tree

4 files changed

+78
-1
lines changed

4 files changed

+78
-1
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
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+
- Added `Remove` method.
11+
912
## [0.9.1] - 2022-04-06
1013

1114
This release brings extensions to the `ValueStringBuilder` API. For `v1.0` the `ValueStringBuilder` tries to be en par with the`System.Text.StringBuilder`.

LinkDotNet.StringBuilder.sln

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ EndProject
1313
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".text", ".text", "{DA9229AB-1A57-414E-A81C-358FA8DAC2AF}"
1414
ProjectSection(SolutionItems) = preProject
1515
README.md = README.md
16+
CHANGELOG.md = CHANGELOG.md
1617
EndProjectSection
1718
EndProject
1819
Global

src/LinkDotNet.StringBuilder/ValueStringBuilder.cs

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,13 +102,51 @@ public void AppendLine(ReadOnlySpan<char> str)
102102
public bool TryCopyTo(Span<char> destination) => buffer[..bufferPosition].TryCopyTo(destination);
103103

104104
/// <summary>
105-
/// Clears the st
105+
/// Clears the internal representation of the string.
106106
/// </summary>
107+
/// <remarks>
108+
/// This will not enforce some allocations or shrinking of the internal buffer.
109+
/// </remarks>
107110
public void Clear()
108111
{
109112
bufferPosition = 0;
110113
}
111114

115+
/// <summary>
116+
/// Removes a range of characters from this builder.
117+
/// </summary>
118+
/// <param name="startIndex">The inclusive index from where the string gets removed.</param>
119+
/// <param name="length">The length of the slice to remove.</param>
120+
/// <remarks>
121+
/// This method will not affect the internal size of the string.
122+
/// </remarks>
123+
public void Remove(int startIndex, int length)
124+
{
125+
if (length < 0)
126+
{
127+
throw new ArgumentOutOfRangeException(nameof(length), "The given length can't be negative.");
128+
}
129+
130+
if (startIndex < 0)
131+
{
132+
throw new ArgumentOutOfRangeException(nameof(startIndex), "The given start index can't be negative.");
133+
}
134+
135+
if (length > Length - startIndex)
136+
{
137+
throw new ArgumentOutOfRangeException(nameof(length), "The given length is longer than the represented string.");
138+
}
139+
140+
if (startIndex >= Length)
141+
{
142+
throw new ArgumentOutOfRangeException(nameof(startIndex), "The given startIndex is larger than the represented string.");
143+
}
144+
145+
var beginIndex = startIndex + length;
146+
buffer[beginIndex..bufferPosition].CopyTo(buffer[startIndex..]);
147+
bufferPosition -= length;
148+
}
149+
112150
private void Grow(int capacity = 0)
113151
{
114152
var currentSize = buffer.Length;

tests/LinkDotNet.StringBuilder.UnitTests/ValueStringBuilderTests.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,4 +140,39 @@ public void ShouldReturnEmptyStringWhenInitialized()
140140

141141
stringBuilder.ToString().Should().Be(string.Empty);
142142
}
143+
144+
[Fact]
145+
public void ShouldRemoveRange()
146+
{
147+
var stringBuilder = new ValueStringBuilder();
148+
stringBuilder.Append("Hello World");
149+
150+
stringBuilder.Remove(0, 6);
151+
152+
stringBuilder.Length.Should().Be(5);
153+
stringBuilder.ToString().Should().Be("World");
154+
}
155+
156+
[Theory]
157+
[InlineData(-1, 2)]
158+
[InlineData(1, -2)]
159+
[InlineData(90, 1)]
160+
[InlineData(1, 90)]
161+
public void ShouldThrowExceptionWhenNegativeStartIndex(int startIndex, int length)
162+
{
163+
var stringBuilder = new ValueStringBuilder();
164+
stringBuilder.Append("Hello World");
165+
166+
try
167+
{
168+
stringBuilder.Remove(startIndex, length);
169+
}
170+
catch (ArgumentOutOfRangeException)
171+
{
172+
Assert.True(true);
173+
return;
174+
}
175+
176+
Assert.False(true);
177+
}
143178
}

0 commit comments

Comments
 (0)