more stuff

This commit is contained in:
sfja 2026-03-27 21:55:18 +01:00
parent dde8c3ee42
commit 283ffaaad6
6 changed files with 56 additions and 38 deletions

2
.vscode/launch.json vendored
View File

@ -9,7 +9,7 @@
"type": "cppdbg", "type": "cppdbg",
"request": "launch", "request": "launch",
"program": "${workspaceFolder}/build/jq", "program": "${workspaceFolder}/build/jq",
"args": ["", "data.json"], "args": ["[11350].payload.issues.user", "large-file-formatted.json"],
"stopAtEntry": true, "stopAtEntry": true,
"cwd": "${workspaceFolder}", "cwd": "${workspaceFolder}",
"environment": [], "environment": [],

View File

@ -4,14 +4,24 @@ MAKEFLAGS += -j16
CFLAGS=-std=c17 -pedantic-errors -Wall -Wextra -Wconversion CFLAGS=-std=c17 -pedantic-errors -Wall -Wextra -Wconversion
LDFLAGS= LDFLAGS=
# CFLAGS+=-fsanitize=address # Some of the sussy stuff needs this
CFLAGS+=-Wno-strict-aliasing
ASAN=0
ifeq ($(ASAN),1)
CFLAGS += -fsanitize=address
endif
LTO=0
ifeq ($(ASAN),1)
CFLAGS += -flto=auto
endif
RELEASE=0 RELEASE=0
ifeq ($(RELEASE),1) ifeq ($(RELEASE),1)
CFLAGS += -O3 CFLAGS += -O3
else else
CFLAGS += -g CFLAGS += -g -ggdb
endif endif
build_dir = build build_dir = build
@ -30,7 +40,8 @@ target=$(build_dir)/jq
all: $(target) all: $(target)
debug: $(target) debug: $(target)
gdb -ex 'r' --args build/jq '.' data.json # gdb -ex 'r' --args build/jq '.' data.json
gdb -ex 'r' --args build/jq '[11350].payload.issues.user' large-file-formatted.json
$(target): $(sources:%.c=$(obj_dir)/%.o) $(target): $(sources:%.c=$(obj_dir)/%.o)
gcc -o $@ $(CFLAGS) $(LDFLAGS) $^ gcc -o $@ $(CFLAGS) $(LDFLAGS) $^

View File

@ -270,10 +270,16 @@ static struct hash_entry *make_entry(struct hashmap *m, uint64_t hash)
static void insert_key( static void insert_key(
struct hashmap *m, char const *key, size_t key_size, uint64_t hash) struct hashmap *m, char const *key, size_t key_size, uint64_t hash)
{ {
key_size = key_size != 0 ? key_size : strlen(key); char *key_value;
char *key_value = malloc(key_size + 1); if (key_size != 0) {
key_value = malloc(key_size + 1);
strncpy(key_value, key, key_size); strncpy(key_value, key, key_size);
key_value[key_size] = '\0'; key_value[key_size] = '\0';
} else {
key_size = strlen(key);
key_value = malloc(key_size + 1);
strcpy(key_value, key);
}
struct hash_key_entry key_entry = { struct hash_key_entry key_entry = {
.hash = hash, .hash = hash,

View File

@ -41,7 +41,8 @@ struct json_value *json_new_float(double val);
size_t json_array_count(struct json_value const *array); size_t json_array_count(struct json_value const *array);
struct json_value *json_idx(struct json_value *array, size_t idx); struct json_value *json_idx(struct json_value *array, size_t idx);
struct json_value const *json_idx_const(struct json_value const *array, size_t idx); struct json_value const *json_idx_const(
struct json_value const *array, size_t idx);
void json_push(struct json_value *array, struct json_value *value); void json_push(struct json_value *array, struct json_value *value);
struct json_value *json_key(struct json_value *object, char const *key); struct json_value *json_key(struct json_value *object, char const *key);
@ -55,19 +56,18 @@ void json_set_sized(struct json_value *object,
char const *key, char const *key,
size_t key_size, size_t key_size,
struct json_value *value); struct json_value *value);
void json_object_keys( void json_object_keys(struct json_value const *object,
struct json_value const *object, struct hash_key_entry const **keys, size_t *count); struct hash_key_entry const **keys,
size_t *count);
struct json_value *json_parse( struct json_value *json_parse(
char const *text, size_t text_size, struct blockalloc *alloc); char const *text, size_t text_size, struct blockalloc *alloc);
struct json_value *json_query(struct json_value *val, char const *query); struct json_value *json_query(struct json_value *val, char const *query);
typedef int JsonWriteCb(void* self, const char* data, size_t size); typedef int JsonWriteCb(void *self, char const *data, size_t size);
int json_stringify( int json_stringify(
struct json_value const *node, struct json_value const *node, JsonWriteCb write, void *self);
JsonWriteCb write,
void* self);
#endif #endif

View File

@ -57,8 +57,7 @@ static struct tok t_tok(struct tokenizer *t, enum tokty ty, size_t idx)
} }
static struct tok tokenizer_next(struct tokenizer *t); static struct tok tokenizer_next(struct tokenizer *t);
static struct tok t_make_number_tok( static struct tok t_make_number_tok(struct tokenizer *t, size_t idx, size_t *i)
struct tokenizer *t, size_t idx, size_t *i)
{ {
while (*i < t->len && t->text[*i] >= '0' && t->text[*i] <= '9') { while (*i < t->len && t->text[*i] >= '0' && t->text[*i] <= '9') {
t_step(t); t_step(t);
@ -66,8 +65,7 @@ static struct tok t_make_number_tok(
return t_tok(t, tt_int, idx); return t_tok(t, tt_int, idx);
} }
static struct tok t_make_string_tok( static struct tok t_make_string_tok(struct tokenizer *t, size_t idx, size_t *i)
struct tokenizer *t, size_t idx, size_t *i)
{ {
t_step(t); t_step(t);
while (*i < t->len && t->text[*i] != '\"') { while (*i < t->len && t->text[*i] != '\"') {
@ -209,22 +207,28 @@ struct json_value *resolve_next_path_seg(
case pt_ident_key: { case pt_ident_key: {
if (!json_is(node, json_object)) if (!json_is(node, json_object))
return NULL; return NULL;
struct json_value *child = struct json_value *child
json_key_sized(node, seg.tok.ptr, seg.tok.len); = json_key_sized(node, seg.tok.ptr, seg.tok.len);
if (!child)
return NULL;
return resolve_next_path_seg(p, child); return resolve_next_path_seg(p, child);
} }
case pt_string_key: { case pt_string_key: {
if (!json_is(node, json_object)) if (!json_is(node, json_object))
return NULL; return NULL;
struct json_value *child = struct json_value *child
json_key_sized(node, seg.tok.ptr + 1, seg.tok.len - 2); = json_key_sized(node, seg.tok.ptr + 1, seg.tok.len - 2);
if (!child)
return NULL;
return resolve_next_path_seg(p, child); return resolve_next_path_seg(p, child);
} }
case pt_idx: { case pt_idx: {
if (!json_is(node, json_array)) if (!json_is(node, json_array))
return NULL; return NULL;
struct json_value *child = struct json_value *child
json_idx(node, strtoull(seg.tok.ptr, NULL, 10)); = json_idx(node, strtoull(seg.tok.ptr, NULL, 10));
if (!child)
return NULL;
return resolve_next_path_seg(p, child); return resolve_next_path_seg(p, child);
} }
} }
@ -248,5 +252,3 @@ struct json_value *json_query(struct json_value *val, char const *query)
return resolve_next_path_seg(&p, val); return resolve_next_path_seg(&p, val);
} }

View File

@ -49,8 +49,7 @@ static void set_shallow_val(struct json_value **ptr, size_t val)
static struct json_value *new_shallow(enum json_type ty) static struct json_value *new_shallow(enum json_type ty)
{ {
struct json_value *shallow_val = 0; struct json_value *shallow_val = (struct json_value *)0x2;
*(size_t *)&shallow_val |= 0x2;
set_shallow_ty(&shallow_val, ty); set_shallow_ty(&shallow_val, ty);
return shallow_val; return shallow_val;
} }
@ -259,7 +258,8 @@ struct json_value *json_idx(struct json_value *array, size_t idx)
return smallarray_get(&array->array_vals, idx); return smallarray_get(&array->array_vals, idx);
} }
struct json_value const *json_idx_const(struct json_value const *array, size_t idx) struct json_value const *json_idx_const(
struct json_value const *array, size_t idx)
{ {
assert(array->ty == json_array); assert(array->ty == json_array);
if (idx >= smallarray_count(&array->array_vals)) if (idx >= smallarray_count(&array->array_vals))
@ -293,7 +293,6 @@ struct json_value const *json_key_hash_const(
return hashmap_get_hash_const(&object->object_fields, hash); return hashmap_get_hash_const(&object->object_fields, hash);
} }
void json_set( void json_set(
struct json_value *object, char const *key, struct json_value *value) struct json_value *object, char const *key, struct json_value *value)
{ {
@ -310,11 +309,11 @@ void json_set_sized(struct json_value *object,
hashmap_set_sized(&object->object_fields, key, key_size, value); hashmap_set_sized(&object->object_fields, key, key_size, value);
} }
void json_object_keys( void json_object_keys(struct json_value const *object,
struct json_value const *object, struct hash_key_entry const **keys, size_t *count) struct hash_key_entry const **keys,
size_t *count)
{ {
assert(object->ty == json_object); assert(object->ty == json_object);
*keys = object->object_fields.keys; *keys = object->object_fields.keys;
*count = object->object_fields.keys_count; *count = object->object_fields.keys_count;
} }