gen: bytes array print not ending prematurely when null byte (#6401)

pull/6411/head
Henrixounez 2020-09-18 01:12:32 +02:00 committed by GitHub
parent 14743458e5
commit 4038ac463c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 0 deletions

View File

@ -383,6 +383,22 @@ pub fn (b byte) str() string {
return str
}
pub fn (b byte) str_escaped() string {
str := match b {
0 { '`\\' + '0`' } // Bug is preventing \\0 in a literal
7 { '`\\a`' }
8 { '`\\b`' }
9 { '`\\t`' }
10 { '`\\n`' }
11 { '`\\v`' }
12 { '`\\f`' }
13 { '`\\r`' }
32...126 { b.str() }
else { '0x' + b.hex() }
}
return str
}
// TODO generic
pub fn (a []byte) contains(val byte) bool {
for aa in a {

View File

@ -74,6 +74,9 @@ fn (mut g Gen) gen_str_for_array(info table.Array, styp, str_fn_name string) {
if sym_has_str_method {
elem_str_fn_name = if is_elem_ptr { field_styp.replace('*', '') + '_str' } else { field_styp +
'_str' }
if sym.kind == .byte {
elem_str_fn_name = elem_str_fn_name + '_escaped'
}
} else {
elem_str_fn_name = styp_to_str_fn_name(field_styp)
}

View File

@ -13,6 +13,9 @@ fn test_array_to_string_conversion() {
e := [i64(1), 2, 3]
assert e.str() == '[1, 2, 3]'
f := [byte(66), 32, 126, 10, 13, 5, 18, 127, 255]
assert f.str() == '[B, , ~, `\\n`, `\\r`, 0x05, 0x12, 0x7f, 0xff]'
}
fn test_interpolation_array_to_string() {