encoding_rs準拠のShift_JISデコーダー for MoonBit
[dependencies]
encoding_sjis = "{ version = \"0.1.0\" }"test "simple decode" {
let bytes = [0x82, 0xA0, 0x82, 0xA2, 0x82, 0xA4] // "あいう"
let (result, had_replacements) = decode(src=bytes)
@test.eq(result, "あいう")
@test.eq(had_replacements, false)
}test "streaming decode" {
let decoder = new_decoder()
let chunk1 = [0x82, 0xA0] // "あ"
let chunk2 = [0x82, 0xA2] // "い"
let result1 = decoder.decode_to_string(src=chunk1, last=false)
let result2 = decoder.decode_to_string(src=chunk2, last=true)
@test.eq(result1, "あ")
@test.eq(result2, "い")
}test "chunk boundary" {
let decoder = new_decoder()
let chunk1 = [0x82] // First byte of "あ"
let chunk2 = [0xA0] // Second byte of "あ"
let result1 = decoder.decode_to_string(src=chunk1, last=false)
let result2 = decoder.decode_to_string(src=chunk2, last=true)
@test.eq(result1, "") // Incomplete sequence is buffered
@test.eq(result2, "あ") // Completed after second chunk
}test "error handling" {
let bytes = [0x41, 0x80, 0x42] // "A" + invalid + "B"
let (result, had_replacements) = decode(src=bytes)
@test.eq(result, "A\ufffdB") // U+FFFD for invalid byte
@test.eq(had_replacements, true)
}test "mixed content" {
let bytes = [
0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x20, // "Hello "
0x82, 0xA0, 0x82, 0xA2, // "あい"
0xB1, 0xB2, 0xB3 // "アイウ" (half-width katakana)
]
let (result, _) = decode(src=bytes)
@test.eq(result, "Hello あいアイウ")
}test "shift_jis_to_utf8" {
let bytes = [0x82, 0xA0, 0x82, 0xA2]
let result = shift_jis_to_utf8(data=bytes)
@test.eq(result, "あい")
}pub enum CoderResult {
InputEmpty
OutputFull
}impl Eq for CoderResultimpl Show for CoderResultpub 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) -> Stringencoding_rs準拠のShift_JISデコーダー for MoonBit