Skip to content

Commit

Permalink
Merge branch 'main' into String-concat-for-Arrayview
Browse files Browse the repository at this point in the history
  • Loading branch information
Lampese authored Nov 27, 2024
2 parents 6c4f1cd + b0415d2 commit 91f7355
Show file tree
Hide file tree
Showing 54 changed files with 233 additions and 302 deletions.
2 changes: 1 addition & 1 deletion buffer/buffer.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ pub fn write_sub_string(
/// Write a char into buffer.
pub fn write_char(self : T, value : Char) -> Unit {
self.grow_if_necessary(self.len + 4)
let inc = self.data.set_utf16_char(self.len, value)
let inc = self.data.set_utf16le_char(self.len, value)
self.len += inc
}

Expand Down
6 changes: 0 additions & 6 deletions builtin/array.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -1029,12 +1029,6 @@ pub fn split[T](self : Array[T], pred : (T) -> Bool) -> Array[Array[T]] {
chunks
}

///|
/// Converts the array to a string.
pub fn to_string[T : Show](self : Array[T]) -> String {
Show::to_string(self)
}

///|
pub fn iter[T](self : Array[T]) -> Iter[T] {
Iter::new(
Expand Down
26 changes: 4 additions & 22 deletions builtin/builtin.mbti
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ impl Array {
strip_suffix[T : Eq](Self[T], Self[T]) -> Self[T]?
swap[T](Self[T], Int, Int) -> Unit
to_json[X : ToJson](Self[X]) -> Json
to_string[T : Show](Self[T]) -> String
unsafe_blit[A](Self[A], Int, Self[A], Int, Int) -> Unit
unsafe_blit_fixed[A](Self[A], Int, FixedArray[A], Int, Int) -> Unit
unsafe_pop[T](Self[T]) -> T
Expand Down Expand Up @@ -244,7 +243,6 @@ impl Iter {
take_while[T](Self[T], (T) -> Bool) -> Self[T]
tap[T](Self[T], (T) -> Unit) -> Self[T] //deprecated
to_array[T](Self[T]) -> Array[T]
to_string[T : Show](Self[T]) -> String
}
impl[T : Show] Show for Iter[T]

Expand All @@ -256,7 +254,6 @@ impl Iter2 {
new[A, B](((A, B) -> IterResult) -> IterResult) -> Self[A, B]
run[A, B](Self[A, B], (A, B) -> IterResult) -> IterResult
to_array[A, B](Self[A, B]) -> Array[(A, B)]
to_string[A : Show, B : Show](Self[A, B]) -> String
}
impl[A : Show, B : Show] Show for Iter2[A, B]

Expand Down Expand Up @@ -311,7 +308,6 @@ impl Map {
size[K, V](Self[K, V]) -> Int
to_array[K, V](Self[K, V]) -> Array[(K, V)]
to_json[K : Show, V : ToJson](Self[K, V]) -> Json
to_string[K : Show, V : Show](Self[K, V]) -> String
values[K, V](Self[K, V]) -> Iter[V]
}
impl[K : Show, V : Show] Show for Map[K, V]
Expand Down Expand Up @@ -341,7 +337,6 @@ impl Set {
symmetric_difference[K : Hash + Eq](Self[K], Self[K]) -> Self[K]
to_array[K](Self[K]) -> Array[K]
to_json[X : ToJson](Self[X]) -> Json
to_string[K : Show](Self[K]) -> String
union[K : Hash + Eq](Self[K], Self[K]) -> Self[K]
}
impl[K : Show] Show for Set[K]
Expand Down Expand Up @@ -667,9 +662,11 @@ impl FixedArray {
op_get[T](Self[T], Int) -> T
op_set[T](Self[T], Int, T) -> Unit
set[T](Self[T], Int, T) -> Unit
set_utf16_char(Self[Byte], Int, Char) -> Int
set_utf16_char(Self[Byte], Int, Char) -> Int //deprecated
set_utf16be_char(Self[Byte], Int, Char) -> Int
set_utf16le_char(Self[Byte], Int, Char) -> Int
set_utf8_char(Self[Byte], Int, Char) -> Int
to_json[X : ToJson](Self[X]) -> Json
to_string[X : Show](Self[X]) -> String
unsafe_blit[A](Self[A], Int, Self[A], Int, Int) -> Unit
}

Expand Down Expand Up @@ -702,105 +699,90 @@ impl Logger {
impl Tuple(2) {
compare[T0 : Compare, T1 : Compare]((T0, T1), (T0, T1)) -> Int
op_equal[T0 : Eq, T1 : Eq]((T0, T1), (T0, T1)) -> Bool
to_string[A : Show, B : Show]((A, B)) -> String
}


impl Tuple(3) {
compare[T0 : Compare, T1 : Compare, T2 : Compare]((T0, T1, T2), (T0, T1, T2)) -> Int
op_equal[T0 : Eq, T1 : Eq, T2 : Eq]((T0, T1, T2), (T0, T1, T2)) -> Bool
to_string[A : Show, B : Show, C : Show]((A, B, C)) -> String
}


impl Tuple(4) {
compare[T0 : Compare, T1 : Compare, T2 : Compare, T3 : Compare]((T0, T1, T2, T3), (T0, T1, T2, T3)) -> Int
op_equal[T0 : Eq, T1 : Eq, T2 : Eq, T3 : Eq]((T0, T1, T2, T3), (T0, T1, T2, T3)) -> Bool
to_string[A : Show, B : Show, C : Show, D : Show]((A, B, C, D)) -> String
}


impl Tuple(5) {
compare[T0 : Compare, T1 : Compare, T2 : Compare, T3 : Compare, T4 : Compare]((T0, T1, T2, T3, T4), (T0, T1, T2, T3, T4)) -> Int
op_equal[T0 : Eq, T1 : Eq, T2 : Eq, T3 : Eq, T4 : Eq]((T0, T1, T2, T3, T4), (T0, T1, T2, T3, T4)) -> Bool
to_string[A : Show, B : Show, C : Show, D : Show, E : Show]((A, B, C, D, E)) -> String
}


impl Tuple(6) {
compare[T0 : Compare, T1 : Compare, T2 : Compare, T3 : Compare, T4 : Compare, T5 : Compare]((T0, T1, T2, T3, T4, T5), (T0, T1, T2, T3, T4, T5)) -> Int
op_equal[T0 : Eq, T1 : Eq, T2 : Eq, T3 : Eq, T4 : Eq, T5 : Eq]((T0, T1, T2, T3, T4, T5), (T0, T1, T2, T3, T4, T5)) -> Bool
to_string[A : Show, B : Show, C : Show, D : Show, E : Show, F : Show]((A, B, C, D, E, F)) -> String
}


impl Tuple(7) {
compare[T0 : Compare, T1 : Compare, T2 : Compare, T3 : Compare, T4 : Compare, T5 : Compare, T6 : Compare]((T0, T1, T2, T3, T4, T5, T6), (T0, T1, T2, T3, T4, T5, T6)) -> Int
op_equal[T0 : Eq, T1 : Eq, T2 : Eq, T3 : Eq, T4 : Eq, T5 : Eq, T6 : Eq]((T0, T1, T2, T3, T4, T5, T6), (T0, T1, T2, T3, T4, T5, T6)) -> Bool
to_string[A : Show, B : Show, C : Show, D : Show, E : Show, F : Show, G : Show]((A, B, C, D, E, F, G)) -> String
}


impl Tuple(8) {
compare[T0 : Compare, T1 : Compare, T2 : Compare, T3 : Compare, T4 : Compare, T5 : Compare, T6 : Compare, T7 : Compare]((T0, T1, T2, T3, T4, T5, T6, T7), (T0, T1, T2, T3, T4, T5, T6, T7)) -> Int
op_equal[T0 : Eq, T1 : Eq, T2 : Eq, T3 : Eq, T4 : Eq, T5 : Eq, T6 : Eq, T7 : Eq]((T0, T1, T2, T3, T4, T5, T6, T7), (T0, T1, T2, T3, T4, T5, T6, T7)) -> Bool
to_string[T0 : Show, T1 : Show, T2 : Show, T3 : Show, T4 : Show, T5 : Show, T6 : Show, T7 : Show]((T0, T1, T2, T3, T4, T5, T6, T7)) -> String
}


impl Tuple(9) {
compare[T0 : Compare, T1 : Compare, T2 : Compare, T3 : Compare, T4 : Compare, T5 : Compare, T6 : Compare, T7 : Compare, T8 : Compare]((T0, T1, T2, T3, T4, T5, T6, T7, T8), (T0, T1, T2, T3, T4, T5, T6, T7, T8)) -> Int
op_equal[T0 : Eq, T1 : Eq, T2 : Eq, T3 : Eq, T4 : Eq, T5 : Eq, T6 : Eq, T7 : Eq, T8 : Eq]((T0, T1, T2, T3, T4, T5, T6, T7, T8), (T0, T1, T2, T3, T4, T5, T6, T7, T8)) -> Bool
to_string[T0 : Show, T1 : Show, T2 : Show, T3 : Show, T4 : Show, T5 : Show, T6 : Show, T7 : Show, T8 : Show]((T0, T1, T2, T3, T4, T5, T6, T7, T8)) -> String
}


impl Tuple(10) {
compare[T0 : Compare, T1 : Compare, T2 : Compare, T3 : Compare, T4 : Compare, T5 : Compare, T6 : Compare, T7 : Compare, T8 : Compare, T9 : Compare]((T0, T1, T2, T3, T4, T5, T6, T7, T8, T9), (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9)) -> Int
op_equal[T0 : Eq, T1 : Eq, T2 : Eq, T3 : Eq, T4 : Eq, T5 : Eq, T6 : Eq, T7 : Eq, T8 : Eq, T9 : Eq]((T0, T1, T2, T3, T4, T5, T6, T7, T8, T9), (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9)) -> Bool
to_string[T0 : Show, T1 : Show, T2 : Show, T3 : Show, T4 : Show, T5 : Show, T6 : Show, T7 : Show, T8 : Show, T9 : Show]((T0, T1, T2, T3, T4, T5, T6, T7, T8, T9)) -> String
}


impl Tuple(11) {
compare[T0 : Compare, T1 : Compare, T2 : Compare, T3 : Compare, T4 : Compare, T5 : Compare, T6 : Compare, T7 : Compare, T8 : Compare, T9 : Compare, T10 : Compare]((T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10), (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10)) -> Int
op_equal[T0 : Eq, T1 : Eq, T2 : Eq, T3 : Eq, T4 : Eq, T5 : Eq, T6 : Eq, T7 : Eq, T8 : Eq, T9 : Eq, T10 : Eq]((T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10), (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10)) -> Bool
to_string[T0 : Show, T1 : Show, T2 : Show, T3 : Show, T4 : Show, T5 : Show, T6 : Show, T7 : Show, T8 : Show, T9 : Show, T10 : Show]((T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10)) -> String
}


impl Tuple(12) {
compare[T0 : Compare, T1 : Compare, T2 : Compare, T3 : Compare, T4 : Compare, T5 : Compare, T6 : Compare, T7 : Compare, T8 : Compare, T9 : Compare, T10 : Compare, T11 : Compare]((T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11), (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11)) -> Int
op_equal[T0 : Eq, T1 : Eq, T2 : Eq, T3 : Eq, T4 : Eq, T5 : Eq, T6 : Eq, T7 : Eq, T8 : Eq, T9 : Eq, T10 : Eq, T11 : Eq]((T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11), (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11)) -> Bool
to_string[T0 : Show, T1 : Show, T2 : Show, T3 : Show, T4 : Show, T5 : Show, T6 : Show, T7 : Show, T8 : Show, T9 : Show, T10 : Show, T11 : Show]((T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11)) -> String
}


impl Tuple(13) {
compare[T0 : Compare, T1 : Compare, T2 : Compare, T3 : Compare, T4 : Compare, T5 : Compare, T6 : Compare, T7 : Compare, T8 : Compare, T9 : Compare, T10 : Compare, T11 : Compare, T12 : Compare]((T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12), (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12)) -> Int
op_equal[T0 : Eq, T1 : Eq, T2 : Eq, T3 : Eq, T4 : Eq, T5 : Eq, T6 : Eq, T7 : Eq, T8 : Eq, T9 : Eq, T10 : Eq, T11 : Eq, T12 : Eq]((T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12), (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12)) -> Bool
to_string[T0 : Show, T1 : Show, T2 : Show, T3 : Show, T4 : Show, T5 : Show, T6 : Show, T7 : Show, T8 : Show, T9 : Show, T10 : Show, T11 : Show, T12 : Show]((T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12)) -> String
}


impl Tuple(14) {
compare[T0 : Compare, T1 : Compare, T2 : Compare, T3 : Compare, T4 : Compare, T5 : Compare, T6 : Compare, T7 : Compare, T8 : Compare, T9 : Compare, T10 : Compare, T11 : Compare, T12 : Compare, T13 : Compare]((T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13), (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13)) -> Int
op_equal[T0 : Eq, T1 : Eq, T2 : Eq, T3 : Eq, T4 : Eq, T5 : Eq, T6 : Eq, T7 : Eq, T8 : Eq, T9 : Eq, T10 : Eq, T11 : Eq, T12 : Eq, T13 : Eq]((T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13), (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13)) -> Bool
to_string[T0 : Show, T1 : Show, T2 : Show, T3 : Show, T4 : Show, T5 : Show, T6 : Show, T7 : Show, T8 : Show, T9 : Show, T10 : Show, T11 : Show, T12 : Show, T13 : Show]((T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13)) -> String
}


impl Tuple(15) {
compare[T0 : Compare, T1 : Compare, T2 : Compare, T3 : Compare, T4 : Compare, T5 : Compare, T6 : Compare, T7 : Compare, T8 : Compare, T9 : Compare, T10 : Compare, T11 : Compare, T12 : Compare, T13 : Compare, T14 : Compare]((T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14), (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14)) -> Int
op_equal[T0 : Eq, T1 : Eq, T2 : Eq, T3 : Eq, T4 : Eq, T5 : Eq, T6 : Eq, T7 : Eq, T8 : Eq, T9 : Eq, T10 : Eq, T11 : Eq, T12 : Eq, T13 : Eq, T14 : Eq]((T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14), (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14)) -> Bool
to_string[T0 : Show, T1 : Show, T2 : Show, T3 : Show, T4 : Show, T5 : Show, T6 : Show, T7 : Show, T8 : Show, T9 : Show, T10 : Show, T11 : Show, T12 : Show, T13 : Show, T14 : Show]((T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14)) -> String
}


impl Tuple(16) {
compare[T0 : Compare, T1 : Compare, T2 : Compare, T3 : Compare, T4 : Compare, T5 : Compare, T6 : Compare, T7 : Compare, T8 : Compare, T9 : Compare, T10 : Compare, T11 : Compare, T12 : Compare, T13 : Compare, T14 : Compare, T15 : Compare]((T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15), (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15)) -> Int
op_equal[T0 : Eq, T1 : Eq, T2 : Eq, T3 : Eq, T4 : Eq, T5 : Eq, T6 : Eq, T7 : Eq, T8 : Eq, T9 : Eq, T10 : Eq, T11 : Eq, T12 : Eq, T13 : Eq, T14 : Eq, T15 : Eq]((T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15), (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15)) -> Bool
to_string[T0 : Show, T1 : Show, T2 : Show, T3 : Show, T4 : Show, T5 : Show, T6 : Show, T7 : Show, T8 : Show, T9 : Show, T10 : Show, T11 : Show, T12 : Show, T13 : Show, T14 : Show, T15 : Show]((T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15)) -> String
}

// Type aliases
Expand Down
93 changes: 91 additions & 2 deletions builtin/bytes.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ pub fn copy(self : Bytes) -> Bytes {
}

///|
/// Fill utf8 encoded char `value` into byte sequence `self`, starting at `offset`.
/// Fill UTF8 encoded char `value` into byte sequence `self`, starting at `offset`.
/// It return the length of bytes has been written.
/// @alert deprecated "The type Bytes is about to be changed to be immutable. Use `FixedArray[Byte]` or `Buffer` instead."
pub fn set_utf8_char(self : Bytes, offset : Int, value : Char) -> Int {
Expand Down Expand Up @@ -169,7 +169,39 @@ pub fn set_utf8_char(self : Bytes, offset : Int, value : Char) -> Int {
}

///|
/// Fill utf16 encoded char `value` into byte sequence `self`, starting at `offset`.
/// Fill UTF8 encoded char `value` into byte sequence `self`, starting at `offset`.
/// It return the length of bytes has been written.
pub fn set_utf8_char(
self : FixedArray[Byte],
offset : Int,
value : Char
) -> Int {
let code = value.to_uint()
if code < 0x80 {
self[offset] = ((code & 0x7F) | 0x00).to_byte()
1
} else if code < 0x0800 {
self[offset] = (((code >> 6) & 0x1F) | 0xC0).to_byte()
self[offset + 1] = ((code & 0x3F) | 0x80).to_byte()
2
} else if code < 0x010000 {
self[offset] = (((code >> 12) & 0x0F) | 0xE0).to_byte()
self[offset + 1] = (((code >> 6) & 0x3F) | 0x80).to_byte()
self[offset + 2] = ((code & 0x3F) | 0x80).to_byte()
3
} else if code < 0x110000 {
self[offset] = (((code >> 18) & 0x07) | 0xF0).to_byte()
self[offset + 1] = (((code >> 12) & 0x3F) | 0x80).to_byte()
self[offset + 2] = (((code >> 6) & 0x3F) | 0x80).to_byte()
self[offset + 3] = ((code & 0x3F) | 0x80).to_byte()
4
} else {
abort("Char out of range")
}
}

///|
/// Fill UTF16 encoded char `value` into byte sequence `self`, starting at `offset`.
/// It return the length of bytes has been written.
/// @alert unsafe "Panic if the [value] is out of range"
pub fn set_utf16_char(self : Bytes, offset : Int, value : Char) -> Int {
Expand All @@ -196,6 +228,7 @@ pub fn set_utf16_char(self : Bytes, offset : Int, value : Char) -> Int {
/// Fill utf16 encoded char `value` into byte sequence `self`, starting at `offset`.
/// It return the length of bytes has been written.
/// @alert unsafe "Panic if the [value] is out of range"
/// @alert deprecated "Use `set_utf16le_char` instead"
pub fn set_utf16_char(
self : FixedArray[Byte],
offset : Int,
Expand All @@ -220,6 +253,62 @@ pub fn set_utf16_char(
}
}

///|
/// Fill UTF16LE encoded char `value` into byte sequence `self`, starting at `offset`.
/// It return the length of bytes has been written.
/// @alert unsafe "Panic if the [value] is out of range"
pub fn set_utf16le_char(
self : FixedArray[Byte],
offset : Int,
value : Char
) -> Int {
let code = value.to_uint()
if code < 0x10000 {
self[offset] = (code & 0xFF).to_byte()
self[offset + 1] = (code >> 8).to_byte()
2
} else if code < 0x110000 {
let hi = code - 0x10000
let lo = (hi >> 10) | 0xD800
let hi = (hi & 0x3FF) | 0xDC00
self[offset] = (lo & 0xFF).to_byte()
self[offset + 1] = (lo >> 8).to_byte()
self[offset + 2] = (hi & 0xFF).to_byte()
self[offset + 3] = (hi >> 8).to_byte()
4
} else {
abort("Char out of range")
}
}

///|
/// Fill UTF16BE encoded char `value` into byte sequence `self`, starting at `offset`.
/// It return the length of bytes has been written.
/// @alert unsafe "Panic if the [value] is out of range"
pub fn set_utf16be_char(
self : FixedArray[Byte],
offset : Int,
value : Char
) -> Int {
let code = value.to_uint()
if code < 0x10000 {
self[offset] = (code >> 0xFF).to_byte()
self[offset + 1] = (code & 0xFF).to_byte()
2
} else if code < 0x110000 {
let hi = code - 0x10000
let lo = (hi >> 10) | 0xD800
let hi = (hi & 0x3FF) | 0xDC00
self[offset] = (lo >> 8).to_byte()
self[offset + 1] = (lo & 0xFF).to_byte()
self[offset + 2] = (hi >> 8).to_byte()
self[offset + 3] = (hi & 0xFF).to_byte()
4
} else {
abort("Char out of range")
}
}

///|
pub fn op_equal(self : Bytes, other : Bytes) -> Bool {
if self.length() != other.length() {
Expand Down
84 changes: 81 additions & 3 deletions builtin/bytes_test.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,90 @@ test "copy" {
assert_eq!(copy_bytes.to_unchecked_string(), bytes.to_unchecked_string())
}

test "set_utf16_char" {
let bytes = Bytes::new(10)
test "set_utf8_char (ASCII)" {
let arr = FixedArray::makei(4, fn { _ => b'\x00' })
let char = 'A'
let len = arr.set_utf8_char(0, char)
let bytes = @bytes.from_fixedarray(arr)
assert_eq!(len, 1)
inspect!(
bytes,
content=
#|b"\x41\x00\x00\x00"
,
)
}

test "set_utf8_char (CJK)" {
let arr = FixedArray::makei(4, fn { _ => b'\x00' })
let char = '拼'
let len = arr.set_utf8_char(0, char)
let bytes = @bytes.from_fixedarray(arr)
assert_eq!(len, 3)
inspect!(
bytes,
content=
#|b"\xe6\x8b\xbc\x00"
,
)
}

test "set_utf8_char (Emoji)" {
let arr = FixedArray::makei(4, fn { _ => b'\x00' })
let char = '🙅'
let len = arr.set_utf8_char(0, char)
let bytes = @bytes.from_fixedarray(arr)
assert_eq!(len, 4)
inspect!(
bytes,
content=
#|b"\xf0\x9f\x99\x85"
,
)
}

test "set_utf16le_char" {
let arr = FixedArray::makei(4, fn { _ => b'\x00' })
let char = 'A'
let len = bytes.set_utf16_char(0, char)
let len = arr.set_utf16le_char(0, char)
let bytes = @bytes.from_fixedarray(arr)
assert_eq!(len, 2)
assert_eq!(bytes.to_unchecked_string(offset=0, length=2), "A")
inspect!(
bytes,
content=
#|b"\x41\x00\x00\x00"
,
)
}

test "set_utf16le_char (surrogate pair)" {
let arr = FixedArray::makei(4, fn { _ => b'\x00' })
let char = '🉑'
let len = arr.set_utf16le_char(0, char)
let bytes = @bytes.from_fixedarray(arr)
assert_eq!(len, 4)
assert_eq!(bytes.to_unchecked_string(offset=0, length=4), "🉑")
inspect!(
bytes,
content=
#|b"\x3c\xd8\x51\xde"
,
)
}

test "set_utf16be_char" {
let arr = FixedArray::makei(4, fn { _ => b'\x00' })
let char = 'A'
let len = arr.set_utf16be_char(0, char)
let bytes = @bytes.from_fixedarray(arr)
assert_eq!(len, 2)
inspect!(
bytes,
content=
#|b"\x00\x41\x00\x00"
,
)
}

test "op_equal" {
Expand Down
Loading

0 comments on commit 91f7355

Please sign in to comment.