A MoonBit package for converting bytes to strings with efficient memory operations
moon add illusory0x0/string_of_bytestest "basic string conversion" {
// "Hello" in UTF-16 LE encoding
let bytes : Bytes = b"\x48\x00\x65\x00\x6C\x00\x6C\x00\x6F\x00"
let result = @string_of_bytes.string_of_bytes(bytes, 0, bytes.length())
inspect(result, content="Hello")
}test "substring extraction" {
// "Hello World" but extract only "World"
let bytes : Bytes = b"\x48\x00\x65\x00\x6C\x00\x6C\x00\x6F\x00\x20\x00\x57\x00\x6F\x00\x72\x00\x6C\x00\x64\x00"
let result = @string_of_bytes.string_of_bytes(bytes, 12, 10) // Skip "Hello " (12 bytes), take "World" (10 bytes)
inspect(result, content="World")
}test "unicode characters" {
// Chinese characters "你好" in UTF-16 LE
let bytes : Bytes = b"\x60\x4F\x7D\x59"
let result = @string_of_bytes.string_of_bytes(bytes, 0, bytes.length())
inspect(result, content="你好")
}
test "emoji support" {
// Grinning face emoji 😀 with surrogate pairs
let bytes : Bytes = b"\x3D\xD8\x00\xDE"
let result = @string_of_bytes.string_of_bytes(bytes, 0, bytes.length())
inspect(result, content="😀")
}test "move semantics conversion" {
let bytes : Bytes = b"\x48\x00\x65\x00\x6C\x00\x6C\x00\x6F\x00"
let copied_bytes = copy_bytes(bytes)
let result = @string_of_bytes.move_bytes_to_string(copied_bytes)
inspect(result, content="Hello")
// ⚠️ Do not use 'copied_bytes' after this point!
}test "large string performance" {
// Create a larger byte array
let byte_array : Array[Byte] = []
for i = 0; i < 10; i = i + 1 {
byte_array.push(0x41) // A
byte_array.push(0x00)
byte_array.push(0x42) // B
byte_array.push(0x00)
byte_array.push(0x43) // C
byte_array.push(0x00)
byte_array.push(0x44) // D
byte_array.push(0x00)
}
let bytes = Bytes::from_array(byte_array)
let copied_bytes = copy_bytes(bytes)
let result = @string_of_bytes.move_bytes_to_string(copied_bytes)
inspect(result, content="ABCDABCDABCDABCDABCDABCDABCDABCDABCDABCD")
}test "mixed scripts" {
// "Hi你" - English + Chinese
let bytes : Bytes = b"\x48\x00\x69\x00\x60\x4F"
let result = @string_of_bytes.string_of_bytes(bytes, 0, bytes.length())
inspect(result, content="Hi你")
}test "special characters" {
// Mathematical symbols ∑∏∫∆
let bytes : Bytes = b"\x11\x22\x0F\x22\x2B\x22\x06\x22"
let result = @string_of_bytes.string_of_bytes(bytes, 0, bytes.length())
inspect(result, content="∑∏∫∆")
}
test "currency symbols" {
// Various currency symbols $€£¥₹
let bytes : Bytes = b"\x24\x00\xAC\x20\xA3\x00\xA5\x00\xB9\x20"
let result = @string_of_bytes.string_of_bytes(bytes, 0, bytes.length())
inspect(result, content="$€£¥₹")
}test "empty string" {
let bytes : Bytes = b""
let result = @string_of_bytes.string_of_bytes(bytes, 0, 0)
inspect(result, content="")
}
test "null character" {
let bytes : Bytes = b"\x00\x00"
let result = @string_of_bytes.string_of_bytes(bytes, 0, 2)
inspect(result, content="\u0000")
}Install
Download zipA MoonBit package for converting bytes to strings with efficient memory operations