Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 44 additions & 16 deletions src/LinkDotNet.StringBuilder/ValueStringBuilder.Trim.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace LinkDotNet.StringBuilder;
public ref partial struct ValueStringBuilder
{
/// <summary>
/// Removes a set of whitespace characters from the beginning and ending of this string.
/// Removes all whitespace characters from the start and end of this builder.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Trim()
Expand Down Expand Up @@ -33,22 +33,22 @@ public void Trim()
}

/// <summary>
/// Removes the specified character from the beginning and end of this string.
/// Removes all occurrences of the specified character from the start and end of this builder.
/// </summary>
/// <param name="c">The character to remove.</param>
/// <param name="value">The character to remove.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Trim(char c)
public void Trim(char value)
{
// Remove character from the beginning
var start = 0;
while (start < bufferPosition && buffer[start] == c)
while (start < bufferPosition && buffer[start] == value)
{
start++;
}

// Remove character from the end
var end = bufferPosition - 1;
while (end >= start && buffer[end] == c)
while (end >= start && buffer[end] == value)
{
end--;
}
Expand All @@ -62,7 +62,7 @@ public void Trim(char c)
}

/// <summary>
/// Removes a set of whitespace characters from the beginning of this string.
/// Removes all whitespace characters from the start of this builder.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void TrimStart()
Expand All @@ -82,14 +82,14 @@ public void TrimStart()
}

/// <summary>
/// Removes the specified character from the beginning of this string.
/// Removes all occurrences of the specified character from the start of this builder.
/// </summary>
/// <param name="c">The character to remove.</param>
/// <param name="value">The character to remove.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void TrimStart(char c)
public void TrimStart(char value)
{
var start = 0;
while (start < bufferPosition && buffer[start] == c)
while (start < bufferPosition && buffer[start] == value)
{
start++;
}
Expand All @@ -103,7 +103,7 @@ public void TrimStart(char c)
}

/// <summary>
/// Removes a set of whitespace characters from the ending of this string.
/// Removes all whitespace characters from the end of this builder.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void TrimEnd()
Expand All @@ -118,18 +118,46 @@ public void TrimEnd()
}

/// <summary>
/// Removes the specified character from the end of this string.
/// Removes all occurrences of the specified character from the end of this builder.
/// </summary>
/// <param name="c">The character to remove.</param>
/// <param name="value">The character to remove.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void TrimEnd(char c)
public void TrimEnd(char value)
{
var end = bufferPosition - 1;
while (end >= 0 && buffer[end] == c)
while (end >= 0 && buffer[end] == value)
{
end--;
}

bufferPosition = end + 1;
}

/// <summary>
/// Removes the specified sequence of characters from the start of this builder.
/// </summary>
/// <param name="value">The sequence of characters to remove.</param>
/// <param name="comparisonType">The way to compare the sequences of characters.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void TrimPrefix(ReadOnlySpan<char> value, StringComparison comparisonType = StringComparison.Ordinal)
{
if (AsSpan().StartsWith(value, comparisonType))
{
Remove(0, value.Length);
}
}

/// <summary>
/// Removes the specified sequence of characters from the end of this builder.
/// </summary>
/// <param name="value">The sequence of characters to remove.</param>
/// <param name="comparisonType">The way to compare the sequences of characters.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void TrimSuffix(ReadOnlySpan<char> value, StringComparison comparisonType = StringComparison.Ordinal)
{
if (AsSpan().EndsWith(value, comparisonType))
{
Remove(Length - value.Length, value.Length);
}
}
}
19 changes: 4 additions & 15 deletions src/LinkDotNet.StringBuilder/ValueStringBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -199,26 +199,15 @@ public void EnsureCapacity(int newCapacity)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Remove(int startIndex, int length)
{
ArgumentOutOfRangeException.ThrowIfLessThan(length, 0);
ArgumentOutOfRangeException.ThrowIfLessThan(startIndex, 0);
ArgumentOutOfRangeException.ThrowIfGreaterThan(startIndex + length, Length);

if (length == 0)
{
return;
}

if (length < 0)
{
throw new ArgumentOutOfRangeException(nameof(length), "The given length can't be negative.");
}

if (startIndex < 0)
{
throw new ArgumentOutOfRangeException(nameof(startIndex), "The given start index can't be negative.");
}

if (length > Length - startIndex)
{
throw new ArgumentOutOfRangeException(nameof(length), $"The given Span ({startIndex}..{length})length is outside the the represented string.");
}

var beginIndex = startIndex + length;
buffer[beginIndex..bufferPosition].CopyTo(buffer[startIndex..]);
bufferPosition -= length;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System;

namespace LinkDotNet.StringBuilder.UnitTests;

public class ValueStringBuilderTrimTests
Expand Down Expand Up @@ -79,4 +81,26 @@ public void GivenString_WhenTrimCharacter_ThenShouldRemoveCharacter()

valueStringBuilder.ToString().ShouldBe("ee");
}

[Fact]
public void GivenString_WhenTrimPrefix_ThenShouldRemoveSpan()
{
using var valueStringBuilder = new ValueStringBuilder();
valueStringBuilder.Append("Hello world");

valueStringBuilder.TrimPrefix("hell", StringComparison.InvariantCultureIgnoreCase);

valueStringBuilder.ToString().ShouldBe("o world");
}

[Fact]
public void GivenString_WhenTrimSuffix_ThenShouldRemoveSpan()
{
using var valueStringBuilder = new ValueStringBuilder();
valueStringBuilder.Append("Hello world");

valueStringBuilder.TrimSuffix("RlD", StringComparison.InvariantCultureIgnoreCase);

valueStringBuilder.ToString().ShouldBe("Hello wo");
}
}
Loading