| 1 | module scheme |
| 2 | |
| 3 | fn (mut source CustomTranscodedInput) decode_available_positioned(value Value) !bool { |
| 4 | mut port := value.input_pointer() |
| 5 | if port.position < port.buffer.len || port.ended { return true } |
| 6 | commit_custom_text_cursor(value) |
| 7 | port.buffer = []u8{} |
| 8 | port.position = 0 |
| 9 | if source.decoded.len == 0 { |
| 10 | source.decoded = []u8{len: int_max(4, int_min(4096, port.read_size))} |
| 11 | } |
| 12 | mut position := source.position |
| 13 | if position.transmitting { |
| 14 | // Writes retain no codec provenance. Seed it only when input resumes, |
| 15 | // using the latest acknowledged boundary rather than the first packet. |
| 16 | position.track.begin(position.history.raw_end, 0)! |
| 17 | position.transmitting = false |
| 18 | } |
| 19 | mut base := position.history.raw_end |
| 20 | held := source.cursor.decoder.count > 0 || source.cursor.pending_cr || source.cursor.has_queued |
| 21 | // A consumed ignored suffix is not a visible boundary until EOF/error is |
| 22 | // observed. A fresh, gap-free publication may begin at a different origin. |
| 23 | if !held && base.same(position.track.frontier()) { |
| 24 | base = CustomRawPoint{ ...position.raw_origin, offset: position.raw_origin.offset + source.raw.position } |
| 25 | } |
| 26 | position.history.reset(base) |
| 27 | position.decoded_base = 0 |
| 28 | mut produced := 0 |
| 29 | for !source.finished && produced <= source.decoded.len - 4 { |
| 30 | start := CustomRawPoint{ ...position.raw_origin, offset: position.raw_origin.offset + source.raw.position } |
| 31 | position.track.begin(start, source.cursor.decoder.count)! |
| 32 | copied := copy_transcoded_ascii(mut source.raw, &source.cursor, mut source.decoded, produced) |
| 33 | if copied > 0 { |
| 34 | end := position.track.observe(0, copied, 0) |
| 35 | position.track.scalar_end = end |
| 36 | if !position.history.raw_end.same(start) { |
| 37 | position.history.append(1, CustomRawPoint{ ...start, offset: start.offset + 1 }) |
| 38 | if copied > 1 { position.history.append(copied - 1, end) } |
| 39 | } else { |
| 40 | position.history.append(copied, end) |
| 41 | } |
| 42 | produced += copied |
| 43 | continue |
| 44 | } |
| 45 | previous := source.cursor |
| 46 | previous_track := position.track |
| 47 | source.step_input = codec_input(unsafe { source.raw.buffer[source.raw.position..] }, source.raw.ended) |
| 48 | step := position.track.next(mut source.cursor, mut source.step_input) or { |
| 49 | if produced > 0 { |
| 50 | source.cursor = previous |
| 51 | position.track = previous_track |
| 52 | source.step_input = CodecInput{} |
| 53 | break |
| 54 | } |
| 55 | source.raw.position += source.step_input.position |
| 56 | source.step_input = CodecInput{} |
| 57 | header := position.track.take_header() |
| 58 | if header.valid { position.history.commit(header) } |
| 59 | position.history.commit(position.track.scalar_end) |
| 60 | return err |
| 61 | } |
| 62 | source.raw.position += source.step_input.position |
| 63 | source.step_input = CodecInput{} |
| 64 | header := position.track.take_header() |
| 65 | if header.valid { position.history.commit(header) } |
| 66 | match step.kind { |
| 67 | .scalar { |
| 68 | width := step.scalar.length_in_bytes() |
| 69 | position.history.append(width, position.track.scalar_end) |
| 70 | produced += unsafe { utf32_decode_to_buffer(u32(step.scalar), mut &source.decoded[produced]) } |
| 71 | } |
| 72 | .need_input { break } |
| 73 | .finished { source.finished = true } |
| 74 | .malformed { return failure('unhandled transcoder decoding step') } |
| 75 | } |
| 76 | } |
| 77 | port.ended = source.finished |
| 78 | port.buffer = unsafe { source.decoded[..produced] } |
| 79 | return produced > 0 || source.finished |
| 80 | } |
| 81 | |
| 82 | fn (state &CustomPortOperation) attached_probe_positions() &CustomAttachedProbePositions { |
| 83 | if !positioned_custom_attached(state.resource) || isnil(state.position_cursor) { |
| 84 | return unsafe { nil } |
| 85 | } |
| 86 | return state.attached_operation().probe |
| 87 | } |
| 88 | |
| 89 | fn (mut state CustomPortOperation) remember_custom_attached_probe(index int) ! { |
| 90 | point := custom_attached_visible(state.target)! |
| 91 | mut operation := state.attached_operation_state() |
| 92 | if isnil(operation.probe) { operation.probe = &CustomAttachedProbePositions{} } |
| 93 | operation.probe.points[index] = point |
| 94 | } |
| 95 | |
| 96 | fn prepend_custom_attached_coordinates(resource &CustomPortResource, positions &CustomAttachedProbePositions, length int, remaining int) { |
| 97 | if !positioned_custom_attached(resource) { return } |
| 98 | if length + remaining > 4 { panic('invalid attached replay prefix') } |
| 99 | mut position := resource.input_adapter.position |
| 100 | mut prefix := [4]CustomRawPoint{} |
| 101 | if !isnil(positions) { |
| 102 | for i in 0 .. length { prefix[i] = positions.points[i] } |
| 103 | } |
| 104 | for i in 0 .. remaining { prefix[length + i] = position.prefix[i] } |
| 105 | position.prefix = prefix |
| 106 | position.prefix_len = length + remaining |
| 107 | } |
| 108 | |