| 1 | module scheme |
| 2 | |
| 3 | import encoding.hex |
| 4 | |
| 5 | // Authored independently against immutable85, without compiling or execution. |
| 6 | struct CbrReviewEvent { |
| 7 | kind CodecStepKind |
| 8 | scalar rune |
| 9 | raw int |
| 10 | carry int |
| 11 | failed bool |
| 12 | pending_cr bool |
| 13 | queued rune |
| 14 | } |
| 15 | |
| 16 | fn cbr_review_bytes(text string) []u8 { |
| 17 | return hex.decode(text) or { panic(err.msg()) } |
| 18 | } |
| 19 | |
| 20 | fn cbr_review_scalar(ch rune, raw int) CbrReviewEvent { |
| 21 | return CbrReviewEvent{ kind: .scalar, scalar: ch, raw: raw } |
| 22 | } |
| 23 | |
| 24 | fn cbr_review_bad(mode ErrorHandlingMode, raw int, carry int, pending_cr bool) CbrReviewEvent { |
| 25 | return CbrReviewEvent{ |
| 26 | kind: if mode == .replace { CodecStepKind.scalar } else { CodecStepKind.malformed } |
| 27 | scalar: if mode == .replace { rune(0xfffd) } else { rune(0) } |
| 28 | raw: raw |
| 29 | carry: carry |
| 30 | failed: mode == .raise |
| 31 | pending_cr: pending_cr |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | fn cbr_review_step(mut cursor TranscoderDecoder, mut input CodecInput, normalized bool) !CodecStep { |
| 36 | if normalized { return cursor.next(mut input) } |
| 37 | return cursor.next_decoded(mut input) |
| 38 | } |
| 39 | |
| 40 | // Cut positions are absolute and may include an empty first/final chunk. |
| 41 | // Failed steps are observed and resumed on the same cursor, not restarted. |
| 42 | fn cbr_review_trace(bytes []u8, codec CodecKind, mode ErrorHandlingMode, cuts []int, normalized bool) []CbrReviewEvent { |
| 43 | mut cursor := TranscoderDecoder{ eol: .lf, mode: mode, decoder: CodecDecoder{ codec: codec } } |
| 44 | mut result := []CbrReviewEvent{} |
| 45 | mut base := 0 |
| 46 | mut finished := false |
| 47 | for part, end in cuts { |
| 48 | assert end >= base && end <= bytes.len |
| 49 | mut input := codec_input(bytes[base..end], part == cuts.len - 1) |
| 50 | mut stopped := false |
| 51 | for _ in 0 .. bytes.len * 4 + 16 { |
| 52 | mut step := CodecStep{} |
| 53 | if value := cbr_review_step(mut cursor, mut input, normalized) { |
| 54 | step = value |
| 55 | } else { |
| 56 | assert err.msg().contains('decoding'), err.msg() |
| 57 | result << CbrReviewEvent{ |
| 58 | kind: .malformed |
| 59 | raw: base + input.position |
| 60 | carry: cursor.decoder.count |
| 61 | failed: true |
| 62 | pending_cr: cursor.pending_cr |
| 63 | queued: if cursor.has_queued { cursor.queued } else { rune(0) } |
| 64 | } |
| 65 | continue |
| 66 | } |
| 67 | if step.kind == .need_input { |
| 68 | assert !input.final && input.position == input.length |
| 69 | assert cursor.decoder.count <= 4 |
| 70 | stopped = true |
| 71 | break |
| 72 | } |
| 73 | if step.kind == .finished { |
| 74 | assert input.final |
| 75 | finished = true |
| 76 | stopped = true |
| 77 | break |
| 78 | } |
| 79 | result << CbrReviewEvent{ |
| 80 | kind: step.kind |
| 81 | scalar: step.scalar |
| 82 | raw: base + input.position |
| 83 | carry: cursor.decoder.count |
| 84 | pending_cr: cursor.pending_cr |
| 85 | queued: if cursor.has_queued { cursor.queued } else { rune(0) } |
| 86 | } |
| 87 | } |
| 88 | assert stopped |
| 89 | base = end |
| 90 | } |
| 91 | assert finished && base == bytes.len |
| 92 | return result |
| 93 | } |
| 94 | |
| 95 | fn cbr_review_byte_cuts(length int) []int { |
| 96 | mut cuts := []int{} |
| 97 | for end in 1 .. length + 1 { cuts << end } |
| 98 | if cuts.len == 0 { cuts << 0 } |
| 99 | return cuts |
| 100 | } |
| 101 | |
| 102 | fn cbr_review_all_cuts(bytes []u8, codec CodecKind, mode ErrorHandlingMode, normalized bool, expected []CbrReviewEvent) { |
| 103 | for split in 0 .. bytes.len + 1 { |
| 104 | assert cbr_review_trace(bytes, codec, mode, [split, bytes.len], normalized) == expected, 'split=${split},codec=${codec},mode=${mode}' |
| 105 | } |
| 106 | assert cbr_review_trace(bytes, codec, mode, cbr_review_byte_cuts(bytes.len), normalized) == expected |
| 107 | } |
| 108 | |
| 109 | fn test_cbr_review_raw_helper_exposes_ignored_gap_without_normalizing_or_flushing_cr() { |
| 110 | mut cursor := TranscoderDecoder{ eol: .lf, mode: .ignore, decoder: CodecDecoder{ codec: .utf8 } } |
| 111 | mut input := codec_input(cbr_review_bytes('0dff42'), true) |
| 112 | first := cursor.next_decoded(mut input) or { panic(err.msg()) } |
| 113 | assert first.kind == .scalar && first.scalar == `\r` && input.position == 1 |
| 114 | assert !cursor.pending_cr |
| 115 | _, produced := cursor.normalize_scalar(first.scalar) |
| 116 | assert !produced && cursor.pending_cr |
| 117 | bad := cursor.next_decoded(mut input) or { panic(err.msg()) } |
| 118 | assert bad.kind == .malformed && input.position == 2 |
| 119 | assert cursor.pending_cr && !cursor.has_queued |
| 120 | next := cursor.next_decoded(mut input) or { panic(err.msg()) } |
| 121 | assert next.scalar == `B` && input.position == 3 && cursor.pending_cr |
| 122 | lf, emitted := cursor.normalize_scalar(next.scalar) |
| 123 | assert emitted && lf == `\n` && !cursor.pending_cr |
| 124 | assert cursor.has_queued && cursor.queued == `B` |
| 125 | assert (cursor.next(mut input) or { panic(err.msg()) }).scalar == `B` |
| 126 | assert input.position == 3 |
| 127 | } |
| 128 | |
| 129 | fn test_cbr_review_utf8_raw_scalar_and_error_boundaries_are_invariant_at_every_byte_cut() { |
| 130 | bytes := cbr_review_bytes('41e282acff42') |
| 131 | for mode in [ErrorHandlingMode.ignore, .replace, .raise] { |
| 132 | cbr_review_all_cuts(bytes, .utf8, mode, false, [cbr_review_scalar(`A`, 1), |
| 133 | cbr_review_scalar(0x20ac, 4), cbr_review_bad(mode, 5, 0, false), cbr_review_scalar(`B`, 6)]) |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | fn test_cbr_review_utf8_noncontinuation_remains_unconsumed_after_malformed_prefix() { |
| 138 | for mode in [ErrorHandlingMode.ignore, .replace, .raise] { |
| 139 | cbr_review_all_cuts(cbr_review_bytes('e28242'), .utf8, mode, false, |
| 140 | [cbr_review_bad(mode, 2, 0, false), cbr_review_scalar(`B`, 3)]) |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | fn test_cbr_review_utf8_truncated_prefix_finishes_one_error_piece_at_all_cuts() { |
| 145 | for mode in [ErrorHandlingMode.ignore, .replace, .raise] { |
| 146 | cbr_review_all_cuts(cbr_review_bytes('e282'), .utf8, mode, false, |
| 147 | [cbr_review_bad(mode, 2, 0, false)]) |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | fn test_cbr_review_utf16_bad_high_surrogate_prefetches_valid_word_without_committing_its_unit() { |
| 152 | for encoded in ['feffd80000410042', 'fffe00d841004200'] { |
| 153 | for mode in [ErrorHandlingMode.ignore, .replace, .raise] { |
| 154 | // Physical6 minus retained2 locates the malformed high-unit end4. |
| 155 | // A then emits at physical6 with zero extra caller bytes consumed. |
| 156 | cbr_review_all_cuts(cbr_review_bytes(encoded), .utf16, mode, false, |
| 157 | [cbr_review_bad(mode, 6, 2, false), cbr_review_scalar(`A`, 6), |
| 158 | cbr_review_scalar(`B`, 8)]) |
| 159 | } |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | fn test_cbr_review_utf16_high_surrogate_plus_odd_tail_remains_two_error_pieces() { |
| 164 | for encoded in ['d80000', 'fffe00d800'] { |
| 165 | prefix := if encoded.starts_with('fffe') { 2 } else { 0 } |
| 166 | for mode in [ErrorHandlingMode.ignore, .replace, .raise] { |
| 167 | cbr_review_all_cuts(cbr_review_bytes(encoded), .utf16, mode, false, |
| 168 | [cbr_review_bad(mode, prefix + 3, 1, false), |
| 169 | cbr_review_bad(mode, prefix + 3, 0, false)]) |
| 170 | } |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | fn test_cbr_review_utf16_bom_and_surrogate_raw_endpoints_survive_all_splits_and_byte_chunks() { |
| 175 | for encoded in ['feff0041d83dde000042', 'fffe41003dd800de4200'] { |
| 176 | cbr_review_all_cuts(cbr_review_bytes(encoded), .utf16, .raise, false, |
| 177 | [cbr_review_scalar(`A`, 4), cbr_review_scalar(0x1f600, 8), cbr_review_scalar(`B`, 10)]) |
| 178 | } |
| 179 | for encoded in ['feff', 'fffe'] { |
| 180 | cbr_review_all_cuts(cbr_review_bytes(encoded), .utf16, .raise, false, []) |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | fn test_cbr_review_utf16_only_initial_bom_is_hidden_and_no_bom_defaults_big_endian() { |
| 185 | for encoded in ['fefffeff0041', 'fffefffe4100'] { |
| 186 | cbr_review_all_cuts(cbr_review_bytes(encoded), .utf16, .raise, false, |
| 187 | [cbr_review_scalar(0xfeff, 4), cbr_review_scalar(`A`, 6)]) |
| 188 | } |
| 189 | cbr_review_all_cuts(cbr_review_bytes('0041feff0042'), .utf16, .raise, false, |
| 190 | [cbr_review_scalar(`A`, 2), cbr_review_scalar(0xfeff, 4), cbr_review_scalar(`B`, 6)]) |
| 191 | } |
| 192 | |
| 193 | fn test_cbr_review_normalizer_none_and_every_enabled_eol_keep_raw_decode_separate() { |
| 194 | for ch in [rune(`\r`), `\n`, 0x85, 0x2028, `Z`] { |
| 195 | mut no_eol := TranscoderDecoder{ eol: .none } |
| 196 | out, produced := no_eol.normalize_scalar(ch) |
| 197 | assert produced && out == ch && !no_eol.pending_cr && !no_eol.has_queued |
| 198 | for eol in [EolStyle.lf, .cr, .crlf, .nel, .crnel, .ls] { |
| 199 | mut cursor := TranscoderDecoder{ eol: eol } |
| 200 | scalar, emitted := cursor.normalize_scalar(ch) |
| 201 | if ch == `\r` { |
| 202 | assert !emitted && cursor.pending_cr && !cursor.has_queued |
| 203 | } else { |
| 204 | assert emitted && !cursor.pending_cr && !cursor.has_queued |
| 205 | assert scalar == if ch == `Z` { rune(`Z`) } else { rune(`\n`) } |
| 206 | } |
| 207 | } |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | fn test_cbr_review_pending_cr_queues_only_non_lf_nel_successor_and_replays_without_raw_read() { |
| 212 | for ch in [rune(`\n`), 0x85, 0x2028, `\r`, `λ`, 0] { |
| 213 | mut cursor := TranscoderDecoder{ eol: .lf, mode: .raise } |
| 214 | _, first := cursor.normalize_scalar(`\r`) |
| 215 | assert !first && cursor.pending_cr |
| 216 | out, produced := cursor.normalize_scalar(ch) |
| 217 | assert produced && out == `\n` && !cursor.pending_cr |
| 218 | assert cursor.has_queued == (ch != `\n` && ch != 0x85) |
| 219 | mut empty := CodecInput{ final: true } |
| 220 | if cursor.has_queued { |
| 221 | assert cursor.queued == ch |
| 222 | expected := if ch == `λ` || ch == 0 { ch } else { rune(`\n`) } |
| 223 | assert (cursor.next(mut empty) or { panic(err.msg()) }).scalar == expected |
| 224 | assert empty.position == 0 && !cursor.has_queued && !cursor.pending_cr |
| 225 | } |
| 226 | assert (cursor.next(mut empty) or { panic(err.msg()) }).kind == .finished |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | fn test_cbr_review_full_next_cr_ignore_gap_and_non_lf_queue_show_physical_lookahead() { |
| 231 | cbr_review_all_cuts(cbr_review_bytes('0dffcebb42'), .utf8, .ignore, true, |
| 232 | [CbrReviewEvent{ kind: .scalar, scalar: `\n`, raw: 4, queued: `λ` }, |
| 233 | cbr_review_scalar(`λ`, 4), cbr_review_scalar(`B`, 5)]) |
| 234 | } |
| 235 | |
| 236 | fn test_cbr_review_full_next_cr_gap_lf_and_nel_modes_preserve_error_and_queue_order() { |
| 237 | for encoded in ['0dff0a42', '0dffc28542'] { |
| 238 | end := if encoded == '0dff0a42' { 3 } else { 4 } |
| 239 | bytes := cbr_review_bytes(encoded) |
| 240 | cbr_review_all_cuts(bytes, .utf8, .ignore, true, |
| 241 | [cbr_review_scalar(`\n`, end), cbr_review_scalar(`B`, end + 1)]) |
| 242 | cbr_review_all_cuts(bytes, .utf8, .replace, true, |
| 243 | [CbrReviewEvent{ kind: .scalar, scalar: `\n`, raw: 2, queued: 0xfffd }, |
| 244 | cbr_review_scalar(0xfffd, 2), cbr_review_scalar(`\n`, end), |
| 245 | cbr_review_scalar(`B`, end + 1)]) |
| 246 | cbr_review_all_cuts(bytes, .utf8, .raise, true, |
| 247 | [cbr_review_bad(.raise, 2, 0, true), cbr_review_scalar(`\n`, end), |
| 248 | cbr_review_scalar(`B`, end + 1)]) |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | fn test_cbr_review_queued_cr_chains_preserve_three_newlines_and_unicode_lookahead() { |
| 253 | cbr_review_all_cuts(cbr_review_bytes('0d0d0a0dcebb42'), .utf8, .raise, true, |
| 254 | [CbrReviewEvent{ kind: .scalar, scalar: `\n`, raw: 2, queued: `\r` }, |
| 255 | cbr_review_scalar(`\n`, 3), |
| 256 | CbrReviewEvent{ kind: .scalar, scalar: `\n`, raw: 6, queued: `λ` }, |
| 257 | cbr_review_scalar(`λ`, 6), cbr_review_scalar(`B`, 7)]) |
| 258 | } |
| 259 | |
| 260 | fn test_cbr_review_pending_cr_survives_ignored_bad_byte_and_split_utf8_nel_until_complete() { |
| 261 | mut cursor := TranscoderDecoder{ eol: .lf, mode: .ignore, decoder: CodecDecoder{ codec: .utf8 } } |
| 262 | mut first := codec_input(cbr_review_bytes('0dff'), false) |
| 263 | assert (cursor.next(mut first) or { panic(err.msg()) }).kind == .need_input |
| 264 | assert first.position == 2 && cursor.pending_cr && !cursor.has_queued |
| 265 | mut second := codec_input([u8(0xc2)], false) |
| 266 | assert (cursor.next(mut second) or { panic(err.msg()) }).kind == .need_input |
| 267 | assert second.position == 1 && cursor.pending_cr && cursor.decoder.count == 1 |
| 268 | mut last := codec_input([u8(0x85), `B`], true) |
| 269 | assert (cursor.next(mut last) or { panic(err.msg()) }).scalar == `\n` |
| 270 | assert last.position == 1 && !cursor.pending_cr && !cursor.has_queued |
| 271 | assert (cursor.next(mut last) or { panic(err.msg()) }).scalar == `B` |
| 272 | assert last.position == 2 |
| 273 | } |
| 274 | |
| 275 | fn test_cbr_review_utf16_codec_retained_word_and_normalizer_queue_are_independent() { |
| 276 | bytes := cbr_review_bytes('000dd8000041000a') |
| 277 | cbr_review_all_cuts(bytes, .utf16, .ignore, true, |
| 278 | [CbrReviewEvent{ kind: .scalar, scalar: `\n`, raw: 6, queued: `A` }, cbr_review_scalar(`A`, 6), |
| 279 | cbr_review_scalar(`\n`, 8)]) |
| 280 | cbr_review_all_cuts(bytes, .utf16, .replace, true, |
| 281 | [ |
| 282 | CbrReviewEvent{ kind: .scalar, scalar: `\n`, raw: 6, carry: 2, queued: 0xfffd }, |
| 283 | CbrReviewEvent{ kind: .scalar, scalar: 0xfffd, raw: 6, carry: 2 }, |
| 284 | cbr_review_scalar(`A`, 6), |
| 285 | cbr_review_scalar(`\n`, 8), |
| 286 | ]) |
| 287 | cbr_review_all_cuts(bytes, .utf16, .raise, true, |
| 288 | [cbr_review_bad(.raise, 6, 2, true), |
| 289 | CbrReviewEvent{ kind: .scalar, scalar: `\n`, raw: 6, queued: `A` }, |
| 290 | cbr_review_scalar(`A`, 6), cbr_review_scalar(`\n`, 8)]) |
| 291 | } |
| 292 | |
| 293 | fn test_cbr_review_raw_finished_does_not_flush_cr_but_full_next_does_once() { |
| 294 | mut cursor := TranscoderDecoder{ eol: .lf, mode: .raise } |
| 295 | _, produced := cursor.normalize_scalar(`\r`) |
| 296 | assert !produced && cursor.pending_cr |
| 297 | mut empty := CodecInput{ final: true } |
| 298 | assert (cursor.next_decoded(mut empty) or { panic(err.msg()) }).kind == .finished |
| 299 | assert cursor.pending_cr |
| 300 | assert (cursor.next(mut empty) or { panic(err.msg()) }).scalar == `\n` |
| 301 | assert !cursor.pending_cr && !cursor.has_queued && empty.position == 0 |
| 302 | assert (cursor.next(mut empty) or { panic(err.msg()) }).kind == .finished |
| 303 | } |
| 304 | |