blob: 24a10a7f3335f955db60508e2a06fdb8174e2a80 [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 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 Taod1c928a2020-02-28 12:43:53 +110072#define WUFFS_BASE__TOKEN__VBD__FILLER__COMMENT_LINE 0x00001
73#define WUFFS_BASE__TOKEN__VBD__FILLER__COMMENT_BLOCK 0x00002
Nigel Taoa9d14882020-02-25 12:12:31 +110074
Nigel Taof45f01f2020-06-05 14:06:56 +100075// --------
Nigel Tao85fba7f2020-02-29 16:28:06 +110076
77#define WUFFS_BASE__TOKEN__VBD__STRUCTURE__PUSH 0x00001
78#define WUFFS_BASE__TOKEN__VBD__STRUCTURE__POP 0x00002
79#define WUFFS_BASE__TOKEN__VBD__STRUCTURE__FROM_NONE 0x00010
80#define WUFFS_BASE__TOKEN__VBD__STRUCTURE__FROM_LIST 0x00020
81#define WUFFS_BASE__TOKEN__VBD__STRUCTURE__FROM_DICT 0x00040
82#define WUFFS_BASE__TOKEN__VBD__STRUCTURE__TO_NONE 0x01000
83#define WUFFS_BASE__TOKEN__VBD__STRUCTURE__TO_LIST 0x02000
84#define WUFFS_BASE__TOKEN__VBD__STRUCTURE__TO_DICT 0x04000
85
Nigel Tao9d4eeb72020-02-26 11:36:30 +110086// --------
87
Nigel Taob9afc262020-07-03 13:36:13 +100088// DEFINITELY_FOO means that the destination bytes (and also the source bytes,
89// for 1_DST_1_SRC_COPY) are in the FOO format. Definitely means that the lack
90// of the bit means "maybe FOO". It does not necessarily mean "not FOO".
91//
92// CHAIN_ETC means that decoding the entire token chain forms a UTF-8 or ASCII
93// string, not just this current token. CHAIN_ETC_UTF_8 therefore distinguishes
94// Unicode (UTF-8) strings from byte strings. MUST means that the the token
95// producer (e.g. parser) must verify this. SHOULD means that the token
96// consumer (e.g. renderer) should verify this.
97//
98// When a CHAIN_ETC_UTF_8 bit is set, the parser must ensure that non-ASCII
99// code points (with multi-byte UTF-8 encodings) do not straddle token
100// boundaries. Checking UTF-8 validity can inspect each token separately.
101//
102// The lack of any particular bit is conservative: it is valid for all-ASCII
103// strings, in a single- or multi-token chain, to have none of these bits set.
Nigel Taod1c928a2020-02-28 12:43:53 +1100104#define WUFFS_BASE__TOKEN__VBD__STRING__DEFINITELY_UTF_8 0x00001
Nigel Taob9afc262020-07-03 13:36:13 +1000105#define WUFFS_BASE__TOKEN__VBD__STRING__CHAIN_MUST_BE_UTF_8 0x00002
106#define WUFFS_BASE__TOKEN__VBD__STRING__CHAIN_SHOULD_BE_UTF_8 0x00004
107#define WUFFS_BASE__TOKEN__VBD__STRING__DEFINITELY_ASCII 0x00010
108#define WUFFS_BASE__TOKEN__VBD__STRING__CHAIN_MUST_BE_ASCII 0x00020
109#define WUFFS_BASE__TOKEN__VBD__STRING__CHAIN_SHOULD_BE_ASCII 0x00040
Nigel Taoa9d14882020-02-25 12:12:31 +1100110
Nigel Taob9afc262020-07-03 13:36:13 +1000111// CONVERT_D_DST_S_SRC means that multiples of S source bytes (possibly padded)
112// produces multiples of D destination bytes. For example,
Nigel Tao9d4eeb72020-02-26 11:36:30 +1100113// CONVERT_1_DST_4_SRC_BACKSLASH_X means a source like "\\x23\\x67\\xAB", where
114// 12 src bytes encode 3 dst bytes.
115//
Nigel Tao478d1b82020-04-08 23:03:51 +1000116// Post-processing may further transform those D destination bytes (e.g. treat
117// "\\xFF" as the Unicode code point U+00FF instead of the byte 0xFF), but that
118// is out of scope of this VBD's semantics.
119//
Nigel Tao9d4eeb72020-02-26 11:36:30 +1100120// When src is the empty string, multiple conversion algorithms are applicable
121// (so these bits are not necessarily mutually exclusive), all producing the
122// same empty dst string.
Nigel Taob9afc262020-07-03 13:36:13 +1000123#define WUFFS_BASE__TOKEN__VBD__STRING__CONVERT_0_DST_1_SRC_DROP 0x00100
124#define WUFFS_BASE__TOKEN__VBD__STRING__CONVERT_1_DST_1_SRC_COPY 0x00200
125#define WUFFS_BASE__TOKEN__VBD__STRING__CONVERT_1_DST_2_SRC_HEXADECIMAL 0x00400
126#define WUFFS_BASE__TOKEN__VBD__STRING__CONVERT_1_DST_4_SRC_BACKSLASH_X 0x00800
127#define WUFFS_BASE__TOKEN__VBD__STRING__CONVERT_3_DST_4_SRC_BASE_64_STD 0x01000
128#define WUFFS_BASE__TOKEN__VBD__STRING__CONVERT_3_DST_4_SRC_BASE_64_URL 0x02000
129#define WUFFS_BASE__TOKEN__VBD__STRING__CONVERT_4_DST_5_SRC_ASCII_85 0x04000
Nigel Tao15cc8502020-07-09 21:23:38 +1000130#define WUFFS_BASE__TOKEN__VBD__STRING__CONVERT_5_DST_8_SRC_BASE_32_HEX 0x08000
131#define WUFFS_BASE__TOKEN__VBD__STRING__CONVERT_5_DST_8_SRC_BASE_32_STD 0x10000
Nigel Tao9d4eeb72020-02-26 11:36:30 +1100132
Nigel Taof45f01f2020-06-05 14:06:56 +1000133// --------
Nigel Taoa9d14882020-02-25 12:12:31 +1100134
Nigel Tao85fba7f2020-02-29 16:28:06 +1100135#define WUFFS_BASE__TOKEN__VBD__LITERAL__UNDEFINED 0x00001
136#define WUFFS_BASE__TOKEN__VBD__LITERAL__NULL 0x00002
137#define WUFFS_BASE__TOKEN__VBD__LITERAL__FALSE 0x00004
138#define WUFFS_BASE__TOKEN__VBD__LITERAL__TRUE 0x00008
139
Nigel Taof45f01f2020-06-05 14:06:56 +1000140// --------
Nigel Tao9d4eeb72020-02-26 11:36:30 +1100141
Nigel Taoa9d14882020-02-25 12:12:31 +1100142// For a source string of "123" or "0x9A", it is valid for a tokenizer to
Nigel Tao4e193592020-07-15 12:48:57 +1000143// return any combination of:
Nigel Taoc3ca77a2020-03-11 12:06:50 +1100144// - WUFFS_BASE__TOKEN__VBD__NUMBER__CONTENT_FLOATING_POINT.
145// - WUFFS_BASE__TOKEN__VBD__NUMBER__CONTENT_INTEGER_SIGNED.
146// - WUFFS_BASE__TOKEN__VBD__NUMBER__CONTENT_INTEGER_UNSIGNED.
Nigel Taoa9d14882020-02-25 12:12:31 +1100147//
148// For a source string of "+123" or "-0x9A", only the first two are valid.
149//
150// For a source string of "123.", only the first one is valid.
Nigel Taoc3ca77a2020-03-11 12:06:50 +1100151#define WUFFS_BASE__TOKEN__VBD__NUMBER__CONTENT_FLOATING_POINT 0x00001
152#define WUFFS_BASE__TOKEN__VBD__NUMBER__CONTENT_INTEGER_SIGNED 0x00002
153#define WUFFS_BASE__TOKEN__VBD__NUMBER__CONTENT_INTEGER_UNSIGNED 0x00004
154
Nigel Taod7c72372020-03-24 13:58:38 +1100155#define WUFFS_BASE__TOKEN__VBD__NUMBER__CONTENT_NEG_INF 0x00010
156#define WUFFS_BASE__TOKEN__VBD__NUMBER__CONTENT_POS_INF 0x00020
157#define WUFFS_BASE__TOKEN__VBD__NUMBER__CONTENT_NEG_NAN 0x00040
158#define WUFFS_BASE__TOKEN__VBD__NUMBER__CONTENT_POS_NAN 0x00080
159
Nigel Taoc3ca77a2020-03-11 12:06:50 +1100160// The number 300 might be represented as "\x01\x2C", "\x2C\x01\x00\x00" or
161// "300", which are big-endian, little-endian or text. For binary formats, the
Nigel Tao4e193592020-07-15 12:48:57 +1000162// token length (after adjusting for FORMAT_IGNORE_ETC) discriminates
163// e.g. u16 little-endian vs u32 little-endian.
Nigel Taoc3ca77a2020-03-11 12:06:50 +1100164#define WUFFS_BASE__TOKEN__VBD__NUMBER__FORMAT_BINARY_BIG_ENDIAN 0x00100
165#define WUFFS_BASE__TOKEN__VBD__NUMBER__FORMAT_BINARY_LITTLE_ENDIAN 0x00200
166#define WUFFS_BASE__TOKEN__VBD__NUMBER__FORMAT_TEXT 0x00400
Nigel Tao9d4eeb72020-02-26 11:36:30 +1100167
Nigel Tao4e193592020-07-15 12:48:57 +1000168#define WUFFS_BASE__TOKEN__VBD__NUMBER__FORMAT_IGNORE_FIRST_BYTE 0x01000
169
Nigel Tao9d4eeb72020-02-26 11:36:30 +1100170// --------
Nigel Taoa9d14882020-02-25 12:12:31 +1100171
Nigel Tao462f8662020-04-01 23:01:51 +1100172// wuffs_base__token__value returns the token's high 46 bits, sign-extended. A
173// negative value means an extended token, non-negative means a simple token.
174static inline int64_t //
Nigel Tao36857982020-02-12 11:33:13 +1100175wuffs_base__token__value(const wuffs_base__token* t) {
Nigel Tao462f8662020-04-01 23:01:51 +1100176 return ((int64_t)(t->repr)) >> WUFFS_BASE__TOKEN__VALUE__SHIFT;
Nigel Tao36857982020-02-12 11:33:13 +1100177}
178
Nigel Tao462f8662020-04-01 23:01:51 +1100179// wuffs_base__token__value_extension returns a negative value if the token was
180// not an extended token.
181static inline int64_t //
182wuffs_base__token__value_extension(const wuffs_base__token* t) {
183 return (~(int64_t)(t->repr)) >> WUFFS_BASE__TOKEN__VALUE_EXTENSION__SHIFT;
184}
185
186// wuffs_base__token__value_major returns a negative value if the token was not
187// a simple token.
188static inline int64_t //
Nigel Tao36857982020-02-12 11:33:13 +1100189wuffs_base__token__value_major(const wuffs_base__token* t) {
Nigel Tao462f8662020-04-01 23:01:51 +1100190 return ((int64_t)(t->repr)) >> WUFFS_BASE__TOKEN__VALUE_MAJOR__SHIFT;
191}
192
193// wuffs_base__token__value_base_category returns a negative value if the token
194// was not a simple token.
195static inline int64_t //
196wuffs_base__token__value_base_category(const wuffs_base__token* t) {
197 return ((int64_t)(t->repr)) >> WUFFS_BASE__TOKEN__VALUE_BASE_CATEGORY__SHIFT;
Nigel Tao36857982020-02-12 11:33:13 +1100198}
199
200static inline uint64_t //
201wuffs_base__token__value_minor(const wuffs_base__token* t) {
Nigel Tao496e88b2020-04-09 22:10:08 +1000202 return (t->repr >> WUFFS_BASE__TOKEN__VALUE_MINOR__SHIFT) & 0x1FFFFFF;
Nigel Tao36857982020-02-12 11:33:13 +1100203}
204
205static inline uint64_t //
206wuffs_base__token__value_base_detail(const wuffs_base__token* t) {
Nigel Tao462f8662020-04-01 23:01:51 +1100207 return (t->repr >> WUFFS_BASE__TOKEN__VALUE_BASE_DETAIL__SHIFT) & 0x1FFFFF;
Nigel Tao36857982020-02-12 11:33:13 +1100208}
209
Nigel Taoc9d4e342020-07-21 15:20:34 +1000210static inline int64_t //
211wuffs_base__token__value_base_detail__sign_extended(
212 const wuffs_base__token* t) {
213 // The VBD is 21 bits in the middle of t->repr. Left shift the high (64 - 21
214 // - ETC__SHIFT) bits off, then right shift (sign-extending) back down.
215 uint64_t u = t->repr << (43 - WUFFS_BASE__TOKEN__VALUE_BASE_DETAIL__SHIFT);
216 return ((int64_t)u) >> 43;
217}
218
Nigel Taod1c928a2020-02-28 12:43:53 +1100219static inline bool //
Nigel Tao496e88b2020-04-09 22:10:08 +1000220wuffs_base__token__continued(const wuffs_base__token* t) {
221 return t->repr & 0x10000;
Nigel Taod1c928a2020-02-28 12:43:53 +1100222}
223
Nigel Tao36857982020-02-12 11:33:13 +1100224static inline uint64_t //
225wuffs_base__token__length(const wuffs_base__token* t) {
Nigel Tao462f8662020-04-01 23:01:51 +1100226 return (t->repr >> WUFFS_BASE__TOKEN__LENGTH__SHIFT) & 0xFFFF;
Nigel Tao36857982020-02-12 11:33:13 +1100227}
228
229#ifdef __cplusplus
230
Nigel Tao462f8662020-04-01 23:01:51 +1100231inline int64_t //
Nigel Tao36857982020-02-12 11:33:13 +1100232wuffs_base__token::value() const {
233 return wuffs_base__token__value(this);
234}
235
Nigel Tao462f8662020-04-01 23:01:51 +1100236inline int64_t //
237wuffs_base__token::value_extension() const {
238 return wuffs_base__token__value_extension(this);
239}
240
241inline int64_t //
Nigel Tao36857982020-02-12 11:33:13 +1100242wuffs_base__token::value_major() const {
243 return wuffs_base__token__value_major(this);
244}
245
Nigel Tao462f8662020-04-01 23:01:51 +1100246inline int64_t //
247wuffs_base__token::value_base_category() const {
248 return wuffs_base__token__value_base_category(this);
249}
250
Nigel Tao36857982020-02-12 11:33:13 +1100251inline uint64_t //
252wuffs_base__token::value_minor() const {
253 return wuffs_base__token__value_minor(this);
254}
255
256inline uint64_t //
Nigel Tao36857982020-02-12 11:33:13 +1100257wuffs_base__token::value_base_detail() const {
258 return wuffs_base__token__value_base_detail(this);
259}
260
Nigel Taoc9d4e342020-07-21 15:20:34 +1000261inline int64_t //
262wuffs_base__token::value_base_detail__sign_extended() const {
263 return wuffs_base__token__value_base_detail__sign_extended(this);
264}
265
Nigel Taod1c928a2020-02-28 12:43:53 +1100266inline bool //
Nigel Tao496e88b2020-04-09 22:10:08 +1000267wuffs_base__token::continued() const {
268 return wuffs_base__token__continued(this);
Nigel Taod1c928a2020-02-28 12:43:53 +1100269}
270
Nigel Tao36857982020-02-12 11:33:13 +1100271inline uint64_t //
272wuffs_base__token::length() const {
273 return wuffs_base__token__length(this);
274}
275
276#endif // __cplusplus
277
278// --------
279
Nigel Tao737e31f2020-02-11 11:23:17 +1100280typedef WUFFS_BASE__SLICE(wuffs_base__token) wuffs_base__slice_token;
281
282static inline wuffs_base__slice_token //
283wuffs_base__make_slice_token(wuffs_base__token* ptr, size_t len) {
284 wuffs_base__slice_token ret;
285 ret.ptr = ptr;
286 ret.len = len;
287 return ret;
288}
289
Nigel Tao36857982020-02-12 11:33:13 +1100290// --------
291
Nigel Tao737e31f2020-02-11 11:23:17 +1100292// wuffs_base__token_buffer_meta is the metadata for a
293// wuffs_base__token_buffer's data.
294typedef struct {
295 size_t wi; // Write index. Invariant: wi <= len.
296 size_t ri; // Read index. Invariant: ri <= wi.
297 uint64_t pos; // Position of the buffer start relative to the stream start.
298 bool closed; // No further writes are expected.
299} wuffs_base__token_buffer_meta;
300
301// wuffs_base__token_buffer is a 1-dimensional buffer (a pointer and length)
302// plus additional metadata.
303//
304// A value with all fields zero is a valid, empty buffer.
305typedef struct {
306 wuffs_base__slice_token data;
307 wuffs_base__token_buffer_meta meta;
308
309#ifdef __cplusplus
Nigel Tao9fd96e82020-03-16 21:46:21 +1100310 inline bool is_valid() const;
Nigel Tao737e31f2020-02-11 11:23:17 +1100311 inline void compact();
312 inline uint64_t reader_available() const;
313 inline uint64_t reader_token_position() const;
314 inline uint64_t writer_available() const;
315 inline uint64_t writer_token_position() const;
316#endif // __cplusplus
317
318} wuffs_base__token_buffer;
319
320static inline wuffs_base__token_buffer //
321wuffs_base__make_token_buffer(wuffs_base__slice_token data,
322 wuffs_base__token_buffer_meta meta) {
323 wuffs_base__token_buffer ret;
324 ret.data = data;
325 ret.meta = meta;
326 return ret;
327}
328
329static inline wuffs_base__token_buffer_meta //
330wuffs_base__make_token_buffer_meta(size_t wi,
331 size_t ri,
332 uint64_t pos,
333 bool closed) {
334 wuffs_base__token_buffer_meta ret;
335 ret.wi = wi;
336 ret.ri = ri;
337 ret.pos = pos;
338 ret.closed = closed;
339 return ret;
340}
341
342static inline wuffs_base__token_buffer //
Nigel Tao64dbd002020-04-02 22:11:42 +1100343wuffs_base__slice_token__reader(wuffs_base__slice_token s, bool closed) {
Nigel Tao9fd96e82020-03-16 21:46:21 +1100344 wuffs_base__token_buffer ret;
345 ret.data.ptr = s.ptr;
346 ret.data.len = s.len;
347 ret.meta.wi = s.len;
348 ret.meta.ri = 0;
349 ret.meta.pos = 0;
350 ret.meta.closed = closed;
351 return ret;
352}
353
354static inline wuffs_base__token_buffer //
Nigel Tao64dbd002020-04-02 22:11:42 +1100355wuffs_base__slice_token__writer(wuffs_base__slice_token s) {
Nigel Tao9fd96e82020-03-16 21:46:21 +1100356 wuffs_base__token_buffer ret;
357 ret.data.ptr = s.ptr;
358 ret.data.len = s.len;
359 ret.meta.wi = 0;
360 ret.meta.ri = 0;
361 ret.meta.pos = 0;
362 ret.meta.closed = false;
363 return ret;
364}
365
366static inline wuffs_base__token_buffer //
Nigel Tao737e31f2020-02-11 11:23:17 +1100367wuffs_base__empty_token_buffer() {
368 wuffs_base__token_buffer ret;
369 ret.data.ptr = NULL;
370 ret.data.len = 0;
371 ret.meta.wi = 0;
372 ret.meta.ri = 0;
373 ret.meta.pos = 0;
374 ret.meta.closed = false;
375 return ret;
376}
377
378static inline wuffs_base__token_buffer_meta //
379wuffs_base__empty_token_buffer_meta() {
380 wuffs_base__token_buffer_meta ret;
381 ret.wi = 0;
382 ret.ri = 0;
383 ret.pos = 0;
384 ret.closed = false;
385 return ret;
386}
387
Nigel Tao9fd96e82020-03-16 21:46:21 +1100388static inline bool //
389wuffs_base__token_buffer__is_valid(const wuffs_base__token_buffer* buf) {
390 if (buf) {
391 if (buf->data.ptr) {
392 return (buf->meta.ri <= buf->meta.wi) && (buf->meta.wi <= buf->data.len);
393 } else {
394 return (buf->meta.ri == 0) && (buf->meta.wi == 0) && (buf->data.len == 0);
395 }
396 }
397 return false;
398}
399
Nigel Tao737e31f2020-02-11 11:23:17 +1100400// wuffs_base__token_buffer__compact moves any written but unread tokens to the
401// start of the buffer.
402static inline void //
403wuffs_base__token_buffer__compact(wuffs_base__token_buffer* buf) {
404 if (!buf || (buf->meta.ri == 0)) {
405 return;
406 }
407 buf->meta.pos = wuffs_base__u64__sat_add(buf->meta.pos, buf->meta.ri);
408 size_t n = buf->meta.wi - buf->meta.ri;
409 if (n != 0) {
410 memmove(buf->data.ptr, buf->data.ptr + buf->meta.ri,
411 n * sizeof(wuffs_base__token));
412 }
413 buf->meta.wi = n;
414 buf->meta.ri = 0;
415}
416
417static inline uint64_t //
418wuffs_base__token_buffer__reader_available(
419 const wuffs_base__token_buffer* buf) {
420 return buf ? buf->meta.wi - buf->meta.ri : 0;
421}
422
423static inline uint64_t //
424wuffs_base__token_buffer__reader_token_position(
425 const wuffs_base__token_buffer* buf) {
426 return buf ? wuffs_base__u64__sat_add(buf->meta.pos, buf->meta.ri) : 0;
427}
428
429static inline uint64_t //
430wuffs_base__token_buffer__writer_available(
431 const wuffs_base__token_buffer* buf) {
432 return buf ? buf->data.len - buf->meta.wi : 0;
433}
434
435static inline uint64_t //
436wuffs_base__token_buffer__writer_token_position(
437 const wuffs_base__token_buffer* buf) {
438 return buf ? wuffs_base__u64__sat_add(buf->meta.pos, buf->meta.wi) : 0;
439}
440
441#ifdef __cplusplus
442
Nigel Tao9fd96e82020-03-16 21:46:21 +1100443inline bool //
444wuffs_base__token_buffer::is_valid() const {
445 return wuffs_base__token_buffer__is_valid(this);
446}
447
Nigel Tao737e31f2020-02-11 11:23:17 +1100448inline void //
449wuffs_base__token_buffer::compact() {
450 wuffs_base__token_buffer__compact(this);
451}
452
453inline uint64_t //
454wuffs_base__token_buffer::reader_available() const {
455 return wuffs_base__token_buffer__reader_available(this);
456}
457
458inline uint64_t //
459wuffs_base__token_buffer::reader_token_position() const {
460 return wuffs_base__token_buffer__reader_token_position(this);
461}
462
463inline uint64_t //
464wuffs_base__token_buffer::writer_available() const {
465 return wuffs_base__token_buffer__writer_available(this);
466}
467
468inline uint64_t //
469wuffs_base__token_buffer::writer_token_position() const {
470 return wuffs_base__token_buffer__writer_token_position(this);
471}
472
473#endif // __cplusplus