Skip to content

Copy only formatted data into right-aligned buffer output - #152

Open
dajiaohuang wants to merge 1 commit into
Cysharp:masterfrom
dajiaohuang:fix/zstring-format-written-span-20261015
Open

dajiaohuang wants to merge 1 commit into
Cysharp:masterfrom
dajiaohuang:fix/zstring-format-written-span-20261015

Conversation

@dajiaohuang

Copy link
Copy Markdown

Problem

Right-aligned numeric formatting copies the entire scratch span rather than the formatted portion. A valid small buffer writer therefore throws even though it provides the requested capacity:

var writer = new ArrayBufferWriter<byte>(1);
ZString.Utf8Format(writer, "{0,2}", 1);
// Expected UTF8 output: " 1"; actual: ArgumentException

The same root affects UTF16/UTF8 prepared FormatTo, and UTF8 builder AppendFormat when close to its buffer capacity. IBufferWriter.GetSpan(charsWritten) does not promise enough space for the unused remainder of the scratch buffer.

Change

Copy only s.Slice(0, charsWritten). Update the canonical FormatHelper.tt, regenerate FormatHelper.cs using dotnet-t4 3.0.0, and apply the same correction to the non-generated UTF8 builder path. Unity copies are produced by the existing netstandard2.1 post-build target. The generator also emits the generated helper without its previous BOM.

No alignment-width, Unicode-counting, numeric-format, or formatter-contract changes are included; this is separate from #128 / #146.

Validation

  • All 7 added cases fail at master b9c1d8a and pass with this change.
  • Full maintained test project: 154 passed.
  • Release solution build passes (existing warnings remain).
  • Regeneration of the committed canonical template produces the committed helper unchanged.
  • Tests use DOTNET_ROLL_FORWARD=Major because .NET 7 runtime is unavailable locally. No Unity editor/TMP/IL2CPP execution or performance claims.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant