blob: 2934538cc2a8309360169eb98433ca044cd5b2ba [file] [log] [blame]
Nigel Taoa4f2bbb2020-07-28 14:15:24 +10001// After editing this file, run "go generate" in the ../data directory.
Nigel Tao737e31f2020-02-11 11:23:17 +11002
3// Copyright 2020 The Wuffs Authors.
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// https://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16
17// ---------------- Tokens
18
Nigel Tao478d1b82020-04-08 23:03:51 +100019// wuffs_base__token is an element of a byte stream's tokenization.
20//
Nigel Tao2f2bbc62021-01-25 11:30:15 +110021// See https://github.com/google/wuffs/blob/main/doc/note/tokens.md
Nigel Tao4f1d24c2020-09-23 22:02:53 +100022typedef struct wuffs_base__token__struct {
Nigel Tao737e31f2020-02-11 11:23:17 +110023 uint64_t repr;
Nigel Tao36857982020-02-12 11:33:13 +110024
25#ifdef __cplusplus
Nigel Tao462f8662020-04-01 23:01:51 +110026 inline int64_t value() const;
27 inline int64_t value_extension() const;
28 inline int64_t value_major() const;
29 inline int64_t value_base_category() const;
Nigel Tao36857982020-02-12 11:33:13 +110030 inline uint64_t value_minor() const;
Nigel Tao36857982020-02-12 11:33:13 +110031 inline uint64_t value_base_detail() const;
Nigel Taoc9d4e342020-07-21 15:20:34 +100032 inline int64_t value_base_detail__sign_extended() const;
Nigel Tao496e88b2020-04-09 22:10:08 +100033 inline bool continued() const;
Nigel Tao36857982020-02-12 11:33:13 +110034 inline uint64_t length() const;
35#endif // __cplusplus
36
Nigel Tao737e31f2020-02-11 11:23:17 +110037} wuffs_base__token;
38
39static inline wuffs_base__token //
40wuffs_base__make_token(uint64_t repr) {
41 wuffs_base__token ret;
42 ret.repr = repr;
43 return ret;
44}
45
Nigel Taof45f01f2020-06-05 14:06:56 +100046// --------
Nigel Tao9d4eeb72020-02-26 11:36:30 +110047
Nigel Tao43f0a872020-03-02 13:59:46 +110048#define WUFFS_BASE__TOKEN__LENGTH__MAX_INCL 0xFFFF
49
Nigel Tao496e88b2020-04-09 22:10:08 +100050#define WUFFS_BASE__TOKEN__VALUE__SHIFT 17
51#define WUFFS_BASE__TOKEN__VALUE_EXTENSION__SHIFT 17
Nigel Taod1c928a2020-02-28 12:43:53 +110052#define WUFFS_BASE__TOKEN__VALUE_MAJOR__SHIFT 42
Nigel Tao496e88b2020-04-09 22:10:08 +100053#define WUFFS_BASE__TOKEN__VALUE_MINOR__SHIFT 17
54#define WUFFS_BASE__TOKEN__VALUE_BASE_CATEGORY__SHIFT 38
55#define WUFFS_BASE__TOKEN__VALUE_BASE_DETAIL__SHIFT 17
56#define WUFFS_BASE__TOKEN__CONTINUED__SHIFT 16
Nigel Taoa9d14882020-02-25 12:12:31 +110057#define WUFFS_BASE__TOKEN__LENGTH__SHIFT 0
58
Nigel Tao850dc182020-07-21 22:52:04 +100059#define WUFFS_BASE__TOKEN__VALUE_EXTENSION__NUM_BITS 46
60
Nigel Taof45f01f2020-06-05 14:06:56 +100061// --------
Nigel Tao9d4eeb72020-02-26 11:36:30 +110062
Nigel Taoa9d14882020-02-25 12:12:31 +110063#define WUFFS_BASE__TOKEN__VBC__FILLER 0
Nigel Tao85fba7f2020-02-29 16:28:06 +110064#define WUFFS_BASE__TOKEN__VBC__STRUCTURE 1
65#define WUFFS_BASE__TOKEN__VBC__STRING 2
66#define WUFFS_BASE__TOKEN__VBC__UNICODE_CODE_POINT 3
67#define WUFFS_BASE__TOKEN__VBC__LITERAL 4
68#define WUFFS_BASE__TOKEN__VBC__NUMBER 5
Nigel Taoc9d4e342020-07-21 15:20:34 +100069#define WUFFS_BASE__TOKEN__VBC__INLINE_INTEGER_SIGNED 6
70#define WUFFS_BASE__TOKEN__VBC__INLINE_INTEGER_UNSIGNED 7
Nigel Taoa9d14882020-02-25 12:12:31 +110071
Nigel Taof45f01f2020-06-05 14:06:56 +100072// --------
Nigel Tao9d4eeb72020-02-26 11:36:30 +110073
Nigel Tao42843692020-08-17 16:48:30 +100074#define WUFFS_BASE__TOKEN__VBD__FILLER__PUNCTUATION 0x00001
Nigel Taod1c928a2020-02-28 12:43:53 +110075#define WUFFS_BASE__TOKEN__VBD__FILLER__COMMENT_BLOCK 0x00002
Nigel Tao42843692020-08-17 16:48:30 +100076#define WUFFS_BASE__TOKEN__VBD__FILLER__COMMENT_LINE 0x00004
Nigel Taoa9d14882020-02-25 12:12:31 +110077
Nigel Taofc304132020-08-19 09:40:02 +100078// COMMENT_ANY is a bit-wise or of COMMENT_BLOCK AND COMMENT_LINE.
79#define WUFFS_BASE__TOKEN__VBD__FILLER__COMMENT_ANY 0x00006
80
Nigel Taof45f01f2020-06-05 14:06:56 +100081// --------
Nigel Tao85fba7f2020-02-29 16:28:06 +110082
83#define WUFFS_BASE__TOKEN__VBD__STRUCTURE__PUSH 0x00001
84#define WUFFS_BASE__TOKEN__VBD__STRUCTURE__POP 0x00002
85#define WUFFS_BASE__TOKEN__VBD__STRUCTURE__FROM_NONE 0x00010
86#define WUFFS_BASE__TOKEN__VBD__STRUCTURE__FROM_LIST 0x00020
87#define WUFFS_BASE__TOKEN__VBD__STRUCTURE__FROM_DICT 0x00040
88#define WUFFS_BASE__TOKEN__VBD__STRUCTURE__TO_NONE 0x01000
89#define WUFFS_BASE__TOKEN__VBD__STRUCTURE__TO_LIST 0x02000
90#define WUFFS_BASE__TOKEN__VBD__STRUCTURE__TO_DICT 0x04000
91
Nigel Tao9d4eeb72020-02-26 11:36:30 +110092// --------
93
Nigel Taob9afc262020-07-03 13:36:13 +100094// DEFINITELY_FOO means that the destination bytes (and also the source bytes,
95// for 1_DST_1_SRC_COPY) are in the FOO format. Definitely means that the lack
96// of the bit means "maybe FOO". It does not necessarily mean "not FOO".
97//
98// CHAIN_ETC means that decoding the entire token chain forms a UTF-8 or ASCII
99// string, not just this current token. CHAIN_ETC_UTF_8 therefore distinguishes
100// Unicode (UTF-8) strings from byte strings. MUST means that the the token
101// producer (e.g. parser) must verify this. SHOULD means that the token
102// consumer (e.g. renderer) should verify this.
103//
104// When a CHAIN_ETC_UTF_8 bit is set, the parser must ensure that non-ASCII
105// code points (with multi-byte UTF-8 encodings) do not straddle token
106// boundaries. Checking UTF-8 validity can inspect each token separately.
107//
108// The lack of any particular bit is conservative: it is valid for all-ASCII
109// strings, in a single- or multi-token chain, to have none of these bits set.
Nigel Taod1c928a2020-02-28 12:43:53 +1100110#define WUFFS_BASE__TOKEN__VBD__STRING__DEFINITELY_UTF_8 0x00001
Nigel Taob9afc262020-07-03 13:36:13 +1000111#define WUFFS_BASE__TOKEN__VBD__STRING__CHAIN_MUST_BE_UTF_8 0x00002
112#define WUFFS_BASE__TOKEN__VBD__STRING__CHAIN_SHOULD_BE_UTF_8 0x00004
113#define WUFFS_BASE__TOKEN__VBD__STRING__DEFINITELY_ASCII 0x00010
114#define WUFFS_BASE__TOKEN__VBD__STRING__CHAIN_MUST_BE_ASCII 0x00020
115#define WUFFS_BASE__TOKEN__VBD__STRING__CHAIN_SHOULD_BE_ASCII 0x00040
Nigel Taoa9d14882020-02-25 12:12:31 +1100116
Nigel Taob9afc262020-07-03 13:36:13 +1000117// CONVERT_D_DST_S_SRC means that multiples of S source bytes (possibly padded)
118// produces multiples of D destination bytes. For example,
Nigel Tao9d4eeb72020-02-26 11:36:30 +1100119// CONVERT_1_DST_4_SRC_BACKSLASH_X means a source like "\\x23\\x67\\xAB", where
120// 12 src bytes encode 3 dst bytes.
121//
Nigel Tao478d1b82020-04-08 23:03:51 +1000122// Post-processing may further transform those D destination bytes (e.g. treat
123// "\\xFF" as the Unicode code point U+00FF instead of the byte 0xFF), but that
124// is out of scope of this VBD's semantics.
125//
Nigel Tao9d4eeb72020-02-26 11:36:30 +1100126// When src is the empty string, multiple conversion algorithms are applicable
127// (so these bits are not necessarily mutually exclusive), all producing the
128// same empty dst string.
Nigel Taob9afc262020-07-03 13:36:13 +1000129#define WUFFS_BASE__TOKEN__VBD__STRING__CONVERT_0_DST_1_SRC_DROP 0x00100
130#define WUFFS_BASE__TOKEN__VBD__STRING__CONVERT_1_DST_1_SRC_COPY 0x00200
131#define WUFFS_BASE__TOKEN__VBD__STRING__CONVERT_1_DST_2_SRC_HEXADECIMAL 0x00400
132#define WUFFS_BASE__TOKEN__VBD__STRING__CONVERT_1_DST_4_SRC_BACKSLASH_X 0x00800
133#define WUFFS_BASE__TOKEN__VBD__STRING__CONVERT_3_DST_4_SRC_BASE_64_STD 0x01000
134#define WUFFS_BASE__TOKEN__VBD__STRING__CONVERT_3_DST_4_SRC_BASE_64_URL 0x02000
135#define WUFFS_BASE__TOKEN__VBD__STRING__CONVERT_4_DST_5_SRC_ASCII_85 0x04000
Nigel Tao15cc8502020-07-09 21:23:38 +1000136#define WUFFS_BASE__TOKEN__VBD__STRING__CONVERT_5_DST_8_SRC_BASE_32_HEX 0x08000
137#define WUFFS_BASE__TOKEN__VBD__STRING__CONVERT_5_DST_8_SRC_BASE_32_STD 0x10000
Nigel Tao9d4eeb72020-02-26 11:36:30 +1100138
Nigel Taof45f01f2020-06-05 14:06:56 +1000139// --------
Nigel Taoa9d14882020-02-25 12:12:31 +1100140
Nigel Tao85fba7f2020-02-29 16:28:06 +1100141#define WUFFS_BASE__TOKEN__VBD__LITERAL__UNDEFINED 0x00001
142#define WUFFS_BASE__TOKEN__VBD__LITERAL__NULL 0x00002
143#define WUFFS_BASE__TOKEN__VBD__LITERAL__FALSE 0x00004
144#define WUFFS_BASE__TOKEN__VBD__LITERAL__TRUE 0x00008
145
Nigel Taof45f01f2020-06-05 14:06:56 +1000146// --------
Nigel Tao9d4eeb72020-02-26 11:36:30 +1100147
Nigel Taoa9d14882020-02-25 12:12:31 +1100148// For a source string of "123" or "0x9A", it is valid for a tokenizer to
Nigel Tao4e193592020-07-15 12:48:57 +1000149// return any combination of:
Nigel Taoc3ca77a2020-03-11 12:06:50 +1100150// - WUFFS_BASE__TOKEN__VBD__NUMBER__CONTENT_FLOATING_POINT.
151// - WUFFS_BASE__TOKEN__VBD__NUMBER__CONTENT_INTEGER_SIGNED.
152// - WUFFS_BASE__TOKEN__VBD__NUMBER__CONTENT_INTEGER_UNSIGNED.
Nigel Taoa9d14882020-02-25 12:12:31 +1100153//
154// For a source string of "+123" or "-0x9A", only the first two are valid.
155//
156// For a source string of "123.", only the first one is valid.
Nigel Taoc3ca77a2020-03-11 12:06:50 +1100157#define WUFFS_BASE__TOKEN__VBD__NUMBER__CONTENT_FLOATING_POINT 0x00001
158#define WUFFS_BASE__TOKEN__VBD__NUMBER__CONTENT_INTEGER_SIGNED 0x00002
159#define WUFFS_BASE__TOKEN__VBD__NUMBER__CONTENT_INTEGER_UNSIGNED 0x00004
160
Nigel Taod7c72372020-03-24 13:58:38 +1100161#define WUFFS_BASE__TOKEN__VBD__NUMBER__CONTENT_NEG_INF 0x00010
162#define WUFFS_BASE__TOKEN__VBD__NUMBER__CONTENT_POS_INF 0x00020
163#define WUFFS_BASE__TOKEN__VBD__NUMBER__CONTENT_NEG_NAN 0x00040
164#define WUFFS_BASE__TOKEN__VBD__NUMBER__CONTENT_POS_NAN 0x00080
165
Nigel Taoc3ca77a2020-03-11 12:06:50 +1100166// The number 300 might be represented as "\x01\x2C", "\x2C\x01\x00\x00" or
167// "300", which are big-endian, little-endian or text. For binary formats, the
Nigel Tao4e193592020-07-15 12:48:57 +1000168// token length (after adjusting for FORMAT_IGNORE_ETC) discriminates
169// e.g. u16 little-endian vs u32 little-endian.
Nigel Taoc3ca77a2020-03-11 12:06:50 +1100170#define WUFFS_BASE__TOKEN__VBD__NUMBER__FORMAT_BINARY_BIG_ENDIAN 0x00100
171#define WUFFS_BASE__TOKEN__VBD__NUMBER__FORMAT_BINARY_LITTLE_ENDIAN 0x00200
172#define WUFFS_BASE__TOKEN__VBD__NUMBER__FORMAT_TEXT 0x00400
Nigel Tao9d4eeb72020-02-26 11:36:30 +1100173
Nigel Tao4e193592020-07-15 12:48:57 +1000174#define WUFFS_BASE__TOKEN__VBD__NUMBER__FORMAT_IGNORE_FIRST_BYTE 0x01000
175
Nigel Tao9d4eeb72020-02-26 11:36:30 +1100176// --------
Nigel Taoa9d14882020-02-25 12:12:31 +1100177
Nigel Tao462f8662020-04-01 23:01:51 +1100178// wuffs_base__token__value returns the token's high 46 bits, sign-extended. A
179// negative value means an extended token, non-negative means a simple token.
180static inline int64_t //
Nigel Tao36857982020-02-12 11:33:13 +1100181wuffs_base__token__value(const wuffs_base__token* t) {
Nigel Tao462f8662020-04-01 23:01:51 +1100182 return ((int64_t)(t->repr)) >> WUFFS_BASE__TOKEN__VALUE__SHIFT;
Nigel Tao36857982020-02-12 11:33:13 +1100183}
184
Nigel Tao462f8662020-04-01 23:01:51 +1100185// wuffs_base__token__value_extension returns a negative value if the token was
186// not an extended token.
187static inline int64_t //
188wuffs_base__token__value_extension(const wuffs_base__token* t) {
189 return (~(int64_t)(t->repr)) >> WUFFS_BASE__TOKEN__VALUE_EXTENSION__SHIFT;
190}
191
192// wuffs_base__token__value_major returns a negative value if the token was not
193// a simple token.
194static inline int64_t //
Nigel Tao36857982020-02-12 11:33:13 +1100195wuffs_base__token__value_major(const wuffs_base__token* t) {
Nigel Tao462f8662020-04-01 23:01:51 +1100196 return ((int64_t)(t->repr)) >> WUFFS_BASE__TOKEN__VALUE_MAJOR__SHIFT;
197}
198
199// wuffs_base__token__value_base_category returns a negative value if the token
200// was not a simple token.
201static inline int64_t //
202wuffs_base__token__value_base_category(const wuffs_base__token* t) {
203 return ((int64_t)(t->repr)) >> WUFFS_BASE__TOKEN__VALUE_BASE_CATEGORY__SHIFT;
Nigel Tao36857982020-02-12 11:33:13 +1100204}
205
206static inline uint64_t //
207wuffs_base__token__value_minor(const wuffs_base__token* t) {
Nigel Tao496e88b2020-04-09 22:10:08 +1000208 return (t->repr >> WUFFS_BASE__TOKEN__VALUE_MINOR__SHIFT) & 0x1FFFFFF;
Nigel Tao36857982020-02-12 11:33:13 +1100209}
210
211static inline uint64_t //
212wuffs_base__token__value_base_detail(const wuffs_base__token* t) {
Nigel Tao462f8662020-04-01 23:01:51 +1100213 return (t->repr >> WUFFS_BASE__TOKEN__VALUE_BASE_DETAIL__SHIFT) & 0x1FFFFF;
Nigel Tao36857982020-02-12 11:33:13 +1100214}
215
Nigel Taoc9d4e342020-07-21 15:20:34 +1000216static inline int64_t //
217wuffs_base__token__value_base_detail__sign_extended(
218 const wuffs_base__token* t) {
219 // The VBD is 21 bits in the middle of t->repr. Left shift the high (64 - 21
220 // - ETC__SHIFT) bits off, then right shift (sign-extending) back down.
221 uint64_t u = t->repr << (43 - WUFFS_BASE__TOKEN__VALUE_BASE_DETAIL__SHIFT);
222 return ((int64_t)u) >> 43;
223}
224
Nigel Taod1c928a2020-02-28 12:43:53 +1100225static inline bool //
Nigel Tao496e88b2020-04-09 22:10:08 +1000226wuffs_base__token__continued(const wuffs_base__token* t) {
227 return t->repr & 0x10000;
Nigel Taod1c928a2020-02-28 12:43:53 +1100228}
229
Nigel Tao36857982020-02-12 11:33:13 +1100230static inline uint64_t //
231wuffs_base__token__length(const wuffs_base__token* t) {
Nigel Tao462f8662020-04-01 23:01:51 +1100232 return (t->repr >> WUFFS_BASE__TOKEN__LENGTH__SHIFT) & 0xFFFF;
Nigel Tao36857982020-02-12 11:33:13 +1100233}
234
235#ifdef __cplusplus
236
Nigel Tao462f8662020-04-01 23:01:51 +1100237inline int64_t //
Nigel Tao36857982020-02-12 11:33:13 +1100238wuffs_base__token::value() const {
239 return wuffs_base__token__value(this);
240}
241
Nigel Tao462f8662020-04-01 23:01:51 +1100242inline int64_t //
243wuffs_base__token::value_extension() const {
244 return wuffs_base__token__value_extension(this);
245}
246
247inline int64_t //
Nigel Tao36857982020-02-12 11:33:13 +1100248wuffs_base__token::value_major() const {
249 return wuffs_base__token__value_major(this);
250}
251
Nigel Tao462f8662020-04-01 23:01:51 +1100252inline int64_t //
253wuffs_base__token::value_base_category() const {
254 return wuffs_base__token__value_base_category(this);
255}
256
Nigel Tao36857982020-02-12 11:33:13 +1100257inline uint64_t //
258wuffs_base__token::value_minor() const {
259 return wuffs_base__token__value_minor(this);
260}
261
262inline uint64_t //
Nigel Tao36857982020-02-12 11:33:13 +1100263wuffs_base__token::value_base_detail() const {
264 return wuffs_base__token__value_base_detail(this);
265}
266
Nigel Taoc9d4e342020-07-21 15:20:34 +1000267inline int64_t //
268wuffs_base__token::value_base_detail__sign_extended() const {
269 return wuffs_base__token__value_base_detail__sign_extended(this);
270}
271
Nigel Taod1c928a2020-02-28 12:43:53 +1100272inline bool //
Nigel Tao496e88b2020-04-09 22:10:08 +1000273wuffs_base__token::continued() const {
274 return wuffs_base__token__continued(this);
Nigel Taod1c928a2020-02-28 12:43:53 +1100275}
276
Nigel Tao36857982020-02-12 11:33:13 +1100277inline uint64_t //
278wuffs_base__token::length() const {
279 return wuffs_base__token__length(this);
280}
281
282#endif // __cplusplus
283
284// --------
285
Nigel Tao737e31f2020-02-11 11:23:17 +1100286typedef WUFFS_BASE__SLICE(wuffs_base__token) wuffs_base__slice_token;
287
288static inline wuffs_base__slice_token //
289wuffs_base__make_slice_token(wuffs_base__token* ptr, size_t len) {
290 wuffs_base__slice_token ret;
291 ret.ptr = ptr;
292 ret.len = len;
293 return ret;
294}
295
Nigel Tao934136b2020-08-06 23:51:13 +1000296static inline wuffs_base__slice_token //
297wuffs_base__empty_slice_token() {
298 wuffs_base__slice_token ret;
299 ret.ptr = NULL;
300 ret.len = 0;
301 return ret;
302}
303
Nigel Tao36857982020-02-12 11:33:13 +1100304// --------
305
Nigel Tao737e31f2020-02-11 11:23:17 +1100306// wuffs_base__token_buffer_meta is the metadata for a
307// wuffs_base__token_buffer's data.
Nigel Tao4f1d24c2020-09-23 22:02:53 +1000308typedef struct wuffs_base__token_buffer_meta__struct {
Nigel Tao737e31f2020-02-11 11:23:17 +1100309 size_t wi; // Write index. Invariant: wi <= len.
310 size_t ri; // Read index. Invariant: ri <= wi.
311 uint64_t pos; // Position of the buffer start relative to the stream start.
312 bool closed; // No further writes are expected.
313} wuffs_base__token_buffer_meta;
314
315// wuffs_base__token_buffer is a 1-dimensional buffer (a pointer and length)
316// plus additional metadata.
317//
318// A value with all fields zero is a valid, empty buffer.
Nigel Tao4f1d24c2020-09-23 22:02:53 +1000319typedef struct wuffs_base__token_buffer__struct {
Nigel Tao737e31f2020-02-11 11:23:17 +1100320 wuffs_base__slice_token data;
321 wuffs_base__token_buffer_meta meta;
322
323#ifdef __cplusplus
Nigel Tao9fd96e82020-03-16 21:46:21 +1100324 inline bool is_valid() const;
Nigel Tao737e31f2020-02-11 11:23:17 +1100325 inline void compact();
Nigel Tao82a96cb2020-08-06 23:41:36 +1000326 inline uint64_t reader_length() const;
Nigel Tao934136b2020-08-06 23:51:13 +1000327 inline wuffs_base__token* reader_pointer() const;
328 inline wuffs_base__slice_token reader_slice() const;
Nigel Tao737e31f2020-02-11 11:23:17 +1100329 inline uint64_t reader_token_position() const;
Nigel Tao82a96cb2020-08-06 23:41:36 +1000330 inline uint64_t writer_length() const;
Nigel Tao737e31f2020-02-11 11:23:17 +1100331 inline uint64_t writer_token_position() const;
Nigel Tao934136b2020-08-06 23:51:13 +1000332 inline wuffs_base__token* writer_pointer() const;
333 inline wuffs_base__slice_token writer_slice() const;
Nigel Tao737e31f2020-02-11 11:23:17 +1100334#endif // __cplusplus
335
336} wuffs_base__token_buffer;
337
338static inline wuffs_base__token_buffer //
339wuffs_base__make_token_buffer(wuffs_base__slice_token data,
340 wuffs_base__token_buffer_meta meta) {
341 wuffs_base__token_buffer ret;
342 ret.data = data;
343 ret.meta = meta;
344 return ret;
345}
346
347static inline wuffs_base__token_buffer_meta //
348wuffs_base__make_token_buffer_meta(size_t wi,
349 size_t ri,
350 uint64_t pos,
351 bool closed) {
352 wuffs_base__token_buffer_meta ret;
353 ret.wi = wi;
354 ret.ri = ri;
355 ret.pos = pos;
356 ret.closed = closed;
357 return ret;
358}
359
360static inline wuffs_base__token_buffer //
Nigel Tao64dbd002020-04-02 22:11:42 +1100361wuffs_base__slice_token__reader(wuffs_base__slice_token s, bool closed) {
Nigel Tao9fd96e82020-03-16 21:46:21 +1100362 wuffs_base__token_buffer ret;
363 ret.data.ptr = s.ptr;
364 ret.data.len = s.len;
365 ret.meta.wi = s.len;
366 ret.meta.ri = 0;
367 ret.meta.pos = 0;
368 ret.meta.closed = closed;
369 return ret;
370}
371
372static inline wuffs_base__token_buffer //
Nigel Tao64dbd002020-04-02 22:11:42 +1100373wuffs_base__slice_token__writer(wuffs_base__slice_token s) {
Nigel Tao9fd96e82020-03-16 21:46:21 +1100374 wuffs_base__token_buffer ret;
375 ret.data.ptr = s.ptr;
376 ret.data.len = s.len;
377 ret.meta.wi = 0;
378 ret.meta.ri = 0;
379 ret.meta.pos = 0;
380 ret.meta.closed = false;
381 return ret;
382}
383
384static inline wuffs_base__token_buffer //
Nigel Tao737e31f2020-02-11 11:23:17 +1100385wuffs_base__empty_token_buffer() {
386 wuffs_base__token_buffer ret;
387 ret.data.ptr = NULL;
388 ret.data.len = 0;
389 ret.meta.wi = 0;
390 ret.meta.ri = 0;
391 ret.meta.pos = 0;
392 ret.meta.closed = false;
393 return ret;
394}
395
396static inline wuffs_base__token_buffer_meta //
397wuffs_base__empty_token_buffer_meta() {
398 wuffs_base__token_buffer_meta ret;
399 ret.wi = 0;
400 ret.ri = 0;
401 ret.pos = 0;
402 ret.closed = false;
403 return ret;
404}
405
Nigel Tao9fd96e82020-03-16 21:46:21 +1100406static inline bool //
407wuffs_base__token_buffer__is_valid(const wuffs_base__token_buffer* buf) {
408 if (buf) {
409 if (buf->data.ptr) {
410 return (buf->meta.ri <= buf->meta.wi) && (buf->meta.wi <= buf->data.len);
411 } else {
412 return (buf->meta.ri == 0) && (buf->meta.wi == 0) && (buf->data.len == 0);
413 }
414 }
415 return false;
416}
417
Nigel Tao737e31f2020-02-11 11:23:17 +1100418// wuffs_base__token_buffer__compact moves any written but unread tokens to the
419// start of the buffer.
420static inline void //
421wuffs_base__token_buffer__compact(wuffs_base__token_buffer* buf) {
422 if (!buf || (buf->meta.ri == 0)) {
423 return;
424 }
425 buf->meta.pos = wuffs_base__u64__sat_add(buf->meta.pos, buf->meta.ri);
426 size_t n = buf->meta.wi - buf->meta.ri;
427 if (n != 0) {
428 memmove(buf->data.ptr, buf->data.ptr + buf->meta.ri,
429 n * sizeof(wuffs_base__token));
430 }
431 buf->meta.wi = n;
432 buf->meta.ri = 0;
433}
434
435static inline uint64_t //
Nigel Tao934136b2020-08-06 23:51:13 +1000436wuffs_base__token_buffer__reader_length(const wuffs_base__token_buffer* buf) {
Nigel Tao737e31f2020-02-11 11:23:17 +1100437 return buf ? buf->meta.wi - buf->meta.ri : 0;
438}
439
Nigel Tao934136b2020-08-06 23:51:13 +1000440static inline wuffs_base__token* //
441wuffs_base__token_buffer__reader_pointer(const wuffs_base__token_buffer* buf) {
442 return buf ? (buf->data.ptr + buf->meta.ri) : NULL;
443}
444
445static inline wuffs_base__slice_token //
446wuffs_base__token_buffer__reader_slice(const wuffs_base__token_buffer* buf) {
447 return buf ? wuffs_base__make_slice_token(buf->data.ptr + buf->meta.ri,
448 buf->meta.wi - buf->meta.ri)
449 : wuffs_base__empty_slice_token();
450}
451
Nigel Tao737e31f2020-02-11 11:23:17 +1100452static inline uint64_t //
453wuffs_base__token_buffer__reader_token_position(
454 const wuffs_base__token_buffer* buf) {
455 return buf ? wuffs_base__u64__sat_add(buf->meta.pos, buf->meta.ri) : 0;
456}
457
458static inline uint64_t //
Nigel Tao934136b2020-08-06 23:51:13 +1000459wuffs_base__token_buffer__writer_length(const wuffs_base__token_buffer* buf) {
Nigel Tao737e31f2020-02-11 11:23:17 +1100460 return buf ? buf->data.len - buf->meta.wi : 0;
461}
462
Nigel Tao934136b2020-08-06 23:51:13 +1000463static inline wuffs_base__token* //
464wuffs_base__token_buffer__writer_pointer(const wuffs_base__token_buffer* buf) {
465 return buf ? (buf->data.ptr + buf->meta.wi) : NULL;
466}
467
468static inline wuffs_base__slice_token //
469wuffs_base__token_buffer__writer_slice(const wuffs_base__token_buffer* buf) {
470 return buf ? wuffs_base__make_slice_token(buf->data.ptr + buf->meta.wi,
471 buf->data.len - buf->meta.wi)
472 : wuffs_base__empty_slice_token();
473}
474
Nigel Tao737e31f2020-02-11 11:23:17 +1100475static inline uint64_t //
476wuffs_base__token_buffer__writer_token_position(
477 const wuffs_base__token_buffer* buf) {
478 return buf ? wuffs_base__u64__sat_add(buf->meta.pos, buf->meta.wi) : 0;
479}
480
481#ifdef __cplusplus
482
Nigel Tao9fd96e82020-03-16 21:46:21 +1100483inline bool //
484wuffs_base__token_buffer::is_valid() const {
485 return wuffs_base__token_buffer__is_valid(this);
486}
487
Nigel Tao737e31f2020-02-11 11:23:17 +1100488inline void //
489wuffs_base__token_buffer::compact() {
490 wuffs_base__token_buffer__compact(this);
491}
492
493inline uint64_t //
Nigel Tao82a96cb2020-08-06 23:41:36 +1000494wuffs_base__token_buffer::reader_length() const {
495 return wuffs_base__token_buffer__reader_length(this);
Nigel Tao737e31f2020-02-11 11:23:17 +1100496}
497
Nigel Tao934136b2020-08-06 23:51:13 +1000498inline wuffs_base__token* //
499wuffs_base__token_buffer::reader_pointer() const {
500 return wuffs_base__token_buffer__reader_pointer(this);
501}
502
503inline wuffs_base__slice_token //
504wuffs_base__token_buffer::reader_slice() const {
505 return wuffs_base__token_buffer__reader_slice(this);
506}
507
Nigel Tao737e31f2020-02-11 11:23:17 +1100508inline uint64_t //
509wuffs_base__token_buffer::reader_token_position() const {
510 return wuffs_base__token_buffer__reader_token_position(this);
511}
512
513inline uint64_t //
Nigel Tao82a96cb2020-08-06 23:41:36 +1000514wuffs_base__token_buffer::writer_length() const {
515 return wuffs_base__token_buffer__writer_length(this);
Nigel Tao737e31f2020-02-11 11:23:17 +1100516}
517
Nigel Tao934136b2020-08-06 23:51:13 +1000518inline wuffs_base__token* //
519wuffs_base__token_buffer::writer_pointer() const {
520 return wuffs_base__token_buffer__writer_pointer(this);
521}
522
523inline wuffs_base__slice_token //
524wuffs_base__token_buffer::writer_slice() const {
525 return wuffs_base__token_buffer__writer_slice(this);
526}
527
Nigel Tao737e31f2020-02-11 11:23:17 +1100528inline uint64_t //
529wuffs_base__token_buffer::writer_token_position() const {
530 return wuffs_base__token_buffer__writer_token_position(this);
531}
532
533#endif // __cplusplus