module scheme import io @[heap] struct TranscodedInput { mut: raw InputPort cursor TranscoderDecoder pending u32 pending_count int finished bool step_input CodecInput } fn (mut source TranscodedInput) read(mut bytes []u8) !int { return source.read_impl(mut bytes, false) } fn (mut source TranscodedInput) read_ready(mut bytes []u8) !int { return source.read_impl(mut bytes, true) } // Complete ASCII spans bypass scalar decode/encode. Newline normalization only // needs to stop at CR; LF already has its normalized representation. @[inline] fn (mut source TranscodedInput) copy_ascii(mut destination []u8, start int) int { return copy_transcoded_ascii(mut source.raw, &source.cursor, mut destination, start) } @[inline] fn copy_transcoded_ascii(mut raw InputPort, cursor &TranscoderDecoder, mut destination []u8, start int) int { if cursor.decoder.codec == .utf16 || cursor.decoder.count != 0 || cursor.pending_cr || cursor.has_queued { return 0 } mut count := 0 limit := int_min(destination.len - start, raw.buffer.len - raw.position) for count < limit { byte := raw.buffer[raw.position + count] if byte >= 128 || (byte == `\r` && cursor.eol != .none) { break } destination[start + count] = byte count++ } raw.position += count return count } // No input view survives a refill. Prefix state owns at most four codec bytes // and four UTF8 result bytes. Returning an available prefix avoids blocking just // to fill the facade's buffer, including when a CR or multibyte unit is pending. fn (mut source TranscodedInput) read_impl(mut destination []u8, nonblocking bool) !int { if destination.len == 0 { return 0 } mut produced := 0 for produced < destination.len { if source.pending_count > 0 { count := int_min(source.pending_count, destination.len - produced) unsafe { vmemcpy(&u8(destination.data) + produced, &source.pending, count) } source.pending_count -= count if source.pending_count > 0 { unsafe { vmemmove(&source.pending, &u8(&source.pending) + count, source.pending_count) } } produced += count } if produced == destination.len || source.finished { break } copied := source.copy_ascii(mut destination, produced) if copied > 0 { produced += copied continue } // A decoder can consume a malformed prefix before raising. If valid // output precedes it, restore this scalar checkpoint and publish that // output first. The error object itself is never retained in the port. previous := source.cursor source.step_input = codec_input(unsafe { source.raw.buffer[source.raw.position..] }, source.raw.ended) step := source.cursor.next(mut source.step_input) or { if produced > 0 { source.cursor = previous source.step_input = CodecInput{} return produced } source.raw.position += source.step_input.position source.step_input = CodecInput{} return err } source.raw.position += source.step_input.position source.step_input = CodecInput{} match step.kind { .scalar { if destination.len - produced >= step.scalar.length_in_bytes() { produced += unsafe { utf32_decode_to_buffer(u32(step.scalar), mut &destination[produced]) } } else { // Encode directly into owned scalar storage. A temporary fixed // array is heap-promoted by V when its address crosses this call. source.pending = 0 source.pending_count = unsafe { utf32_decode_to_buffer(u32(step.scalar), mut &u8(&source.pending)) } } } .need_input { if produced > 0 { return produced } if nonblocking { if !source.raw.fill_buffer(true)! { return -1 } } else { source.raw.ensure_bytes(1)! } } .finished { source.finished = true } .malformed { return failure('unhandled transcoder decoding step') } } } if produced == 0 && source.finished { return io.Eof{} } return produced } fn (mut source TranscodedInput) close_original() ! { if source.raw.closed { return } source.raw.closed = true defer { source.raw.buffer = []u8{} source.raw.source = &closed_port source.raw.location = unsafe { nil } } if source.raw.owns_file { source.raw.stream.close()! } }