refactor: use <+ template writing in place of write_string sequences - #4195
refactor: use <+ template writing in place of write_string sequences#4195bobzhang wants to merge 2 commits into
<+ template writing in place of write_string sequences#4195Conversation
Replaces literal and sequence StringBuilder writes with the `<+` template
writer across Show implementations and streaming helpers:
- builtin: BytesView Show and ToJson now share one `escape_to` helper;
Char/StringView non-printable `\u{...}` writes become single templates;
Iter and Map Show stream through `<+`; a base64 `==` tail and a
FixedArray test builder simplify
- diff, hashmap, json, set, v128: Show impls write through `<+` templates
with inline-writer holes (`\{l => ...}`) preserving monomorphic
write_object dispatch; v128 hex formatting streams via a new `u64_hex_to`
Generated interfaces refreshed for the new pub `BytesView::escape_to`.
Co-Authored-By: SeekMoon <seekmoon@moonbitlang.com>
Co-Authored-By: SeekMoon <seekmoon@moonbitlang.com>
Coverage Report for CI Build 6523Coverage decreased (-0.02%) to 89.176%Details
Uncovered Changes
Coverage RegressionsNo coverage regressions found. Coverage Stats
💛 - Coveralls |
There was a problem hiding this comment.
🟡 Changes recommended
It introduces a new exported builtin API surface (BytesView::escape_to) and related interface drift that appears inconsistent with the stated intent for internal-only helpers, and should be resolved before approval.
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Pull request overview
Refactors a set of Show/streaming helpers to use the <+ template writer (and inline-writer holes where needed) instead of sequences of write_string/write_char calls, aiming to keep output stable while simplifying streaming code.
Changes:
- Introduces/uses small streaming helpers (e.g.,
u64_hex_to,BytesView::escape_to) to write directly into aLogger. - Converts several
Showimplementations (and a few related helpers/tests) to emit punctuation and structured output via<+templates. - Simplifies a few formatting/escaping sequences by collapsing them into single templates.
File summaries
| File | Description |
|---|---|
| v128/simd_basic.mbt | Adds u64_hex_to and updates V128 Show to stream via <+ with inline-writer holes. |
| set/linked_hash_set.mbt | Switches Set Show punctuation writes to <+. |
| json/types.mbt | Collapses ParseError/deprecated Json Show output into <+ templates. |
| json/json_path.mbt | Streams JSON Pointer token/path rendering using <+ templates. |
| hashmap/utils.mbt | Refactors HashMap Show output to a single <+ template per entry. |
| diff/hunk.mbt | Streams Range and HunkHeader Show output via <+ (with some interpolation changes). |
| builtin/show.mbt | Collapses non-printable \u{...} escaping writes into a single template. |
| builtin/pkg.generated.mbti | Updates generated builtin interface (currently includes BytesView::escape_to). |
| builtin/linked_hash_map.mbt | Switches Map Show punctuation/object writes to <+ templates. |
| builtin/iterator.mbt | Refactors Iter Show output to stream via <+. |
| builtin/fixedarray.mbt | Simplifies an iterator test’s output builder using <+ interpolation. |
| builtin/console.mbt | Collapses base64 padding writes into a single literal <+ "==". |
| builtin/char.mbt | Collapses non-printable \u{...} escaping writes into a single template. |
| builtin/bytesview.mbt | Introduces BytesView::escape_to helper and rewrites BytesView Show/ToJson to share it. |
Review details
- Files reviewed: 14/14 changed files
- Comments generated: 3
- Review effort level: Lite
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| /// Writes the bytes with printable ASCII kept as-is and everything else | ||
| /// rendered as `\xHH`, without the surrounding `b"`/`"`. Shared by `Show`, | ||
| /// which adds the quotes, and `ToJson`, which does not. | ||
| pub fn BytesView::escape_to(self : BytesView, logger : &Logger) -> Unit { | ||
| for byte in self { |
| pub fn BytesView::equal(Self, Self) -> Bool | ||
| pub fn BytesView::equal_to_bytes(Self, Bytes) -> Bool | ||
| pub fn BytesView::escape_to(Self, &Logger) -> Unit | ||
| pub fn BytesView::find(Self, Self) -> Int? |
| impl Show for HunkHeader with fn output(self, logger) { | ||
| logger.write_string("@@ -\{self.0} +\{self.1} @@") | ||
| logger <+ "@@ -\{self.0} +\{self.1} @@" | ||
| } |
|
Splitting this into reviewable, per-area PRs as requested:
Each PR is independently verified and CI will run per PR. Closing this |
Replaces literal and sequence
StringBuilder/&Loggerwrites with the<+template writer across
Showimplementations and streaming helpers, part ofthe ongoing project-wide simplification from
write_stringsequences to<+template writing.Changes:
BytesViewShowandToJsonnow share oneBytesView::escape_tostreaming helper;Char/StringViewwrites ofnon-printable
\u{...}escapes collapse into a single template;Iterand
MapShowstream through<+; a base64==tail and aFixedArrayiterator-test builder simplify.Range/HunkHeaderShowstream via inline-writer holes.HashMap/SetShowcollapse punctuation writesinto
<+templates.ParseErrorand the deprecatedJsonShowimpls collapsewrite sequences into templates.
one template per step instead of interleaved
write_*calls.u64_hex_tohelper used by theShowtemplate.\{l => ...}inline-writer holes preserve monomorphicwrite_objectdispatch, so output is unchanged throughout (full test suite: 7499/7499).
This re-applies the still-relevant content of the closed
#4110 (simplify-template-writing) on top of current
main; the JSONstringify/
escape_topart of that PR was superseded by the mergedSIMD
need_escape/write_escapedwork, so it is intentionally notre-applied.
Generated with SeekMoon