blob: f7df668a9497cbd301624152e23a928f2ded219e [file] [log] [blame]
Nigel Tao737e31f2020-02-11 11:23:17 +11001// Copyright 2020 The Wuffs Authors.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15// ---------------- Tokens
16
Nigel Tao478d1b82020-04-08 23:03:51 +100017// wuffs_base__token is an element of a byte stream's tokenization.
18//
Nigel Tao2f2bbc62021-01-25 11:30:15 +110019// See https://github.com/google/wuffs/blob/main/doc/note/tokens.md
Nigel Tao4f1d24c2020-09-23 22:02:53 +100020typedef struct wuffs_base__token__struct {
Nigel Tao737e31f2020-02-11 11:23:17 +110021 uint64_t repr;
Nigel Tao36857982020-02-12 11:33:13 +110022
23#ifdef __cplusplus
Nigel Tao462f8662020-04-01 23:01:51 +110024 inline int64_t value() const;
25 inline int64_t value_extension() const;
26 inline int64_t value_major() const;
27 inline int64_t value_base_category() const;
Nigel Tao36857982020-02-12 11:33:13 +110028 inline uint64_t value_minor() const;
Nigel Tao36857982020-02-12 11:33:13 +110029 inline uint64_t value_base_detail() const;
Nigel Taoc9d4e342020-07-21 15:20:34 +100030 inline int64_t value_base_detail__sign_extended() const;
Nigel Tao496e88b2020-04-09 22:10:08 +100031 inline bool continued() const;
Nigel Tao36857982020-02-12 11:33:13 +110032 inline uint64_t length() const;
33#endif // __cplusplus
34
Nigel Tao737e31f2020-02-11 11:23:17 +110035} wuffs_base__token;
36
37static inline wuffs_base__token //
38wuffs_base__make_token(uint64_t repr) {
39 wuffs_base__token ret;
40 ret.repr = repr;
41 return ret;
42}
43
Nigel Taof45f01f2020-06-05 14:06:56 +100044// --------
Nigel Tao9d4eeb72020-02-26 11:36:30 +110045
Nigel Tao43f0a872020-03-02 13:59:46 +110046#define WUFFS_BASE__TOKEN__LENGTH__MAX_INCL 0xFFFF
47
Nigel Tao496e88b2020-04-09 22:10:08 +100048#define WUFFS_BASE__TOKEN__VALUE__SHIFT 17
49#define WUFFS_BASE__TOKEN__VALUE_EXTENSION__SHIFT 17
Nigel Taod1c928a2020-02-28 12:43:53 +110050#define WUFFS_BASE__TOKEN__VALUE_MAJOR__SHIFT 42
Nigel Tao496e88b2020-04-09 22:10:08 +100051#define WUFFS_BASE__TOKEN__VALUE_MINOR__SHIFT 17
52#define WUFFS_BASE__TOKEN__VALUE_BASE_CATEGORY__SHIFT 38
53#define WUFFS_BASE__TOKEN__VALUE_BASE_DETAIL__SHIFT 17
54#define WUFFS_BASE__TOKEN__CONTINUED__SHIFT 16
Nigel Taoa9d14882020-02-25 12:12:31 +110055#define WUFFS_BASE__TOKEN__LENGTH__SHIFT 0
56
Nigel Tao850dc182020-07-21 22:52:04 +100057#define WUFFS_BASE__TOKEN__VALUE_EXTENSION__NUM_BITS 46
58
Nigel Taof45f01f2020-06-05 14:06:56 +100059// --------
Nigel Tao9d4eeb72020-02-26 11:36:30 +110060
Nigel Taoa9d14882020-02-25 12:12:31 +110061#define WUFFS_BASE__TOKEN__VBC__FILLER 0
Nigel Tao85fba7f2020-02-29 16:28:06 +110062#define WUFFS_BASE__TOKEN__VBC__STRUCTURE 1
63#define WUFFS_BASE__TOKEN__VBC__STRING 2
64#define WUFFS_BASE__TOKEN__VBC__UNICODE_CODE_POINT 3
65#define WUFFS_BASE__TOKEN__VBC__LITERAL 4
66#define WUFFS_BASE__TOKEN__VBC__NUMBER 5
Nigel Taoc9d4e342020-07-21 15:20:34 +100067#define WUFFS_BASE__TOKEN__VBC__INLINE_INTEGER_SIGNED 6
68#define WUFFS_BASE__TOKEN__VBC__INLINE_INTEGER_UNSIGNED 7
Nigel Taoa9d14882020-02-25 12:12:31 +110069
Nigel Taof45f01f2020-06-05 14:06:56 +100070// --------
Nigel Tao9d4eeb72020-02-26 11:36:30 +110071
Nigel Tao42843692020-08-17 16:48:30 +100072#define WUFFS_BASE__TOKEN__VBD__FILLER__PUNCTUATION 0x00001
Nigel Taod1c928a2020-02-28 12:43:53 +110073#define WUFFS_BASE__TOKEN__VBD__FILLER__COMMENT_BLOCK 0x00002
Nigel Tao42843692020-08-17 16:48:30 +100074#define WUFFS_BASE__TOKEN__VBD__FILLER__COMMENT_LINE 0x00004
Nigel Taoa9d14882020-02-25 12:12:31 +110075
Nigel Taofc304132020-08-19 09:40:02 +100076// COMMENT_ANY is a bit-wise or of COMMENT_BLOCK AND COMMENT_LINE.
77#define WUFFS_BASE__TOKEN__VBD__FILLER__COMMENT_ANY 0x00006
78
Nigel Taof45f01f2020-06-05 14:06:56 +100079// --------
Nigel Tao85fba7f2020-02-29 16:28:06 +110080
81#define WUFFS_BASE__TOKEN__VBD__STRUCTURE__PUSH 0x00001
82#define WUFFS_BASE__TOKEN__VBD__STRUCTURE__POP 0x00002
83#define WUFFS_BASE__TOKEN__VBD__STRUCTURE__FROM_NONE 0x00010
84#define WUFFS_BASE__TOKEN__VBD__STRUCTURE__FROM_LIST 0x00020
85#define WUFFS_BASE__TOKEN__VBD__STRUCTURE__FROM_DICT 0x00040
86#define WUFFS_BASE__TOKEN__VBD__STRUCTURE__TO_NONE 0x01000
87#define WUFFS_BASE__TOKEN__VBD__STRUCTURE__TO_LIST 0x02000
88#define WUFFS_BASE__TOKEN__VBD__STRUCTURE__TO_DICT 0x04000
89
Nigel Tao9d4eeb72020-02-26 11:36:30 +110090// --------
91
Nigel Taob9afc262020-07-03 13:36:13 +100092// DEFINITELY_FOO means that the destination bytes (and also the source bytes,
93// for 1_DST_1_SRC_COPY) are in the FOO format. Definitely means that the lack
94// of the bit means "maybe FOO". It does not necessarily mean "not FOO".
95//
96// CHAIN_ETC means that decoding the entire token chain forms a UTF-8 or ASCII
97// string, not just this current token. CHAIN_ETC_UTF_8 therefore distinguishes
98// Unicode (UTF-8) strings from byte strings. MUST means that the the token
99// producer (e.g. parser) must verify this. SHOULD means that the token
100// consumer (e.g. renderer) should verify this.
101//
102// When a CHAIN_ETC_UTF_8 bit is set, the parser must ensure that non-ASCII
103// code points (with multi-byte UTF-8 encodings) do not straddle token
104// boundaries. Checking UTF-8 validity can inspect each token separately.
105//
106// The lack of any particular bit is conservative: it is valid for all-ASCII
107// strings, in a single- or multi-token chain, to have none of these bits set.
Nigel Taod1c928a2020-02-28 12:43:53 +1100108#define WUFFS_BASE__TOKEN__VBD__STRING__DEFINITELY_UTF_8 0x00001
Nigel Taob9afc262020-07-03 13:36:13 +1000109#define WUFFS_BASE__TOKEN__VBD__STRING__CHAIN_MUST_BE_UTF_8 0x00002
110#define WUFFS_BASE__TOKEN__VBD__STRING__CHAIN_SHOULD_BE_UTF_8 0x00004
111#define WUFFS_BASE__TOKEN__VBD__STRING__DEFINITELY_ASCII 0x00010
112#define WUFFS_BASE__TOKEN__VBD__STRING__CHAIN_MUST_BE_ASCII 0x00020
113#define WUFFS_BASE__TOKEN__VBD__STRING__CHAIN_SHOULD_BE_ASCII 0x00040
Nigel Taoa9d14882020-02-25 12:12:31 +1100114
Nigel Taob9afc262020-07-03 13:36:13 +1000115// CONVERT_D_DST_S_SRC means that multiples of S source bytes (possibly padded)
116// produces multiples of D destination bytes. For example,
Nigel Tao9d4eeb72020-02-26 11:36:30 +1100117// CONVERT_1_DST_4_SRC_BACKSLASH_X means a source like "\\x23\\x67\\xAB", where
118// 12 src bytes encode 3 dst bytes.
119//
Nigel Tao478d1b82020-04-08 23:03:51 +1000120// Post-processing may further transform those D destination bytes (e.g. treat
121// "\\xFF" as the Unicode code point U+00FF instead of the byte 0xFF), but that
122// is out of scope of this VBD's semantics.
123//
Nigel Tao9d4eeb72020-02-26 11:36:30 +1100124// When src is the empty string, multiple conversion algorithms are applicable
125// (so these bits are not necessarily mutually exclusive), all producing the
126// same empty dst string.
Nigel Taob9afc262020-07-03 13:36:13 +1000127#define WUFFS_BASE__TOKEN__VBD__STRING__CONVERT_0_DST_1_SRC_DROP 0x00100
128#define WUFFS_BASE__TOKEN__VBD__STRING__CONVERT_1_DST_1_SRC_COPY 0x00200
129#define WUFFS_BASE__TOKEN__VBD__STRING__CONVERT_1_DST_2_SRC_HEXADECIMAL 0x00400
130#define WUFFS_BASE__TOKEN__VBD__STRING__CONVERT_1_DST_4_SRC_BACKSLASH_X 0x00800
131#define WUFFS_BASE__TOKEN__VBD__STRING__CONVERT_3_DST_4_SRC_BASE_64_STD 0x01000
132#define WUFFS_BASE__TOKEN__VBD__STRING__CONVERT_3_DST_4_SRC_BASE_64_URL 0x02000
133#define WUFFS_BASE__TOKEN__VBD__STRING__CONVERT_4_DST_5_SRC_ASCII_85 0x04000
Nigel Tao15cc8502020-07-09 21:23:38 +1000134#define WUFFS_BASE__TOKEN__VBD__STRING__CONVERT_5_DST_8_SRC_BASE_32_HEX 0x08000
135#define WUFFS_BASE__TOKEN__VBD__STRING__CONVERT_5_DST_8_SRC_BASE_32_STD 0x10000
Nigel Tao9d4eeb72020-02-26 11:36:30 +1100136
Nigel Taof45f01f2020-06-05 14:06:56 +1000137// --------
Nigel Taoa9d14882020-02-25 12:12:31 +1100138
Nigel Tao85fba7f2020-02-29 16:28:06 +1100139#define WUFFS_BASE__TOKEN__VBD__LITERAL__UNDEFINED 0x00001
140#define WUFFS_BASE__TOKEN__VBD__LITERAL__NULL 0x00002
141#define WUFFS_BASE__TOKEN__VBD__LITERAL__FALSE 0x00004
142#define WUFFS_BASE__TOKEN__VBD__LITERAL__TRUE 0x00008
143
Nigel Taof45f01f2020-06-05 14:06:56 +1000144// --------
Nigel Tao9d4eeb72020-02-26 11:36:30 +1100145
Nigel Taoa9d14882020-02-25 12:12:31 +1100146// For a source string of "123" or "0x9A", it is valid for a tokenizer to
Nigel Tao4e193592020-07-15 12:48:57 +1000147// return any combination of:
Nigel Taoc3ca77a2020-03-11 12:06:50 +1100148// - WUFFS_BASE__TOKEN__VBD__NUMBER__CONTENT_FLOATING_POINT.
149// - WUFFS_BASE__TOKEN__VBD__NUMBER__CONTENT_INTEGER_SIGNED.
150// - WUFFS_BASE__TOKEN__VBD__NUMBER__CONTENT_INTEGER_UNSIGNED.
Nigel Taoa9d14882020-02-25 12:12:31 +1100151//
152// For a source string of "+123" or "-0x9A", only the first two are valid.
153//
154// For a source string of "123.", only the first one is valid.
Nigel Taoc3ca77a2020-03-11 12:06:50 +1100155#define WUFFS_BASE__TOKEN__VBD__NUMBER__CONTENT_FLOATING_POINT 0x00001
156#define WUFFS_BASE__TOKEN__VBD__NUMBER__CONTENT_INTEGER_SIGNED 0x00002
157#define WUFFS_BASE__TOKEN__VBD__NUMBER__CONTENT_INTEGER_UNSIGNED 0x00004
158
Nigel Taod7c72372020-03-24 13:58:38 +1100159#define WUFFS_BASE__TOKEN__VBD__NUMBER__CONTENT_NEG_INF 0x00010
160#define WUFFS_BASE__TOKEN__VBD__NUMBER__CONTENT_POS_INF 0x00020
161#define WUFFS_BASE__TOKEN__VBD__NUMBER__CONTENT_NEG_NAN 0x00040
162#define WUFFS_BASE__TOKEN__VBD__NUMBER__CONTENT_POS_NAN 0x00080
163
Nigel Taoc3ca77a2020-03-11 12:06:50 +1100164// The number 300 might be represented as "\x01\x2C", "\x2C\x01\x00\x00" or
165// "300", which are big-endian, little-endian or text. For binary formats, the
Nigel Tao4e193592020-07-15 12:48:57 +1000166// token length (after adjusting for FORMAT_IGNORE_ETC) discriminates
167// e.g. u16 little-endian vs u32 little-endian.
Nigel Taoc3ca77a2020-03-11 12:06:50 +1100168#define WUFFS_BASE__TOKEN__VBD__NUMBER__FORMAT_BINARY_BIG_ENDIAN 0x00100
169#define WUFFS_BASE__TOKEN__VBD__NUMBER__FORMAT_BINARY_LITTLE_ENDIAN 0x00200
170#define WUFFS_BASE__TOKEN__VBD__NUMBER__FORMAT_TEXT 0x00400
Nigel Tao9d4eeb72020-02-26 11:36:30 +1100171
Nigel Tao4e193592020-07-15 12:48:57 +1000172#define WUFFS_BASE__TOKEN__VBD__NUMBER__FORMAT_IGNORE_FIRST_BYTE 0x01000
173
Nigel Tao9d4eeb72020-02-26 11:36:30 +1100174// --------
Nigel Taoa9d14882020-02-25 12:12:31 +1100175
Nigel Tao462f8662020-04-01 23:01:51 +1100176// wuffs_base__token__value returns the token's high 46 bits, sign-extended. A
177// negative value means an extended token, non-negative means a simple token.
178static inline int64_t //
Nigel Tao36857982020-02-12 11:33:13 +1100179wuffs_base__token__value(const wuffs_base__token* t) {
Nigel Tao462f8662020-04-01 23:01:51 +1100180 return ((int64_t)(t->repr)) >> WUFFS_BASE__TOKEN__VALUE__SHIFT;
Nigel Tao36857982020-02-12 11:33:13 +1100181}
182
Nigel Tao462f8662020-04-01 23:01:51 +1100183// wuffs_base__token__value_extension returns a negative value if the token was
184// not an extended token.
185static inline int64_t //
186wuffs_base__token__value_extension(const wuffs_base__token* t) {
187 return (~(int64_t)(t->repr)) >> WUFFS_BASE__TOKEN__VALUE_EXTENSION__SHIFT;
188}
189
190// wuffs_base__token__value_major returns a negative value if the token was not
191// a simple token.
192static inline int64_t //
Nigel Tao36857982020-02-12 11:33:13 +1100193wuffs_base__token__value_major(const wuffs_base__token* t) {
Nigel Tao462f8662020-04-01 23:01:51 +1100194 return ((int64_t)(t->repr)) >> WUFFS_BASE__TOKEN__VALUE_MAJOR__SHIFT;
195}
196
197// wuffs_base__token__value_base_category returns a negative value if the token
198// was not a simple token.
199static inline int64_t //
200wuffs_base__token__value_base_category(const wuffs_base__token* t) {
201 return ((int64_t)(t->repr)) >> WUFFS_BASE__TOKEN__VALUE_BASE_CATEGORY__SHIFT;
Nigel Tao36857982020-02-12 11:33:13 +1100202}
203
204static inline uint64_t //
205wuffs_base__token__value_minor(const wuffs_base__token* t) {
Nigel Tao496e88b2020-04-09 22:10:08 +1000206 return (t->repr >> WUFFS_BASE__TOKEN__VALUE_MINOR__SHIFT) & 0x1FFFFFF;
Nigel Tao36857982020-02-12 11:33:13 +1100207}
208
209static inline uint64_t //
210wuffs_base__token__value_base_detail(const wuffs_base__token* t) {
Nigel Tao462f8662020-04-01 23:01:51 +1100211 return (t->repr >> WUFFS_BASE__TOKEN__VALUE_BASE_DETAIL__SHIFT) & 0x1FFFFF;
Nigel Tao36857982020-02-12 11:33:13 +1100212}
213
Nigel Taoc9d4e342020-07-21 15:20:34 +1000214static inline int64_t //
215wuffs_base__token__value_base_detail__sign_extended(
216 const wuffs_base__token* t) {
217 // The VBD is 21 bits in the middle of t->repr. Left shift the high (64 - 21
218 // - ETC__SHIFT) bits off, then right shift (sign-extending) back down.
219 uint64_t u = t->repr << (43 - WUFFS_BASE__TOKEN__VALUE_BASE_DETAIL__SHIFT);
220 return ((int64_t)u) >> 43;
221}
222
Nigel Taod1c928a2020-02-28 12:43:53 +1100223static inline bool //
Nigel Tao496e88b2020-04-09 22:10:08 +1000224wuffs_base__token__continued(const wuffs_base__token* t) {
225 return t->repr & 0x10000;
Nigel Taod1c928a2020-02-28 12:43:53 +1100226}
227
Nigel Tao36857982020-02-12 11:33:13 +1100228static inline uint64_t //
229wuffs_base__token__length(const wuffs_base__token* t) {
Nigel Tao462f8662020-04-01 23:01:51 +1100230 return (t->repr >> WUFFS_BASE__TOKEN__LENGTH__SHIFT) & 0xFFFF;
Nigel Tao36857982020-02-12 11:33:13 +1100231}
232
233#ifdef __cplusplus
234
Nigel Tao462f8662020-04-01 23:01:51 +1100235inline int64_t //
Nigel Tao36857982020-02-12 11:33:13 +1100236wuffs_base__token::value() const {
237 return wuffs_base__token__value(this);
238}
239
Nigel Tao462f8662020-04-01 23:01:51 +1100240inline int64_t //
241wuffs_base__token::value_extension() const {
242 return wuffs_base__token__value_extension(this);
243}
244
245inline int64_t //
Nigel Tao36857982020-02-12 11:33:13 +1100246wuffs_base__token::value_major() const {
247 return wuffs_base__token__value_major(this);
248}
249
Nigel Tao462f8662020-04-01 23:01:51 +1100250inline int64_t //
251wuffs_base__token::value_base_category() const {
252 return wuffs_base__token__value_base_category(this);
253}
254
Nigel Tao36857982020-02-12 11:33:13 +1100255inline uint64_t //
256wuffs_base__token::value_minor() const {
257 return wuffs_base__token__value_minor(this);
258}
259
260inline uint64_t //
Nigel Tao36857982020-02-12 11:33:13 +1100261wuffs_base__token::value_base_detail() const {
262 return wuffs_base__token__value_base_detail(this);
263}
264
Nigel Taoc9d4e342020-07-21 15:20:34 +1000265inline int64_t //
266wuffs_base__token::value_base_detail__sign_extended() const {
267 return wuffs_base__token__value_base_detail__sign_extended(this);
268}
269
Nigel Taod1c928a2020-02-28 12:43:53 +1100270inline bool //
Nigel Tao496e88b2020-04-09 22:10:08 +1000271wuffs_base__token::continued() const {
272 return wuffs_base__token__continued(this);
Nigel Taod1c928a2020-02-28 12:43:53 +1100273}
274
Nigel Tao36857982020-02-12 11:33:13 +1100275inline uint64_t //
276wuffs_base__token::length() const {
277 return wuffs_base__token__length(this);
278}
279
280#endif // __cplusplus
281
282// --------
283
Nigel Tao737e31f2020-02-11 11:23:17 +1100284typedef WUFFS_BASE__SLICE(wuffs_base__token) wuffs_base__slice_token;
285
286static inline wuffs_base__slice_token //
287wuffs_base__make_slice_token(wuffs_base__token* ptr, size_t len) {
288 wuffs_base__slice_token ret;
289 ret.ptr = ptr;
290 ret.len = len;
291 return ret;
292}
293
Nigel Tao934136b2020-08-06 23:51:13 +1000294static inline wuffs_base__slice_token //
295wuffs_base__empty_slice_token() {
296 wuffs_base__slice_token ret;
297 ret.ptr = NULL;
298 ret.len = 0;
299 return ret;
300}
301
Nigel Tao36857982020-02-12 11:33:13 +1100302// --------
303
Nigel Tao737e31f2020-02-11 11:23:17 +1100304// wuffs_base__token_buffer_meta is the metadata for a
305// wuffs_base__token_buffer's data.
Nigel Tao4f1d24c2020-09-23 22:02:53 +1000306typedef struct wuffs_base__token_buffer_meta__struct {
Nigel Tao737e31f2020-02-11 11:23:17 +1100307 size_t wi; // Write index. Invariant: wi <= len.
308 size_t ri; // Read index. Invariant: ri <= wi.
309 uint64_t pos; // Position of the buffer start relative to the stream start.
310 bool closed; // No further writes are expected.
311} wuffs_base__token_buffer_meta;
312
313// wuffs_base__token_buffer is a 1-dimensional buffer (a pointer and length)
314// plus additional metadata.
315//
316// A value with all fields zero is a valid, empty buffer.
Nigel Tao4f1d24c2020-09-23 22:02:53 +1000317typedef struct wuffs_base__token_buffer__struct {
Nigel Tao737e31f2020-02-11 11:23:17 +1100318 wuffs_base__slice_token data;
319 wuffs_base__token_buffer_meta meta;
320
321#ifdef __cplusplus
Nigel Tao9fd96e82020-03-16 21:46:21 +1100322 inline bool is_valid() const;
Nigel Tao737e31f2020-02-11 11:23:17 +1100323 inline void compact();
Nigel Tao82a96cb2020-08-06 23:41:36 +1000324 inline uint64_t reader_length() const;
Nigel Tao934136b2020-08-06 23:51:13 +1000325 inline wuffs_base__token* reader_pointer() const;
326 inline wuffs_base__slice_token reader_slice() const;
Nigel Tao737e31f2020-02-11 11:23:17 +1100327 inline uint64_t reader_token_position() const;
Nigel Tao82a96cb2020-08-06 23:41:36 +1000328 inline uint64_t writer_length() const;
Nigel Tao737e31f2020-02-11 11:23:17 +1100329 inline uint64_t writer_token_position() const;
Nigel Tao934136b2020-08-06 23:51:13 +1000330 inline wuffs_base__token* writer_pointer() const;
331 inline wuffs_base__slice_token writer_slice() const;
Nigel Tao737e31f2020-02-11 11:23:17 +1100332#endif // __cplusplus
333
334} wuffs_base__token_buffer;
335
336static inline wuffs_base__token_buffer //
337wuffs_base__make_token_buffer(wuffs_base__slice_token data,
338 wuffs_base__token_buffer_meta meta) {
339 wuffs_base__token_buffer ret;
340 ret.data = data;
341 ret.meta = meta;
342 return ret;
343}
344
345static inline wuffs_base__token_buffer_meta //
346wuffs_base__make_token_buffer_meta(size_t wi,
347 size_t ri,
348 uint64_t pos,
349 bool closed) {
350 wuffs_base__token_buffer_meta ret;
351 ret.wi = wi;
352 ret.ri = ri;
353 ret.pos = pos;
354 ret.closed = closed;
355 return ret;
356}
357
358static inline wuffs_base__token_buffer //
Nigel Tao64dbd002020-04-02 22:11:42 +1100359wuffs_base__slice_token__reader(wuffs_base__slice_token s, bool closed) {
Nigel Tao9fd96e82020-03-16 21:46:21 +1100360 wuffs_base__token_buffer ret;
361 ret.data.ptr = s.ptr;
362 ret.data.len = s.len;
363 ret.meta.wi = s.len;
364 ret.meta.ri = 0;
365 ret.meta.pos = 0;
366 ret.meta.closed = closed;
367 return ret;
368}
369
370static inline wuffs_base__token_buffer //
Nigel Tao64dbd002020-04-02 22:11:42 +1100371wuffs_base__slice_token__writer(wuffs_base__slice_token s) {
Nigel Tao9fd96e82020-03-16 21:46:21 +1100372 wuffs_base__token_buffer ret;
373 ret.data.ptr = s.ptr;
374 ret.data.len = s.len;
375 ret.meta.wi = 0;
376 ret.meta.ri = 0;
377 ret.meta.pos = 0;
378 ret.meta.closed = false;
379 return ret;
380}
381
382static inline wuffs_base__token_buffer //
Nigel Tao737e31f2020-02-11 11:23:17 +1100383wuffs_base__empty_token_buffer() {
384 wuffs_base__token_buffer ret;
385 ret.data.ptr = NULL;
386 ret.data.len = 0;
387 ret.meta.wi = 0;
388 ret.meta.ri = 0;
389 ret.meta.pos = 0;
390 ret.meta.closed = false;
391 return ret;
392}
393
394static inline wuffs_base__token_buffer_meta //
395wuffs_base__empty_token_buffer_meta() {
396 wuffs_base__token_buffer_meta ret;
397 ret.wi = 0;
398 ret.ri = 0;
399 ret.pos = 0;
400 ret.closed = false;
401 return ret;
402}
403
Nigel Tao9fd96e82020-03-16 21:46:21 +1100404static inline bool //
405wuffs_base__token_buffer__is_valid(const wuffs_base__token_buffer* buf) {
406 if (buf) {
407 if (buf->data.ptr) {
408 return (buf->meta.ri <= buf->meta.wi) && (buf->meta.wi <= buf->data.len);
409 } else {
410 return (buf->meta.ri == 0) && (buf->meta.wi == 0) && (buf->data.len == 0);
411 }
412 }
413 return false;
414}
415
Nigel Tao737e31f2020-02-11 11:23:17 +1100416// wuffs_base__token_buffer__compact moves any written but unread tokens to the
417// start of the buffer.
418static inline void //
419wuffs_base__token_buffer__compact(wuffs_base__token_buffer* buf) {
420 if (!buf || (buf->meta.ri == 0)) {
421 return;
422 }
423 buf->meta.pos = wuffs_base__u64__sat_add(buf->meta.pos, buf->meta.ri);
424 size_t n = buf->meta.wi - buf->meta.ri;
425 if (n != 0) {
426 memmove(buf->data.ptr, buf->data.ptr + buf->meta.ri,
427 n * sizeof(wuffs_base__token));
428 }
429 buf->meta.wi = n;
430 buf->meta.ri = 0;
431}
432
433static inline uint64_t //
Nigel Tao934136b2020-08-06 23:51:13 +1000434wuffs_base__token_buffer__reader_length(const wuffs_base__token_buffer* buf) {
Nigel Tao737e31f2020-02-11 11:23:17 +1100435 return buf ? buf->meta.wi - buf->meta.ri : 0;
436}
437
Nigel Tao934136b2020-08-06 23:51:13 +1000438static inline wuffs_base__token* //
439wuffs_base__token_buffer__reader_pointer(const wuffs_base__token_buffer* buf) {
440 return buf ? (buf->data.ptr + buf->meta.ri) : NULL;
441}
442
443static inline wuffs_base__slice_token //
444wuffs_base__token_buffer__reader_slice(const wuffs_base__token_buffer* buf) {
445 return buf ? wuffs_base__make_slice_token(buf->data.ptr + buf->meta.ri,
446 buf->meta.wi - buf->meta.ri)
447 : wuffs_base__empty_slice_token();
448}
449
Nigel Tao737e31f2020-02-11 11:23:17 +1100450static inline uint64_t //
451wuffs_base__token_buffer__reader_token_position(
452 const wuffs_base__token_buffer* buf) {
453 return buf ? wuffs_base__u64__sat_add(buf->meta.pos, buf->meta.ri) : 0;
454}
455
456static inline uint64_t //
Nigel Tao934136b2020-08-06 23:51:13 +1000457wuffs_base__token_buffer__writer_length(const wuffs_base__token_buffer* buf) {
Nigel Tao737e31f2020-02-11 11:23:17 +1100458 return buf ? buf->data.len - buf->meta.wi : 0;
459}
460
Nigel Tao934136b2020-08-06 23:51:13 +1000461static inline wuffs_base__token* //
462wuffs_base__token_buffer__writer_pointer(const wuffs_base__token_buffer* buf) {
463 return buf ? (buf->data.ptr + buf->meta.wi) : NULL;
464}
465
466static inline wuffs_base__slice_token //
467wuffs_base__token_buffer__writer_slice(const wuffs_base__token_buffer* buf) {
468 return buf ? wuffs_base__make_slice_token(buf->data.ptr + buf->meta.wi,
469 buf->data.len - buf->meta.wi)
470 : wuffs_base__empty_slice_token();
471}
472
Nigel Tao737e31f2020-02-11 11:23:17 +1100473static inline uint64_t //
474wuffs_base__token_buffer__writer_token_position(
475 const wuffs_base__token_buffer* buf) {
476 return buf ? wuffs_base__u64__sat_add(buf->meta.pos, buf->meta.wi) : 0;
477}
478
479#ifdef __cplusplus
480
Nigel Tao9fd96e82020-03-16 21:46:21 +1100481inline bool //
482wuffs_base__token_buffer::is_valid() const {
483 return wuffs_base__token_buffer__is_valid(this);
484}
485
Nigel Tao737e31f2020-02-11 11:23:17 +1100486inline void //
487wuffs_base__token_buffer::compact() {
488 wuffs_base__token_buffer__compact(this);
489}
490
491inline uint64_t //
Nigel Tao82a96cb2020-08-06 23:41:36 +1000492wuffs_base__token_buffer::reader_length() const {
493 return wuffs_base__token_buffer__reader_length(this);
Nigel Tao737e31f2020-02-11 11:23:17 +1100494}
495
Nigel Tao934136b2020-08-06 23:51:13 +1000496inline wuffs_base__token* //
497wuffs_base__token_buffer::reader_pointer() const {
498 return wuffs_base__token_buffer__reader_pointer(this);
499}
500
501inline wuffs_base__slice_token //
502wuffs_base__token_buffer::reader_slice() const {
503 return wuffs_base__token_buffer__reader_slice(this);
504}
505
Nigel Tao737e31f2020-02-11 11:23:17 +1100506inline uint64_t //
507wuffs_base__token_buffer::reader_token_position() const {
508 return wuffs_base__token_buffer__reader_token_position(this);
509}
510
511inline uint64_t //
Nigel Tao82a96cb2020-08-06 23:41:36 +1000512wuffs_base__token_buffer::writer_length() const {
513 return wuffs_base__token_buffer__writer_length(this);
Nigel Tao737e31f2020-02-11 11:23:17 +1100514}
515
Nigel Tao934136b2020-08-06 23:51:13 +1000516inline wuffs_base__token* //
517wuffs_base__token_buffer::writer_pointer() const {
518 return wuffs_base__token_buffer__writer_pointer(this);
519}
520
521inline wuffs_base__slice_token //
522wuffs_base__token_buffer::writer_slice() const {
523 return wuffs_base__token_buffer__writer_slice(this);
524}
525
Nigel Tao737e31f2020-02-11 11:23:17 +1100526inline uint64_t //
527wuffs_base__token_buffer::writer_token_position() const {
528 return wuffs_base__token_buffer__writer_token_position(this);
529}
530
531#endif // __cplusplus