more stuff
This commit is contained in:
parent
dde8c3ee42
commit
283ffaaad6
2
.vscode/launch.json
vendored
2
.vscode/launch.json
vendored
@ -9,7 +9,7 @@
|
||||
"type": "cppdbg",
|
||||
"request": "launch",
|
||||
"program": "${workspaceFolder}/build/jq",
|
||||
"args": ["", "data.json"],
|
||||
"args": ["[11350].payload.issues.user", "large-file-formatted.json"],
|
||||
"stopAtEntry": true,
|
||||
"cwd": "${workspaceFolder}",
|
||||
"environment": [],
|
||||
|
||||
19
Makefile
19
Makefile
@ -4,14 +4,24 @@ MAKEFLAGS += -j16
|
||||
CFLAGS=-std=c17 -pedantic-errors -Wall -Wextra -Wconversion
|
||||
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
|
||||
|
||||
ifeq ($(RELEASE),1)
|
||||
CFLAGS += -O3
|
||||
else
|
||||
CFLAGS += -g
|
||||
CFLAGS += -g -ggdb
|
||||
endif
|
||||
|
||||
build_dir = build
|
||||
@ -30,7 +40,8 @@ target=$(build_dir)/jq
|
||||
all: $(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)
|
||||
gcc -o $@ $(CFLAGS) $(LDFLAGS) $^
|
||||
|
||||
@ -270,10 +270,16 @@ static struct hash_entry *make_entry(struct hashmap *m, uint64_t hash)
|
||||
static void insert_key(
|
||||
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 = malloc(key_size + 1);
|
||||
char *key_value;
|
||||
if (key_size != 0) {
|
||||
key_value = malloc(key_size + 1);
|
||||
strncpy(key_value, key, key_size);
|
||||
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 = {
|
||||
.hash = hash,
|
||||
|
||||
14
src/json.h
14
src/json.h
@ -41,7 +41,8 @@ struct json_value *json_new_float(double val);
|
||||
|
||||
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 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);
|
||||
|
||||
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,
|
||||
size_t key_size,
|
||||
struct json_value *value);
|
||||
void json_object_keys(
|
||||
struct json_value const *object, struct hash_key_entry const **keys, size_t *count);
|
||||
void json_object_keys(struct json_value const *object,
|
||||
struct hash_key_entry const **keys,
|
||||
size_t *count);
|
||||
|
||||
struct json_value *json_parse(
|
||||
char const *text, size_t text_size, struct blockalloc *alloc);
|
||||
|
||||
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(
|
||||
struct json_value const *node,
|
||||
JsonWriteCb write,
|
||||
void* self);
|
||||
struct json_value const *node, JsonWriteCb write, void *self);
|
||||
|
||||
#endif
|
||||
|
||||
@ -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 t_make_number_tok(
|
||||
struct tokenizer *t, size_t idx, size_t *i)
|
||||
static struct tok t_make_number_tok(struct tokenizer *t, size_t idx, size_t *i)
|
||||
{
|
||||
while (*i < t->len && t->text[*i] >= '0' && t->text[*i] <= '9') {
|
||||
t_step(t);
|
||||
@ -66,8 +65,7 @@ static struct tok t_make_number_tok(
|
||||
return t_tok(t, tt_int, idx);
|
||||
}
|
||||
|
||||
static struct tok t_make_string_tok(
|
||||
struct tokenizer *t, size_t idx, size_t *i)
|
||||
static struct tok t_make_string_tok(struct tokenizer *t, size_t idx, size_t *i)
|
||||
{
|
||||
t_step(t);
|
||||
while (*i < t->len && t->text[*i] != '\"') {
|
||||
@ -209,22 +207,28 @@ struct json_value *resolve_next_path_seg(
|
||||
case pt_ident_key: {
|
||||
if (!json_is(node, json_object))
|
||||
return NULL;
|
||||
struct json_value *child =
|
||||
json_key_sized(node, seg.tok.ptr, seg.tok.len);
|
||||
struct json_value *child
|
||||
= json_key_sized(node, seg.tok.ptr, seg.tok.len);
|
||||
if (!child)
|
||||
return NULL;
|
||||
return resolve_next_path_seg(p, child);
|
||||
}
|
||||
case pt_string_key: {
|
||||
if (!json_is(node, json_object))
|
||||
return NULL;
|
||||
struct json_value *child =
|
||||
json_key_sized(node, seg.tok.ptr + 1, seg.tok.len - 2);
|
||||
struct json_value *child
|
||||
= json_key_sized(node, seg.tok.ptr + 1, seg.tok.len - 2);
|
||||
if (!child)
|
||||
return NULL;
|
||||
return resolve_next_path_seg(p, child);
|
||||
}
|
||||
case pt_idx: {
|
||||
if (!json_is(node, json_array))
|
||||
return NULL;
|
||||
struct json_value *child =
|
||||
json_idx(node, strtoull(seg.tok.ptr, NULL, 10));
|
||||
struct json_value *child
|
||||
= json_idx(node, strtoull(seg.tok.ptr, NULL, 10));
|
||||
if (!child)
|
||||
return NULL;
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -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)
|
||||
{
|
||||
struct json_value *shallow_val = 0;
|
||||
*(size_t *)&shallow_val |= 0x2;
|
||||
struct json_value *shallow_val = (struct json_value *)0x2;
|
||||
set_shallow_ty(&shallow_val, ty);
|
||||
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);
|
||||
}
|
||||
|
||||
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);
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
void json_set(
|
||||
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);
|
||||
}
|
||||
|
||||
void json_object_keys(
|
||||
struct json_value const *object, struct hash_key_entry const **keys, size_t *count)
|
||||
void json_object_keys(struct json_value const *object,
|
||||
struct hash_key_entry const **keys,
|
||||
size_t *count)
|
||||
{
|
||||
assert(object->ty == json_object);
|
||||
*keys = object->object_fields.keys;
|
||||
*count = object->object_fields.keys_count;
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user