diff --git a/src/ZString.Unity/Assets/Scripts/ZString/Utf16ValueStringBuilder.cs b/src/ZString.Unity/Assets/Scripts/ZString/Utf16ValueStringBuilder.cs index df6ec3b..c7deef8 100644 --- a/src/ZString.Unity/Assets/Scripts/ZString/Utf16ValueStringBuilder.cs +++ b/src/ZString.Unity/Assets/Scripts/ZString/Utf16ValueStringBuilder.cs @@ -166,7 +166,7 @@ public void AppendLine() /// Appends the string representation of a specified value to this instance. [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void Append(char value) + public Utf16ValueStringBuilder Append(char value) { if (buffer!.Length - index < 1) { @@ -174,10 +174,11 @@ public void Append(char value) } 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) { @@ -186,51 +187,53 @@ public void Append(char value, int repeatCount) GetSpan(repeatCount).Fill(value); Advance(repeatCount); + return this; } /// Appends the string representation of a specified value followed by the default line terminator to the end of this instance. [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void AppendLine(char value) + public Utf16ValueStringBuilder AppendLine(char value) { Append(value); AppendLine(); + return this; } /// Appends the string representation of a specified value to this instance. [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void Append(string value) + public Utf16ValueStringBuilder Append(string value) { Append(value.AsSpan()); + return this; } /// Appends the string representation of a specified value followed by the default line terminator to the end of this instance. [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) { @@ -238,11 +241,12 @@ public void Append(char[] value, int startIndex, int charCount) } Array.Copy(value, startIndex, buffer, index, charCount); index += charCount; + return this; } /// Appends a contiguous region of arbitrary memory to this instance. [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void Append(ReadOnlySpan value) + public Utf16ValueStringBuilder Append(ReadOnlySpan value) { if (buffer!.Length - index < value.Length) { @@ -251,17 +255,19 @@ public void Append(ReadOnlySpan value) value.CopyTo(buffer.AsSpan(index)); index += value.Length; + return this; } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void AppendLine(ReadOnlySpan value) + public Utf16ValueStringBuilder AppendLine(ReadOnlySpan value) { Append(value); AppendLine(); + return this; } /// Appends the string representation of a specified value to this instance. - public void Append(T value) + public Utf16ValueStringBuilder Append(T value) { if (!FormatterCache.TryFormatDelegate(value, buffer.AsSpan(index), out var written, default)) { @@ -272,13 +278,15 @@ public void Append(T value) } } index += written; + return this; } /// Appends the string representation of a specified value followed by the default line terminator to the end of this instance. - public void AppendLine(T value) + public Utf16ValueStringBuilder AppendLine(T value) { Append(value); AppendLine(); + return this; } static class ExceptionUtil diff --git a/src/ZString.Unity/Assets/Scripts/ZString/Utf8ValueStringBuilder.cs b/src/ZString.Unity/Assets/Scripts/ZString/Utf8ValueStringBuilder.cs index 19df488..818010f 100644 --- a/src/ZString.Unity/Assets/Scripts/ZString/Utf8ValueStringBuilder.cs +++ b/src/ZString.Unity/Assets/Scripts/ZString/Utf8ValueStringBuilder.cs @@ -153,7 +153,7 @@ public void Grow(int sizeHint) /// Appends the default line terminator to the end of this instance. [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void AppendLine() + public Utf8ValueStringBuilder AppendLine() { if (crlf) { @@ -168,11 +168,12 @@ public void AppendLine() buffer[index] = newLine1; index += 1; } + return this; } /// Appends the string representation of a specified value to this instance. [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) @@ -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) { @@ -215,52 +217,54 @@ public void Append(char value, int repeatCount) Advance(len); } } + return this; } /// Appends the string representation of a specified value followed by the default line terminator to the end of this instance. [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; } /// Appends the string representation of a specified value to this instance. [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void Append(string value) + public Utf8ValueStringBuilder Append(string value) { Append(value.AsSpan()); + return this; } /// Appends the string representation of a specified value followed by the default line terminator to the end of this instance. [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void AppendLine(string value) + public Utf8ValueStringBuilder AppendLine(string value) { Append(value); AppendLine(); + return this; } /// Appends a contiguous region of arbitrary memory to this instance. [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void Append(ReadOnlySpan value) + public Utf8ValueStringBuilder Append(ReadOnlySpan value) { var maxLen = UTF8NoBom.GetMaxByteCount(value.Length); if (buffer!.Length - index < maxLen) @@ -269,16 +273,18 @@ public void Append(ReadOnlySpan value) } index += UTF8NoBom.GetBytes(value, buffer.AsSpan(index)); + return this; } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void AppendLine(ReadOnlySpan value) + public Utf8ValueStringBuilder AppendLine(ReadOnlySpan value) { Append(value); AppendLine(); + return this; } - public void AppendLiteral(ReadOnlySpan value) + public Utf8ValueStringBuilder AppendLiteral(ReadOnlySpan value) { if ((buffer!.Length - index) < value.Length) { @@ -287,10 +293,11 @@ public void AppendLiteral(ReadOnlySpan value) value.CopyTo(buffer.AsSpan(index)); index += value.Length; + return this; } /// Appends the string representation of a specified value to this instance. - public void Append(T value) + public Utf8ValueStringBuilder Append(T value) { if (!FormatterCache.TryFormatDelegate(value, buffer.AsSpan(index), out var written, default)) { @@ -301,13 +308,15 @@ public void Append(T value) } } index += written; + return this; } /// Appends the string representation of a specified value followed by the default line terminator to the end of this instance. - public void AppendLine(T value) + public Utf8ValueStringBuilder AppendLine(T value) { Append(value); AppendLine(); + return this; } // Output diff --git a/src/ZString/Utf16ValueStringBuilder.cs b/src/ZString/Utf16ValueStringBuilder.cs index df6ec3b..c7deef8 100644 --- a/src/ZString/Utf16ValueStringBuilder.cs +++ b/src/ZString/Utf16ValueStringBuilder.cs @@ -166,7 +166,7 @@ public void AppendLine() /// Appends the string representation of a specified value to this instance. [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void Append(char value) + public Utf16ValueStringBuilder Append(char value) { if (buffer!.Length - index < 1) { @@ -174,10 +174,11 @@ public void Append(char value) } 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) { @@ -186,51 +187,53 @@ public void Append(char value, int repeatCount) GetSpan(repeatCount).Fill(value); Advance(repeatCount); + return this; } /// Appends the string representation of a specified value followed by the default line terminator to the end of this instance. [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void AppendLine(char value) + public Utf16ValueStringBuilder AppendLine(char value) { Append(value); AppendLine(); + return this; } /// Appends the string representation of a specified value to this instance. [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void Append(string value) + public Utf16ValueStringBuilder Append(string value) { Append(value.AsSpan()); + return this; } /// Appends the string representation of a specified value followed by the default line terminator to the end of this instance. [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) { @@ -238,11 +241,12 @@ public void Append(char[] value, int startIndex, int charCount) } Array.Copy(value, startIndex, buffer, index, charCount); index += charCount; + return this; } /// Appends a contiguous region of arbitrary memory to this instance. [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void Append(ReadOnlySpan value) + public Utf16ValueStringBuilder Append(ReadOnlySpan value) { if (buffer!.Length - index < value.Length) { @@ -251,17 +255,19 @@ public void Append(ReadOnlySpan value) value.CopyTo(buffer.AsSpan(index)); index += value.Length; + return this; } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void AppendLine(ReadOnlySpan value) + public Utf16ValueStringBuilder AppendLine(ReadOnlySpan value) { Append(value); AppendLine(); + return this; } /// Appends the string representation of a specified value to this instance. - public void Append(T value) + public Utf16ValueStringBuilder Append(T value) { if (!FormatterCache.TryFormatDelegate(value, buffer.AsSpan(index), out var written, default)) { @@ -272,13 +278,15 @@ public void Append(T value) } } index += written; + return this; } /// Appends the string representation of a specified value followed by the default line terminator to the end of this instance. - public void AppendLine(T value) + public Utf16ValueStringBuilder AppendLine(T value) { Append(value); AppendLine(); + return this; } static class ExceptionUtil diff --git a/src/ZString/Utf8ValueStringBuilder.cs b/src/ZString/Utf8ValueStringBuilder.cs index 19df488..818010f 100644 --- a/src/ZString/Utf8ValueStringBuilder.cs +++ b/src/ZString/Utf8ValueStringBuilder.cs @@ -153,7 +153,7 @@ public void Grow(int sizeHint) /// Appends the default line terminator to the end of this instance. [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void AppendLine() + public Utf8ValueStringBuilder AppendLine() { if (crlf) { @@ -168,11 +168,12 @@ public void AppendLine() buffer[index] = newLine1; index += 1; } + return this; } /// Appends the string representation of a specified value to this instance. [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) @@ -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) { @@ -215,52 +217,54 @@ public void Append(char value, int repeatCount) Advance(len); } } + return this; } /// Appends the string representation of a specified value followed by the default line terminator to the end of this instance. [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; } /// Appends the string representation of a specified value to this instance. [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void Append(string value) + public Utf8ValueStringBuilder Append(string value) { Append(value.AsSpan()); + return this; } /// Appends the string representation of a specified value followed by the default line terminator to the end of this instance. [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void AppendLine(string value) + public Utf8ValueStringBuilder AppendLine(string value) { Append(value); AppendLine(); + return this; } /// Appends a contiguous region of arbitrary memory to this instance. [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void Append(ReadOnlySpan value) + public Utf8ValueStringBuilder Append(ReadOnlySpan value) { var maxLen = UTF8NoBom.GetMaxByteCount(value.Length); if (buffer!.Length - index < maxLen) @@ -269,16 +273,18 @@ public void Append(ReadOnlySpan value) } index += UTF8NoBom.GetBytes(value, buffer.AsSpan(index)); + return this; } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void AppendLine(ReadOnlySpan value) + public Utf8ValueStringBuilder AppendLine(ReadOnlySpan value) { Append(value); AppendLine(); + return this; } - public void AppendLiteral(ReadOnlySpan value) + public Utf8ValueStringBuilder AppendLiteral(ReadOnlySpan value) { if ((buffer!.Length - index) < value.Length) { @@ -287,10 +293,11 @@ public void AppendLiteral(ReadOnlySpan value) value.CopyTo(buffer.AsSpan(index)); index += value.Length; + return this; } /// Appends the string representation of a specified value to this instance. - public void Append(T value) + public Utf8ValueStringBuilder Append(T value) { if (!FormatterCache.TryFormatDelegate(value, buffer.AsSpan(index), out var written, default)) { @@ -301,13 +308,15 @@ public void Append(T value) } } index += written; + return this; } /// Appends the string representation of a specified value followed by the default line terminator to the end of this instance. - public void AppendLine(T value) + public Utf8ValueStringBuilder AppendLine(T value) { Append(value); AppendLine(); + return this; } // Output