Deepen escape strategies and pooled builder parity#33
Open
CorentinGS wants to merge 11 commits into
Open
Conversation
c38e202 to
0d01293
Compare
03d0319 to
d6b00e3
Compare
There was a problem hiding this comment.
Pull request overview
This PR refactors the escaping/encoding implementation to use reusable span-to-span “escape strategy” modules (JSON/HTML/CSV/URL/form-url), adds equivalent escaping APIs for ZaPooledStringBuilder, and updates projects to target .NET 10.
Changes:
- Introduces
ZaString.Escaping.*EscapeStrategyhelpers and updatesZaSpanStringBuilderescaping extensions to use them. - Adds pooled-builder escaping methods and adds parity + zero-allocation guardrail tests for all escaping kinds.
- Retargets library/tests/benchmarks/demo projects to
net10.0.
Reviewed changes
Copilot reviewed 18 out of 18 changed files in this pull request and generated 10 comments.
Show a summary per file
| File | Description |
|---|---|
| tests/ZaString.Tests/ZaString.Tests.csproj | Retargets test project to net10.0 only. |
| tests/ZaString.Tests/ZaPooledStringBuilderTests.cs | Adds pooled builder escaping parity + growth tests. |
| tests/ZaString.Tests/UrlEscapeStrategyTests.cs | Adds direct tests for URL escape strategy behavior. |
| tests/ZaString.Tests/JsonEscapeStrategyTests.cs | Adds direct tests for JSON escape strategy behavior. |
| tests/ZaString.Tests/HtmlEscapeStrategyTests.cs | Adds direct tests for HTML escape strategy behavior. |
| tests/ZaString.Tests/FormUrlEscapeStrategyTests.cs | Adds direct tests for form-url strategy behavior (incl. surrogate handling). |
| tests/ZaString.Tests/EscapeStrategyParityTests.cs | Adds parity + allocation guardrail tests across builders/strategies. |
| tests/ZaString.Tests/CsvEscapeStrategyTests.cs | Adds direct tests for CSV escape strategy behavior. |
| tests/ZaString.Benchmarks/ZaString.Benchmarks.csproj | Retargets benchmarks to net10.0. |
| src/ZaString/ZaString.csproj | Retargets library to net10.0 and updates package description. |
| src/ZaString/Extensions/ZaSpanStringBuilderExtensions.cs | Replaces inline escaping logic with escape strategies. |
| src/ZaString/Escaping/UrlEscapeStrategy.cs | Adds URL percent-encoding implementation. |
| src/ZaString/Escaping/JsonEscapeStrategy.cs | Adds JSON escaping implementation. |
| src/ZaString/Escaping/HtmlEscapeStrategy.cs | Adds HTML escaping implementation. |
| src/ZaString/Escaping/FormUrlEscapeStrategy.cs | Adds form-url encoding implementation. |
| src/ZaString/Escaping/CsvEscapeStrategy.cs | Adds CSV field escaping implementation. |
| src/ZaString/Core/ZaPooledStringBuilder.cs | Adds append-span/advance support and pooled escaping APIs. |
| samples/ZaString.Demo/ZaString.Demo.csproj | Retargets demo app to net10.0. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
665
to
669
| public static bool TryAppendJsonEscaped(ref this ZaSpanStringBuilder builder, ReadOnlySpan<char> value) | ||
| { | ||
| var needsEscape = value.IndexOfAny("\"\\\b\f\n\r\t".AsSpan()) >= 0; | ||
| if (!needsEscape) | ||
| { | ||
| for (int i = 0; i < value.Length; i++) | ||
| { | ||
| if (value[i] < ' ' || value[i] is '\u2028' or '\u2029') | ||
| { | ||
| needsEscape = true; | ||
| break; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if (!needsEscape) | ||
| { | ||
| return builder.TryAppend(value); | ||
| } | ||
|
|
||
| var required = GetJsonEscapedLength(value); | ||
| var required = JsonEscapeStrategy.GetEscapedLength(value); | ||
| if (required > builder.RemainingSpan.Length) | ||
| { |
Comment on lines
701
to
705
| public static bool TryAppendHtmlEscaped(ref this ZaSpanStringBuilder builder, ReadOnlySpan<char> value) | ||
| { | ||
| if (value.IndexOfAny("&<>\"'".AsSpan()) < 0) | ||
| { | ||
| return builder.TryAppend(value); | ||
| } | ||
|
|
||
| var required = GetHtmlEscapedLength(value); | ||
| var required = HtmlEscapeStrategy.GetEscapedLength(value); | ||
| if (required > builder.RemainingSpan.Length) | ||
| { |
Comment on lines
724
to
728
| public static bool TryAppendCsvEscaped(ref this ZaSpanStringBuilder builder, ReadOnlySpan<char> value) | ||
| { | ||
| var needsQuote = NeedsCsvQuoting(value); | ||
| if (!needsQuote) | ||
| { | ||
| return builder.TryAppend(value); | ||
| } | ||
|
|
||
| var quoteCount = 0; | ||
| foreach (var t in value) | ||
| if (t == '"') | ||
| quoteCount++; | ||
|
|
||
| var required = value.Length + quoteCount + 2; | ||
| var required = CsvEscapeStrategy.GetEscapedLength(value); | ||
| if (required > builder.RemainingSpan.Length) | ||
| { |
Comment on lines
761
to
765
| public static bool TryAppendUrlEncoded(ref this ZaSpanStringBuilder builder, ReadOnlySpan<char> value) | ||
| { | ||
| var needsEncoding = false; | ||
| for (int i = 0; i < value.Length; i++) | ||
| { | ||
| if (!IsUnreservedAscii(value[i])) | ||
| { | ||
| needsEncoding = true; | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| if (!needsEncoding) | ||
| { | ||
| return builder.TryAppend(value); | ||
| } | ||
|
|
||
| var required = GetUrlEncodedLength(value); | ||
| var required = UrlEscapeStrategy.GetEscapedLength(value); | ||
| if (required > builder.RemainingSpan.Length) | ||
| { |
Comment on lines
787
to
791
| public static bool TryAppendFormUrlEncoded(ref this ZaSpanStringBuilder builder, ReadOnlySpan<char> value) | ||
| { | ||
| var needsEncoding = false; | ||
| for (int i = 0; i < value.Length; i++) | ||
| { | ||
| if (value[i] == ' ' || !IsUnreservedAscii(value[i])) | ||
| { | ||
| needsEncoding = true; | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| if (!needsEncoding) | ||
| { | ||
| return builder.TryAppend(value); | ||
| } | ||
|
|
||
| var required = GetFormUrlEncodedLengthReplacingInvalid(value); | ||
| var required = FormUrlEscapeStrategy.GetEscapedLength(value); | ||
| if (required > builder.RemainingSpan.Length) | ||
| { |
Comment on lines
+262
to
+269
| public ZaPooledStringBuilder AppendJsonEscaped(ReadOnlySpan<char> value) | ||
| { | ||
| var required = JsonEscapeStrategy.GetEscapedLength(value); | ||
| GetAppendSpan(required, out var destination); | ||
| JsonEscapeStrategy.TryEscape(value, destination, out var written); | ||
| Advance(written); | ||
| return this; | ||
| } |
Comment on lines
+271
to
+278
| public ZaPooledStringBuilder AppendHtmlEscaped(ReadOnlySpan<char> value) | ||
| { | ||
| var required = HtmlEscapeStrategy.GetEscapedLength(value); | ||
| GetAppendSpan(required, out var destination); | ||
| HtmlEscapeStrategy.TryEscape(value, destination, out var written); | ||
| Advance(written); | ||
| return this; | ||
| } |
Comment on lines
+280
to
+287
| public ZaPooledStringBuilder AppendCsvEscaped(ReadOnlySpan<char> value) | ||
| { | ||
| var required = CsvEscapeStrategy.GetEscapedLength(value); | ||
| GetAppendSpan(required, out var destination); | ||
| CsvEscapeStrategy.TryEscape(value, destination, out var written); | ||
| Advance(written); | ||
| return this; | ||
| } |
Comment on lines
+289
to
+296
| public ZaPooledStringBuilder AppendUrlEncoded(ReadOnlySpan<char> value) | ||
| { | ||
| var required = UrlEscapeStrategy.GetEscapedLength(value); | ||
| GetAppendSpan(required, out var destination); | ||
| UrlEscapeStrategy.TryEscape(value, destination, out var written); | ||
| Advance(written); | ||
| return this; | ||
| } |
Comment on lines
+298
to
+305
| public ZaPooledStringBuilder AppendFormUrlEncoded(ReadOnlySpan<char> value) | ||
| { | ||
| var required = FormUrlEscapeStrategy.GetEscapedLength(value); | ||
| GetAppendSpan(required, out var destination); | ||
| FormUrlEscapeStrategy.TryEscape(value, destination, out var written); | ||
| Advance(written); | ||
| return this; | ||
| } |
0d01293 to
fb51237
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Verification
Notes