| 1 | module scheme |
| 2 | |
| 3 | import encoding.utf8 |
| 4 | import encoding.utf16 |
| 5 | import strings |
| 6 | |
| 7 | // This scalar cursor is a thin partial-unit adapter: no rune/unit array, borrowed |
| 8 | // stack slice, or per-character allocation. Malformed UTF16 consumes one complete |
| 9 | // invalid unit; a high surrogate plus a trailing partial unit consumes the tail |
| 10 | // together (one replacement), matching the independently stored codec fixtures. |
| 11 | struct BytevectorUnicodeCursor { |
| 12 | bytes []u8 |
| 13 | little bool |
| 14 | width int |
| 15 | mut: |
| 16 | offset int |
| 17 | } |
| 18 | |
| 19 | fn (mut cursor BytevectorUnicodeCursor) next() ?rune { |
| 20 | if cursor.offset >= cursor.bytes.len { return none } |
| 21 | remaining := cursor.bytes.len - cursor.offset |
| 22 | if remaining < cursor.width { |
| 23 | cursor.offset = cursor.bytes.len |
| 24 | return rune(0xfffd) |
| 25 | } |
| 26 | word := bytevector_word(cursor.bytes, cursor.offset, cursor.width, cursor.little) |
| 27 | cursor.offset += cursor.width |
| 28 | if cursor.width == 4 { |
| 29 | return if word <= 0x10ffff && utf16.is_valid_rune(rune(word)) { |
| 30 | rune(word) |
| 31 | } else { |
| 32 | rune(0xfffd) |
| 33 | } |
| 34 | } |
| 35 | first := u16(word) |
| 36 | if first >= 0xd800 && first <= 0xdbff { |
| 37 | if cursor.bytes.len - cursor.offset < 2 { |
| 38 | cursor.offset = cursor.bytes.len |
| 39 | return rune(0xfffd) |
| 40 | } |
| 41 | second := u16(bytevector_word(cursor.bytes, cursor.offset, 2, cursor.little)) |
| 42 | if second >= 0xdc00 && second <= 0xdfff { |
| 43 | cursor.offset += 2 |
| 44 | return bytevector_utf16_scalar(first, second) |
| 45 | } |
| 46 | return rune(0xfffd) |
| 47 | } |
| 48 | return if utf16.is_surrogate(rune(first)) { rune(0xfffd) } else { rune(first) } |
| 49 | } |
| 50 | |
| 51 | fn bytevector_unicode_encode(value Value, bits int, little bool) !Value { |
| 52 | text := string_data(value)! |
| 53 | width := bits / 8 |
| 54 | mut offset, mut length := 0, 0 |
| 55 | for offset < text.len { |
| 56 | ch, size := utf8.decode_rune_at(text, offset) |
| 57 | needed := if bits == 16 { 2 * utf16.rune_len(ch) } else { 4 } |
| 58 | if needed > max_int - length { |
| 59 | return failure('encoded bytevector length exceeds supported range') |
| 60 | } |
| 61 | length += needed |
| 62 | offset += size |
| 63 | } |
| 64 | mut bytes := []u8{cap: length} |
| 65 | unsafe { bytes.len = length } |
| 66 | offset = 0 |
| 67 | mut at := 0 |
| 68 | for offset < text.len { |
| 69 | ch, size := utf8.decode_rune_at(text, offset) |
| 70 | offset += size |
| 71 | if bits == 16 { |
| 72 | at += bytevector_put_utf16(mut bytes, at, little, ch) |
| 73 | } else { |
| 74 | bytevector_put_word(mut bytes, at, width, little, u32(ch)) |
| 75 | at += width |
| 76 | } |
| 77 | } |
| 78 | return owned_bytevector(bytes) |
| 79 | } |
| 80 | |
| 81 | fn bytevector_unicode_decode(value Value, bits int, initial_little bool, mandatory bool) !Value { |
| 82 | data := bytevector_data(value)! |
| 83 | width := bits / 8 |
| 84 | mut little := initial_little |
| 85 | mut start := 0 |
| 86 | if !mandatory && data.bytes.len >= width { |
| 87 | bom := bytevector_word(data.bytes, 0, width, false) |
| 88 | if bom == 0xfeff { |
| 89 | little = false |
| 90 | start = width |
| 91 | } else if bom == if width == 2 { u64(0xfffe) } else { u64(0xfffe0000) } { |
| 92 | little = true |
| 93 | start = width |
| 94 | } |
| 95 | } |
| 96 | mut cursor := BytevectorUnicodeCursor{ bytes: data.bytes, little: little, width: width, offset: start } |
| 97 | mut length, mut characters := 0, 0 |
| 98 | for { |
| 99 | ch := cursor.next() or { break } |
| 100 | count := ch.length_in_bytes() |
| 101 | if count > max_int - 1 - length { |
| 102 | return failure('decoded string length exceeds supported range') |
| 103 | } |
| 104 | length += count |
| 105 | characters++ |
| 106 | } |
| 107 | mut output := strings.new_buffer(length) |
| 108 | cursor.offset = start |
| 109 | mut scalar := [5]u8{} |
| 110 | for { |
| 111 | ch := cursor.next() or { break } |
| 112 | fragment := unsafe { utf32_to_str_no_malloc(u32(ch), mut &scalar[0]) } |
| 113 | output.write_string(fragment) |
| 114 | } |
| 115 | return owned_string(output.take_string(), characters) |
| 116 | } |
| 117 | |
| 118 | fn bytevector_unicode_codec(args []Value, name string, bits int, encode bool) !Value { |
| 119 | if encode { |
| 120 | check_arity(name, args.len, 1, 2)! |
| 121 | little := if args.len == 2 { bytevector_little(args[1])! } else { false } |
| 122 | return bytevector_unicode_encode(args[0], bits, little) |
| 123 | } |
| 124 | check_arity(name, args.len, 2, 3)! |
| 125 | little := bytevector_little(args[1])! |
| 126 | return bytevector_unicode_decode(args[0], bits, little, args.len == 3 && args[2].is_true()) |
| 127 | } |
| 128 | |
| 129 | // Shared scalar bridge for computational codecs and resumable transcoders. |
| 130 | // Callers validate complete pairs/scalars and reserve the exact destination. |
| 131 | fn bytevector_utf16_scalar(first u16, second u16) rune { |
| 132 | return rune(0x10000 + (u32(first - 0xd800) << 10) + u32(second - 0xdc00)) |
| 133 | } |
| 134 | |
| 135 | fn bytevector_put_utf16(mut bytes []u8, at int, little bool, ch rune) int { |
| 136 | if ch > 0xffff { |
| 137 | supplementary := u32(ch) - 0x10000 |
| 138 | bytevector_put_word(mut bytes, at, 2, little, 0xd800 + (supplementary >> 10)) |
| 139 | bytevector_put_word(mut bytes, at + 2, 2, little, 0xdc00 + (supplementary & 0x3ff)) |
| 140 | return 4 |
| 141 | } |
| 142 | bytevector_put_word(mut bytes, at, 2, little, u32(ch)) |
| 143 | return 2 |
| 144 | } |
| 145 | |