vile / scheme / port_transcoder_boundaries.v
269 lines · 250 sloc · 8.57 KB · 76646891b77a3d5c6af4a1e42b122cd61dc1654b
Raw
1module scheme
2
3// A span stores an affine run of UTF8-byte / physical-byte boundaries. Valid
4// UTF8 is one 1:1 run regardless of character widths; UTF16 ASCII is 1:2.
5// Irregular ignored gaps need a single scalar endpoint, never retained bytes.
6struct TranscodedBoundarySpan[T] {
7mut:
8 decoded_end i64
9 raw_end T
10 decoded_step int
11 raw_step int
12}
13
14struct TranscodedBoundaries[T] {
15mut:
16 spans []TranscodedBoundarySpan[T]
17 head int
18 base_decoded i64
19 base_raw T
20 decoded_end i64
21 raw_end T
22}
23
24fn (mut history TranscodedBoundaries[T]) reset(raw T) {
25 history.clear_retired(0, history.spans.len)
26 unsafe { history.spans.len = 0 }
27 history.head = 0
28 history.base_decoded = 0
29 history.base_raw = raw
30 history.decoded_end = 0
31 history.raw_end = raw
32}
33
34fn (mut history TranscodedBoundaries[T]) append(width int, raw T) {
35 decoded := history.decoded_end + width
36 delta := transcoded_raw_delta(raw, history.raw_end)
37 // A single scalar can follow arbitrarily many ignored malformed bytes.
38 // Such a gap only needs its endpoint; it need not fit an int stride.
39 mut raw_step := if delta >= 0 && delta <= max_int { int(delta) } else { -1 }
40 mut decoded_step := width
41 if raw_step >= 0 {
42 mut a, mut b := decoded_step, raw_step
43 for b != 0 { a, b = b, a % b }
44 decoded_step /= a
45 raw_step /= a
46 }
47 if history.spans.len > history.head && raw_step >= 0 {
48 mut last := &history.spans[history.spans.len - 1]
49 if transcoded_raw_same(last.raw_end, history.raw_end) && last.raw_step >= 0
50 && last.raw_step == raw_step && last.decoded_step == decoded_step {
51 last.decoded_end = decoded
52 last.raw_end = raw
53 history.decoded_end = decoded
54 history.raw_end = raw
55 return
56 }
57 }
58 history.compact_if_full()
59 history.spans << TranscodedBoundarySpan[T]{ decoded_end: decoded, raw_end: raw, decoded_step: decoded_step, raw_step: raw_step }
60 history.decoded_end = decoded
61 history.raw_end = raw
62}
63
64fn (mut history TranscodedBoundaries[T]) compact_if_full() {
65 if history.spans.len == history.spans.cap && history.head > 0 {
66 old_len := history.spans.len
67 remaining := history.spans.len - history.head
68 unsafe {
69 vmemmove(history.spans.data, &history.spans[history.head], remaining * int(sizeof(TranscodedBoundarySpan[T])))
70 }
71 history.clear_retired(remaining, old_len)
72 unsafe { history.spans.len = remaining }
73 history.head = 0
74 }
75}
76
77// The custom specialization contains GC roots. Array capacity remains scanned
78// after a logical truncate/move, so retire roots before dropping their slots.
79// Native i64 spans have no roots and compile without this clearing loop.
80@[inline]
81fn (mut history TranscodedBoundaries[T]) clear_retired(start int, end int) {
82 $if T is CustomRawPoint {
83 for index in start .. end { history.spans[index] = TranscodedBoundarySpan[T]{} }
84 }
85}
86
87// A raised malformed unit or observed EOF can move the current boundary
88// without publishing text. Do not stretch the preceding affine run: its older
89// character boundaries still describe the already-published unread bytes.
90fn (mut history TranscodedBoundaries[T]) commit(raw T) {
91 if transcoded_raw_same(raw, history.raw_end) { return }
92 history.raw_end = raw
93 if history.decoded_end == history.base_decoded {
94 history.base_raw = raw
95 return
96 }
97 if history.spans.len > history.head && history.spans[history.spans.len - 1].decoded_step == 0 {
98 history.spans[history.spans.len - 1].raw_end = raw
99 return
100 }
101 history.compact_if_full()
102 history.spans << TranscodedBoundarySpan[T]{ decoded_end: history.decoded_end, raw_end: raw, raw_step: -1 }
103}
104
105fn (history &TranscodedBoundaries[T]) at(decoded i64) !T {
106 if decoded < history.base_decoded || decoded > history.decoded_end {
107 return failure('transcoded input boundary is outside retained history')
108 }
109 if decoded == history.decoded_end { return history.raw_end }
110 if decoded == history.base_decoded { return history.base_raw }
111 for index := history.head; index < history.spans.len; index++ {
112 span := history.spans[index]
113 if decoded > span.decoded_end { continue }
114 if decoded == span.decoded_end {
115 mut last := index
116 for last + 1 < history.spans.len && history.spans[last + 1].decoded_end == decoded {
117 last++
118 }
119 return history.spans[last].raw_end
120 }
121 distance := span.decoded_end - decoded
122 if span.raw_step < 0 || distance % span.decoded_step != 0 {
123 return failure('transcoded input cursor is inside a character')
124 }
125 return transcoded_raw_retreat(span.raw_end, distance / span.decoded_step * span.raw_step)
126 }
127 return failure('transcoded input boundary is missing')
128}
129
130// Framing can temporarily consume just the first UTF8 byte of a scalar
131// before refilling. Prune complete spans only; retain the covering span rather
132// than inventing a physical position inside that character. A very long affine
133// span still costs one record, even when most of its text is already consumed.
134fn (mut history TranscodedBoundaries[T]) prune(decoded i64) ! {
135 if decoded < history.base_decoded || decoded > history.decoded_end {
136 return failure('transcoded input boundary is outside retained history')
137 }
138 for history.head < history.spans.len && history.spans[history.head].decoded_end <= decoded {
139 span := history.spans[history.head]
140 history.base_decoded = span.decoded_end
141 history.base_raw = span.raw_end
142 history.clear_retired(history.head, history.head + 1)
143 history.head++
144 }
145 if history.head == history.spans.len {
146 unsafe { history.spans.len = 0 }
147 history.head = 0
148 history.base_decoded = history.decoded_end
149 history.base_raw = history.raw_end
150 }
151}
152
153// Compile-time coordinate algebra keeps native files on their existing i64
154// representation and shares the span algorithm with exact custom raw points.
155@[inline]
156fn transcoded_raw_same[T](a T, b T) bool {
157 $if T is i64 {
158 return a == b
159 } $else {
160 return a.same(b)
161 }
162}
163
164@[inline]
165fn transcoded_raw_delta[T](a T, b T) i64 {
166 $if T is i64 {
167 return a - b
168 } $else {
169 return a.delta(b)
170 }
171}
172
173@[inline]
174fn transcoded_raw_retreat[T](a T, distance i64) !T {
175 $if T is i64 {
176 return a - distance
177 } $else {
178 return a.retreat(distance)
179 }
180}
181
182// Physical positions exclude the codec's owned lookahead prefix. In UTF16 a
183// malformed high surrogate may have prefetched a complete valid following word.
184// Newline folding likewise owns a CR boundary and one queued scalar boundary.
185struct TranscodedBoundaryDecoder {
186mut:
187 consumed i64
188 scalar_end i64
189 cr_end i64
190 queued_end i64
191}
192
193fn (mut track TranscodedBoundaryDecoder) next(mut cursor TranscoderDecoder, mut input CodecInput) !CodecStep {
194 return next_transcoded_boundary(mut track, mut cursor, mut input)
195}
196
197@[inline]
198fn (mut track TranscodedBoundaryDecoder) observe(before int, after int, owned int) i64 {
199 track.consumed += after - before
200 return track.consumed - owned
201}
202
203@[inline]
204fn (mut track TranscodedBoundaryDecoder) commit_cr_error() {
205 if track.cr_end < track.scalar_end { track.cr_end = track.scalar_end }
206}
207
208@[inline]
209fn (mut _track TranscodedBoundaryDecoder) queue_taken() {}
210
211@[inline]
212fn (mut _track TranscodedBoundaryDecoder) cr_taken() {}
213
214// Both coordinate policies share the actual codec, error policy and newline
215// state machine. Only observing endpoints and retiring their roots specialize.
216fn next_transcoded_boundary[T](mut track T, mut cursor TranscoderDecoder, mut input CodecInput) !CodecStep {
217 $if T is CustomRawBoundaryDecoder {
218 track.prepare_header(mut cursor, mut input)
219 }
220 for {
221 mut ch := rune(0)
222 mut end := track.scalar_end
223 if cursor.has_queued {
224 ch = cursor.queued
225 end = track.queued_end
226 cursor.has_queued = false
227 track.queue_taken()
228 } else {
229 before := input.position
230 step := cursor.next_decoded(mut input) or {
231 track.scalar_end = track.observe(before, input.position, cursor.decoder.count)
232 // Observed errors commit their frontier even when an older CR
233 // is delivered by a later retry of the same decoder.
234 if cursor.pending_cr { track.commit_cr_error() }
235 return err
236 }
237 end = track.observe(before, input.position, cursor.decoder.count)
238 match step.kind {
239 .need_input { return step }
240 .finished {
241 if cursor.pending_cr {
242 cursor.pending_cr = false
243 track.scalar_end = track.cr_end
244 track.cr_taken()
245 return CodecStep{ kind: .scalar, scalar: `\n` }
246 }
247 return step
248 }
249 .malformed { continue }
250 .scalar { ch = step.scalar }
251 }
252 }
253 was_cr := cursor.pending_cr
254 scalar, produced := cursor.normalize_scalar(ch)
255 if !produced {
256 track.cr_end = end
257 continue
258 }
259 if was_cr && cursor.has_queued {
260 track.queued_end = end
261 track.scalar_end = track.cr_end
262 } else {
263 track.scalar_end = end
264 }
265 if was_cr { track.cr_taken() }
266 return CodecStep{ kind: .scalar, scalar: scalar }
267 }
268 return CodecStep{ kind: .finished }
269}
270