Skip to content

Commit

Permalink
builtin: add a rune.bytes() convenience method (#13129)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffmikels committed Jan 11, 2022
1 parent 791972e commit 078229f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
4 changes: 4 additions & 0 deletions vlib/builtin/rune.v
Expand Up @@ -54,6 +54,10 @@ pub fn (c rune) repeat(count int) string {
return res.repeat(count)
}

pub fn (c rune) bytes() []byte {
return c.str().bytes()
}

pub fn (c rune) length_in_bytes() int {
code := u32(c)
if code <= 0x7F {
Expand Down
5 changes: 5 additions & 0 deletions vlib/builtin/rune_test.v
Expand Up @@ -35,3 +35,8 @@ fn test_length_in_bytes() {
//
assert rune(0x110000).length_in_bytes() == -1
}

fn test_bytes() {
r1 := `★`
assert r1.bytes() == [byte(0xe2), 0x98, 0x85]
}

0 comments on commit 078229f

Please sign in to comment.