module scheme import encoding.utf8 import encoding.utf16 import strings // This scalar cursor is a thin partial-unit adapter: no rune/unit array, borrowed // stack slice, or per-character allocation. Malformed UTF16 consumes one complete // invalid unit; a high surrogate plus a trailing partial unit consumes the tail // together (one replacement), matching the independently stored codec fixtures. struct BytevectorUnicodeCursor { bytes []u8 little bool width int mut: offset int } fn (mut cursor BytevectorUnicodeCursor) next() ?rune { if cursor.offset >= cursor.bytes.len { return none } remaining := cursor.bytes.len - cursor.offset if remaining < cursor.width { cursor.offset = cursor.bytes.len return rune(0xfffd) } word := bytevector_word(cursor.bytes, cursor.offset, cursor.width, cursor.little) cursor.offset += cursor.width if cursor.width == 4 { return if word <= 0x10ffff && utf16.is_valid_rune(rune(word)) { rune(word) } else { rune(0xfffd) } } first := u16(word) if first >= 0xd800 && first <= 0xdbff { if cursor.bytes.len - cursor.offset < 2 { cursor.offset = cursor.bytes.len return rune(0xfffd) } second := u16(bytevector_word(cursor.bytes, cursor.offset, 2, cursor.little)) if second >= 0xdc00 && second <= 0xdfff { cursor.offset += 2 return bytevector_utf16_scalar(first, second) } return rune(0xfffd) } return if utf16.is_surrogate(rune(first)) { rune(0xfffd) } else { rune(first) } } fn bytevector_unicode_encode(value Value, bits int, little bool) !Value { text := string_data(value)! width := bits / 8 mut offset, mut length := 0, 0 for offset < text.len { ch, size := utf8.decode_rune_at(text, offset) needed := if bits == 16 { 2 * utf16.rune_len(ch) } else { 4 } if needed > max_int - length { return failure('encoded bytevector length exceeds supported range') } length += needed offset += size } mut bytes := []u8{cap: length} unsafe { bytes.len = length } offset = 0 mut at := 0 for offset < text.len { ch, size := utf8.decode_rune_at(text, offset) offset += size if bits == 16 { at += bytevector_put_utf16(mut bytes, at, little, ch) } else { bytevector_put_word(mut bytes, at, width, little, u32(ch)) at += width } } return owned_bytevector(bytes) } fn bytevector_unicode_decode(value Value, bits int, initial_little bool, mandatory bool) !Value { data := bytevector_data(value)! width := bits / 8 mut little := initial_little mut start := 0 if !mandatory && data.bytes.len >= width { bom := bytevector_word(data.bytes, 0, width, false) if bom == 0xfeff { little = false start = width } else if bom == if width == 2 { u64(0xfffe) } else { u64(0xfffe0000) } { little = true start = width } } mut cursor := BytevectorUnicodeCursor{ bytes: data.bytes, little: little, width: width, offset: start } mut length, mut characters := 0, 0 for { ch := cursor.next() or { break } count := ch.length_in_bytes() if count > max_int - 1 - length { return failure('decoded string length exceeds supported range') } length += count characters++ } mut output := strings.new_buffer(length) cursor.offset = start mut scalar := [5]u8{} for { ch := cursor.next() or { break } fragment := unsafe { utf32_to_str_no_malloc(u32(ch), mut &scalar[0]) } output.write_string(fragment) } return owned_string(output.take_string(), characters) } fn bytevector_unicode_codec(args []Value, name string, bits int, encode bool) !Value { if encode { check_arity(name, args.len, 1, 2)! little := if args.len == 2 { bytevector_little(args[1])! } else { false } return bytevector_unicode_encode(args[0], bits, little) } check_arity(name, args.len, 2, 3)! little := bytevector_little(args[1])! return bytevector_unicode_decode(args[0], bits, little, args.len == 3 && args[2].is_true()) } // Shared scalar bridge for computational codecs and resumable transcoders. // Callers validate complete pairs/scalars and reserve the exact destination. fn bytevector_utf16_scalar(first u16, second u16) rune { return rune(0x10000 + (u32(first - 0xd800) << 10) + u32(second - 0xdc00)) } fn bytevector_put_utf16(mut bytes []u8, at int, little bool, ch rune) int { if ch > 0xffff { supplementary := u32(ch) - 0x10000 bytevector_put_word(mut bytes, at, 2, little, 0xd800 + (supplementary >> 10)) bytevector_put_word(mut bytes, at + 2, 2, little, 0xdc00 + (supplementary & 0x3ff)) return 4 } bytevector_put_word(mut bytes, at, 2, little, u32(ch)) return 2 }