vile / scheme / port_custom_raw_decoder.v
140 lines · 125 sloc · 4.41 KB · 76646891b77a3d5c6af4a1e42b122cd61dc1654b
Raw
1module scheme
2
3// The endpoint after each of the last four owned codec bytes, plus the point
4// before them. Named inline slots avoid mutable fixed-array promotion in V.
5// Ring updates retain bounded provenance across noncontiguous raw publications.
6struct CustomRawTrail {
7mut:
8 a CustomRawPoint
9 b CustomRawPoint
10 c CustomRawPoint
11 d CustomRawPoint
12 e CustomRawPoint
13 next int
14 len int
15}
16
17@[inline]
18fn (mut trail CustomRawTrail) put(index int, point CustomRawPoint) {
19 match index {
20 0 { trail.a = point }
21 1 { trail.b = point }
22 2 { trail.c = point }
23 3 { trail.d = point }
24 4 { trail.e = point }
25 else { panic('invalid raw provenance slot') }
26 }
27}
28
29@[inline]
30fn (mut trail CustomRawTrail) push(point CustomRawPoint) {
31 trail.put(trail.next, point)
32 trail.next = (trail.next + 1) % 5
33 if trail.len < 5 { trail.len++ }
34}
35
36@[inline]
37fn (trail &CustomRawTrail) back(count int) CustomRawPoint {
38 if count < 0 || count >= trail.len { panic('missing raw codec provenance') }
39 index := (trail.next + 4 - count) % 5
40 return match index {
41 0 { trail.a }
42 1 { trail.b }
43 2 { trail.c }
44 3 { trail.d }
45 else { trail.e }
46 }
47}
48
49fn (mut trail CustomRawTrail) retain(keep int) {
50 for count in keep .. trail.len {
51 trail.put((trail.next + 4 - count) % 5, CustomRawPoint{})
52 }
53 trail.len = keep
54}
55
56struct CustomRawBoundaryDecoder {
57mut:
58 publication CustomRawPoint
59 trail CustomRawTrail
60 scalar_end CustomRawPoint
61 cr_end CustomRawPoint
62 queued_end CustomRawPoint
63 header_end CustomRawPoint
64 header_pending bool
65}
66
67// The publication starts at the beginning of this CodecInput view, which may
68// itself begin partway through an owned callback buffer. Owned codec bytes keep
69// their earlier endpoints; the new publication never relabels their origins.
70fn (mut track CustomRawBoundaryDecoder) begin(origin CustomRawPoint, owned int) ! {
71 if !origin.valid || owned < 0 || owned > 4 { return failure('invalid raw decoder publication') }
72 if owned == 0 {
73 track.trail = CustomRawTrail{}
74 track.trail.push(origin)
75 } else {
76 if track.trail.len < owned + 1 {
77 return failure('raw decoder publication lost codec carry')
78 }
79 track.trail.retain(owned + 1)
80 }
81 track.publication = origin
82 if !track.scalar_end.valid { track.scalar_end = origin }
83}
84
85// There are at most four codec-owned bytes. For a long consumed run only its
86// final five endpoints matter; there is no coordinate counter for port lifetime.
87@[inline]
88fn (mut track CustomRawBoundaryDecoder) observe(before int, after int, owned int) CustomRawPoint {
89 if after > before {
90 for offset in int_max(before + 1, after - 4) .. after + 1 {
91 track.trail.push(CustomRawPoint{ ...track.publication, offset: track.publication.offset + offset })
92 }
93 }
94 point := track.trail.back(owned)
95 track.trail.retain(owned + 1)
96 return point
97}
98
99// An error is later in stream-consumption order than the held CR even if a
100// replayed source coordinate is numerically smaller. No big arithmetic here.
101@[inline]
102fn (mut track CustomRawBoundaryDecoder) commit_cr_error() {
103 track.cr_end = track.scalar_end
104}
105
106@[inline]
107fn (mut track CustomRawBoundaryDecoder) queue_taken() { track.queued_end = CustomRawPoint{} }
108
109@[inline]
110fn (mut track CustomRawBoundaryDecoder) cr_taken() { track.cr_end = CustomRawPoint{} }
111
112// An observed UTF16 BOM commits its end before the first visible character.
113// Fill/header recognition use the actual shared codec helpers. Ordinary codecs
114// keep their existing structs and do not carry this bookkeeping.
115fn (mut track CustomRawBoundaryDecoder) prepare_header(mut cursor TranscoderDecoder, mut input CodecInput) {
116 if cursor.decoder.codec != .utf16 || cursor.decoder.started { return }
117 before := input.position
118 complete := cursor.decoder.fill(mut input, 2)
119 header := complete && cursor.decoder.accept_utf16_header()
120 point := track.observe(before, input.position, cursor.decoder.count)
121 if header {
122 track.header_end = point
123 track.header_pending = true
124 track.scalar_end = point
125 }
126}
127
128fn (mut track CustomRawBoundaryDecoder) take_header() CustomRawPoint {
129 if !track.header_pending { return CustomRawPoint{} }
130 point := track.header_end
131 track.header_end = CustomRawPoint{}
132 track.header_pending = false
133 return point
134}
135
136fn (track &CustomRawBoundaryDecoder) frontier() CustomRawPoint { return track.trail.back(0) }
137
138fn (mut track CustomRawBoundaryDecoder) next(mut cursor TranscoderDecoder, mut input CodecInput) !CodecStep {
139 return next_transcoded_boundary(mut track, mut cursor, mut input)
140}
141