#include "jv_aux.h" #include #include #include "jv_alloc.h" jv jv_get(jv t, jv k) { jv v; if (jv_get_kind(t) == JV_KIND_OBJECT && jv_get_kind(k) == JV_KIND_STRING) { v = jv_object_get(t, k); if (!jv_is_valid(v)) { jv_free(v); v = jv_null(); } } else if (jv_get_kind(t) == JV_KIND_ARRAY && jv_get_kind(k) == JV_KIND_NUMBER) { // FIXME: don't do lookup for noninteger index v = jv_array_get(t, (int)jv_number_value(k)); if (!jv_is_valid(v)) { jv_free(v); v = jv_null(); } } else if (jv_get_kind(t) == JV_KIND_NULL && (jv_get_kind(k) == JV_KIND_STRING || jv_get_kind(k) == JV_KIND_NUMBER)) { jv_free(t); jv_free(k); v = jv_null(); } else { v = jv_invalid_with_msg(jv_string_fmt("Cannot index %s with %s", jv_kind_name(jv_get_kind(t)), jv_kind_name(jv_get_kind(k)))); jv_free(t); jv_free(k); } return v; } jv jv_set(jv t, jv k, jv v) { if (!jv_is_valid(v)) { jv_free(t); jv_free(k); return v; } int isnull = jv_get_kind(t) == JV_KIND_NULL; if (jv_get_kind(k) == JV_KIND_STRING && (jv_get_kind(t) == JV_KIND_OBJECT || isnull)) { if (isnull) t = jv_object(); t = jv_object_set(t, k, v); } else if (jv_get_kind(k) == JV_KIND_NUMBER && (jv_get_kind(t) == JV_KIND_ARRAY || isnull)) { if (isnull) t = jv_array(); t = jv_array_set(t, (int)jv_number_value(k), v); } else { jv err = jv_invalid_with_msg(jv_string_fmt("Cannot update field at %s index of %s", jv_kind_name(jv_get_kind(t)), jv_kind_name(jv_get_kind(v)))); jv_free(t); jv_free(k); jv_free(v); t = err; } return t; } // assumes keys is a sorted array jv jv_dels(jv t, jv keys) { assert(jv_get_kind(keys) == JV_KIND_ARRAY); assert(jv_is_valid(t)); if (jv_get_kind(t) == JV_KIND_NULL || jv_array_length(jv_copy(keys)) == 0) { // no change } else if (jv_get_kind(t) == JV_KIND_ARRAY) { jv new_array = jv_array(); int kidx = 0; jv_array_foreach(t, i, elem) { int del = 0; while (kidx < jv_array_length(jv_copy(keys))) { jv nextdel = jv_array_get(jv_copy(keys), kidx); if (jv_get_kind(nextdel) != JV_KIND_NUMBER) { jv err = jv_invalid_with_msg(jv_string_fmt("Cannot delete %s element of array", jv_kind_name(jv_get_kind(nextdel)))); jv_free(nextdel); jv_free(new_array); jv_free(elem); new_array = err; goto arr_out; // break twice } int delidx = (int)jv_number_value(nextdel); jv_free(nextdel); if (i == delidx) { del = 1; } if (i < delidx) { break; } kidx++; } if (!del) new_array = jv_array_append(new_array, elem); else jv_free(elem); } arr_out: jv_free(t); t = new_array; } else if (jv_get_kind(t) == JV_KIND_OBJECT) { jv_array_foreach(keys, i, k) { if (jv_get_kind(k) != JV_KIND_STRING) { jv_free(t); t = jv_invalid_with_msg(jv_string_fmt("Cannot delete %s field of object", jv_kind_name(jv_get_kind(k)))); jv_free(k); break; } t = jv_object_delete(t, k); } } else { jv err = jv_invalid_with_msg(jv_string_fmt("Cannot delete fields from %s", jv_kind_name(jv_get_kind(t)))); jv_free(t); t = err; } jv_free(keys); return t; } jv jv_setpath(jv root, jv path, jv value) { if (jv_get_kind(path) != JV_KIND_ARRAY) { jv_free(value); jv_free(root); jv_free(path); return jv_invalid_with_msg(jv_string("Path must be specified as an array")); } if (!jv_is_valid(root)){ jv_free(value); jv_free(path); return root; } if (jv_array_length(jv_copy(path)) == 0) { jv_free(path); jv_free(root); return value; } jv pathcurr = jv_array_get(jv_copy(path), 0); jv pathrest = jv_array_slice(path, 1, jv_array_length(jv_copy(path))); return jv_set(root, pathcurr, jv_setpath(jv_get(jv_copy(root), jv_copy(pathcurr)), pathrest, value)); } jv jv_getpath(jv root, jv path) { if (jv_get_kind(path) != JV_KIND_ARRAY) { jv_free(root); jv_free(path); return jv_invalid_with_msg(jv_string("Path must be specified as an array")); } if (!jv_is_valid(root)) { jv_free(path); return root; } if (jv_array_length(jv_copy(path)) == 0) { jv_free(path); return root; } jv pathcurr = jv_array_get(jv_copy(path), 0); jv pathrest = jv_array_slice(path, 1, jv_array_length(jv_copy(path))); return jv_getpath(jv_get(root, pathcurr), pathrest); } // assumes paths is a sorted array of arrays static jv delpaths_sorted(jv object, jv paths, int start) { jv delkeys = jv_array(); for (int i=0; i start); int delkey = jv_array_length(jv_array_get(jv_copy(paths), i)) == start + 1; jv key = jv_array_get(jv_array_get(jv_copy(paths), i), start); while (j < jv_array_length(jv_copy(paths)) && jv_equal(jv_copy(key), jv_array_get(jv_array_get(jv_copy(paths), j), start))) j++; // if i <= entry < j, then entry starts with key if (delkey) { // deleting this entire key, we don't care about any more specific deletions delkeys = jv_array_append(delkeys, key); } else { // deleting certain sub-parts of this key jv subobject = jv_get(jv_copy(object), jv_copy(key)); if (!jv_is_valid(subobject)) { jv_free(key); jv_free(object); object = subobject; break; } else if (jv_get_kind(subobject) == JV_KIND_NULL) { jv_free(key); jv_free(subobject); } else { jv newsubobject = delpaths_sorted(subobject, jv_array_slice(jv_copy(paths), i, j), start+1); if (!jv_is_valid(newsubobject)) { jv_free(key); jv_free(object); object = newsubobject; break; } object = jv_set(object, key, newsubobject); } if (!jv_is_valid(object)) break; } i = j; } jv_free(paths); if (jv_is_valid(object)) object = jv_dels(object, delkeys); else jv_free(delkeys); return object; } jv jv_delpaths(jv object, jv paths) { paths = jv_sort(paths, jv_copy(paths)); jv_array_foreach(paths, i, elem) { if (jv_get_kind(elem) != JV_KIND_ARRAY) { jv_free(object); jv_free(paths); jv err = jv_invalid_with_msg(jv_string_fmt("Path must be specified as array, not %s", jv_kind_name(jv_get_kind(elem)))); jv_free(elem); return err; } jv_free(elem); } if (jv_array_length(jv_copy(paths)) == 0) { // nothing is being deleted jv_free(paths); return object; } if (jv_array_length(jv_array_get(jv_copy(paths), 0)) == 0) { // everything is being deleted jv_free(paths); jv_free(object); return jv_null(); } return delpaths_sorted(object, paths, 0); } static int string_cmp(const void* pa, const void* pb){ const jv* a = pa; const jv* b = pb; int lena = jv_string_length(jv_copy(*a)); int lenb = jv_string_length(jv_copy(*b)); int minlen = lena < lenb ? lena : lenb; int r = memcmp(jv_string_value(*a), jv_string_value(*b), minlen); if (r == 0) r = lena - lenb; return r; } jv jv_keys(jv x) { if (jv_get_kind(x) == JV_KIND_OBJECT) { int nkeys = jv_object_length(jv_copy(x)); jv* keys = jv_mem_alloc(sizeof(jv) * nkeys); int kidx = 0; jv_object_foreach(x, key, value) { keys[kidx++] = key; jv_free(value); } qsort(keys, nkeys, sizeof(jv), string_cmp); jv answer = jv_array_sized(nkeys); for (int i = 0; i= jv_array_length(jv_copy(a)); int b_done = i >= jv_array_length(jv_copy(b)); if (a_done || b_done) { r = b_done - a_done; //suddenly, logic break; } jv xa = jv_array_get(jv_copy(a), i); jv xb = jv_array_get(jv_copy(b), i); r = jv_cmp(xa, xb); i++; } break; } case JV_KIND_OBJECT: { jv keys_a = jv_keys(jv_copy(a)); jv keys_b = jv_keys(jv_copy(b)); r = jv_cmp(jv_copy(keys_a), keys_b); if (r == 0) { jv_array_foreach(keys_a, i, key) { jv xa = jv_object_get(jv_copy(a), jv_copy(key)); jv xb = jv_object_get(jv_copy(b), key); r = jv_cmp(xa, xb); if (r) break; } } jv_free(keys_a); break; } } jv_free(a); jv_free(b); return r; } struct sort_entry { jv object; jv key; }; static int sort_cmp(const void* pa, const void* pb) { const struct sort_entry* a = pa; const struct sort_entry* b = pb; int r = jv_cmp(jv_copy(a->key), jv_copy(b->key)); // comparing by address if r == 0 makes the sort stable return r ? r : (int)(a - b); } static struct sort_entry* sort_items(jv objects, jv keys) { assert(jv_get_kind(objects) == JV_KIND_ARRAY); assert(jv_get_kind(keys) == JV_KIND_ARRAY); assert(jv_array_length(jv_copy(objects)) == jv_array_length(jv_copy(keys))); int n = jv_array_length(jv_copy(objects)); struct sort_entry* entries = jv_mem_alloc(sizeof(struct sort_entry) * n); for (int i=0; i