Skip to content

Commit

Permalink
fix: escape special char when printing bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
qazxcdswe123 committed Oct 21, 2024
1 parent 3686955 commit 4f6bd42
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 24 deletions.
8 changes: 4 additions & 4 deletions builtin/bigint_wbtest.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -709,7 +709,7 @@ test "to_octets" {
inspect!(
a.to_octets(),
content=
#|b"\x01\x23\x45\x67\x89\x01\x23\x45\x67\x89"
#|b"\x01#Eg\x89\x01#Eg\x89"
,
)

Expand All @@ -729,23 +729,23 @@ test "to_octets" {
inspect!(
a.to_octets(),
content=
#|b"\x11\xe3\x44\x4d\xc1\xf3\x5f\x05\x7a\xd2\xcb\xc2\x79\x17\x37\x46\x81\x40\xa4\x26\xfa\xc3\xcb\xa7\xaf\x8c\x92\xa8\xf3\x4e"
#|b"\x11\xe3DM\xc1\xf3_\x05z\xd2\xcb\xc2y\x177F\x81@\xa4&\xfa\xc3\xcb\xa7\xaf\x8c\x92\xa8\xf3N"
,
)
let a = 0x123456789012345678123456789012345678123456789012345678123456789012345678N
check_len!(a)
inspect!(
a.to_octets(),
content=
#|b"\x12\x34\x56\x78\x90\x12\x34\x56\x78\x12\x34\x56\x78\x90\x12\x34\x56\x78\x12\x34\x56\x78\x90\x12\x34\x56\x78\x12\x34\x56\x78\x90\x12\x34\x56\x78"
#|b"\x124Vx\x90\x124Vx\x124Vx\x90\x124Vx\x124Vx\x90\x124Vx\x124Vx\x90\x124Vx"
,
)
let a = 0x805146F2F58580962A0A2E6134BC75E25AD97AE3D09CD34BA4F629AB8911F3F5CB8573A62EDD16B0D46775A415F545A75518DA3439914D9CAA26449067D85E704E8FCF9B29182485B41F952616BACDFDDF52B413B524D0EB743E8264A9C6AE32D12C3D20C5B81189060F4AC5D216713D503A69644EA8E4EA220A720C41F6B3D18BED65B4238318E6B0A41D8540D756865EC92DF40E8D365A230F17DF1D0A440BC6A557CD46D00B10D74C0E75500B2ADB3A0336223F9285B78CD04F485E417E1DB562B9EFCF79433209E6D6E2F43A484E471DE4F1F5AE38E08E7DAEB644C2C0A22697DD6D29BE0B40FF50DB575FEF02FA5525953C7C198B4A3600BA8CE1F917852A4B957151189F09DCDFCB79963E7D850127858A97855B94870ACCBE8203E73FE79791EE6EA1B1282A0CEAC54D6F6B7CD6C7B8D8092E949FF0A84N
check_len!(a)
inspect!(
a.to_octets(),
content=
#|b"\x08\x05\x14\x6f\x2f\x58\x58\x09\x62\xa0\xa2\xe6\x13\x4b\xc7\x5e\x25\xad\x97\xae\x3d\x09\xcd\x34\xba\x4f\x62\x9a\xb8\x91\x1f\x3f\x5c\xb8\x57\x3a\x62\xed\xd1\x6b\x0d\x46\x77\x5a\x41\x5f\x54\x5a\x75\x51\x8d\xa3\x43\x99\x14\xd9\xca\xa2\x64\x49\x06\x7d\x85\xe7\x04\xe8\xfc\xf9\xb2\x91\x82\x48\x5b\x41\xf9\x52\x61\x6b\xac\xdf\xdd\xf5\x2b\x41\x3b\x52\x4d\x0e\xb7\x43\xe8\x26\x4a\x9c\x6a\xe3\x2d\x12\xc3\xd2\x0c\x5b\x81\x18\x90\x60\xf4\xac\x5d\x21\x67\x13\xd5\x03\xa6\x96\x44\xea\x8e\x4e\xa2\x20\xa7\x20\xc4\x1f\x6b\x3d\x18\xbe\xd6\x5b\x42\x38\x31\x8e\x6b\x0a\x41\xd8\x54\x0d\x75\x68\x65\xec\x92\xdf\x40\xe8\xd3\x65\xa2\x30\xf1\x7d\xf1\xd0\xa4\x40\xbc\x6a\x55\x7c\xd4\x6d\x00\xb1\x0d\x74\xc0\xe7\x55\x00\xb2\xad\xb3\xa0\x33\x62\x23\xf9\x28\x5b\x78\xcd\x04\xf4\x85\xe4\x17\xe1\xdb\x56\x2b\x9e\xfc\xf7\x94\x33\x20\x9e\x6d\x6e\x2f\x43\xa4\x84\xe4\x71\xde\x4f\x1f\x5a\xe3\x8e\x08\xe7\xda\xeb\x64\x4c\x2c\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9xU\xb9Hp\xac\xcb\xe8 >s\xfeyy\x1e\xe6\xea\x1b\x12\x82\xa0\xce\xacT\xd6\xf6\xb7\xcdl{\x8d\x80\x92\xe9I\xff\n\x84"
,
)
}
Expand Down
43 changes: 36 additions & 7 deletions builtin/bytes.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,43 @@ pub fn to_string(self : Bytes) -> String {
}
}

