| 1 | module scheme |
| 2 | |
| 3 | @[heap] |
| 4 | struct TranscodedOutput { |
| 5 | codec CodecKind |
| 6 | eol EolStyle |
| 7 | mode ErrorHandlingMode |
| 8 | mut: |
| 9 | little bool |
| 10 | raw OutputPort |
| 11 | pending []u8 |
| 12 | pending_offset int |
| 13 | } |
| 14 | |
| 15 | // Healthy writes borrow one stack chunk. Only an I/O failure needs owned retry |
| 16 | // bytes; the next flush/write sends that suffix before accepting new text. |
| 17 | fn (mut sink TranscodedOutput) send_chunk(bytes []u8) ! { |
| 18 | mut offset := 0 |
| 19 | for offset < bytes.len { |
| 20 | count := sink.raw.sink.write(unsafe { bytes[offset..] }) or { |
| 21 | sink.save_pending(bytes, offset) |
| 22 | return err |
| 23 | } |
| 24 | if count <= 0 || count > bytes.len - offset { |
| 25 | sink.save_pending(bytes, offset) |
| 26 | return failure('writer returned an invalid byte count') |
| 27 | } |
| 28 | offset += count |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | fn (mut sink TranscodedOutput) save_pending(bytes []u8, start int) { |
| 33 | length := bytes.len - start |
| 34 | sink.pending.ensure_cap(length) |
| 35 | unsafe { |
| 36 | sink.pending.len = length |
| 37 | sink.pending.flags.set(.noslices) |
| 38 | vmemcpy(sink.pending.data, &u8(bytes.data) + start, length) |
| 39 | } |
| 40 | sink.pending_offset = 0 |
| 41 | } |
| 42 | |
| 43 | fn (mut sink TranscodedOutput) flush_pending() ! { |
| 44 | for sink.pending_offset < sink.pending.len { |
| 45 | count := sink.raw.sink.write(unsafe { sink.pending[sink.pending_offset..] })! |
| 46 | if count <= 0 || count > sink.pending.len - sink.pending_offset { |
| 47 | return failure('writer returned an invalid byte count') |
| 48 | } |
| 49 | sink.pending_offset += count |
| 50 | } |
| 51 | unsafe { sink.pending.len = 0 } |
| 52 | sink.pending_offset = 0 |
| 53 | } |
| 54 | |
| 55 | fn (mut sink TranscodedOutput) write(bytes []u8) !int { |
| 56 | sink.flush_pending()! |
| 57 | if bytes.len == 0 { return 0 } |
| 58 | text := unsafe { tos(&u8(bytes.data), bytes.len) } |
| 59 | mut cursor := TranscoderEncoder{ text: text, codec: sink.codec, eol: sink.eol, mode: sink.mode } |
| 60 | mut buffer := [4096]u8{} |
| 61 | mut encoded := unsafe { (&buffer[0]).vbytes(buffer.len) } |
| 62 | mut used := 0 |
| 63 | for { |
| 64 | if used > buffer.len - 4 { |
| 65 | sink.send_chunk(unsafe { (&buffer[0]).vbytes(used) })! |
| 66 | used = 0 |
| 67 | } |
| 68 | step := cursor.next() or { |
| 69 | if used > 0 { sink.send_chunk(unsafe { (&buffer[0]).vbytes(used) })! } |
| 70 | sink.flush_line_prefix(text, cursor.offset)! |
| 71 | return err |
| 72 | } |
| 73 | if step.kind == .finished { break } |
| 74 | match sink.codec { |
| 75 | .latin1 { |
| 76 | buffer[used] = u8(step.scalar) |
| 77 | used++ |
| 78 | } |
| 79 | .utf8 { |
| 80 | used += unsafe { utf32_decode_to_buffer(u32(step.scalar), mut &buffer[used]) } |
| 81 | } |
| 82 | .utf16 { used += bytevector_put_utf16(mut encoded, used, sink.little, step.scalar) } |
| 83 | } |
| 84 | } |
| 85 | if used > 0 { sink.send_chunk(unsafe { (&buffer[0]).vbytes(used) })! } |
| 86 | return bytes.len |
| 87 | } |
| 88 | |
| 89 | fn (mut sink TranscodedOutput) flush_line_prefix(text string, end int) ! { |
| 90 | if sink.raw.buffer_mode == .line && text_view(text, 0, end).index_u8(`\n`) >= 0 { |
| 91 | sink.flush()! |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | fn (mut sink TranscodedOutput) flush() ! { |
| 96 | sink.flush_pending()! |
| 97 | if mut sink.raw.sink is OutputFlusher { |
| 98 | mut target := sink.raw.sink |
| 99 | target.flush()! |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | fn (mut sink TranscodedOutput) close_original() ! { |
| 104 | if sink.raw.closed { return } |
| 105 | sink.raw.closed = true |
| 106 | defer { |
| 107 | // Owned file close still releases its descriptor after a flush failure. |
| 108 | if sink.raw.owns_file { sink.raw.stream.close() or {} } |
| 109 | sink.raw.sink = &closed_port |
| 110 | sink.pending = []u8{} |
| 111 | } |
| 112 | sink.flush()! |
| 113 | } |
| 114 | |