encoding_rs-compliant Shift_JIS decoder for MoonBit
moon add f4ah6o/encoding_sjis@0.1.1///|
test "simple decode" {
let bytes = Bytes::from_array([0x82, 0xA0, 0x82, 0xA2, 0x82, 0xA4]) // "あいう"
let (result, had_replacements) = @encoding_sjis.decode(src=bytes)
inspect(result, content="あいう")
inspect(had_replacements, content="false")
}///|
test "streaming decode" {
let decoder = @encoding_sjis.new_decoder()
let chunk1 = Bytes::from_array([0x82, 0xA0]) // "あ"
let chunk2 = Bytes::from_array([0x82, 0xA2]) // "い"
let result1 = decoder.decode_to_string(src=chunk1, false)
let result2 = decoder.decode_to_string(src=chunk2, true)
inspect(result1, content="あ")
inspect(result2, content="い")
}///|
test "chunk boundary" {
let decoder = @encoding_sjis.new_decoder()
let chunk1 = Bytes::from_array([0x82]) // First byte of "あ"
let chunk2 = Bytes::from_array([0xA0]) // Second byte of "あ"
let result1 = decoder.decode_to_string(src=chunk1, false)
let result2 = decoder.decode_to_string(src=chunk2, true)
inspect(result1, content="") // Incomplete sequence is buffered
inspect(result2, content="あ") // Completed after second chunk
}///|
test "error handling" {
let bytes = Bytes::from_array([0x41, 0x80, 0x42]) // "A" + invalid + "B"
let (result, had_replacements) = @encoding_sjis.decode(src=bytes)
inspect(result, content="A�B") // U+FFFD for invalid byte
inspect(had_replacements, content="true")
}///|
test "mixed content" {
let bytes = Bytes::from_array([
0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x20, // "Hello "
0x82, 0xA0, 0x82, 0xA2, // "あい"
0xB1, 0xB2, 0xB3, // "アイウ" (half-width katakana)
])
let (result, _) = @encoding_sjis.decode(src=bytes)
inspect(result, content="Hello あいアイウ")
}///|
test "shift_jis_to_utf8" {
let bytes = Bytes::from_array([0x82, 0xA0, 0x82, 0xA2])
let result = @encoding_sjis.shift_jis_to_utf8(data=bytes)
inspect(result, content="あい")
}impl Show for CoderResultimpl Show for DecodeResultpub struct Decoder {
pending_first_byte : Int
had_replacements : Bool
}fn decode(src~ : Bytes) -> (String, Bool)fn decode_half_width_katakana(byte~ : Int) -> Charfn decode_jis_x_0208(code~ : Int) -> Char?fn shift_jis_to_utf8(data~ : Bytes) -> StringInstall
Download zipencoding_rs-compliant Shift_JIS decoder for MoonBit