fn printable(i : Int) -> Bool {
i >= 0x20 && i < 0x7F
}

sb.write_string("b\"")
for b in self {
let byte = b.to_int()
sb
..write_string("\\x")
..write_char(to_hex_digit(byte / 16))
..write_char(to_hex_digit(byte % 16))
|> ignore
for byte in self {
let ch = byte.to_int()
// escape special characters
if ch == '\\'.to_int() {
sb.write_string("\\\\")
continue
} else if ch == '\"'.to_int() {
sb.write_string("\\\"")
continue
} else if ch == '\n'.to_int() {
sb.write_string("\\n")
continue
} else if ch == '\t'.to_int() {
sb.write_string("\\t")
continue
} else if ch == '\r'.to_int() {
sb.write_string("\\r")
continue
} else if ch == '\b'.to_int() {
sb.write_string("\\b")
continue
}
// ASCII printable characters
if printable(ch) {
sb.write_char(Char::from_int(ch))
} else {
sb
..write_string("\\x")
..write_char(to_hex_digit(ch / 16))
..write_char(to_hex_digit(ch % 16))
|> ignore
}
}
sb.write_string("\"")
sb.to_string()
Expand Down
2 changes: 1 addition & 1 deletion builtin/bytes_test.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ test "to_string" {
inspect!(
bytes.to_string(),
content=
#|b"\x48\x00\x65\x00\x6c\x00\x6c\x00\x6f\x00\x2c\x00\x20\x00\x57\x00\x6f\x00\x72\x00\x6c\x00\x64\x00\x21\x00"
#|b"H\x00e\x00l\x00l\x00o\x00,\x00 \x00W\x00o\x00r\x00l\x00d\x00!\x00"
,
)
}
Expand Down
8 changes: 4 additions & 4 deletions bytes/bytes_test.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ test "bytes literal" {
inspect!(
b"ABC",
content=
#|b"\x41\x42\x43"
#|b"ABC"
,
)
inspect!(
b"\x41\x42\x43",
content=
#|b"\x41\x42\x43"
#|b"ABC"
,
)
}
Expand All @@ -32,7 +32,7 @@ test "from_array" {
inspect!(
b,
content=
#|b"\x41\x00\x42\x00\x43\x00"
#|b"A\x00B\x00C\x00"
,
)
}
Expand All @@ -42,7 +42,7 @@ test "from_array literal" {
inspect!(
b,
content=
#|b"\x41\x00\x42\x00\x43\x00"
#|b"A\x00B\x00C\x00"
,
)
}
Expand Down
34 changes: 26 additions & 8 deletions string/string_test.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -41,27 +41,45 @@ test "compare" {

test "to_bytes" {
inspect!(
"中文".to_bytes().to_string(),
"中文".to_bytes(),
content=
#|b"\x2d\x4e\x87\x65"
#|b"-N\x87e"
,
)
inspect!(
"asdf".to_bytes().to_string(),
"\n\t\"\\\r\b".to_bytes(),
content=
#|b"\x61\x00\x73\x00\x64\x00\x66\x00"
#|b"\n\x00\t\x00\"\x00\\\x00\r\x00\b\x00"
,
)
inspect!(
"\n\t\"\\\r\b".to_array(),
content=
#|['\n', '\t', '"', '\\', '\r', '\b']
,
)
inspect!(
"\"Hello\"\nNew line.\t".to_bytes(),
content=
#|b"\"\x00H\x00e\x00l\x00l\x00o\x00\"\x00\n\x00N\x00e\x00w\x00 \x00l\x00i\x00n\x00e\x00.\x00\t\x00"
,
)
inspect!(
"asdf".to_bytes(),
content=
#|b"a\x00s\x00d\x00f\x00"
,
)
inspect!(
"🤣🤣".to_bytes().to_string(),
"🤣🤣".to_bytes(),
content=
#|b"\x3e\xd8\x23\xdd\x3e\xd8\x23\xdd"
#|b">\xd8#\xdd>\xd8#\xdd"
,
)
inspect!(
"🤔".to_bytes().to_string(),
"🤔".to_bytes(),
content=
#|b"\x3e\xd8\x14\xdd"
#|b">\xd8\x14\xdd"
,
)
}
Expand Down

0 comments on commit 4f6bd42

Please sign in to comment.