feat(sprintf): support full %q format specifications - #810
Open
Vitalii Tverdokhlib (vitaliytv) wants to merge 3 commits into
Open
feat(sprintf): support full %q format specifications#810Vitalii Tverdokhlib (vitaliytv) wants to merge 3 commits into
Vitalii Tverdokhlib (vitaliytv) wants to merge 3 commits into
Conversation
Go's `%q` verb (strconv.Quote) is used by OPA's sprintf, but regorus currently bails on it unconditionally. Add support for `Value::String` arguments only: wrap in double quotes, escape `"` and `\`, use the short escapes for the common control characters (\a \b \f \n \r \t \v), fall back to \xNN/\uNNNN/\UNNNNNNNN for other non-printable characters, and leave printable (including non-ASCII) characters untouched. Unlike json.marshal, %q does not HTML-escape < > &. %q on non-string values (numbers, bools, etc.) still bails as before - Go's %q on those produces different, single-quoted output that is out of scope for this change. Verified byte-for-byte against real OPA output (strconv.Quote semantics) for quotes, backslashes, control characters, DEL, non-ASCII printable text, and the < > & non-escaping case. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
8 tasks
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.
Depends on #809.
This follow-up completes
%qsupport with a dedicatedsprintfformat-spec parser instead of extending the original PR's single-verb parsing incrementally.What this adds
*operands, and explicit argument indexes such as%[3]*.[2]*[1]q.%+q(QuoteToASCIIbehavior),%#q(backquoted strings when valid), left/right alignment, zero padding, and combined width/precision.%qoperands using OPA-compatible value conversion for strings, booleans, null, collections, integral runes, invalid runes, fractional numbers, and out-of-range integers.qverbs. Newly parsed flags that those verbs do not support are rejected explicitly.Compatibility boundary
Regorus currently normalizes numbers into its internal
Numbervariants and does not retain the original Rego numeric token spelling. OPA's%qbehavior can depend on that spelling: for example, OPA distinguishes65.0,65e0, and65, while Regorus may normalize them to different shared numeric variants. Exact parity for those lexical-number edge cases requires a separate change to the number representation; this PR does not disguise or broaden that architectural change.The common
%qmatrix and parser edge cases were compared directly with OPA, including ASCII quoting, raw quoting and fallback, width/precision combinations, dynamic and indexed operands, runes, invalid runes, collections, and diagnostics.Validation
cargo test: 298 passed, 2 ignored.cargo test --locked --no-default-features --lib: 220 passed, 2 ignored.cargo xtask test-no-stdpassed.cargo clippy --locked --all-targets --all-features -- -D warningspassed.This is intentionally stacked on #809. Until #809 is merged, GitHub's diff against
mainincludes that prerequisite commit as well; afterward this PR reduces to the format-spec extension.