Skip to content

Add insert & replace methods for runes #225

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 25, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ All notable changes to **ValueStringBuilder** will be documented in this file. T

- Added `TrimPrefix(ReadOnlySpan<char>, StringComparison)` (by yours truly (@Joy-less) in #226)
- Added `TrimSuffix(ReadOnlySpan<char>, StringComparison)` (also by yours truly (@Joy-less) in #226)
- Added `Insert(int, char)` overload (by yours truly (@Joy-less) in #225)
- Added `Insert(int, Rune)` overload (again by yours truly (@Joy-less) in #225)
- Added `Replace(Rune, Rune)` overload (see yours truly (@Joy-less) in #225)
- Improved `Replace(scoped ReadOnlySpan<char>, scoped ReadOnlySpan<char>, int, int)` fallback (achieved by yours truly (@Joy-less) in #225)

## [2.1.0] - 2025-01-14

Expand Down
6 changes: 3 additions & 3 deletions src/LinkDotNet.StringBuilder/ValueStringBuilder.Append.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,10 @@ public void Append(char value)
public void Append(Rune value)
{
Span<char> valueChars = stackalloc char[2];
int valueCharsWritten = value.EncodeToUtf16(valueChars);
ReadOnlySpan<char> valueCharsReadOnly = valueChars[..valueCharsWritten];
var valueCharsWritten = value.EncodeToUtf16(valueChars);
ReadOnlySpan<char> valueCharsSlice = valueChars[..valueCharsWritten];

Append(valueCharsReadOnly);
Append(valueCharsSlice);
}

/// <summary>
Expand Down
24 changes: 24 additions & 0 deletions src/LinkDotNet.StringBuilder/ValueStringBuilder.Insert.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Runtime.CompilerServices;
using System.Text;

namespace LinkDotNet.StringBuilder;

Expand All @@ -12,6 +13,29 @@ public ref partial struct ValueStringBuilder
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Insert(int index, bool value) => Insert(index, value.ToString());

/// <summary>
/// Insert the string representation of the character to the builder at the given index.
/// </summary>
/// <param name="index">Index where <paramref name="value"/> should be inserted.</param>
/// <param name="value">Character to insert into this builder.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Insert(int index, char value) => Insert(index, [value]);

/// <summary>
/// Insert the string representation of the rune to the builder at the given index.
/// </summary>
/// <param name="index">Index where <paramref name="value"/> should be inserted.</param>
/// <param name="value">Rune to insert into this builder.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Insert(int index, Rune value)
{
Span<char> valueChars = stackalloc char[2];
var valueCharsWritten = value.EncodeToUtf16(valueChars);
ReadOnlySpan<char> valueCharsSlice = valueChars[..valueCharsWritten];

Insert(index, valueCharsSlice);
}

/// <summary>
/// Insert the string representation of the char to the builder at the given index.
/// </summary>
Expand Down
45 changes: 19 additions & 26 deletions src/LinkDotNet.StringBuilder/ValueStringBuilder.Replace.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,6 @@ public ref partial struct ValueStringBuilder
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly void Replace(char oldValue, char newValue) => Replace(oldValue, newValue, 0, Length);

/// <summary>
/// Replaces all instances of one rune with another in this builder.
/// </summary>
/// <param name="oldValue">The rune to replace.</param>
/// <param name="newValue">The rune to replace <paramref name="oldValue"/> with.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Replace(Rune oldValue, Rune newValue)
{
Replace(oldValue, newValue, 0, Length);
}

/// <summary>
/// Replaces all instances of one character with another in this builder.
/// </summary>
Expand All @@ -34,15 +23,8 @@ public void Replace(Rune oldValue, Rune newValue)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly void Replace(char oldValue, char newValue, int startIndex, int count)
{
if (startIndex < 0)
{
throw new ArgumentOutOfRangeException(nameof(startIndex), "Start index can't be smaller than 0.");
}

if (count > bufferPosition)
{
throw new ArgumentOutOfRangeException(nameof(count), $"Count: {count} is bigger than the current size {bufferPosition}.");
}
ArgumentOutOfRangeException.ThrowIfLessThan(startIndex, 0);
ArgumentOutOfRangeException.ThrowIfGreaterThan(startIndex + count, Length);

for (var i = startIndex; i < startIndex + count; i++)
{
Expand All @@ -53,6 +35,14 @@ public readonly void Replace(char oldValue, char newValue, int startIndex, int c
}
}

/// <summary>
/// Replaces all instances of one rune with another in this builder.
/// </summary>
/// <param name="oldValue">The rune to replace.</param>
/// <param name="newValue">The rune to replace <paramref name="oldValue"/> with.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Replace(Rune oldValue, Rune newValue) => Replace(oldValue, newValue, 0, Length);

/// <summary>
/// Replaces all instances of one rune with another in this builder.
/// </summary>
Expand All @@ -64,14 +54,14 @@ public readonly void Replace(char oldValue, char newValue, int startIndex, int c
public void Replace(Rune oldValue, Rune newValue, int startIndex, int count)
{
Span<char> oldValueChars = stackalloc char[2];
int oldValueCharsWritten = oldValue.EncodeToUtf16(oldValueChars);
ReadOnlySpan<char> oldValueCharsReadOnly = oldValueChars[..oldValueCharsWritten];
var oldValueCharsWritten = oldValue.EncodeToUtf16(oldValueChars);
ReadOnlySpan<char> oldValueCharsSlice = oldValueChars[..oldValueCharsWritten];

Span<char> newValueChars = stackalloc char[2];
int newValueCharsWritten = newValue.EncodeToUtf16(newValueChars);
ReadOnlySpan<char> newValueCharsReadOnly = newValueChars[..newValueCharsWritten];
var newValueCharsWritten = newValue.EncodeToUtf16(newValueChars);
ReadOnlySpan<char> newValueCharsSlice = newValueChars[..newValueCharsWritten];

Replace(oldValueCharsReadOnly, newValueCharsReadOnly, startIndex, count);
Replace(oldValueCharsSlice, newValueCharsSlice, startIndex, count);
}

/// <summary>
Expand Down Expand Up @@ -99,6 +89,9 @@ public void Replace(scoped ReadOnlySpan<char> oldValue, scoped ReadOnlySpan<char
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Replace(scoped ReadOnlySpan<char> oldValue, scoped ReadOnlySpan<char> newValue, int startIndex, int count)
{
ArgumentOutOfRangeException.ThrowIfLessThan(startIndex, 0);
ArgumentOutOfRangeException.ThrowIfGreaterThan(startIndex + count, Length);

var length = startIndex + count;
var slice = buffer[startIndex..length];

Expand Down Expand Up @@ -188,7 +181,7 @@ public void ReplaceGeneric<T>(ReadOnlySpan<char> oldValue, T newValue, int start
}
else
{
Replace(oldValue, (ReadOnlySpan<char>)newValue?.ToString(), startIndex, count);
Replace(oldValue, (newValue?.ToString() ?? string.Empty).AsSpan(), startIndex, count);
}
}
}
Loading