| 1 | module scheme |
| 2 | |
| 3 | import io |
| 4 | |
| 5 | @[heap] |
| 6 | struct TranscodedInput { |
| 7 | mut: |
| 8 | raw InputPort |
| 9 | cursor TranscoderDecoder |
| 10 | pending u32 |
| 11 | pending_count int |
| 12 | finished bool |
| 13 | step_input CodecInput |
| 14 | } |
| 15 | |
| 16 | fn (mut source TranscodedInput) read(mut bytes []u8) !int { |
| 17 | return source.read_impl(mut bytes, false) |
| 18 | } |
| 19 | |
| 20 | fn (mut source TranscodedInput) read_ready(mut bytes []u8) !int { |
| 21 | return source.read_impl(mut bytes, true) |
| 22 | } |
| 23 | |
| 24 | // Complete ASCII spans bypass scalar decode/encode. Newline normalization only |
| 25 | // needs to stop at CR; LF already has its normalized representation. |
| 26 | @[inline] |
| 27 | fn (mut source TranscodedInput) copy_ascii(mut destination []u8, start int) int { |
| 28 | return copy_transcoded_ascii(mut source.raw, &source.cursor, mut destination, start) |
| 29 | } |
| 30 | |
| 31 | @[inline] |
| 32 | fn copy_transcoded_ascii(mut raw InputPort, cursor &TranscoderDecoder, mut destination []u8, start int) int { |
| 33 | if cursor.decoder.codec == .utf16 || cursor.decoder.count != 0 || cursor.pending_cr || cursor.has_queued { |
| 34 | return 0 |
| 35 | } |
| 36 | mut count := 0 |
| 37 | limit := int_min(destination.len - start, raw.buffer.len - raw.position) |
| 38 | for count < limit { |
| 39 | byte := raw.buffer[raw.position + count] |
| 40 | if byte >= 128 || (byte == `\r` && cursor.eol != .none) { break } |
| 41 | destination[start + count] = byte |
| 42 | count++ |
| 43 | } |
| 44 | raw.position += count |
| 45 | return count |
| 46 | } |
| 47 | |
| 48 | // No input view survives a refill. Prefix state owns at most four codec bytes |
| 49 | // and four UTF8 result bytes. Returning an available prefix avoids blocking just |
| 50 | // to fill the facade's buffer, including when a CR or multibyte unit is pending. |
| 51 | fn (mut source TranscodedInput) read_impl(mut destination []u8, nonblocking bool) !int { |
| 52 | if destination.len == 0 { return 0 } |
| 53 | mut produced := 0 |
| 54 | for produced < destination.len { |
| 55 | if source.pending_count > 0 { |
| 56 | count := int_min(source.pending_count, destination.len - produced) |
| 57 | unsafe { vmemcpy(&u8(destination.data) + produced, &source.pending, count) } |
| 58 | source.pending_count -= count |
| 59 | if source.pending_count > 0 { |
| 60 | unsafe { vmemmove(&source.pending, &u8(&source.pending) + count, source.pending_count) } |
| 61 | } |
| 62 | produced += count |
| 63 | } |
| 64 | if produced == destination.len || source.finished { break } |
| 65 | copied := source.copy_ascii(mut destination, produced) |
| 66 | if copied > 0 { |
| 67 | produced += copied |
| 68 | continue |
| 69 | } |
| 70 | // A decoder can consume a malformed prefix before raising. If valid |
| 71 | // output precedes it, restore this scalar checkpoint and publish that |
| 72 | // output first. The error object itself is never retained in the port. |
| 73 | previous := source.cursor |
| 74 | source.step_input = codec_input(unsafe { source.raw.buffer[source.raw.position..] }, source.raw.ended) |
| 75 | step := source.cursor.next(mut source.step_input) or { |
| 76 | if produced > 0 { |
| 77 | source.cursor = previous |
| 78 | source.step_input = CodecInput{} |
| 79 | return produced |
| 80 | } |
| 81 | source.raw.position += source.step_input.position |
| 82 | source.step_input = CodecInput{} |
| 83 | return err |
| 84 | } |
| 85 | source.raw.position += source.step_input.position |
| 86 | source.step_input = CodecInput{} |
| 87 | match step.kind { |
| 88 | .scalar { |
| 89 | if destination.len - produced >= step.scalar.length_in_bytes() { |
| 90 | produced += unsafe { utf32_decode_to_buffer(u32(step.scalar), mut &destination[produced]) } |
| 91 | } else { |
| 92 | // Encode directly into owned scalar storage. A temporary fixed |
| 93 | // array is heap-promoted by V when its address crosses this call. |
| 94 | source.pending = 0 |
| 95 | source.pending_count = unsafe { utf32_decode_to_buffer(u32(step.scalar), mut &u8(&source.pending)) } |
| 96 | } |
| 97 | } |
| 98 | .need_input { |
| 99 | if produced > 0 { return produced } |
| 100 | if nonblocking { |
| 101 | if !source.raw.fill_buffer(true)! { return -1 } |
| 102 | } else { |
| 103 | source.raw.ensure_bytes(1)! |
| 104 | } |
| 105 | } |
| 106 | .finished { source.finished = true } |
| 107 | .malformed { return failure('unhandled transcoder decoding step') } |
| 108 | } |
| 109 | } |
| 110 | if produced == 0 && source.finished { return io.Eof{} } |
| 111 | return produced |
| 112 | } |
| 113 | |
| 114 | fn (mut source TranscodedInput) close_original() ! { |
| 115 | if source.raw.closed { return } |
| 116 | source.raw.closed = true |
| 117 | defer { |
| 118 | source.raw.buffer = []u8{} |
| 119 | source.raw.source = &closed_port |
| 120 | source.raw.location = unsafe { nil } |
| 121 | } |
| 122 | if source.raw.owns_file { source.raw.stream.close()! } |
| 123 | } |
| 124 | |