| 580 | } |
| 581 | |
| 582 | static const char **arena_str_array(CBMArena *arena, yyjson_val *arr, bool null_terminated, |
| 583 | int *out_count) { |
| 584 | *out_count = 0; |
| 585 | if (!arr || yyjson_is_null(arr) || !yyjson_is_arr(arr)) { |
| 586 | return NULL; |
| 587 | } |
| 588 | int count = (int)yyjson_arr_size(arr); |
| 589 | const char **items = |
| 590 | cbm_arena_alloc(arena, (size_t)(count + (null_terminated ? 1 : 0)) * sizeof(char *)); |
| 591 | if (!items) { |
| 592 | return NULL; |
| 593 | } |
| 594 | /* Iterate instead of yyjson_arr_get(i): that call is a linear scan for |
| 595 | * non-flat arrays (arrays of objects are not flat), which turns this loop |
| 596 | * into O(n^2) in the element count. */ |
| 597 | yyjson_arr_iter iter = yyjson_arr_iter_with(arr); |
| 598 | int i = 0; |
| 599 | yyjson_val *item; |
| 600 | while ((item = yyjson_arr_iter_next(&iter))) { |
| 601 | const char *s = yyjson_get_str(item); |
| 602 | items[i++] = s ? cbm_arena_strdup(arena, s) : "?"; |
| 603 | } |
| 604 | if (null_terminated) { |
| 605 | items[count] = NULL; |
| 606 | } |
| 607 | *out_count = count; |
| 608 | return items; |
| 609 | } |
| 610 | |
| 611 | int cbm_lsp_surface_defs_from_json(CBMArena *arena, const char *defs_json, CBMLSPDef **out_defs) { |
| 612 | *out_defs = NULL; |
no test coverage detected