Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add conversion between Char and Bytes/BytesView. #1495

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions builtin/builtin.mbti
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions builtin/bytesview.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
38 changes: 38 additions & 0 deletions builtin/bytesview_test.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
10 changes: 10 additions & 0 deletions char/char.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
4 changes: 4 additions & 0 deletions char/char.mbti
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion char/moon.pkg.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"import": ["moonbitlang/core/builtin"]
"import": ["moonbitlang/core/builtin", "moonbitlang/core/uint"]
}
Loading