base: add optional_u63 type

This commit is contained in:
Nigel Tao committed 2023-11-19 12:02:24 +11:00
1 parent e517ca4bee
commit 82ea1985b4
32 files changed
+470 -295

No files matched your search

+4 -2
View File
@@ -111,14 +111,16 @@ DecodeCbor(DecodeCborCallbacks& callbacks,
"wuffs_aux::DecodeCbor: internal error: io_buf is closed";
goto done;
}
io_buf->compact_retaining(dec->history_retain_length());
io_buf->compact_retaining(
dec->history_retain_length().value_or(UINT64_MAX));
if (io_buf->meta.wi >= io_buf->data.len) {
ret_error_message =
"wuffs_aux::DecodeCbor: internal error: io_buf is full";
goto done;
}
cursor_index = io_buf->meta.ri;
io_error_message = input.CopyIn(io_buf, dec->history_retain_length());
io_error_message = input.CopyIn(
io_buf, dec->history_retain_length().value_or(UINT64_MAX));
} else {
ret_error_message = tok_status.message();
goto done;
+10 -8
View File
@@ -436,8 +436,9 @@ redirect:
} else if (io_buf.meta.closed) {
return DecodeImageResult(DecodeImage_UnexpectedEndOfFile);
} else {
std::string error_message =
input.CopyIn(&io_buf, image_decoder->history_retain_length());
std::string error_message = input.CopyIn(
&io_buf,
image_decoder->history_retain_length().value_or(UINT64_MAX));
if (!error_message.empty()) {
return DecodeImageResult(std::move(error_message));
}
@@ -519,8 +520,8 @@ redirect:
} else if (io_buf.meta.closed) {
return DecodeImageResult(DecodeImage_UnexpectedEndOfFile);
} else {
std::string error_message =
input.CopyIn(&io_buf, image_decoder->history_retain_length());
std::string error_message = input.CopyIn(
&io_buf, image_decoder->history_retain_length().value_or(UINT64_MAX));
if (!error_message.empty()) {
return DecodeImageResult(std::move(error_message));
}
@@ -549,8 +550,8 @@ redirect:
message = DecodeImage_UnexpectedEndOfFile;
break;
} else {
std::string error_message =
input.CopyIn(&io_buf, image_decoder->history_retain_length());
std::string error_message = input.CopyIn(
&io_buf, image_decoder->history_retain_length().value_or(UINT64_MAX));
if (!error_message.empty()) {
message = std::move(error_message);
break;
@@ -578,8 +579,9 @@ redirect:
} else if (io_buf.meta.closed) {
return DecodeImageResult(DecodeImage_UnexpectedEndOfFile);
} else {
std::string error_message =
input.CopyIn(&io_buf, image_decoder->history_retain_length());
std::string error_message = input.CopyIn(
&io_buf,
image_decoder->history_retain_length().value_or(UINT64_MAX));
if (!error_message.empty()) {
return DecodeImageResult(std::move(error_message));
}
+54 -52
View File
@@ -54,58 +54,60 @@ DecodeJsonArgJsonPointer::DefaultValue() {
// --------
#define WUFFS_AUX__DECODE_JSON__GET_THE_NEXT_TOKEN \
while (tok_buf.meta.ri >= tok_buf.meta.wi) { \
if (tok_status.repr == nullptr) { \
goto done; \
} else if (tok_status.repr == wuffs_base__suspension__short_write) { \
tok_buf.compact(); \
} else if (tok_status.repr == wuffs_base__suspension__short_read) { \
if (!io_error_message.empty()) { \
ret_error_message = std::move(io_error_message); \
goto done; \
} else if (cursor_index != io_buf->meta.ri) { \
ret_error_message = \
"wuffs_aux::DecodeJson: internal error: bad cursor_index"; \
goto done; \
} else if (io_buf->meta.closed) { \
ret_error_message = \
"wuffs_aux::DecodeJson: internal error: io_buf is closed"; \
goto done; \
} \
io_buf->compact_retaining(dec->history_retain_length()); \
if (io_buf->meta.wi >= io_buf->data.len) { \
ret_error_message = \
"wuffs_aux::DecodeJson: internal error: io_buf is full"; \
goto done; \
} \
cursor_index = io_buf->meta.ri; \
io_error_message = input.CopyIn(io_buf, dec->history_retain_length()); \
} else { \
ret_error_message = tok_status.message(); \
goto done; \
} \
tok_status = \
dec->decode_tokens(&tok_buf, io_buf, wuffs_base__empty_slice_u8()); \
if ((tok_buf.meta.ri > tok_buf.meta.wi) || \
(tok_buf.meta.wi > tok_buf.data.len) || \
(io_buf->meta.ri > io_buf->meta.wi) || \
(io_buf->meta.wi > io_buf->data.len)) { \
ret_error_message = \
"wuffs_aux::DecodeJson: internal error: bad buffer indexes"; \
goto done; \
} \
} \
wuffs_base__token token = tok_buf.data.ptr[tok_buf.meta.ri++]; \
uint64_t token_len = token.length(); \
if ((io_buf->meta.ri < cursor_index) || \
((io_buf->meta.ri - cursor_index) < token_len)) { \
ret_error_message = \
"wuffs_aux::DecodeJson: internal error: bad token indexes"; \
goto done; \
} \
uint8_t* token_ptr = io_buf->data.ptr + cursor_index; \
(void)(token_ptr); \
#define WUFFS_AUX__DECODE_JSON__GET_THE_NEXT_TOKEN \
while (tok_buf.meta.ri >= tok_buf.meta.wi) { \
if (tok_status.repr == nullptr) { \
goto done; \
} else if (tok_status.repr == wuffs_base__suspension__short_write) { \
tok_buf.compact(); \
} else if (tok_status.repr == wuffs_base__suspension__short_read) { \
if (!io_error_message.empty()) { \
ret_error_message = std::move(io_error_message); \
goto done; \
} else if (cursor_index != io_buf->meta.ri) { \
ret_error_message = \
"wuffs_aux::DecodeJson: internal error: bad cursor_index"; \
goto done; \
} else if (io_buf->meta.closed) { \
ret_error_message = \
"wuffs_aux::DecodeJson: internal error: io_buf is closed"; \
goto done; \
} \
io_buf->compact_retaining( \
dec->history_retain_length().value_or(UINT64_MAX)); \
if (io_buf->meta.wi >= io_buf->data.len) { \
ret_error_message = \
"wuffs_aux::DecodeJson: internal error: io_buf is full"; \
goto done; \
} \
cursor_index = io_buf->meta.ri; \
io_error_message = input.CopyIn( \
io_buf, dec->history_retain_length().value_or(UINT64_MAX)); \
} else { \
ret_error_message = tok_status.message(); \
goto done; \
} \
tok_status = \
dec->decode_tokens(&tok_buf, io_buf, wuffs_base__empty_slice_u8()); \
if ((tok_buf.meta.ri > tok_buf.meta.wi) || \
(tok_buf.meta.wi > tok_buf.data.len) || \
(io_buf->meta.ri > io_buf->meta.wi) || \
(io_buf->meta.wi > io_buf->data.len)) { \
ret_error_message = \
"wuffs_aux::DecodeJson: internal error: bad buffer indexes"; \
goto done; \
} \
} \
wuffs_base__token token = tok_buf.data.ptr[tok_buf.meta.ri++]; \
uint64_t token_len = token.length(); \
if ((io_buf->meta.ri < cursor_index) || \
((io_buf->meta.ri - cursor_index) < token_len)) { \
ret_error_message = \
"wuffs_aux::DecodeJson: internal error: bad token indexes"; \
goto done; \
} \
uint8_t* token_ptr = io_buf->data.ptr + cursor_index; \
(void)(token_ptr); \
cursor_index += static_cast<size_t>(token_len)
// --------
+3
View File
@@ -185,6 +185,9 @@ wuffs_private_impl__u64__sat_sub_indirect(uint64_t* x, uint64_t y) {
#define wuffs_base__utility__make_bitvec256(e00, e01, e02, e03) \
wuffs_base__make_bitvec256(e00, e01, e02, e03)
#define wuffs_base__utility__make_optional_u63(h, v) \
wuffs_base__make_optional_u63(h, v)
// ---------------- Slices and Tables
// wuffs_private_impl__slice_u8__prefix returns up to the first up_to bytes of
+64
View File
@@ -894,6 +894,70 @@ wuffs_base__make_bitvec256(uint64_t e00,
// --------
// wuffs_base__optional_u63 is like a std::optional<uint64_t>, but for C (not
// just C++) and the value can only hold 63 bits (not 64).
//
// Do not manipulate repr directly; it is a private implementation detail.
typedef struct wuffs_base__optional_u63__struct {
uint64_t repr;
#ifdef __cplusplus
inline bool has_value() const;
inline uint64_t value() const;
inline uint64_t value_or(uint64_t default_value) const;
#endif // __cplusplus
} wuffs_base__optional_u63;
// wuffs_base__make_optional_u63 ignores value when has_value is false.
//
// Preconditions:
// - value < (1 << 63).
static inline wuffs_base__optional_u63 //
wuffs_base__make_optional_u63(bool has_value, uint64_t value) {
wuffs_base__optional_u63 res;
res.repr = has_value ? ((value << 1u) | 1u) : 0u;
return res;
}
static inline bool //
wuffs_base__optional_u63__has_value(const wuffs_base__optional_u63* o) {
return o->repr;
}
// wuffs_base__optional_u63__value returns zero when o does not have a value.
static inline uint64_t //
wuffs_base__optional_u63__value(const wuffs_base__optional_u63* o) {
return o->repr >> 1u;
}
static inline uint64_t //
wuffs_base__optional_u63__value_or(const wuffs_base__optional_u63* o,
uint64_t default_value) {
return o->repr ? (o->repr >> 1u) : default_value;
}
#ifdef __cplusplus
inline bool //
wuffs_base__optional_u63::has_value() const {
return wuffs_base__optional_u63__has_value(this);
}
inline uint64_t //
wuffs_base__optional_u63::value() const {
return wuffs_base__optional_u63__value(this);
}
inline uint64_t //
wuffs_base__optional_u63::value_or(uint64_t default_value) const {
return wuffs_base__optional_u63__value_or(this, default_value);
}
#endif // __cplusplus
// --------
// The "defined(__clang__)" isn't redundant. While vanilla clang defines
// __GNUC__, clang-cl (which mimics MSVC's cl.exe) does not.
#if (defined(__GNUC__) || defined(__clang__)) && (__SIZEOF_LONG__ == 8)
+2 -1
View File
@@ -169,7 +169,8 @@ wuffs_base__io_buffer__compact(wuffs_base__io_buffer* buf) {
// wuffs_base__io_buffer__compact_retaining moves any written but unread bytes
// closer to the start of the buffer. It retains H bytes of history (the most
// recently read bytes), where H is min(buf->meta.ri, history_retain_length).
// A postcondition is that buf->meta.ri == H.
// It is therefore a no-op if history_retain_length is UINT64_MAX. A
// postcondition is that buf->meta.ri == H.
//
// wuffs_base__io_buffer__compact_retaining(0) is equivalent to
// wuffs_base__io_buffer__compact().
+2 -1
View File
@@ -451,7 +451,8 @@ wuffs_base__token_buffer__compact(wuffs_base__token_buffer* buf) {
// wuffs_base__token_buffer__compact_retaining moves any written but unread
// tokens closer to the start of the buffer. It retains H tokens of history
// (the most recently read tokens), where H is min(buf->meta.ri,
// history_retain_length). A postcondition is that buf->meta.ri == H.
// history_retain_length). It is therefore a no-op if history_retain_length is
// UINT64_MAX. A postcondition is that buf->meta.ri == H.
//
// wuffs_base__token_buffer__compact_retaining(0) is equivalent to
// wuffs_base__token_buffer__compact().
+3
View File
@@ -351,6 +351,9 @@ func writeOutParamZeroValue(b *buffer, tm *t.Map, typ *a.TypeExpr) error {
case t.IDBitvec256:
b.writes("wuffs_base__utility__make_bitvec256(0u, 0u, 0u, 0u)")
return nil
case t.IDOptionalU63:
b.writes("wuffs_base__utility__make_optional_u63(false, 0u)")
return nil
case t.IDRangeIEU32:
b.writes("wuffs_base__utility__empty_range_ie_u32()")
return nil