Skip to content

Commit

Permalink
aux: add QuirkKeyValuePair type
Browse files Browse the repository at this point in the history
  • Loading branch information
nigeltao committed Aug 4, 2024
1 parent 3215f6a commit ffb64df
Show file tree
Hide file tree
Showing 12 changed files with 111 additions and 120 deletions.
5 changes: 4 additions & 1 deletion example/imageviewer/imageviewer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,10 @@ load_image(const char* filename) {
dia_flags |= wuffs_aux::DecodeImageArgFlags::REPORT_METADATA_GAMA;
}

uint32_t wuffs_base__quirk_quality = WUFFS_BASE__QUIRK_QUALITY;
const wuffs_aux::QuirkKeyValuePair wuffs_base__quirk_quality = {
WUFFS_BASE__QUIRK_QUALITY,
WUFFS_BASE__QUIRK_QUALITY__VALUE__LOWER_QUALITY,
};

MyDecodeImageCallbacks callbacks;
wuffs_aux::sync_io::FileInput input(file);
Expand Down
22 changes: 11 additions & 11 deletions example/json-to-cbor/json-to-cbor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ static const char* g_usage =
uint8_t g_dst_array[DST_BUFFER_ARRAY_SIZE];
wuffs_base__io_buffer g_dst;

std::vector<uint32_t> g_quirks;
std::vector<wuffs_aux::QuirkKeyValuePair> g_quirks;

