Skip to content

Commit ecc7acc

Browse files
committed
tests: test with valgrind rune.bytes(), fix leak in .str_escaped()
1 parent 8acd84d commit ecc7acc

File tree

2 files changed

+39
-11
lines changed

2 files changed

+39
-11
lines changed

vlib/builtin/int.v

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -471,19 +471,45 @@ pub fn (b byte) ascii_str() string {
471471

472472
// str_escaped returns the contents of `byte` as an escaped `string`.
473473
// Example: assert byte(0).str_escaped() == r'`\0`'
474+
[manualfree]
474475
pub fn (b byte) str_escaped() string {
475476
str := match b {
476-
0 { r'`\0`' }
477-
7 { r'`\a`' }
478-
8 { r'`\b`' }
479-
9 { r'`\t`' }
480-
10 { r'`\n`' }
481-
11 { r'`\v`' }
482-
12 { r'`\f`' }
483-
13 { r'`\r`' }
484-
27 { r'`\e`' }
485-
32...126 { b.ascii_str() }
486-
else { '0x' + b.hex() }
477+
0 {
478+
r'`\0`'
479+
}
480+
7 {
481+
r'`\a`'
482+
}
483+
8 {
484+
r'`\b`'
485+
}
486+
9 {
487+
r'`\t`'
488+
}
489+
10 {
490+
r'`\n`'
491+
}
492+
11 {
493+
r'`\v`'
494+
}
495+
12 {
496+
r'`\f`'
497+
}
498+
13 {
499+
r'`\r`'
500+
}
501+
27 {
502+
r'`\e`'
503+
}
504+
32...126 {
505+
b.ascii_str()
506+
}
507+
else {
508+
xx := b.hex()
509+
yy := '0x' + xx
510+
unsafe { xx.free() }
511+
yy
512+
}
487513
}
488514
return str
489515
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
fn main() {
22
a := `Y`.str()
33
println(a)
4+
b := `★`.bytes()
5+
println(b)
46
}

0 commit comments

Comments
 (0)