Skip to content

Append methods return ValueStringBuilder #124

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
40 changes: 24 additions & 16 deletions src/ZString.Unity/Assets/Scripts/ZString/Utf16ValueStringBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -166,18 +166,19 @@ public void AppendLine()

/// <summary>Appends the string representation of a specified value to this instance.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Append(char value)
public Utf16ValueStringBuilder Append(char value)
{
if (buffer!.Length - index < 1)
{
Grow(1);
}

buffer[index++] = value;
return this;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Append(char value, int repeatCount)
public Utf16ValueStringBuilder Append(char value, int repeatCount)
{
if (repeatCount < 0)
{
Expand All @@ -186,63 +187,66 @@ public void Append(char value, int repeatCount)

GetSpan(repeatCount).Fill(value);
Advance(repeatCount);
return this;
}

/// <summary>Appends the string representation of a specified value followed by the default line terminator to the end of this instance.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AppendLine(char value)
public Utf16ValueStringBuilder AppendLine(char value)
{
Append(value);
AppendLine();
return this;
}

/// <summary>Appends the string representation of a specified value to this instance.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Append(string value)
public Utf16ValueStringBuilder Append(string value)
{
Append(value.AsSpan());
return this;
}

/// <summary>Appends the string representation of a specified value followed by the default line terminator to the end of this instance.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AppendLine(string value)
public Utf16ValueStringBuilder AppendLine(string value)
{
Append(value);
AppendLine();
return this;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Append(string value, int startIndex, int count)
public Utf16ValueStringBuilder Append(string value, int startIndex, int count)
{
if (value == null)
{
if (startIndex == 0 && count == 0)
{
return;
}
else
{
throw new ArgumentNullException(nameof(value));
return this;
}
throw new ArgumentNullException(nameof(value));
}

Append(value.AsSpan(startIndex, count));
return this;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Append(char[] value, int startIndex, int charCount)
public Utf16ValueStringBuilder Append(char[] value, int startIndex, int charCount)
{
if (buffer!.Length - index < charCount)
{
Grow(charCount);
}
Array.Copy(value, startIndex, buffer, index, charCount);
index += charCount;
return this;
}

/// <summary>Appends a contiguous region of arbitrary memory to this instance.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Append(ReadOnlySpan<char> value)
public Utf16ValueStringBuilder Append(ReadOnlySpan<char> value)
{
if (buffer!.Length - index < value.Length)
{
Expand All @@ -251,17 +255,19 @@ public void Append(ReadOnlySpan<char> value)

value.CopyTo(buffer.AsSpan(index));
index += value.Length;
return this;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AppendLine(ReadOnlySpan<char> value)
public Utf16ValueStringBuilder AppendLine(ReadOnlySpan<char> value)
{
Append(value);
AppendLine();
return this;
}

/// <summary>Appends the string representation of a specified value to this instance.</summary>
public void Append<T>(T value)
public Utf16ValueStringBuilder Append<T>(T value)
{
if (!FormatterCache<T>.TryFormatDelegate(value, buffer.AsSpan(index), out var written, default))
{
Expand All @@ -272,13 +278,15 @@ public void Append<T>(T value)
}
}
index += written;
return this;
}

/// <summary>Appends the string representation of a specified value followed by the default line terminator to the end of this instance.</summary>
public void AppendLine<T>(T value)
public Utf16ValueStringBuilder AppendLine<T>(T value)
{
Append(value);
AppendLine();
return this;
}

static class ExceptionUtil
Expand Down
43 changes: 26 additions & 17 deletions src/ZString.Unity/Assets/Scripts/ZString/Utf8ValueStringBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public void Grow(int sizeHint)

/// <summary>Appends the default line terminator to the end of this instance.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AppendLine()
public Utf8ValueStringBuilder AppendLine()
{
if (crlf)
{
Expand All @@ -168,11 +168,12 @@ public void AppendLine()
buffer[index] = newLine1;
index += 1;
}
return this;
}

/// <summary>Appends the string representation of a specified value to this instance.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public unsafe void Append(char value)
public unsafe Utf8ValueStringBuilder Append(char value)
{
var maxLen = UTF8NoBom.GetMaxByteCount(1);
if (buffer!.Length - index < maxLen)
Expand All @@ -184,10 +185,11 @@ public unsafe void Append(char value)
{
index += UTF8NoBom.GetBytes(&value, 1, bp, maxLen);
}
return this;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Append(char value, int repeatCount)
public Utf8ValueStringBuilder Append(char value, int repeatCount)
{
if (repeatCount < 0)
{
Expand Down Expand Up @@ -215,52 +217,54 @@ public void Append(char value, int repeatCount)
Advance(len);
}
}
return this;
}

/// <summary>Appends the string representation of a specified value followed by the default line terminator to the end of this instance.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AppendLine(char value)
public Utf8ValueStringBuilder AppendLine(char value)
{
Append(value);
AppendLine();
return this;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Append(string value, int startIndex, int count)
public Utf8ValueStringBuilder Append(string value, int startIndex, int count)
{
if (value == null)
{
if (startIndex == 0 && count == 0)
{
return;
}
else
{
throw new ArgumentNullException(nameof(value));
return this;
}
throw new ArgumentNullException(nameof(value));
}

Append(value.AsSpan(startIndex, count));
return this;
}

/// <summary>Appends the string representation of a specified value to this instance.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Append(string value)
public Utf8ValueStringBuilder Append(string value)
{
Append(value.AsSpan());
return this;
}

/// <summary>Appends the string representation of a specified value followed by the default line terminator to the end of this instance.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AppendLine(string value)
public Utf8ValueStringBuilder AppendLine(string value)
{
Append(value);
AppendLine();
return this;
}

/// <summary>Appends a contiguous region of arbitrary memory to this instance.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Append(ReadOnlySpan<char> value)
public Utf8ValueStringBuilder Append(ReadOnlySpan<char> value)
{
var maxLen = UTF8NoBom.GetMaxByteCount(value.Length);
if (buffer!.Length - index < maxLen)
Expand All @@ -269,16 +273,18 @@ public void Append(ReadOnlySpan<char> value)
}

index += UTF8NoBom.GetBytes(value, buffer.AsSpan(index));
return this;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AppendLine(ReadOnlySpan<char> value)
public Utf8ValueStringBuilder AppendLine(ReadOnlySpan<char> value)
{
Append(value);
AppendLine();
return this;
}

public void AppendLiteral(ReadOnlySpan<byte> value)
public Utf8ValueStringBuilder AppendLiteral(ReadOnlySpan<byte> value)
{
if ((buffer!.Length - index) < value.Length)
{
Expand All @@ -287,10 +293,11 @@ public void AppendLiteral(ReadOnlySpan<byte> value)

value.CopyTo(buffer.AsSpan(index));
index += value.Length;
return this;
}

/// <summary>Appends the string representation of a specified value to this instance.</summary>
public void Append<T>(T value)
public Utf8ValueStringBuilder Append<T>(T value)
{
if (!FormatterCache<T>.TryFormatDelegate(value, buffer.AsSpan(index), out var written, default))
{
Expand All @@ -301,13 +308,15 @@ public void Append<T>(T value)
}
}
index += written;
return this;
}

/// <summary>Appends the string representation of a specified value followed by the default line terminator to the end of this instance.</summary>
public void AppendLine<T>(T value)
public Utf8ValueStringBuilder AppendLine<T>(T value)
{
Append(value);
AppendLine();
return this;
}

// Output
Expand Down
Loading