module scheme // The endpoint after each of the last four owned codec bytes, plus the point // before them. Named inline slots avoid mutable fixed-array promotion in V. // Ring updates retain bounded provenance across noncontiguous raw publications. struct CustomRawTrail { mut: a CustomRawPoint b CustomRawPoint c CustomRawPoint d CustomRawPoint e CustomRawPoint next int len int } @[inline] fn (mut trail CustomRawTrail) put(index int, point CustomRawPoint) { match index { 0 { trail.a = point } 1 { trail.b = point } 2 { trail.c = point } 3 { trail.d = point } 4 { trail.e = point } else { panic('invalid raw provenance slot') } } } @[inline] fn (mut trail CustomRawTrail) push(point CustomRawPoint) { trail.put(trail.next, point) trail.next = (trail.next + 1) % 5 if trail.len < 5 { trail.len++ } } @[inline] fn (trail &CustomRawTrail) back(count int) CustomRawPoint { if count < 0 || count >= trail.len { panic('missing raw codec provenance') } index := (trail.next + 4 - count) % 5 return match index { 0 { trail.a } 1 { trail.b } 2 { trail.c } 3 { trail.d } else { trail.e } } } fn (mut trail CustomRawTrail) retain(keep int) { for count in keep .. trail.len { trail.put((trail.next + 4 - count) % 5, CustomRawPoint{}) } trail.len = keep } struct CustomRawBoundaryDecoder { mut: publication CustomRawPoint trail CustomRawTrail scalar_end CustomRawPoint cr_end CustomRawPoint queued_end CustomRawPoint header_end CustomRawPoint header_pending bool } // The publication starts at the beginning of this CodecInput view, which may // itself begin partway through an owned callback buffer. Owned codec bytes keep // their earlier endpoints; the new publication never relabels their origins. fn (mut track CustomRawBoundaryDecoder) begin(origin CustomRawPoint, owned int) ! { if !origin.valid || owned < 0 || owned > 4 { return failure('invalid raw decoder publication') } if owned == 0 { track.trail = CustomRawTrail{} track.trail.push(origin) } else { if track.trail.len < owned + 1 { return failure('raw decoder publication lost codec carry') } track.trail.retain(owned + 1) } track.publication = origin if !track.scalar_end.valid { track.scalar_end = origin } } // There are at most four codec-owned bytes. For a long consumed run only its // final five endpoints matter; there is no coordinate counter for port lifetime. @[inline] fn (mut track CustomRawBoundaryDecoder) observe(before int, after int, owned int) CustomRawPoint { if after > before { for offset in int_max(before + 1, after - 4) .. after + 1 { track.trail.push(CustomRawPoint{ ...track.publication, offset: track.publication.offset + offset }) } } point := track.trail.back(owned) track.trail.retain(owned + 1) return point } // An error is later in stream-consumption order than the held CR even if a // replayed source coordinate is numerically smaller. No big arithmetic here. @[inline] fn (mut track CustomRawBoundaryDecoder) commit_cr_error() { track.cr_end = track.scalar_end } @[inline] fn (mut track CustomRawBoundaryDecoder) queue_taken() { track.queued_end = CustomRawPoint{} } @[inline] fn (mut track CustomRawBoundaryDecoder) cr_taken() { track.cr_end = CustomRawPoint{} } // An observed UTF16 BOM commits its end before the first visible character. // Fill/header recognition use the actual shared codec helpers. Ordinary codecs // keep their existing structs and do not carry this bookkeeping. fn (mut track CustomRawBoundaryDecoder) prepare_header(mut cursor TranscoderDecoder, mut input CodecInput) { if cursor.decoder.codec != .utf16 || cursor.decoder.started { return } before := input.position complete := cursor.decoder.fill(mut input, 2) header := complete && cursor.decoder.accept_utf16_header() point := track.observe(before, input.position, cursor.decoder.count) if header { track.header_end = point track.header_pending = true track.scalar_end = point } } fn (mut track CustomRawBoundaryDecoder) take_header() CustomRawPoint { if !track.header_pending { return CustomRawPoint{} } point := track.header_end track.header_end = CustomRawPoint{} track.header_pending = false return point } fn (track &CustomRawBoundaryDecoder) frontier() CustomRawPoint { return track.trail.back(0) } fn (mut track CustomRawBoundaryDecoder) next(mut cursor TranscoderDecoder, mut input CodecInput) !CodecStep { return next_transcoded_boundary(mut track, mut cursor, mut input) }