From 078229f21332ac3b9f679790c7ada795cfa40d8c Mon Sep 17 00:00:00 2001 From: jeffmikels Date: Tue, 11 Jan 2022 02:55:51 -0500 Subject: [PATCH] builtin: add a rune.bytes() convenience method (#13129) --- vlib/builtin/rune.v | 4 ++++ vlib/builtin/rune_test.v | 5 +++++ 2 files changed, 9 insertions(+) diff --git a/vlib/builtin/rune.v b/vlib/builtin/rune.v index bae1423f0b52c6..2386231a4b8c3d 100644 --- a/vlib/builtin/rune.v +++ b/vlib/builtin/rune.v @@ -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 { diff --git a/vlib/builtin/rune_test.v b/vlib/builtin/rune_test.v index 271c01eb76e957..8a9098f8d57a71 100644 --- a/vlib/builtin/rune_test.v +++ b/vlib/builtin/rune_test.v @@ -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] +}