From 4c5be13a9bcd298f01252f71579cd86c7e487966 Mon Sep 17 00:00:00 2001 From: pommicket Date: Fri, 9 Dec 2022 22:47:31 -0500 Subject: pass by value intead of poitner --- json.c | 44 ++++++++++++++++++++++---------------------- lsp.c | 9 ++++----- 2 files changed, 26 insertions(+), 27 deletions(-) diff --git a/json.c b/json.c index 09ef6e9..5d83b89 100644 --- a/json.c +++ b/json.c @@ -88,35 +88,35 @@ static inline bool json_is_space(char c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t'; } -static void json_debug_print_value(const JSON *json, const JSONValue *value) { - switch (value->type) { +static void json_debug_print_value(const JSON *json, JSONValue value) { + switch (value.type) { case JSON_UNDEFINED: printf("undefined"); break; case JSON_NULL: printf("null"); break; case JSON_FALSE: printf("false"); break; case JSON_TRUE: printf("true"); break; - case JSON_NUMBER: printf("%g", value->val.number); break; + case JSON_NUMBER: printf("%g", value.val.number); break; case JSON_STRING: { - const JSONString *string = &value->val.string; + JSONString string = value.val.string; printf("\"%.*s\"", - (int)string->len, - json->text + string->pos); + (int)string.len, + json->text + string.pos); } break; case JSON_ARRAY: { - const JSONArray *array = &value->val.array; + JSONArray array = value.val.array; printf("["); - for (u32 i = 0; i < array->len; ++i) { - json_debug_print_value(json, &json->values[array->elements + i]); + for (u32 i = 0; i < array.len; ++i) { + json_debug_print_value(json, json->values[array.elements + i]); printf(", "); } printf("]"); } break; case JSON_OBJECT: { - const JSONObject *obj = &value->val.object; + JSONObject obj = value.val.object; printf("{"); - for (u32 i = 0; i < obj->len; ++i) { - json_debug_print_value(json, &json->values[obj->items + i]); + for (u32 i = 0; i < obj.len; ++i) { + json_debug_print_value(json, json->values[obj.items + i]); printf(": "); - json_debug_print_value(json, &json->values[obj->items + obj->len + i]); + json_debug_print_value(json, json->values[obj.items + obj.len + i]); printf(", "); } printf("}"); @@ -433,20 +433,20 @@ static bool json_streq(const JSON *json, const JSONString *string, const char *n } // returns undefined if the property `name` does not exist. -JSONValue json_object_get(const JSON *json, const JSONObject *object, const char *name) { - const JSONValue *items = &json->values[object->items]; - for (u32 i = 0; i < object->len; ++i) { +JSONValue json_object_get(const JSON *json, JSONObject object, const char *name) { + const JSONValue *items = &json->values[object.items]; + for (u32 i = 0; i < object.len; ++i) { const JSONValue *this_name = items++; if (this_name->type == JSON_STRING && json_streq(json, &this_name->val.string, name)) { - return json->values[object->items + object->len + i]; + return json->values[object.items + object.len + i]; } } return (JSONValue){0}; } -JSONValue json_array_get(const JSON *json, const JSONArray *array, u64 i) { - if (i < array->len) { - return json->values[array->elements + i]; +JSONValue json_array_get(const JSON *json, JSONArray array, u64 i) { + if (i < array.len) { + return json->values[array.elements + i]; } return (JSONValue){0}; } @@ -466,7 +466,7 @@ JSONValue json_get(const JSON *json, const char *path) { if (curr_value.type != JSON_OBJECT) { return (JSONValue){0}; } - curr_value = json_object_get(json, &curr_value.val.object, segment); + curr_value = json_object_get(json, curr_value.val.object, segment); p += segment_len; if (*p == '.') ++p; } @@ -600,7 +600,7 @@ static void json_test_time_small(void) { void json_debug_print(const JSON *json) { printf("%u values (capacity %u, text length %zu)\n", arr_len(json->values), arr_cap(json->values), strlen(json->text)); - json_debug_print_value(json, &json->values[0]); + json_debug_print_value(json, json->values[0]); } // e.g. converts "Hello\nworld" to "Hello\\nworld" diff --git a/lsp.c b/lsp.c index 61ba014..83afa0e 100644 --- a/lsp.c +++ b/lsp.c @@ -1,5 +1,4 @@ // @TODO: -// - make json_object/array_get take value not pointer // - documentation // - make sure offsets are utf-16! // - maximum queue size for requests/responses just in case? @@ -484,7 +483,7 @@ static bool parse_completion(LSP *lsp, const JSON *json, LSPResponse *response) items_value = result; break; case JSON_OBJECT: - items_value = json_object_get(json, &result.val.object, "items"); + items_value = json_object_get(json, result.val.object, "items"); break; default: lsp_set_error(lsp, "Weird result type for textDocument/completion response: %s.", json_type_to_str(result.type)); @@ -501,18 +500,18 @@ static bool parse_completion(LSP *lsp, const JSON *json, LSPResponse *response) for (u32 i = 0; i < items.len; ++i) { LSPCompletionItem *item = &completion->items[i]; - JSONValue item_value = json_array_get(json, &items, i); + JSONValue item_value = json_array_get(json, items, i); if (!lsp_expect_object(lsp, item_value, "completion list")) return false; JSONObject item_object = item_value.val.object; - JSONValue label_value = json_object_get(json, &item_object, "label"); + JSONValue label_value = json_object_get(json, item_object, "label"); if (!lsp_expect_string(lsp, label_value, "completion label")) return false; JSONString label = label_value.val.string; JSONString sort_text = label; - JSONValue sort_text_value = json_object_get(json, &item_object, "sortText"); + JSONValue sort_text_value = json_object_get(json, item_object, "sortText"); if (sort_text_value.type == JSON_STRING) { // LSP allows using a different string for sorting. sort_text = sort_text_value.val.string; -- cgit v1.2.3