Cysharp/ZString

.Append(string) does not work when ZString is created as a readonly class member

teobugslayer opened this issue · 1 comments

I am experimenting with substituting StringBuilder with ZString. In my particular case, when I attempt to use .Append, it does not append the string (in the debugger, I can see that .Length stays 0). This is the minimal repro code:

class Program
{
    static void Main() => new Program().Test();

    void Test()
    {
        string sigil = "\u001B[38;5;178m@";

        // StringBuilder
        sb.Append(sigil);
        Console.WriteLine(sb.ToString()); // Prints '@' in yellow

        // ZString
        zs.Append(sigil);
        Console.WriteLine(zs.ToString()); // Does not print anything
    }

    private readonly Cysharp.Text.Utf16ValueStringBuilder zs = Cysharp.Text.ZString.CreateStringBuilder();
    private readonly System.Text.StringBuilder sb = new();
}

Hi, ZString's StringBuilder is a mutable struct. (for performance reason).
If you treat mutable struct as readonly, it will not be in the intended state. Builder should be used as not readonly. Thanks!