module scheme // A span stores an affine run of UTF8-byte / physical-byte boundaries. Valid // UTF8 is one 1:1 run regardless of character widths; UTF16 ASCII is 1:2. // Irregular ignored gaps need a single scalar endpoint, never retained bytes. struct TranscodedBoundarySpan[T] { mut: decoded_end i64 raw_end T decoded_step int raw_step int } struct TranscodedBoundaries[T] { mut: spans []TranscodedBoundarySpan[T] head int base_decoded i64 base_raw T decoded_end i64 raw_end T } fn (mut history TranscodedBoundaries[T]) reset(raw T) { history.clear_retired(0, history.spans.len) unsafe { history.spans.len = 0 } history.head = 0 history.base_decoded = 0 history.base_raw = raw history.decoded_end = 0 history.raw_end = raw } fn (mut history TranscodedBoundaries[T]) append(width int, raw T) { decoded := history.decoded_end + width delta := transcoded_raw_delta(raw, history.raw_end) // A single scalar can follow arbitrarily many ignored malformed bytes. // Such a gap only needs its endpoint; it need not fit an int stride. mut raw_step := if delta >= 0 && delta <= max_int { int(delta) } else { -1 } mut decoded_step := width if raw_step >= 0 { mut a, mut b := decoded_step, raw_step for b != 0 { a, b = b, a % b } decoded_step /= a raw_step /= a } if history.spans.len > history.head && raw_step >= 0 { mut last := &history.spans[history.spans.len - 1] if transcoded_raw_same(last.raw_end, history.raw_end) && last.raw_step >= 0 && last.raw_step == raw_step && last.decoded_step == decoded_step { last.decoded_end = decoded last.raw_end = raw history.decoded_end = decoded history.raw_end = raw return } } history.compact_if_full() history.spans << TranscodedBoundarySpan[T]{ decoded_end: decoded, raw_end: raw, decoded_step: decoded_step, raw_step: raw_step } history.decoded_end = decoded history.raw_end = raw } fn (mut history TranscodedBoundaries[T]) compact_if_full() { if history.spans.len == history.spans.cap && history.head > 0 { old_len := history.spans.len remaining := history.spans.len - history.head unsafe { vmemmove(history.spans.data, &history.spans[history.head], remaining * int(sizeof(TranscodedBoundarySpan[T]))) } history.clear_retired(remaining, old_len) unsafe { history.spans.len = remaining } history.head = 0 } } // The custom specialization contains GC roots. Array capacity remains scanned // after a logical truncate/move, so retire roots before dropping their slots. // Native i64 spans have no roots and compile without this clearing loop. @[inline] fn (mut history TranscodedBoundaries[T]) clear_retired(start int, end int) { $if T is CustomRawPoint { for index in start .. end { history.spans[index] = TranscodedBoundarySpan[T]{} } } } // A raised malformed unit or observed EOF can move the current boundary // without publishing text. Do not stretch the preceding affine run: its older // character boundaries still describe the already-published unread bytes. fn (mut history TranscodedBoundaries[T]) commit(raw T) { if transcoded_raw_same(raw, history.raw_end) { return } history.raw_end = raw if history.decoded_end == history.base_decoded { history.base_raw = raw return } if history.spans.len > history.head && history.spans[history.spans.len - 1].decoded_step == 0 { history.spans[history.spans.len - 1].raw_end = raw return } history.compact_if_full() history.spans << TranscodedBoundarySpan[T]{ decoded_end: history.decoded_end, raw_end: raw, raw_step: -1 } } fn (history &TranscodedBoundaries[T]) at(decoded i64) !T { if decoded < history.base_decoded || decoded > history.decoded_end { return failure('transcoded input boundary is outside retained history') } if decoded == history.decoded_end { return history.raw_end } if decoded == history.base_decoded { return history.base_raw } for index := history.head; index < history.spans.len; index++ { span := history.spans[index] if decoded > span.decoded_end { continue } if decoded == span.decoded_end { mut last := index for last + 1 < history.spans.len && history.spans[last + 1].decoded_end == decoded { last++ } return history.spans[last].raw_end } distance := span.decoded_end - decoded if span.raw_step < 0 || distance % span.decoded_step != 0 { return failure('transcoded input cursor is inside a character') } return transcoded_raw_retreat(span.raw_end, distance / span.decoded_step * span.raw_step) } return failure('transcoded input boundary is missing') } // Framing can temporarily consume just the first UTF8 byte of a scalar // before refilling. Prune complete spans only; retain the covering span rather // than inventing a physical position inside that character. A very long affine // span still costs one record, even when most of its text is already consumed. fn (mut history TranscodedBoundaries[T]) prune(decoded i64) ! { if decoded < history.base_decoded || decoded > history.decoded_end { return failure('transcoded input boundary is outside retained history') } for history.head < history.spans.len && history.spans[history.head].decoded_end <= decoded { span := history.spans[history.head] history.base_decoded = span.decoded_end history.base_raw = span.raw_end history.clear_retired(history.head, history.head + 1) history.head++ } if history.head == history.spans.len { unsafe { history.spans.len = 0 } history.head = 0 history.base_decoded = history.decoded_end history.base_raw = history.raw_end } } // Compile-time coordinate algebra keeps native files on their existing i64 // representation and shares the span algorithm with exact custom raw points. @[inline] fn transcoded_raw_same[T](a T, b T) bool { $if T is i64 { return a == b } $else { return a.same(b) } } @[inline] fn transcoded_raw_delta[T](a T, b T) i64 { $if T is i64 { return a - b } $else { return a.delta(b) } } @[inline] fn transcoded_raw_retreat[T](a T, distance i64) !T { $if T is i64 { return a - distance } $else { return a.retreat(distance) } } // Physical positions exclude the codec's owned lookahead prefix. In UTF16 a // malformed high surrogate may have prefetched a complete valid following word. // Newline folding likewise owns a CR boundary and one queued scalar boundary. struct TranscodedBoundaryDecoder { mut: consumed i64 scalar_end i64 cr_end i64 queued_end i64 } fn (mut track TranscodedBoundaryDecoder) next(mut cursor TranscoderDecoder, mut input CodecInput) !CodecStep { return next_transcoded_boundary(mut track, mut cursor, mut input) } @[inline] fn (mut track TranscodedBoundaryDecoder) observe(before int, after int, owned int) i64 { track.consumed += after - before return track.consumed - owned } @[inline] fn (mut track TranscodedBoundaryDecoder) commit_cr_error() { if track.cr_end < track.scalar_end { track.cr_end = track.scalar_end } } @[inline] fn (mut _track TranscodedBoundaryDecoder) queue_taken() {} @[inline] fn (mut _track TranscodedBoundaryDecoder) cr_taken() {} // Both coordinate policies share the actual codec, error policy and newline // state machine. Only observing endpoints and retiring their roots specialize. fn next_transcoded_boundary[T](mut track T, mut cursor TranscoderDecoder, mut input CodecInput) !CodecStep { $if T is CustomRawBoundaryDecoder { track.prepare_header(mut cursor, mut input) } for { mut ch := rune(0) mut end := track.scalar_end if cursor.has_queued { ch = cursor.queued end = track.queued_end cursor.has_queued = false track.queue_taken() } else { before := input.position step := cursor.next_decoded(mut input) or { track.scalar_end = track.observe(before, input.position, cursor.decoder.count) // Observed errors commit their frontier even when an older CR // is delivered by a later retry of the same decoder. if cursor.pending_cr { track.commit_cr_error() } return err } end = track.observe(before, input.position, cursor.decoder.count) match step.kind { .need_input { return step } .finished { if cursor.pending_cr { cursor.pending_cr = false track.scalar_end = track.cr_end track.cr_taken() return CodecStep{ kind: .scalar, scalar: `\n` } } return step } .malformed { continue } .scalar { ch = step.scalar } } } was_cr := cursor.pending_cr scalar, produced := cursor.normalize_scalar(ch) if !produced { track.cr_end = end continue } if was_cr && cursor.has_queued { track.queued_end = end track.scalar_end = track.cr_end } else { track.scalar_end = end } if was_cr { track.cr_taken() } return CodecStep{ kind: .scalar, scalar: scalar } } return CodecStep{ kind: .finished } }