struct {
int remaining_argc;
Expand All @@ -149,9 +149,9 @@ struct {
std::string //
parse_flags(int argc, char** argv) {
#if defined(WUFFS_EXAMPLE_SPEAK_JWCC_NOT_JSON)
g_quirks.push_back(WUFFS_JSON__QUIRK_ALLOW_COMMENT_BLOCK);
g_quirks.push_back(WUFFS_JSON__QUIRK_ALLOW_COMMENT_LINE);
g_quirks.push_back(WUFFS_JSON__QUIRK_ALLOW_EXTRA_COMMA);
g_quirks.push_back({WUFFS_JSON__QUIRK_ALLOW_COMMENT_BLOCK, 1});
g_quirks.push_back({WUFFS_JSON__QUIRK_ALLOW_COMMENT_LINE, 1});
g_quirks.push_back({WUFFS_JSON__QUIRK_ALLOW_EXTRA_COMMA, 1});
#endif

int c = (argc > 0) ? 1 : 0; // Skip argv[0], the program name.
Expand All @@ -175,22 +175,22 @@ parse_flags(int argc, char** argv) {
}

if (!strcmp(arg, "input-allow-comments")) {
g_quirks.push_back(WUFFS_JSON__QUIRK_ALLOW_COMMENT_BLOCK);
g_quirks.push_back(WUFFS_JSON__QUIRK_ALLOW_COMMENT_LINE);
g_quirks.push_back({WUFFS_JSON__QUIRK_ALLOW_COMMENT_BLOCK, 1});
g_quirks.push_back({WUFFS_JSON__QUIRK_ALLOW_COMMENT_LINE, 1});
continue;
}
if (!strcmp(arg, "input-allow-extra-comma")) {
g_quirks.push_back(WUFFS_JSON__QUIRK_ALLOW_EXTRA_COMMA);
g_quirks.push_back({WUFFS_JSON__QUIRK_ALLOW_EXTRA_COMMA, 1});
continue;
}
if (!strcmp(arg, "input-allow-inf-nan-numbers")) {
g_quirks.push_back(WUFFS_JSON__QUIRK_ALLOW_INF_NAN_NUMBERS);
g_quirks.push_back({WUFFS_JSON__QUIRK_ALLOW_INF_NAN_NUMBERS, 1});
continue;
}
if (!strcmp(arg, "input-jwcc") || !strcmp(arg, "jwcc")) {
g_quirks.push_back(WUFFS_JSON__QUIRK_ALLOW_COMMENT_BLOCK);
g_quirks.push_back(WUFFS_JSON__QUIRK_ALLOW_COMMENT_LINE);
g_quirks.push_back(WUFFS_JSON__QUIRK_ALLOW_EXTRA_COMMA);
g_quirks.push_back({WUFFS_JSON__QUIRK_ALLOW_COMMENT_BLOCK, 1});
g_quirks.push_back({WUFFS_JSON__QUIRK_ALLOW_COMMENT_LINE, 1});
g_quirks.push_back({WUFFS_JSON__QUIRK_ALLOW_EXTRA_COMMA, 1});
continue;
}

Expand Down
24 changes: 12 additions & 12 deletions example/jsonfindptrs/jsonfindptrs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ static const char* g_usage =

// ----

std::vector<uint32_t> g_quirks;
std::vector<wuffs_aux::QuirkKeyValuePair> g_quirks;

std::string g_dst;

Expand All @@ -239,9 +239,9 @@ parse_flags(int argc, char** argv) {
g_flags.max_output_depth = 0xFFFFFFFF;

#if defined(WUFFS_EXAMPLE_SPEAK_JWCC_NOT_JSON)
g_quirks.push_back(WUFFS_JSON__QUIRK_ALLOW_COMMENT_BLOCK);
g_quirks.push_back(WUFFS_JSON__QUIRK_ALLOW_COMMENT_LINE);
g_quirks.push_back(WUFFS_JSON__QUIRK_ALLOW_EXTRA_COMMA);
g_quirks.push_back({WUFFS_JSON__QUIRK_ALLOW_COMMENT_BLOCK, 1});
g_quirks.push_back({WUFFS_JSON__QUIRK_ALLOW_COMMENT_LINE, 1});
g_quirks.push_back({WUFFS_JSON__QUIRK_ALLOW_EXTRA_COMMA, 1});
#endif

int c = (argc > 0) ? 1 : 0; // Skip argv[0], the program name.
Expand Down Expand Up @@ -281,22 +281,22 @@ parse_flags(int argc, char** argv) {
return g_usage;
}
if (!strcmp(arg, "input-allow-comments")) {
g_quirks.push_back(WUFFS_JSON__QUIRK_ALLOW_COMMENT_BLOCK);
g_quirks.push_back(WUFFS_JSON__QUIRK_ALLOW_COMMENT_LINE);
g_quirks.push_back({WUFFS_JSON__QUIRK_ALLOW_COMMENT_BLOCK, 1});
g_quirks.push_back({WUFFS_JSON__QUIRK_ALLOW_COMMENT_LINE, 1});
continue;
}
if (!strcmp(arg, "input-allow-extra-comma")) {
g_quirks.push_back(WUFFS_JSON__QUIRK_ALLOW_EXTRA_COMMA);
g_quirks.push_back({WUFFS_JSON__QUIRK_ALLOW_EXTRA_COMMA, 1});
continue;
}
if (!strcmp(arg, "input-allow-inf-nan-numbers")) {
g_quirks.push_back(WUFFS_JSON__QUIRK_ALLOW_INF_NAN_NUMBERS);
g_quirks.push_back({WUFFS_JSON__QUIRK_ALLOW_INF_NAN_NUMBERS, 1});
continue;
}
if (!strcmp(arg, "input-jwcc") || !strcmp(arg, "jwcc")) {
g_quirks.push_back(WUFFS_JSON__QUIRK_ALLOW_COMMENT_BLOCK);
g_quirks.push_back(WUFFS_JSON__QUIRK_ALLOW_COMMENT_LINE);
g_quirks.push_back(WUFFS_JSON__QUIRK_ALLOW_EXTRA_COMMA);
g_quirks.push_back({WUFFS_JSON__QUIRK_ALLOW_COMMENT_BLOCK, 1});
g_quirks.push_back({WUFFS_JSON__QUIRK_ALLOW_COMMENT_LINE, 1});
g_quirks.push_back({WUFFS_JSON__QUIRK_ALLOW_EXTRA_COMMA, 1});
continue;
}
if (!strncmp(arg, "q=", 2) || !strncmp(arg, "query=", 6)) {
Expand Down Expand Up @@ -547,7 +547,7 @@ main1(int argc, char** argv) {
TRY(parse_flags(argc, argv));
if (!g_flags.strict_json_pointer_syntax) {
g_quirks.push_back(
WUFFS_JSON__QUIRK_JSON_POINTER_ALLOW_TILDE_N_TILDE_R_TILDE_T);
{WUFFS_JSON__QUIRK_JSON_POINTER_ALLOW_TILDE_N_TILDE_R_TILDE_T, 1});
}

FILE* in = stdin;
Expand Down
4 changes: 2 additions & 2 deletions fuzz/c/std/json_fuzzer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -499,11 +499,11 @@ fuzz_cpp(const uint8_t* in_ptr, size_t in_len, uint64_t hash) {
const char* json_pointer = json_pointers[hash & 15];
hash = wuffs_base__u64__rotate_right(hash, 4);

std::vector<uint32_t> quirks;
std::vector<wuffs_aux::QuirkKeyValuePair> quirks;
for (uint32_t i = 0; g_quirks[i]; i++) {
uint64_t bit = 1 << (i & 63);
if (hash & bit) {
quirks.push_back(g_quirks[i]);
quirks.push_back({g_quirks[i], 1});
}
}

Expand Down
3 changes: 3 additions & 0 deletions internal/cgen/auxiliary/base.hh
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <stdio.h>

#include <string>
#include <utility>

namespace wuffs_aux {

Expand All @@ -28,6 +29,8 @@ using IOBuffer = wuffs_base__io_buffer;
// nullptr, since calling free(nullptr) is a no-op.
using MemOwner = std::unique_ptr<void, decltype(&free)>;

using QuirkKeyValuePair = std::pair<uint32_t, uint64_t>;

namespace sync_io {

// --------
Expand Down
14 changes: 6 additions & 8 deletions internal/cgen/auxiliary/cbor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,13 @@ DecodeCborCallbacks::Done(DecodeCborResult& result,
sync_io::Input& input,
IOBuffer& buffer) {}

DecodeCborArgQuirks::DecodeCborArgQuirks(wuffs_base__slice_u32 repr0)
: repr(repr0) {}

DecodeCborArgQuirks::DecodeCborArgQuirks(uint32_t* ptr0, size_t len0)
: repr(wuffs_base__make_slice_u32(ptr0, len0)) {}
DecodeCborArgQuirks::DecodeCborArgQuirks(const QuirkKeyValuePair* ptr0,
const size_t len0)
: ptr(ptr0), len(len0) {}

DecodeCborArgQuirks //
DecodeCborArgQuirks::DefaultValue() {
return DecodeCborArgQuirks(wuffs_base__empty_slice_u32());
return DecodeCborArgQuirks(nullptr, 0);
}

DecodeCborResult //
Expand Down Expand Up @@ -65,8 +63,8 @@ DecodeCbor(DecodeCborCallbacks& callbacks,
ret_error_message = "wuffs_aux::DecodeCbor: out of memory";
goto done;
}
for (size_t i = 0; i < quirks.repr.len; i++) {
dec->set_quirk(quirks.repr.ptr[i], 1);
for (size_t i = 0; i < quirks.len; i++) {
dec->set_quirk(quirks.ptr[i].first, quirks.ptr[i].second);
}

// Prepare the wuffs_base__tok_buffer. 256 tokens is 2KiB.
Expand Down
7 changes: 4 additions & 3 deletions internal/cgen/auxiliary/cbor.hh
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,14 @@ class DecodeCborCallbacks {

// DecodeCborArgQuirks wraps an optional argument to DecodeCbor.
struct DecodeCborArgQuirks {
explicit DecodeCborArgQuirks(wuffs_base__slice_u32 repr0);
explicit DecodeCborArgQuirks(uint32_t* ptr, size_t len);
explicit DecodeCborArgQuirks(const QuirkKeyValuePair* ptr0,
const size_t len0);

// DefaultValue returns an empty slice.
static DecodeCborArgQuirks DefaultValue();

wuffs_base__slice_u32 repr;
const QuirkKeyValuePair* ptr;
const size_t len;
};

// DecodeCbor calls callbacks based on the CBOR-formatted data in input.
Expand Down
32 changes: 12 additions & 20 deletions internal/cgen/auxiliary/image.cc
Original file line number Diff line number Diff line change
Expand Up @@ -197,15 +197,13 @@ const char DecodeImage_UnsupportedPixelConfiguration[] = //
const char DecodeImage_UnsupportedPixelFormat[] = //
"wuffs_aux::DecodeImage: unsupported pixel format";

DecodeImageArgQuirks::DecodeImageArgQuirks(wuffs_base__slice_u32 repr0)
: repr(repr0) {}

DecodeImageArgQuirks::DecodeImageArgQuirks(uint32_t* ptr0, size_t len0)
: repr(wuffs_base__make_slice_u32(ptr0, len0)) {}
DecodeImageArgQuirks::DecodeImageArgQuirks(const QuirkKeyValuePair* ptr0,
const size_t len0)
: ptr(ptr0), len(len0) {}

DecodeImageArgQuirks //
DecodeImageArgQuirks::DefaultValue() {
return DecodeImageArgQuirks(wuffs_base__empty_slice_u32());
return DecodeImageArgQuirks(nullptr, 0);
}

DecodeImageArgFlags::DecodeImageArgFlags(uint64_t repr0) : repr(repr0) {}
Expand Down Expand Up @@ -303,7 +301,8 @@ DecodeImage0(wuffs_base__image_decoder::unique_ptr& image_decoder,
DecodeImageCallbacks& callbacks,
sync_io::Input& input,
wuffs_base__io_buffer& io_buf,
wuffs_base__slice_u32 quirks,
const QuirkKeyValuePair* quirks_ptr,
const size_t quirks_len,
uint64_t flags,
wuffs_base__pixel_blend pixel_blend,
wuffs_base__color_u32_argb_premul background_color,
Expand Down Expand Up @@ -386,15 +385,8 @@ DecodeImage0(wuffs_base__image_decoder::unique_ptr& image_decoder,
}

// Apply quirks.
for (size_t i = 0; i < quirks.len; i++) {
// TODO: don't special-case this.
if (quirks.ptr[i] == WUFFS_BASE__QUIRK_QUALITY) {
image_decoder->set_quirk(
WUFFS_BASE__QUIRK_QUALITY,
WUFFS_BASE__QUIRK_QUALITY__VALUE__LOWER_QUALITY);
continue;
}
image_decoder->set_quirk(quirks.ptr[i], 1);
for (size_t i = 0; i < quirks_len; i++) {
image_decoder->set_quirk(quirks_ptr[i].first, quirks_ptr[i].second);
}

// Apply flags.
Expand Down Expand Up @@ -621,10 +613,10 @@ DecodeImage(DecodeImageCallbacks& callbacks,
}

wuffs_base__image_decoder::unique_ptr image_decoder(nullptr);
DecodeImageResult result =
DecodeImage0(image_decoder, callbacks, input, *io_buf, quirks.repr,
flags.repr, pixel_blend.repr, background_color.repr,
max_incl_dimension.repr, max_incl_metadata_length.repr);
DecodeImageResult result = DecodeImage0(
image_decoder, callbacks, input, *io_buf, quirks.ptr, quirks.len,
flags.repr, pixel_blend.repr, background_color.repr,
max_incl_dimension.repr, max_incl_metadata_length.repr);
callbacks.Done(result, input, *io_buf, std::move(image_decoder));
return result;
}
Expand Down
7 changes: 4 additions & 3 deletions internal/cgen/auxiliary/image.hh
Original file line number Diff line number Diff line change
Expand Up @@ -200,13 +200,14 @@ extern const char DecodeImage_UnsupportedPixelFormat[];

// DecodeImageArgQuirks wraps an optional argument to DecodeImage.
struct DecodeImageArgQuirks {
explicit DecodeImageArgQuirks(wuffs_base__slice_u32 repr0);
explicit DecodeImageArgQuirks(uint32_t* ptr, size_t len);
explicit DecodeImageArgQuirks(const QuirkKeyValuePair* ptr0,
const size_t len0);

// DefaultValue returns an empty slice.
static DecodeImageArgQuirks DefaultValue();

wuffs_base__slice_u32 repr;
const QuirkKeyValuePair* ptr;
const size_t len;
};

// DecodeImageArgFlags wraps an optional argument to DecodeImage.
Expand Down
18 changes: 8 additions & 10 deletions internal/cgen/auxiliary/json.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,13 @@ const char DecodeJson_BadJsonPointer[] = //
const char DecodeJson_NoMatch[] = //
"wuffs_aux::DecodeJson: no match";

DecodeJsonArgQuirks::DecodeJsonArgQuirks(wuffs_base__slice_u32 repr0)
: repr(repr0) {}

DecodeJsonArgQuirks::DecodeJsonArgQuirks(uint32_t* ptr0, size_t len0)
: repr(wuffs_base__make_slice_u32(ptr0, len0)) {}
DecodeJsonArgQuirks::DecodeJsonArgQuirks(const QuirkKeyValuePair* ptr0,
const size_t len0)
: ptr(ptr0), len(len0) {}

DecodeJsonArgQuirks //
DecodeJsonArgQuirks::DefaultValue() {
return DecodeJsonArgQuirks(wuffs_base__empty_slice_u32());
return DecodeJsonArgQuirks(nullptr, 0);
}

DecodeJsonArgJsonPointer::DecodeJsonArgJsonPointer(std::string repr0)
Expand Down Expand Up @@ -386,11 +384,11 @@ DecodeJson(DecodeJsonCallbacks& callbacks,
goto done;
}
bool allow_tilde_n_tilde_r_tilde_t = false;
for (size_t i = 0; i < quirks.repr.len; i++) {
dec->set_quirk(quirks.repr.ptr[i], 1);
if (quirks.repr.ptr[i] ==
for (size_t i = 0; i < quirks.len; i++) {
dec->set_quirk(quirks.ptr[i].first, quirks.ptr[i].second);
if (quirks.ptr[i].first ==
WUFFS_JSON__QUIRK_JSON_POINTER_ALLOW_TILDE_N_TILDE_R_TILDE_T) {
allow_tilde_n_tilde_r_tilde_t = true;
allow_tilde_n_tilde_r_tilde_t = (quirks.ptr[i].second != 0);
}
}

Expand Down
7 changes: 4 additions & 3 deletions internal/cgen/auxiliary/json.hh
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,14 @@ extern const char DecodeJson_NoMatch[];

// DecodeJsonArgQuirks wraps an optional argument to DecodeJson.
struct DecodeJsonArgQuirks {
explicit DecodeJsonArgQuirks(wuffs_base__slice_u32 repr0);
explicit DecodeJsonArgQuirks(uint32_t* ptr, size_t len);
explicit DecodeJsonArgQuirks(const QuirkKeyValuePair* ptr0,
const size_t len0);

// DefaultValue returns an empty slice.
static DecodeJsonArgQuirks DefaultValue();

wuffs_base__slice_u32 repr;
const QuirkKeyValuePair* ptr;
const size_t len;
};

// DecodeJsonArgJsonPointer wraps an optional argument to DecodeJson.
Expand Down
Loading

0 comments on commit ffb64df

Please sign in to comment.