builtin: add a rune.bytes() convenience method (#13129)

pull/13135/head
jeffmikels 2022-01-11 02:55:51 -05:00 committed by GitHub
parent 791972ebc9
commit 078229f213
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 0 deletions

View File

@ -54,6 +54,10 @@ pub fn (c rune) repeat(count int) string {
return res.repeat(count) return res.repeat(count)
} }
pub fn (c rune) bytes() []byte {
return c.str().bytes()
}
pub fn (c rune) length_in_bytes() int { pub fn (c rune) length_in_bytes() int {
code := u32(c) code := u32(c)
if code <= 0x7F { if code <= 0x7F {

View File

@ -35,3 +35,8 @@ fn test_length_in_bytes() {
// //
assert rune(0x110000).length_in_bytes() == -1 assert rune(0x110000).length_in_bytes() == -1
} }
fn test_bytes() {
r1 := ``
assert r1.bytes() == [byte(0xe2), 0x98, 0x85]
}