blob: 0f74e2b9bc51fa29e227616f95bf651bd07a65e5 [file] [log] [blame]
Nigel Tao737e31f2020-02-11 11:23:17 +11001// After editing this file, run "go generate" in the parent directory.
2
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//
21// See https://github.com/google/wuffs/blob/master/doc/note/tokens.md
Nigel Tao737e31f2020-02-11 11:23:17 +110022typedef 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 Taod1c928a2020-02-28 12:43:53 +110074#define WUFFS_BASE__TOKEN__VBD__FILLER__COMMENT_LINE 0x00001
75#define WUFFS_BASE__TOKEN__VBD__FILLER__COMMENT_BLOCK 0x00002
Nigel Taoa9d14882020-02-25 12:12:31 +110076
Nigel Taof45f01f2020-06-05 14:06:56 +100077// --------
Nigel Tao85fba7f2020-02-29 16:28:06 +110078
79#define WUFFS_BASE__TOKEN__VBD__STRUCTURE__PUSH 0x00001
80#define WUFFS_BASE__TOKEN__VBD__STRUCTURE__POP 0x00002
81#define WUFFS_BASE__TOKEN__VBD__STRUCTURE__FROM_NONE 0x00010
82#define WUFFS_BASE__TOKEN__VBD__STRUCTURE__FROM_LIST 0x00020
83#define WUFFS_BASE__TOKEN__VBD__STRUCTURE__FROM_DICT 0x00040
84#define WUFFS_BASE__TOKEN__VBD__STRUCTURE__TO_NONE 0x01000
85#define WUFFS_BASE__TOKEN__VBD__STRUCTURE__TO_LIST 0x02000
86#define WUFFS_BASE__TOKEN__VBD__STRUCTURE__TO_DICT 0x04000
87
Nigel Tao9d4eeb72020-02-26 11:36:30 +110088// --------
89
Nigel Taob9afc262020-07-03 13:36:13 +100090// DEFINITELY_FOO means that the destination bytes (and also the source bytes,
91// for 1_DST_1_SRC_COPY) are in the FOO format. Definitely means that the lack
92// of the bit means "maybe FOO". It does not necessarily mean "not FOO".
93//
94// CHAIN_ETC means that decoding the entire token chain forms a UTF-8 or ASCII
95// string, not just this current token. CHAIN_ETC_UTF_8 therefore distinguishes
96// Unicode (UTF-8) strings from byte strings. MUST means that the the token
97// producer (e.g. parser) must verify this. SHOULD means that the token
98// consumer (e.g. renderer) should verify this.
99//
100// When a CHAIN_ETC_UTF_8 bit is set, the parser must ensure that non-ASCII
101// code points (with multi-byte UTF-8 encodings) do not straddle token
102// boundaries. Checking UTF-8 validity can inspect each token separately.
103//
104// The lack of any particular bit is conservative: it is valid for all-ASCII
105// strings, in a single- or multi-token chain, to have none of these bits set.
Nigel Taod1c928a2020-02-28 12:43:53 +1100106#define WUFFS_BASE__TOKEN__VBD__STRING__DEFINITELY_UTF_8 0x00001
Nigel Taob9afc262020-07-03 13:36:13 +1000107#define WUFFS_BASE__TOKEN__VBD__STRING__CHAIN_MUST_BE_UTF_8 0x00002
108#define WUFFS_BASE__TOKEN__VBD__STRING__CHAIN_SHOULD_BE_UTF_8 0x00004
109#define WUFFS_BASE__TOKEN__VBD__STRING__DEFINITELY_ASCII 0x00010
110#define WUFFS_BASE__TOKEN__VBD__STRING__CHAIN_MUST_BE_ASCII 0x00020
111#define WUFFS_BASE__TOKEN__VBD__STRING__CHAIN_SHOULD_BE_ASCII 0x00040
Nigel Taoa9d14882020-02-25 12:12:31 +1100112
Nigel Taob9afc262020-07-03 13:36:13 +1000113// CONVERT_D_DST_S_SRC means that multiples of S source bytes (possibly padded)
114// produces multiples of D destination bytes. For example,
Nigel Tao9d4eeb72020-02-26 11:36:30 +1100115// CONVERT_1_DST_4_SRC_BACKSLASH_X means a source like "\\x23\\x67\\xAB", where
116// 12 src bytes encode 3 dst bytes.
117//
Nigel Tao478d1b82020-04-08 23:03:51 +1000118// Post-processing may further transform those D destination bytes (e.g. treat
119// "\\xFF" as the Unicode code point U+00FF instead of the byte 0xFF), but that
120// is out of scope of this VBD's semantics.
121//
Nigel Tao9d4eeb72020-02-26 11:36:30 +1100122// When src is the empty string, multiple conversion algorithms are applicable
123// (so these bits are not necessarily mutually exclusive), all producing the
124// same empty dst string.
Nigel Taob9afc262020-07-03 13:36:13 +1000125#define WUFFS_BASE__TOKEN__VBD__STRING__CONVERT_0_DST_1_SRC_DROP 0x00100
126#define WUFFS_BASE__TOKEN__VBD__STRING__CONVERT_1_DST_1_SRC_COPY 0x00200
127#define WUFFS_BASE__TOKEN__VBD__STRING__CONVERT_1_DST_2_SRC_HEXADECIMAL 0x00400
128#define WUFFS_BASE__TOKEN__VBD__STRING__CONVERT_1_DST_4_SRC_BACKSLASH_X 0x00800
129#define WUFFS_BASE__TOKEN__VBD__STRING__CONVERT_3_DST_4_SRC_BASE_64_STD 0x01000
130#define WUFFS_BASE__TOKEN__VBD__STRING__CONVERT_3_DST_4_SRC_BASE_64_URL 0x02000
131#define WUFFS_BASE__TOKEN__VBD__STRING__CONVERT_4_DST_5_SRC_ASCII_85 0x04000
Nigel Tao15cc8502020-07-09 21:23:38 +1000132#define WUFFS_BASE__TOKEN__VBD__STRING__CONVERT_5_DST_8_SRC_BASE_32_HEX 0x08000
133#define WUFFS_BASE__TOKEN__VBD__STRING__CONVERT_5_DST_8_SRC_BASE_32_STD 0x10000
Nigel Tao9d4eeb72020-02-26 11:36:30 +1100134
Nigel Taof45f01f2020-06-05 14:06:56 +1000135// --------
Nigel Taoa9d14882020-02-25 12:12:31 +1100136
Nigel Tao85fba7f2020-02-29 16:28:06 +1100137#define WUFFS_BASE__TOKEN__VBD__LITERAL__UNDEFINED 0x00001
138#define WUFFS_BASE__TOKEN__VBD__LITERAL__NULL 0x00002
139#define WUFFS_BASE__TOKEN__VBD__LITERAL__FALSE 0x00004
140#define WUFFS_BASE__TOKEN__VBD__LITERAL__TRUE 0x00008
141
Nigel Taof45f01f2020-06-05 14:06:56 +1000142// --------
Nigel Tao9d4eeb72020-02-26 11:36:30 +1100143
Nigel Taoa9d14882020-02-25 12:12:31 +1100144// For a source string of "123" or "0x9A", it is valid for a tokenizer to
Nigel Tao4e193592020-07-15 12:48:57 +1000145// return any combination of:
Nigel Taoc3ca77a2020-03-11 12:06:50 +1100146// - WUFFS_BASE__TOKEN__VBD__NUMBER__CONTENT_FLOATING_POINT.
147// - WUFFS_BASE__TOKEN__VBD__NUMBER__CONTENT_INTEGER_SIGNED.
148// - WUFFS_BASE__TOKEN__VBD__NUMBER__CONTENT_INTEGER_UNSIGNED.
Nigel Taoa9d14882020-02-25 12:12:31 +1100149//
150// For a source string of "+123" or "-0x9A", only the first two are valid.
151//
152// For a source string of "123.", only the first one is valid.
Nigel Taoc3ca77a2020-03-11 12:06:50 +1100153#define WUFFS_BASE__TOKEN__VBD__NUMBER__CONTENT_FLOATING_POINT 0x00001
154#define WUFFS_BASE__TOKEN__VBD__NUMBER__CONTENT_INTEGER_SIGNED 0x00002
155#define WUFFS_BASE__TOKEN__VBD__NUMBER__CONTENT_INTEGER_UNSIGNED 0x00004
156
Nigel Taod7c72372020-03-24 13:58:38 +1100157#define WUFFS_BASE__TOKEN__VBD__NUMBER__CONTENT_NEG_INF 0x00010
158#define WUFFS_BASE__TOKEN__VBD__NUMBER__CONTENT_POS_INF 0x00020
159#define WUFFS_BASE__TOKEN__VBD__NUMBER__CONTENT_NEG_NAN 0x00040
160#define WUFFS_BASE__TOKEN__VBD__NUMBER__CONTENT_POS_NAN 0x00080
161
Nigel Taoc3ca77a2020-03-11 12:06:50 +1100162// The number 300 might be represented as "\x01\x2C", "\x2C\x01\x00\x00" or
163// "300", which are big-endian, little-endian or text. For binary formats, the
Nigel Tao4e193592020-07-15 12:48:57 +1000164// token length (after adjusting for FORMAT_IGNORE_ETC) discriminates
165// e.g. u16 little-endian vs u32 little-endian.
Nigel Taoc3ca77a2020-03-11 12:06:50 +1100166#define WUFFS_BASE__TOKEN__VBD__NUMBER__FORMAT_BINARY_BIG_ENDIAN 0x00100
167#define WUFFS_BASE__TOKEN__VBD__NUMBER__FORMAT_BINARY_LITTLE_ENDIAN 0x00200
168#define WUFFS_BASE__TOKEN__VBD__NUMBER__FORMAT_TEXT 0x00400
Nigel Tao9d4eeb72020-02-26 11:36:30 +1100169
Nigel Tao4e193592020-07-15 12:48:57 +1000170#define WUFFS_BASE__TOKEN__VBD__NUMBER__FORMAT_IGNORE_FIRST_BYTE 0x01000
171
Nigel Tao9d4eeb72020-02-26 11:36:30 +1100172// --------
Nigel Taoa9d14882020-02-25 12:12:31 +1100173
Nigel Tao462f8662020-04-01 23:01:51 +1100174// wuffs_base__token__value returns the token's high 46 bits, sign-extended. A
175// negative value means an extended token, non-negative means a simple token.
176static inline int64_t //
Nigel Tao36857982020-02-12 11:33:13 +1100177wuffs_base__token__value(const wuffs_base__token* t) {
Nigel Tao462f8662020-04-01 23:01:51 +1100178 return ((int64_t)(t->repr)) >> WUFFS_BASE__TOKEN__VALUE__SHIFT;
Nigel Tao36857982020-02-12 11:33:13 +1100179}
180
Nigel Tao462f8662020-04-01 23:01:51 +1100181// wuffs_base__token__value_extension returns a negative value if the token was
182// not an extended token.
183static inline int64_t //
184wuffs_base__token__value_extension(const wuffs_base__token* t) {
185 return (~(int64_t)(t->repr)) >> WUFFS_BASE__TOKEN__VALUE_EXTENSION__SHIFT;
186}
187
188// wuffs_base__token__value_major returns a negative value if the token was not
189// a simple token.
190static inline int64_t //
Nigel Tao36857982020-02-12 11:33:13 +1100191wuffs_base__token__value_major(const wuffs_base__token* t) {
Nigel Tao462f8662020-04-01 23:01:51 +1100192 return ((int64_t)(t->repr)) >> WUFFS_BASE__TOKEN__VALUE_MAJOR__SHIFT;
193}
194
195// wuffs_base__token__value_base_category returns a negative value if the token
196// was not a simple token.
197static inline int64_t //
198wuffs_base__token__value_base_category(const wuffs_base__token* t) {
199 return ((int64_t)(t->repr)) >> WUFFS_BASE__TOKEN__VALUE_BASE_CATEGORY__SHIFT;
Nigel Tao36857982020-02-12 11:33:13 +1100200}
201
202static inline uint64_t //
203wuffs_base__token__value_minor(const wuffs_base__token* t) {
Nigel Tao496e88b2020-04-09 22:10:08 +1000204 return (t->repr >> WUFFS_BASE__TOKEN__VALUE_MINOR__SHIFT) & 0x1FFFFFF;
Nigel Tao36857982020-02-12 11:33:13 +1100205}
206
207static inline uint64_t //
208wuffs_base__token__value_base_detail(const wuffs_base__token* t) {
Nigel Tao462f8662020-04-01 23:01:51 +1100209 return (t->repr >> WUFFS_BASE__TOKEN__VALUE_BASE_DETAIL__SHIFT) & 0x1FFFFF;
Nigel Tao36857982020-02-12 11:33:13 +1100210}
211
Nigel Taoc9d4e342020-07-21 15:20:34 +1000212static inline int64_t //
213wuffs_base__token__value_base_detail__sign_extended(
214 const wuffs_base__token* t) {
215 // The VBD is 21 bits in the middle of t->repr. Left shift the high (64 - 21
216 // - ETC__SHIFT) bits off, then right shift (sign-extending) back down.
217 uint64_t u = t->repr << (43 - WUFFS_BASE__TOKEN__VALUE_BASE_DETAIL__SHIFT);
218 return ((int64_t)u) >> 43;
219}
220
Nigel Taod1c928a2020-02-28 12:43:53 +1100221static inline bool //
Nigel Tao496e88b2020-04-09 22:10:08 +1000222wuffs_base__token__continued(const wuffs_base__token* t) {
223 return t->repr & 0x10000;
Nigel Taod1c928a2020-02-28 12:43:53 +1100224}
225
Nigel Tao36857982020-02-12 11:33:13 +1100226static inline uint64_t //
227wuffs_base__token__length(const wuffs_base__token* t) {
Nigel Tao462f8662020-04-01 23:01:51 +1100228 return (t->repr >> WUFFS_BASE__TOKEN__LENGTH__SHIFT) & 0xFFFF;
Nigel Tao36857982020-02-12 11:33:13 +1100229}
230
231#ifdef __cplusplus
232
Nigel Tao462f8662020-04-01 23:01:51 +1100233inline int64_t //
Nigel Tao36857982020-02-12 11:33:13 +1100234wuffs_base__token::value() const {
235 return wuffs_base__token__value(this);
236}
237
Nigel Tao462f8662020-04-01 23:01:51 +1100238inline int64_t //
239wuffs_base__token::value_extension() const {
240 return wuffs_base__token__value_extension(this);
241}
242
243inline int64_t //
Nigel Tao36857982020-02-12 11:33:13 +1100244wuffs_base__token::value_major() const {
245 return wuffs_base__token__value_major(this);
246}
247
Nigel Tao462f8662020-04-01 23:01:51 +1100248inline int64_t //
249wuffs_base__token::value_base_category() const {
250 return wuffs_base__token__value_base_category(this);
251}
252
Nigel Tao36857982020-02-12 11:33:13 +1100253inline uint64_t //
254wuffs_base__token::value_minor() const {
255 return wuffs_base__token__value_minor(this);
256}
257
258inline uint64_t //
Nigel Tao36857982020-02-12 11:33:13 +1100259wuffs_base__token::value_base_detail() const {
260 return wuffs_base__token__value_base_detail(this);
261}
262
Nigel Taoc9d4e342020-07-21 15:20:34 +1000263inline int64_t //
264wuffs_base__token::value_base_detail__sign_extended() const {
265 return wuffs_base__token__value_base_detail__sign_extended(this);
266}
267
Nigel Taod1c928a2020-02-28 12:43:53 +1100268inline bool //
Nigel Tao496e88b2020-04-09 22:10:08 +1000269wuffs_base__token::continued() const {
270 return wuffs_base__token__continued(this);
Nigel Taod1c928a2020-02-28 12:43:53 +1100271}
272
Nigel Tao36857982020-02-12 11:33:13 +1100273inline uint64_t //
274wuffs_base__token::length() const {
275 return wuffs_base__token__length(this);
276}
277
278#endif // __cplusplus
279
280// --------
281
Nigel Tao737e31f2020-02-11 11:23:17 +1100282typedef WUFFS_BASE__SLICE(wuffs_base__token) wuffs_base__slice_token;
283
284static inline wuffs_base__slice_token //
285wuffs_base__make_slice_token(wuffs_base__token* ptr, size_t len) {
286 wuffs_base__slice_token ret;
287 ret.ptr = ptr;
288 ret.len = len;
289 return ret;
290}
291
Nigel Tao36857982020-02-12 11:33:13 +1100292// --------
293
Nigel Tao737e31f2020-02-11 11:23:17 +1100294// wuffs_base__token_buffer_meta is the metadata for a
295// wuffs_base__token_buffer's data.
296typedef struct {
297 size_t wi; // Write index. Invariant: wi <= len.
298 size_t ri; // Read index. Invariant: ri <= wi.
299 uint64_t pos; // Position of the buffer start relative to the stream start.
300 bool closed; // No further writes are expected.
301} wuffs_base__token_buffer_meta;
302
303// wuffs_base__token_buffer is a 1-dimensional buffer (a pointer and length)
304// plus additional metadata.
305//
306// A value with all fields zero is a valid, empty buffer.
307typedef struct {
308 wuffs_base__slice_token data;
309 wuffs_base__token_buffer_meta meta;
310
311#ifdef __cplusplus
Nigel Tao9fd96e82020-03-16 21:46:21 +1100312 inline bool is_valid() const;
Nigel Tao737e31f2020-02-11 11:23:17 +1100313 inline void compact();
314 inline uint64_t reader_available() const;
315 inline uint64_t reader_token_position() const;
316 inline uint64_t writer_available() const;
317 inline uint64_t writer_token_position() const;
318#endif // __cplusplus
319
320} wuffs_base__token_buffer;
321
322static inline wuffs_base__token_buffer //
323wuffs_base__make_token_buffer(wuffs_base__slice_token data,
324 wuffs_base__token_buffer_meta meta) {
325 wuffs_base__token_buffer ret;
326 ret.data = data;
327 ret.meta = meta;
328 return ret;
329}
330
331static inline wuffs_base__token_buffer_meta //
332wuffs_base__make_token_buffer_meta(size_t wi,
333 size_t ri,
334 uint64_t pos,
335 bool closed) {
336 wuffs_base__token_buffer_meta ret;
337 ret.wi = wi;
338 ret.ri = ri;
339 ret.pos = pos;
340 ret.closed = closed;
341 return ret;
342}
343
344static inline wuffs_base__token_buffer //
Nigel Tao64dbd002020-04-02 22:11:42 +1100345wuffs_base__slice_token__reader(wuffs_base__slice_token s, bool closed) {
Nigel Tao9fd96e82020-03-16 21:46:21 +1100346 wuffs_base__token_buffer ret;
347 ret.data.ptr = s.ptr;
348 ret.data.len = s.len;
349 ret.meta.wi = s.len;
350 ret.meta.ri = 0;
351 ret.meta.pos = 0;
352 ret.meta.closed = closed;
353 return ret;
354}
355
356static inline wuffs_base__token_buffer //
Nigel Tao64dbd002020-04-02 22:11:42 +1100357wuffs_base__slice_token__writer(wuffs_base__slice_token s) {
Nigel Tao9fd96e82020-03-16 21:46:21 +1100358 wuffs_base__token_buffer ret;
359 ret.data.ptr = s.ptr;
360 ret.data.len = s.len;
361 ret.meta.wi = 0;
362 ret.meta.ri = 0;
363 ret.meta.pos = 0;
364 ret.meta.closed = false;
365 return ret;
366}
367
368static inline wuffs_base__token_buffer //
Nigel Tao737e31f2020-02-11 11:23:17 +1100369wuffs_base__empty_token_buffer() {
370 wuffs_base__token_buffer ret;
371 ret.data.ptr = NULL;
372 ret.data.len = 0;
373 ret.meta.wi = 0;
374 ret.meta.ri = 0;
375 ret.meta.pos = 0;
376 ret.meta.closed = false;
377 return ret;
378}
379
380static inline wuffs_base__token_buffer_meta //
381wuffs_base__empty_token_buffer_meta() {
382 wuffs_base__token_buffer_meta ret;
383 ret.wi = 0;
384 ret.ri = 0;
385 ret.pos = 0;
386 ret.closed = false;
387 return ret;
388}
389
Nigel Tao9fd96e82020-03-16 21:46:21 +1100390static inline bool //
391wuffs_base__token_buffer__is_valid(const wuffs_base__token_buffer* buf) {
392 if (buf) {
393 if (buf->data.ptr) {
394 return (buf->meta.ri <= buf->meta.wi) && (buf->meta.wi <= buf->data.len);
395 } else {
396 return (buf->meta.ri == 0) && (buf->meta.wi == 0) && (buf->data.len == 0);
397 }
398 }
399 return false;
400}
401
Nigel Tao737e31f2020-02-11 11:23:17 +1100402// wuffs_base__token_buffer__compact moves any written but unread tokens to the
403// start of the buffer.
404static inline void //
405wuffs_base__token_buffer__compact(wuffs_base__token_buffer* buf) {
406 if (!buf || (buf->meta.ri == 0)) {
407 return;
408 }
409 buf->meta.pos = wuffs_base__u64__sat_add(buf->meta.pos, buf->meta.ri);
410 size_t n = buf->meta.wi - buf->meta.ri;
411 if (n != 0) {
412 memmove(buf->data.ptr, buf->data.ptr + buf->meta.ri,
413 n * sizeof(wuffs_base__token));
414 }
415 buf->meta.wi = n;
416 buf->meta.ri = 0;
417}
418
419static inline uint64_t //
420wuffs_base__token_buffer__reader_available(
421 const wuffs_base__token_buffer* buf) {
422 return buf ? buf->meta.wi - buf->meta.ri : 0;
423}
424
425static inline uint64_t //
426wuffs_base__token_buffer__reader_token_position(
427 const wuffs_base__token_buffer* buf) {
428 return buf ? wuffs_base__u64__sat_add(buf->meta.pos, buf->meta.ri) : 0;
429}
430
431static inline uint64_t //
432wuffs_base__token_buffer__writer_available(
433 const wuffs_base__token_buffer* buf) {
434 return buf ? buf->data.len - buf->meta.wi : 0;
435}
436
437static inline uint64_t //
438wuffs_base__token_buffer__writer_token_position(
439 const wuffs_base__token_buffer* buf) {
440 return buf ? wuffs_base__u64__sat_add(buf->meta.pos, buf->meta.wi) : 0;
441}
442
443#ifdef __cplusplus
444
Nigel Tao9fd96e82020-03-16 21:46:21 +1100445inline bool //
446wuffs_base__token_buffer::is_valid() const {
447 return wuffs_base__token_buffer__is_valid(this);
448}
449
Nigel Tao737e31f2020-02-11 11:23:17 +1100450inline void //
451wuffs_base__token_buffer::compact() {
452 wuffs_base__token_buffer__compact(this);
453}
454
455inline uint64_t //
456wuffs_base__token_buffer::reader_available() const {
457 return wuffs_base__token_buffer__reader_available(this);
458}
459
460inline uint64_t //
461wuffs_base__token_buffer::reader_token_position() const {
462 return wuffs_base__token_buffer__reader_token_position(this);
463}
464
465inline uint64_t //
466wuffs_base__token_buffer::writer_available() const {
467 return wuffs_base__token_buffer__writer_available(this);
468}
469
470inline uint64_t //
471wuffs_base__token_buffer::writer_token_position() const {
472 return wuffs_base__token_buffer__writer_token_position(this);
473}
474
475#endif // __cplusplus