diff --git a/builtin/builtin.mbti b/builtin/builtin.mbti index 0dc8717d4..850d7c2b3 100644 --- a/builtin/builtin.mbti +++ b/builtin/builtin.mbti @@ -195,6 +195,8 @@ impl BytesView { length(Self) -> Int op_as_view(Self, start~ : Int = .., end? : Int) -> Self op_get(Self, Int) -> Byte + to_char_be(Self) -> Char + to_char_le(Self) -> Char to_double_be(Self) -> Double to_double_le(Self) -> Double to_float_be(Self) -> Float diff --git a/builtin/bytesview.mbt b/builtin/bytesview.mbt index 3498558f4..538b4b241 100644 --- a/builtin/bytesview.mbt +++ b/builtin/bytesview.mbt @@ -213,3 +213,13 @@ pub fn BytesView::to_double_be(self : BytesView) -> Double { pub fn BytesView::to_double_le(self : BytesView) -> Double { self.to_uint64_le().reinterpret_as_double() } + +///| Converts the 4 bytes long BytesView to a Char in big-endian byte order. +pub fn BytesView::to_char_be(self : BytesView) -> Char { + Char::from_int(self.to_int_be()) +} + +///| Converts the 4 bytes long BytesView to a Char in little-endian byte order. +pub fn BytesView::to_char_le(self : BytesView) -> Char { + Char::from_int(self.to_int_le()) +} diff --git a/builtin/bytesview_test.mbt b/builtin/bytesview_test.mbt index b902a8779..97f094f89 100644 --- a/builtin/bytesview_test.mbt +++ b/builtin/bytesview_test.mbt @@ -290,3 +290,41 @@ test "panic negative index2" { let _ = bs[-1:-2] } + +test "to_char_be" { + // test ascii + let code_point = 65 + let bs = code_point.to_be_bytes() + let ch = bs[:].to_char_be() + assert_eq!('A', ch) + // test cjk chars + let ch = '清' + let i = ch.to_be_bytes()[:].to_int_be() + assert_eq!(0x6e05, i) + let ch = '浄' + let i = ch.to_be_bytes()[:].to_int_be() + assert_eq!(0x6d44, i) + // test arabic + let ch = 'ل' + let i = ch.to_be_bytes()[:].to_int_be() + assert_eq!(0x0644, i) +} + +test "to_char_le" { + // test ascii + let code_point = 97 + let bs = code_point.to_le_bytes() + let ch = bs[:].to_char_le() + assert_eq!('a', ch) + // test cjk + let ch = '清' + let i = ch.to_le_bytes()[:].to_int_le() + assert_eq!(0x6e05, i) + let ch = '浄' + let i = ch.to_le_bytes()[:].to_int_le() + assert_eq!(0x6d44, i) + // test arabic + let ch = 'ل' + let i = ch.to_le_bytes()[:].to_int_le() + assert_eq!(0x0644, i) +} diff --git a/char/char.mbt b/char/char.mbt index edcc08155..8e198faee 100644 --- a/char/char.mbt +++ b/char/char.mbt @@ -19,3 +19,13 @@ pub impl Hash for Char with hash(self) { self.to_int() } pub impl Hash for Char with hash_combine(self, hasher) -> Unit { hasher.combine_char(self) } + +///| +pub fn to_be_bytes(self : Char) -> Bytes { + self.to_uint().to_be_bytes() +} + +///| +pub fn to_le_bytes(self : Char) -> Bytes { + self.to_uint().to_le_bytes() +} diff --git a/char/char.mbti b/char/char.mbti index d2221301d..afcfc77d7 100644 --- a/char/char.mbti +++ b/char/char.mbti @@ -3,6 +3,10 @@ package moonbitlang/core/char // Values // Types and methods +impl Char { + to_be_bytes(Char) -> Bytes + to_le_bytes(Char) -> Bytes +} impl Hash for Char // Type aliases diff --git a/char/moon.pkg.json b/char/moon.pkg.json index 56cd56a09..2f6c209f3 100644 --- a/char/moon.pkg.json +++ b/char/moon.pkg.json @@ -1,3 +1,3 @@ { - "import": ["moonbitlang/core/builtin"] + "import": ["moonbitlang/core/builtin", "moonbitlang/core/uint"] }