Skip to content

Commit a02edf5

Browse files
committed
feat(bytes): implement ToJson for BytesView
1 parent 59f6cfe commit a02edf5

File tree

4 files changed

+18
-32
lines changed

4 files changed

+18
-32
lines changed

builtin/json.mbt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,7 @@ pub impl ToJson for Bytes with to_json(self : Bytes) -> Json {
189189
sb.write_char(b.to_char())
190190
} else {
191191
sb.write_string("\\x")
192-
sb.write_char(to_hex_digit(b.to_int() / 16))
193-
sb.write_char(to_hex_digit(b.to_int() % 16))
192+
sb.write_string(b.to_int().to_string(radix=16))
194193
}
195194
}
196195
Json::string(sb.to_string())

builtin/show.mbt

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -61,38 +61,11 @@ pub impl Show for UInt16 with output(self, logger) {
6161
self.to_int().output(logger)
6262
}
6363

64-
///|
65-
fn to_hex_digit(i : Int) -> Char {
66-
if i < 10 {
67-
(i + '0').unsafe_to_char()
68-
} else {
69-
(i + 'a' - 10).unsafe_to_char()
70-
}
71-
}
72-
73-
///|
74-
test "to_hex_digit" {
75-
for i in 0..<10 {
76-
guard to_hex_digit(i) == ('0'.to_int() + i).unsafe_to_char() else {
77-
fail("to_hex_digit(\{i}) does not match")
78-
}
79-
}
80-
for i in 10..<16 {
81-
guard to_hex_digit(i) == ('a'.to_int() + (i - 10)).unsafe_to_char() else {
82-
fail("to_hex_digit(\{i}) does not match")
83-
}
84-
}
85-
}
86-
8764
///|
8865
pub impl Show for Bytes with output(self, logger) {
8966
logger.write_string("b\"")
9067
for b in self {
91-
let byte = b.to_int()
92-
logger
93-
..write_string("\\x")
94-
..write_char(to_hex_digit(byte / 16))
95-
..write_char(to_hex_digit(byte % 16))
68+
logger..write_string("\\x")..write_string(b.to_int().to_string(radix=16))
9669
}
9770
logger.write_string("\"")
9871
}
@@ -151,8 +124,7 @@ pub impl Show for String with output(self, logger) {
151124
flush_segment(seg, i)
152125
logger
153126
..write_string("\\u{")
154-
..write_char(to_hex_digit(code / 16))
155-
..write_char(to_hex_digit(code % 16))
127+
..write_string(code.to_string(radix=16))
156128
..write_char('}')
157129
continue i + 1, i + 1
158130
} else {

bytes/pkg.generated.mbti

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ impl Compare for BytesView
7171
impl Eq for BytesView
7272
impl Hash for BytesView
7373
impl Show for BytesView
74+
impl ToJson for BytesView
7475

7576
// Type aliases
7677
pub typealias BytesView as View

bytes/view.mbt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -653,3 +653,17 @@ pub impl Hash for BytesView with hash_combine(self : BytesView, hasher : Hasher)
653653
pub impl Hash for BytesView with hash(self : BytesView) -> Int {
654654
xxhash32(self.data(), 0, offset=self.start_offset(), len=self.length())
655655
}
656+
657+
///|
658+
pub impl ToJson for BytesView with to_json(self) -> Json {
659+
let sb = StringBuilder::new()
660+
for b in self {
661+
if b is (b' '..=b'~') && b != b'"' && b != b'\\' {
662+
sb.write_char(b.to_char())
663+
} else {
664+
sb.write_string("\\x")
665+
sb.write_string(b.to_int().to_string(radix=16))
666+
}
667+
}
668+
Json::string(sb.to_string())
669+
}

0 commit comments

Comments
 (0)