Skip to content

Commit da04c8b

Browse files
refactor(builtin): dedupe BytesView escaping and template escape writes
- BytesView Show and ToJson now share one streaming `escape_to` helper (which prints printable ASCII as-is and renders the rest as `\xHH`), and Show renders through a `b"..."` `<+` template - Char/StringView non-printable `\u{...}` writes collapse into single `<+` templates whose hole delegates to a local writer Inline-writer holes use `\{cb => ...}` so delimiters stay paired and the template stays readable. Co-Authored-By: SeekMoon <seekmoon@moonbitlang.com>
1 parent a55f866 commit da04c8b

4 files changed

Lines changed: 13 additions & 17 deletions

File tree

builtin/bytesview.mbt

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -346,8 +346,10 @@ fn write_byte_hex(logger : &Logger, byte : Byte) -> Unit {
346346
}
347347

348348
///|
349-
pub impl Show for BytesView with fn output(self, logger) {
350-
logger.write_string("b\"")
349+
/// Writes the bytes with printable ASCII kept as-is and everything else
350+
/// rendered as `\xHH`, without the surrounding `b"`/`"`. Shared by `Show`,
351+
/// which adds the quotes, and `ToJson`, which does not.
352+
pub fn BytesView::escape_to(self : BytesView, logger : &Logger) -> Unit {
351353
for byte in self {
352354
if byte is (b' '..=b'~') && byte != b'"' && byte != b'\\' {
353355
logger.write_char(byte.to_char())
@@ -356,7 +358,11 @@ pub impl Show for BytesView with fn output(self, logger) {
356358
write_byte_hex(logger, byte)
357359
}
358360
}
359-
logger.write_string("\"")
361+
}
362+
363+
///|
364+
pub impl Show for BytesView with fn output(self, logger) {
365+
logger <+ "b\"\{cb => self.escape_to(cb)}\""
360366
}
361367

362368
///|
@@ -593,14 +599,7 @@ pub fn BytesView::to_owned(self : BytesView) -> Bytes {
593599
///|
594600
pub impl ToJson for BytesView with fn to_json(self) -> Json {
595601
let sb = StringBuilder()
596-
for byte in self {
597-
if byte is (b' '..=b'~') && byte != b'"' && byte != b'\\' {
598-
sb.write_char(byte.to_char())
599-
} else {
600-
sb.write_string("\\x")
601-
write_byte_hex(sb, byte)
602-
}
603-
}
602+
self.escape_to(sb)
604603
Json::string(sb.to_string())
605604
}
606605

builtin/char.mbt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -504,9 +504,7 @@ fn Char::escape_to(self : Char, logger : &Logger, quote? : Bool = true) -> Unit
504504
' '..='~' => logger.write_char(self)
505505
_ =>
506506
if !self.is_printable() {
507-
logger.write_string("\\u{")
508-
logger.write_string(self.to_hex())
509-
logger.write_char('}')
507+
logger <+ "\\u{\{cb => cb.write_string(self.to_hex())}}"
510508
} else {
511509
logger.write_char(self)
512510
}

builtin/pkg.generated.mbti

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1170,6 +1170,7 @@ pub fn BytesView::compare(Self, Self) -> Int
11701170
pub fn BytesView::data(Self) -> Bytes
11711171
pub fn BytesView::equal(Self, Self) -> Bool
11721172
pub fn BytesView::equal_to_bytes(Self, Bytes) -> Bool
1173+
pub fn BytesView::escape_to(Self, &Logger) -> Unit
11731174
pub fn BytesView::find(Self, Self) -> Int?
11741175
pub fn BytesView::get(Self, Int) -> Byte?
11751176
pub fn BytesView::get_view(Self, start? : Int, end? : Int) -> Self?

builtin/show.mbt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,9 +191,7 @@ fn StringView::escape_to(
191191
code =>
192192
if code < ' ' {
193193
flush_segment(seg, i)
194-
logger.write_string("\\u{")
195-
logger.write_string(code.to_byte().to_hex())
196-
logger.write_char('}')
194+
logger <+ "\\u{\{cb => cb.write_string(code.to_byte().to_hex())}}"
197195
continue i + 1, i + 1
198196
} else {
199197
continue i + 1, seg

0 commit comments

Comments
 (0)