feat: Adds cliloc support and fixes valuestringbuilder ctor (#1013)
- [X] Adds cliloc support using the following API:
```cs
public static class Localization
{
string GetText(int number);
string GetText(int number, string lang);
string Format(int number, params object[] args);
string Format(int number, string lang, params object[] args);
string Format(int number, $"{arg1}{arg2}{arg3}");
string Format(int number, lang, $"{arg1}{arg2}{arg3}");
bool TryGetLocalization(int number, out LocalizationEntry entry);
bool TryGetLocalization(int number, string lang, out LocalizationEntry entry);
}
public class LocalizationEntry
{
string Language { get; }
string Number { get; }
string Text { get; }
string?[] TextSlices { get; } // Used for string building
string StringFormatter { get; }
string Format(params object[] args);
string Format($"{arg1}{arg2}{arg3}");
}
```
- [X] Fixes a bug with ValueStringBuilder and default initialization size
- [X] Optimizes ValueStringBuilder to use `ISpanFormattable`
- [X] Adds `Append<T>(T value);` support ValueStringBuilder
- [X] Adds `Append($"");` string interpolation support to ValueStringBuider
2022-05-08 21:24:32 -07:00
/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* ModernUO *
2026-03-05 19:36:54 -08:00
* Copyright 2019 - 2026 - ModernUO Development Team *
feat: Adds cliloc support and fixes valuestringbuilder ctor (#1013)
- [X] Adds cliloc support using the following API:
```cs
public static class Localization
{
string GetText(int number);
string GetText(int number, string lang);
string Format(int number, params object[] args);
string Format(int number, string lang, params object[] args);
string Format(int number, $"{arg1}{arg2}{arg3}");
string Format(int number, lang, $"{arg1}{arg2}{arg3}");
bool TryGetLocalization(int number, out LocalizationEntry entry);
bool TryGetLocalization(int number, string lang, out LocalizationEntry entry);
}
public class LocalizationEntry
{
string Language { get; }
string Number { get; }
string Text { get; }
string?[] TextSlices { get; } // Used for string building
string StringFormatter { get; }
string Format(params object[] args);
string Format($"{arg1}{arg2}{arg3}");
}
```
- [X] Fixes a bug with ValueStringBuilder and default initialization size
- [X] Optimizes ValueStringBuilder to use `ISpanFormattable`
- [X] Adds `Append<T>(T value);` support ValueStringBuilder
- [X] Adds `Append($"");` string interpolation support to ValueStringBuider
2022-05-08 21:24:32 -07:00
* Email : hi @modernuo . com *
* File : LocalizationEntry . cs *
* *
* This program is free software : you can redistribute it and / or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation , either version 3 of the License , or *
* ( at your option ) any later version . *
* *
* You should have received a copy of the GNU General Public License *
* along with this program . If not , see < http : //www.gnu.org/licenses/>. *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /
2022-05-10 00:37:07 -07:00
using System ;
feat: Adds cliloc support and fixes valuestringbuilder ctor (#1013)
- [X] Adds cliloc support using the following API:
```cs
public static class Localization
{
string GetText(int number);
string GetText(int number, string lang);
string Format(int number, params object[] args);
string Format(int number, string lang, params object[] args);
string Format(int number, $"{arg1}{arg2}{arg3}");
string Format(int number, lang, $"{arg1}{arg2}{arg3}");
bool TryGetLocalization(int number, out LocalizationEntry entry);
bool TryGetLocalization(int number, string lang, out LocalizationEntry entry);
}
public class LocalizationEntry
{
string Language { get; }
string Number { get; }
string Text { get; }
string?[] TextSlices { get; } // Used for string building
string StringFormatter { get; }
string Format(params object[] args);
string Format($"{arg1}{arg2}{arg3}");
}
```
- [X] Fixes a bug with ValueStringBuilder and default initialization size
- [X] Optimizes ValueStringBuilder to use `ISpanFormattable`
- [X] Adds `Append<T>(T value);` support ValueStringBuilder
- [X] Adds `Append($"");` string interpolation support to ValueStringBuider
2022-05-08 21:24:32 -07:00
using System.Runtime.CompilerServices ;
using System.Text.RegularExpressions ;
using Server.Buffers ;
2023-08-01 04:46:49 +01:00
using Server.Text ;
feat: Adds cliloc support and fixes valuestringbuilder ctor (#1013)
- [X] Adds cliloc support using the following API:
```cs
public static class Localization
{
string GetText(int number);
string GetText(int number, string lang);
string Format(int number, params object[] args);
string Format(int number, string lang, params object[] args);
string Format(int number, $"{arg1}{arg2}{arg3}");
string Format(int number, lang, $"{arg1}{arg2}{arg3}");
bool TryGetLocalization(int number, out LocalizationEntry entry);
bool TryGetLocalization(int number, string lang, out LocalizationEntry entry);
}
public class LocalizationEntry
{
string Language { get; }
string Number { get; }
string Text { get; }
string?[] TextSlices { get; } // Used for string building
string StringFormatter { get; }
string Format(params object[] args);
string Format($"{arg1}{arg2}{arg3}");
}
```
- [X] Fixes a bug with ValueStringBuilder and default initialization size
- [X] Optimizes ValueStringBuilder to use `ISpanFormattable`
- [X] Adds `Append<T>(T value);` support ValueStringBuilder
- [X] Adds `Append($"");` string interpolation support to ValueStringBuider
2022-05-08 21:24:32 -07:00
using Server.Collections ;
namespace Server ;
public class LocalizationEntry
{
private static readonly Regex _textRegex = new (
@"~(\d+)[_\w]+~" ,
RegexOptions . Compiled |
RegexOptions . IgnoreCase |
RegexOptions . Singleline |
RegexOptions . CultureInvariant
) ;
public string Language { get ; }
public int Number { get ; }
public string Text { get ; }
public string [ ] TextSlices { get ; }
public string StringFormatter { get ; }
public LocalizationEntry ( string lang , int number , string text )
{
Language = lang ;
Number = number ;
Text = text ;
ParseText ( text , out var textSlices , out var stringFormatter ) ;
TextSlices = textSlices ;
StringFormatter = stringFormatter ;
}
private static void ParseText ( string text , out string [ ] textSlices , out string stringFormatter )
{
feat: Add zero-alloc interpolation handler to ValueStringBuilder, replace all StringBuilder usage (#2387)
## Summary
- **Add a self-referencing `InterpolationHandler` to `ValueStringBuilder`** that writes directly into the builder's buffer — zero intermediate allocation, works with `stackalloc`-backed builders
- **Replace all `System.Text.StringBuilder` usage** across the codebase with `ValueStringBuilder`
- **Convert `ValueStringBuilder.Create()` to `stackalloc`** at 10 sites where output length is provably bounded
- **Convert manual `Dispose()` to `using var`** where possible, and hoist loop-scoped builders outside loops with `Reset()`
- **Convert verbose `Append()` chains to `Append($"...")`** interpolation for readability
- **Add comprehensive documentation** for string handling patterns
## InterpolationHandler Design
`ValueStringBuilder` is a `ref struct`, which creates challenges for C#'s interpolated string handler pattern:
- **`ref` fields to ref structs are not allowed** (CS9050)
- **`[InterpolatedStringHandlerArgument("")]` passes struct receivers by value**, not by ref
- **`ISelfInterpolatedStringHandler` requires boxing** ref structs into interface fields
**Solution: Copy-and-reconcile pattern.** The handler receives a value copy of the builder. The copy shares the same underlying `char` buffer (`Span` points to the same `stackalloc`/pooled memory), so writes go to the original buffer. `Append()` reconciles by `this = handler._builder`, updating `_length` and any buffer references changed by `Grow()`.
This is safe because:
- The game loop is single-threaded — no concurrent access between handler construction and reconciliation
- If `Grow()` occurs in the copy, the original's stale buffer isn't accessed until `Append()` replaces it
- `Dispose()` correctly returns the reconciled buffer to the pool
## Changes by Category
### ValueStringBuilder (`Projects/Server/Buffers/ValueStringBuilder.cs`)
- Added nested `InterpolationHandler` ref struct with copy-and-reconcile pattern
- Added `Append([InterpolatedStringHandlerArgument("")] scoped ref InterpolationHandler)` method
- Removed `RawInterpolatedStringHandler` overloads (new handler replaces them)
- All `AppendFormatted` overloads delegate to existing `Append` methods (no code duplication)
- Alignment support via direct private field access (nested type privilege)
### StringBuilder → ValueStringBuilder (15 files)
Replaced all `new StringBuilder()` with `ValueStringBuilder.Create()` or `stackalloc`:
- ConPVP games: KingOfTheHill, DoubleDom, CTF, BombingRun, TourneyMatch
- ConPVP infrastructure: Tournament, Participant, TourneyParticipant
- ConPVP gumps: ArenaGump, TournamentBracketGump, AcceptTeamGump, ConfirmSignupGump
- Commands: Handlers, Logging, Add
- Other: TownCrier, SpeechLogGump, TestCenter
Key patterns:
- `sb = new StringBuilder()` reassignment → `sb.Reset()`
- `sb.AppendFormat("{0:N0}", value)` → `sb.Append($"{value:N0}")`
- `sb.Append(x).Append(y)` chains → separate statements (VSB returns void)
### Create() → stackalloc (10 files)
Converted heap-allocated builders to stackalloc where output is bounded:
- ClientVersion (32), MapSelection (160), HouseRaffleStone (48)
- HolySense (96), UnholySense (96), ClientVerification (192)
- AcceptTeamGump (64), ConfirmSignupGump (64)
- BaseWeapon (160), BaseArmor (128)
### Loop optimizations (2 files)
Hoisted `ValueStringBuilder` creation outside loops with `Reset()` per iteration:
- TourneyMatch.cs: `using var` inside for loop → stackalloc before loop
- ArenaGump.cs: `Create()` + `Dispose()` per iteration → stackalloc before loop
### Append chain → interpolation (5 files)
Converted multi-line `Append()` chains to `Append($"...")`:
- BountyMessage.cs: title switch (6 cases), paragraph (15→1 Append), description lines, closing
- AcceptTeamGump, ConfirmSignupGump, TournamentBracketGump: tournament type strings
- AdminGump: comment/tag formatting in loops
### Documentation
- `dev-docs/string-handling.md`: Full reference — construction, interpolation, disposal, decision guide
- `dev-docs/claude-skills/modernuo-string-handling.md`: Claude skill with quick reference
- `CLAUDE.md`: Added rule 17 (no StringBuilder), dev-docs table entry, skills table entry
- `dev-docs/code-standards.md`: Updated memory management section
## Test Plan
- [x] `dotnet build` — 0 errors, 0 warnings
- [x] `dotnet test` — 940/940 tests pass
- [x] 28 ValueStringBuilder tests covering all reconciliation scenarios:
- Stackalloc no-grow, stackalloc with grow (→pool transition)
- Heap no-grow, heap with grow, heap double grow
- Pre-existing content with and without grow
- Sequential multiple `Append($"...")` calls
- Mixed plain + interpolated Append
- Empty interpolation, literal-only, format specifiers
- Null string holes, ISpanFormattable types
- Dispose after stackalloc→pool grow
2026-03-22 14:23:44 -07:00
using var sb = ValueStringBuilder . Create ( 256 ) ;
2023-02-13 23:09:50 -08:00
using var queue = PooledRefQueue < string > . Create ( ) ;
2025-12-27 16:47:28 -08:00
var hasMatch = false ;
feat: Adds cliloc support and fixes valuestringbuilder ctor (#1013)
- [X] Adds cliloc support using the following API:
```cs
public static class Localization
{
string GetText(int number);
string GetText(int number, string lang);
string Format(int number, params object[] args);
string Format(int number, string lang, params object[] args);
string Format(int number, $"{arg1}{arg2}{arg3}");
string Format(int number, lang, $"{arg1}{arg2}{arg3}");
bool TryGetLocalization(int number, out LocalizationEntry entry);
bool TryGetLocalization(int number, string lang, out LocalizationEntry entry);
}
public class LocalizationEntry
{
string Language { get; }
string Number { get; }
string Text { get; }
string?[] TextSlices { get; } // Used for string building
string StringFormatter { get; }
string Format(params object[] args);
string Format($"{arg1}{arg2}{arg3}");
}
```
- [X] Fixes a bug with ValueStringBuilder and default initialization size
- [X] Optimizes ValueStringBuilder to use `ISpanFormattable`
- [X] Adds `Append<T>(T value);` support ValueStringBuilder
- [X] Adds `Append($"");` string interpolation support to ValueStringBuider
2022-05-08 21:24:32 -07:00
var prevIndex = 0 ;
foreach ( Match match in _textRegex . Matches ( text ) )
{
if ( prevIndex < match . Index )
{
var substr = text [ prevIndex . . match . Index ] ;
2023-02-13 23:09:50 -08:00
sb . Append ( substr ) ;
feat: Adds cliloc support and fixes valuestringbuilder ctor (#1013)
- [X] Adds cliloc support using the following API:
```cs
public static class Localization
{
string GetText(int number);
string GetText(int number, string lang);
string Format(int number, params object[] args);
string Format(int number, string lang, params object[] args);
string Format(int number, $"{arg1}{arg2}{arg3}");
string Format(int number, lang, $"{arg1}{arg2}{arg3}");
bool TryGetLocalization(int number, out LocalizationEntry entry);
bool TryGetLocalization(int number, string lang, out LocalizationEntry entry);
}
public class LocalizationEntry
{
string Language { get; }
string Number { get; }
string Text { get; }
string?[] TextSlices { get; } // Used for string building
string StringFormatter { get; }
string Format(params object[] args);
string Format($"{arg1}{arg2}{arg3}");
}
```
- [X] Fixes a bug with ValueStringBuilder and default initialization size
- [X] Optimizes ValueStringBuilder to use `ISpanFormattable`
- [X] Adds `Append<T>(T value);` support ValueStringBuilder
- [X] Adds `Append($"");` string interpolation support to ValueStringBuider
2022-05-08 21:24:32 -07:00
queue . Enqueue ( substr ) ;
}
queue . Enqueue ( null ) ;
hasMatch = true ;
2023-02-13 23:09:50 -08:00
sb . Append ( $"{{{int.Parse(match.Groups[1].Value) - 1}}}" ) ;
feat: Adds cliloc support and fixes valuestringbuilder ctor (#1013)
- [X] Adds cliloc support using the following API:
```cs
public static class Localization
{
string GetText(int number);
string GetText(int number, string lang);
string Format(int number, params object[] args);
string Format(int number, string lang, params object[] args);
string Format(int number, $"{arg1}{arg2}{arg3}");
string Format(int number, lang, $"{arg1}{arg2}{arg3}");
bool TryGetLocalization(int number, out LocalizationEntry entry);
bool TryGetLocalization(int number, string lang, out LocalizationEntry entry);
}
public class LocalizationEntry
{
string Language { get; }
string Number { get; }
string Text { get; }
string?[] TextSlices { get; } // Used for string building
string StringFormatter { get; }
string Format(params object[] args);
string Format($"{arg1}{arg2}{arg3}");
}
```
- [X] Fixes a bug with ValueStringBuilder and default initialization size
- [X] Optimizes ValueStringBuilder to use `ISpanFormattable`
- [X] Adds `Append<T>(T value);` support ValueStringBuilder
- [X] Adds `Append($"");` string interpolation support to ValueStringBuider
2022-05-08 21:24:32 -07:00
prevIndex = match . Index + match . Length ;
}
if ( prevIndex < text . Length - 1 )
{
var substr = prevIndex = = 0 ? text : text [ prevIndex . . ] ;
2023-02-13 23:09:50 -08:00
sb . Append ( substr ) ;
feat: Adds cliloc support and fixes valuestringbuilder ctor (#1013)
- [X] Adds cliloc support using the following API:
```cs
public static class Localization
{
string GetText(int number);
string GetText(int number, string lang);
string Format(int number, params object[] args);
string Format(int number, string lang, params object[] args);
string Format(int number, $"{arg1}{arg2}{arg3}");
string Format(int number, lang, $"{arg1}{arg2}{arg3}");
bool TryGetLocalization(int number, out LocalizationEntry entry);
bool TryGetLocalization(int number, string lang, out LocalizationEntry entry);
}
public class LocalizationEntry
{
string Language { get; }
string Number { get; }
string Text { get; }
string?[] TextSlices { get; } // Used for string building
string StringFormatter { get; }
string Format(params object[] args);
string Format($"{arg1}{arg2}{arg3}");
}
```
- [X] Fixes a bug with ValueStringBuilder and default initialization size
- [X] Optimizes ValueStringBuilder to use `ISpanFormattable`
- [X] Adds `Append<T>(T value);` support ValueStringBuilder
- [X] Adds `Append($"");` string interpolation support to ValueStringBuider
2022-05-08 21:24:32 -07:00
queue . Enqueue ( substr ) ;
}
textSlices = queue . ToArray ( ) ;
2023-02-13 23:09:50 -08:00
stringFormatter = hasMatch ? sb . ToString ( ) : null ;
feat: Adds cliloc support and fixes valuestringbuilder ctor (#1013)
- [X] Adds cliloc support using the following API:
```cs
public static class Localization
{
string GetText(int number);
string GetText(int number, string lang);
string Format(int number, params object[] args);
string Format(int number, string lang, params object[] args);
string Format(int number, $"{arg1}{arg2}{arg3}");
string Format(int number, lang, $"{arg1}{arg2}{arg3}");
bool TryGetLocalization(int number, out LocalizationEntry entry);
bool TryGetLocalization(int number, string lang, out LocalizationEntry entry);
}
public class LocalizationEntry
{
string Language { get; }
string Number { get; }
string Text { get; }
string?[] TextSlices { get; } // Used for string building
string StringFormatter { get; }
string Format(params object[] args);
string Format($"{arg1}{arg2}{arg3}");
}
```
- [X] Fixes a bug with ValueStringBuilder and default initialization size
- [X] Optimizes ValueStringBuilder to use `ISpanFormattable`
- [X] Adds `Append<T>(T value);` support ValueStringBuilder
- [X] Adds `Append($"");` string interpolation support to ValueStringBuider
2022-05-08 21:24:32 -07:00
}
/// <summary>
/// Creates a formatted string of the localization entry.
/// Uses string interpolation under the hood. This method is preferably relative to the object array method signature.
/// Example:
/// Format($"{totalItems}{maxItems}{totalWeight}");
/// </summary>
/// <param name="handler">interpolated string handler used by the compiler as a string builder during compilation</param>
/// <returns>A copy of the localization text where the placeholder arguments have been replaced with string representations of the provided interpolation arguments</returns>
public PooledArraySpanFormattable Format (
[InterpolatedStringHandlerArgument("")]
ref LocalizationInterpolationHandler handler
)
{
var chars = handler . ToPooledArray ( out var length ) ;
handler = default ; // Defensive clear
return new PooledArraySpanFormattable ( chars , length ) ;
}
2022-05-10 00:37:07 -07:00
[InterpolatedStringHandler]
public ref struct LocalizationInterpolationHandler
{
private static string [ ] _empty = Array . Empty < string > ( ) ;
private char [ ] ? _arrayToReturnToPool ;
private Span < char > _chars ;
private int _pos ;
private int _index ;
private string? [ ] _slices ;
private string? _current ;
private string _lang ;
public LocalizationInterpolationHandler ( int literalLength , int formattedCount , LocalizationEntry entry , out bool isValid )
{
_slices = entry . TextSlices ;
2022-05-10 18:50:23 -07:00
_chars = _arrayToReturnToPool = STArrayPool < char > . Shared . Rent ( 256 ) ;
2022-05-10 00:37:07 -07:00
isValid = true ;
_pos = 0 ;
_index = 0 ;
_current = null ;
_lang = entry . Language ;
}
2022-05-10 00:42:59 -07:00
public LocalizationInterpolationHandler (
int literalLength , int formattedCount , int number , out bool isValid
) : this ( literalLength , formattedCount , number , Localization . FallbackLanguage , out isValid )
{
}
public LocalizationInterpolationHandler (
int literalLength , int formattedCount , int number , string lang , out bool isValid
)
2022-05-10 00:37:07 -07:00
{
if ( Localization . TryGetLocalization ( lang , number , out var entry ) )
{
_slices = entry . TextSlices ;
2022-05-10 18:50:23 -07:00
_chars = _arrayToReturnToPool = STArrayPool < char > . Shared . Rent ( 256 ) ;
2022-05-10 00:37:07 -07:00
isValid = true ;
}
else
{
_slices = _empty ;
_chars = _arrayToReturnToPool = default ;
isValid = false ;
}
_pos = 0 ;
_index = 0 ;
_current = null ;
_lang = lang ;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private bool MoveNext ( )
{
if ( ( uint ) _index > = ( uint ) _slices . Length )
{
return false ;
}
_current = _slices [ _index + + ] ;
return true ;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private bool ReadyToAppend ( )
{
if ( ! MoveNext ( ) )
{
return false ;
}
if ( _current = = null )
{
return true ;
}
AppendStringDirect ( _current ) ;
return MoveNext ( ) ;
}
public void AppendLiteral ( string value )
{
}
private void AppendStringDirect ( string value )
{
if ( value . TryCopyTo ( _chars [ _pos . . ] ) )
{
_pos + = value . Length ;
}
else
{
GrowThenCopyString ( value ) ;
}
}
public void AppendFormatted < T > ( T value )
{
if ( ! ReadyToAppend ( ) )
{
return ;
}
string? s ;
if ( value is IFormattable )
{
// If the value can format itself directly into our buffer, do so.
if ( value is ISpanFormattable )
{
int charsWritten ;
while ( ! ( ( ISpanFormattable ) value ) . TryFormat ( _chars [ _pos . . ] , out charsWritten , default , default ) ) // constrained call avoiding boxing for value types
{
Grow ( ) ;
}
_pos + = charsWritten ;
return ;
}
s = ( ( IFormattable ) value ) . ToString ( format : null , default ) ; // constrained call avoiding boxing for value types
}
else
{
s = value ? . ToString ( ) ;
}
if ( s is not null )
{
AppendStringDirect ( s ) ;
}
}
2022-06-04 11:53:32 -07:00
// Each numeric needs its own override
public void AppendFormatted ( int value , string? format )
{
if ( ! ReadyToAppend ( ) )
{
return ;
}
if ( ! TryAppendCliloc ( value , format ) )
{
AppendFormattedDirect ( value , format ) ;
}
}
public void AppendFormatted ( uint value , string? format )
2022-05-10 00:37:07 -07:00
{
if ( ! ReadyToAppend ( ) )
{
return ;
}
2022-06-04 11:53:32 -07:00
if ( ! TryAppendCliloc ( ( int ) value , format ) )
{
AppendFormattedDirect ( value , format ) ;
}
}
public void AppendFormatted ( long value , string? format )
{
if ( ! ReadyToAppend ( ) )
{
return ;
}
if ( ! TryAppendCliloc ( ( int ) value , format ) )
{
AppendFormattedDirect ( value , format ) ;
}
}
public void AppendFormatted ( ulong value , string? format )
{
if ( ! ReadyToAppend ( ) )
{
return ;
}
if ( ! TryAppendCliloc ( ( int ) value , format ) )
{
AppendFormattedDirect ( value , format ) ;
}
}
public void AppendFormatted < T > ( T value , string? format )
{
if ( ReadyToAppend ( ) )
{
AppendFormattedDirect ( value , format ) ;
}
}
private void AppendFormattedDirect < T > ( T value , string? format )
{
2022-05-10 00:37:07 -07:00
string? s ;
if ( value is IFormattable )
{
// If the value can format itself directly into our buffer, do so.
if ( value is ISpanFormattable )
{
int charsWritten ;
while ( ! ( ( ISpanFormattable ) value ) . TryFormat ( _chars [ _pos . . ] , out charsWritten , format , default ) ) // constrained call avoiding boxing for value types
{
Grow ( ) ;
}
_pos + = charsWritten ;
return ;
}
s = ( ( IFormattable ) value ) . ToString ( format , default ) ; // constrained call avoiding boxing for value types
}
else
{
s = value ? . ToString ( ) ;
}
if ( s is not null )
{
AppendStringDirect ( s ) ;
}
}
public void AppendFormatted < T > ( T value , int alignment )
{
if ( ! ReadyToAppend ( ) )
{
return ;
}
var startingPos = _pos ;
AppendFormatted ( value ) ;
if ( alignment ! = 0 )
{
AppendOrInsertAlignmentIfNeeded ( startingPos , alignment ) ;
}
}
2022-06-04 11:53:32 -07:00
// Each numeric needs its own override
public void AppendFormatted ( int value , int alignment , string? format )
{
if ( ! ReadyToAppend ( ) )
{
return ;
}
var startingPos = _pos ;
if ( TryAppendCliloc ( value , format ) )
{
AppendFormatted ( value , format ) ;
if ( alignment ! = 0 )
{
AppendOrInsertAlignmentIfNeeded ( startingPos , alignment ) ;
}
}
else
{
AppendFormattedDirect ( value , alignment , format ) ;
}
}
public void AppendFormatted ( uint value , int alignment , string? format )
{
if ( ! ReadyToAppend ( ) )
{
return ;
}
var startingPos = _pos ;
if ( TryAppendCliloc ( ( int ) value , format ) )
{
AppendFormatted ( value , format ) ;
if ( alignment ! = 0 )
{
AppendOrInsertAlignmentIfNeeded ( startingPos , alignment ) ;
}
}
else
{
AppendFormattedDirect ( value , alignment , format ) ;
}
}
public void AppendFormatted ( long value , int alignment , string? format )
{
if ( ! ReadyToAppend ( ) )
{
return ;
}
var startingPos = _pos ;
if ( TryAppendCliloc ( ( int ) value , format ) )
{
AppendFormatted ( value , format ) ;
if ( alignment ! = 0 )
{
AppendOrInsertAlignmentIfNeeded ( startingPos , alignment ) ;
}
}
else
{
AppendFormattedDirect ( value , alignment , format ) ;
}
}
public void AppendFormatted ( ulong value , int alignment , string? format )
{
if ( ! ReadyToAppend ( ) )
{
return ;
}
var startingPos = _pos ;
if ( TryAppendCliloc ( ( int ) value , format ) )
{
AppendFormatted ( value , format ) ;
if ( alignment ! = 0 )
{
AppendOrInsertAlignmentIfNeeded ( startingPos , alignment ) ;
}
}
else
{
AppendFormattedDirect ( value , alignment , format ) ;
}
}
2022-05-10 00:37:07 -07:00
public void AppendFormatted < T > ( T value , int alignment , string? format )
{
if ( ! ReadyToAppend ( ) )
{
return ;
}
2022-06-04 11:53:32 -07:00
AppendFormattedDirect ( value , alignment , format ) ;
}
private void AppendFormattedDirect < T > ( T value , int alignment , string? format )
{
2022-05-10 00:37:07 -07:00
var startingPos = _pos ;
AppendFormatted ( value , format ) ;
if ( alignment ! = 0 )
{
AppendOrInsertAlignmentIfNeeded ( startingPos , alignment ) ;
}
}
public void AppendFormatted ( ReadOnlySpan < char > value )
{
2022-06-04 11:53:32 -07:00
if ( ! ReadyToAppend ( ) | | TryAppendClilocByNumericString ( value ) )
2022-05-10 00:37:07 -07:00
{
return ;
}
if ( value . TryCopyTo ( _chars [ _pos . . ] ) )
{
_pos + = value . Length ;
}
else
{
GrowThenCopySpan ( value ) ;
}
}
public void AppendFormatted ( ReadOnlySpan < char > value , int alignment , string? format = null )
{
if ( ! ReadyToAppend ( ) )
{
return ;
}
var leftAlign = false ;
if ( alignment < 0 )
{
leftAlign = true ;
alignment = - alignment ;
}
var paddingRequired = alignment - value . Length ;
if ( paddingRequired < = 0 )
{
// The value is as large or larger than the required amount of padding,
// so just write the value.
AppendFormatted ( value ) ;
return ;
}
// Write the value along with the appropriate padding.
EnsureCapacityForAdditionalChars ( value . Length + paddingRequired ) ;
if ( leftAlign )
{
value . CopyTo ( _chars [ _pos . . ] ) ;
_pos + = value . Length ;
_chars . Slice ( _pos , paddingRequired ) . Fill ( ' ' ) ;
_pos + = paddingRequired ;
}
else
{
_chars . Slice ( _pos , paddingRequired ) . Fill ( ' ' ) ;
_pos + = paddingRequired ;
value . CopyTo ( _chars [ _pos . . ] ) ;
_pos + = value . Length ;
}
}
2022-06-04 11:53:32 -07:00
public void AppendFormatted ( object? value , int alignment = 0 , string? format = null )
{
if ( value is int i )
{
AppendFormatted ( i , alignment , format ) ;
}
else if ( value is uint ui )
{
AppendFormatted ( ui , alignment , format ) ;
}
else if ( value is long l )
{
AppendFormatted ( l , alignment , format ) ;
}
else if ( value is ulong ul )
{
AppendFormatted ( ul , alignment , format ) ;
}
else
{
AppendFormatted < object? > ( value , alignment , format ) ;
}
}
2022-05-10 00:37:07 -07:00
public void AppendFormatted ( string? value )
{
2022-06-04 11:53:32 -07:00
if ( ! ReadyToAppend ( ) | | TryAppendClilocByNumericString ( value ) )
2022-05-10 00:37:07 -07:00
{
return ;
}
if ( value ? . TryCopyTo ( _chars [ _pos . . ] ) = = true )
{
_pos + = value . Length ;
}
else
{
AppendFormattedSlow ( value ) ;
}
}
public void AppendFormatted ( string? value , int alignment , string? format = null ) = >
AppendFormatted < string? > ( value , alignment , format ) ;
2022-06-04 11:53:32 -07:00
public bool TryAppendCliloc ( int number , string? format )
2022-05-10 00:37:07 -07:00
{
2022-06-04 11:53:32 -07:00
if ( format ! = "#" | | ! Localization . TryGetLocalization ( _lang , number , out var entry ) )
2022-05-10 00:37:07 -07:00
{
return false ;
}
var text = entry . Text ;
if ( text . TryCopyTo ( _chars [ _pos . . ] ) )
{
_pos + = text . Length ;
}
else
{
AppendFormattedSlow ( text ) ;
}
return true ;
}
2022-06-04 11:53:32 -07:00
private bool TryAppendClilocByNumericString ( ReadOnlySpan < char > value ) = >
value [ 0 ] = = '#' & & long . TryParse ( value [ 1. . ] , out var number ) & & TryAppendCliloc ( ( int ) number , "#" ) ;
2022-05-10 00:37:07 -07:00
private void AppendOrInsertAlignmentIfNeeded ( int startingPos , int alignment )
{
var charsWritten = _pos - startingPos ;
var leftAlign = false ;
if ( alignment < 0 )
{
leftAlign = true ;
alignment = - alignment ;
}
var paddingNeeded = alignment - charsWritten ;
if ( paddingNeeded > 0 )
{
EnsureCapacityForAdditionalChars ( paddingNeeded ) ;
if ( leftAlign )
{
_chars . Slice ( _pos , paddingNeeded ) . Fill ( ' ' ) ;
}
else
{
_chars . Slice ( startingPos , charsWritten ) . CopyTo ( _chars [ ( startingPos + paddingNeeded ) . . ] ) ;
_chars . Slice ( startingPos , paddingNeeded ) . Fill ( ' ' ) ;
}
_pos + = paddingNeeded ;
}
}
[MethodImpl(MethodImplOptions.NoInlining)]
private void AppendFormattedSlow ( string? value )
{
if ( value is not null )
{
EnsureCapacityForAdditionalChars ( value . Length ) ;
value . CopyTo ( _chars [ _pos . . ] ) ;
_pos + = value . Length ;
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void EnsureCapacityForAdditionalChars ( int additionalChars )
{
if ( _chars . Length - _pos < additionalChars )
{
Grow ( additionalChars ) ;
}
}
[MethodImpl(MethodImplOptions.NoInlining)]
private void GrowThenCopyString ( string value )
{
Grow ( value . Length ) ;
value . CopyTo ( _chars [ _pos . . ] ) ;
_pos + = value . Length ;
}
[MethodImpl(MethodImplOptions.NoInlining)]
private void GrowThenCopySpan ( ReadOnlySpan < char > value )
{
Grow ( value . Length ) ;
value . CopyTo ( _chars [ _pos . . ] ) ;
_pos + = value . Length ;
}
[MethodImpl(MethodImplOptions.NoInlining)] // keep consumers as streamlined as possible
private void Grow ( int additionalChars )
{
GrowCore ( ( uint ) _pos + ( uint ) additionalChars ) ;
}
[MethodImpl(MethodImplOptions.NoInlining)] // keep consumers as streamlined as possible
private void Grow ( )
{
GrowCore ( ( uint ) _chars . Length + 1 ) ;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void GrowCore ( uint requiredMinCapacity )
{
2022-05-10 18:50:23 -07:00
var newCapacity = Math . Max ( requiredMinCapacity , Math . Min ( ( uint ) _chars . Length * 2 , 0x3FFFFFDF ) ) ;
2022-05-10 00:37:07 -07:00
var arraySize = ( int ) Math . Clamp ( newCapacity , 256 , int . MaxValue ) ;
2022-05-10 18:50:23 -07:00
var newArray = STArrayPool < char > . Shared . Rent ( arraySize ) ;
2022-05-10 00:37:07 -07:00
_chars [ . . _pos ] . CopyTo ( newArray ) ;
var toReturn = _arrayToReturnToPool ;
_chars = _arrayToReturnToPool = newArray ;
if ( toReturn is not null )
{
2022-05-10 18:50:23 -07:00
STArrayPool < char > . Shared . Return ( toReturn ) ;
2022-05-10 00:37:07 -07:00
}
}
internal ReadOnlySpan < char > Text = > _chars [ . . _pos ] ;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal void Clear ( )
{
var toReturn = _arrayToReturnToPool ;
this = default ; // defensive clear
if ( toReturn is not null )
{
2022-05-10 18:50:23 -07:00
STArrayPool < char > . Shared . Return ( toReturn ) ;
2022-05-10 00:37:07 -07:00
}
}
public string ToStringAndClear ( )
{
if ( MoveNext ( ) & & _current ! = null )
{
AppendStringDirect ( _current ) ;
}
var result = new string ( Text ) ;
Clear ( ) ;
return result ;
}
public char [ ] ToPooledArray ( out int length )
{
if ( MoveNext ( ) & & _current ! = null )
{
AppendStringDirect ( _current ) ;
}
length = _pos ;
return _arrayToReturnToPool ;
}
}
feat: Adds cliloc support and fixes valuestringbuilder ctor (#1013)
- [X] Adds cliloc support using the following API:
```cs
public static class Localization
{
string GetText(int number);
string GetText(int number, string lang);
string Format(int number, params object[] args);
string Format(int number, string lang, params object[] args);
string Format(int number, $"{arg1}{arg2}{arg3}");
string Format(int number, lang, $"{arg1}{arg2}{arg3}");
bool TryGetLocalization(int number, out LocalizationEntry entry);
bool TryGetLocalization(int number, string lang, out LocalizationEntry entry);
}
public class LocalizationEntry
{
string Language { get; }
string Number { get; }
string Text { get; }
string?[] TextSlices { get; } // Used for string building
string StringFormatter { get; }
string Format(params object[] args);
string Format($"{arg1}{arg2}{arg3}");
}
```
- [X] Fixes a bug with ValueStringBuilder and default initialization size
- [X] Optimizes ValueStringBuilder to use `ISpanFormattable`
- [X] Adds `Append<T>(T value);` support ValueStringBuilder
- [X] Adds `Append($"");` string interpolation support to ValueStringBuider
2022-05-08 21:24:32 -07:00
}