Skip to content

Commit 7b61fce

Browse files
committed
Refactored Remove method
1 parent fb1ffff commit 7b61fce

File tree

2 files changed

+19
-9
lines changed

2 files changed

+19
-9
lines changed

src/LinkDotNet.StringBuilder/ValueStringBuilder.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,11 @@ public void Clear()
7777
/// </remarks>
7878
public void Remove(int startIndex, int length)
7979
{
80+
if (length == 0)
81+
{
82+
return;
83+
}
84+
8085
if (length < 0)
8186
{
8287
throw new ArgumentOutOfRangeException(nameof(length), "The given length can't be negative.");
@@ -89,12 +94,7 @@ public void Remove(int startIndex, int length)
8994

9095
if (length > Length - startIndex)
9196
{
92-
throw new ArgumentOutOfRangeException(nameof(length), "The given length is longer than the represented string.");
93-
}
94-
95-
if (startIndex >= Length)
96-
{
97-
throw new ArgumentOutOfRangeException(nameof(startIndex), "The given startIndex is larger than the represented string.");
97+
throw new ArgumentOutOfRangeException(nameof(length), $"The given Span ({startIndex}..{length})length is outside the the represented string.");
9898
}
9999

100100
var beginIndex = startIndex + length;

tests/LinkDotNet.StringBuilder.UnitTests/ValueStringBuilderTests.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,11 +157,10 @@ public void ShouldRemoveRange()
157157
[InlineData(-1, 2)]
158158
[InlineData(1, -2)]
159159
[InlineData(90, 1)]
160-
[InlineData(1, 90)]
161-
public void ShouldThrowExceptionWhenNegativeStartIndex(int startIndex, int length)
160+
public void ShouldThrowExceptionWhenOutOfRangeIndex(int startIndex, int length)
162161
{
163162
var stringBuilder = new ValueStringBuilder();
164-
stringBuilder.Append("Hello World");
163+
stringBuilder.Append("Hello");
165164

166165
try
167166
{
@@ -175,4 +174,15 @@ public void ShouldThrowExceptionWhenNegativeStartIndex(int startIndex, int lengt
175174

176175
Assert.False(true);
177176
}
177+
178+
[Fact]
179+
public void ShouldNotRemoveEntriesWhenLengthIsEqualToZero()
180+
{
181+
var stringBuilder = new ValueStringBuilder();
182+
stringBuilder.Append("Hello");
183+
184+
stringBuilder.Remove(0, 0);
185+
186+
stringBuilder.ToString().Should().Be("Hello");
187+
}
178188
}

0 commit comments

Comments
 (0)