| 673 | } |
| 674 | |
| 675 | static void tree_table_rows_impl(cbm_sb_t *sb, const char *key, int nrows, const char *const *cols, |
| 676 | int ncols, const char *const *cells, const bool *string_cols, |
| 677 | const bool *prefix_cols) { |
| 678 | if (!sb || !key || !cols || !cells || nrows < TREE_PREFIX_MIN_USES || ncols < 1 || |
| 679 | !prefix_cols || (size_t)nrows > (size_t)-1 / (size_t)ncols) { |
| 680 | if (sb && key && cols && cells && nrows >= 0 && ncols >= 0) { |
| 681 | tree_render_table_direct(sb, key, nrows, cols, ncols, cells, string_cols); |
| 682 | } |
| 683 | return; |
| 684 | } |
| 685 | |
| 686 | size_t cell_count = (size_t)nrows * (size_t)ncols; |
| 687 | if (cell_count > TREE_PREFIX_MAX_CELLS) { |
| 688 | tree_render_table_direct(sb, key, nrows, cols, ncols, cells, string_cols); |
| 689 | return; |
| 690 | } |
| 691 | |
| 692 | const char **seeds = (const char **)malloc(sizeof(*seeds) * TREE_PREFIX_MAX_SEEDS); |
| 693 | tree_prefix_candidate_t *candidates = |
| 694 | (tree_prefix_candidate_t *)calloc(TREE_PREFIX_MAX_CANDIDATES, sizeof(*candidates)); |
| 695 | if (!seeds || !candidates) { |
| 696 | free(seeds); |
| 697 | free(candidates); |
| 698 | tree_render_table_direct(sb, key, nrows, cols, ncols, cells, string_cols); |
| 699 | return; |
| 700 | } |
| 701 | |
| 702 | int seed_count = 0; |
| 703 | for (size_t i = 0; i < cell_count && seed_count < TREE_PREFIX_MAX_SEEDS; i++) { |
| 704 | int col = (int)(i % (size_t)ncols); |
| 705 | if (!tree_column_is_prefixable(string_cols, prefix_cols, col)) { |
| 706 | continue; |
| 707 | } |
| 708 | const char *value = cells[i]; |
| 709 | if (value && strlen(value) >= TREE_PREFIX_MIN_LEN && !needs_quotes(value)) { |
| 710 | seeds[seed_count++] = value; |
| 711 | } |
| 712 | } |
| 713 | if (seed_count < TREE_PREFIX_MIN_USES) { |
| 714 | free(seeds); |
| 715 | free(candidates); |
| 716 | tree_render_table_direct(sb, key, nrows, cols, ncols, cells, string_cols); |
| 717 | return; |
| 718 | } |
| 719 | qsort(seeds, (size_t)seed_count, sizeof(*seeds), tree_string_ptr_compare); |
| 720 | |
| 721 | int candidate_count = 0; |
| 722 | bool candidate_oom = false; |
| 723 | for (int i = 1; i < seed_count && !candidate_oom; i++) { |
| 724 | const char *left = seeds[i - 1]; |
| 725 | const char *right = seeds[i]; |
| 726 | size_t shared = 0; |
| 727 | while (left[shared] && right[shared] && left[shared] == right[shared] && |
| 728 | shared < TREE_PREFIX_MAX_LEN) { |
| 729 | shared++; |
| 730 | } |
| 731 | if (left[shared] == '\0' && right[shared] == '\0') { |
| 732 | candidate_oom = !tree_prefix_candidate_add(candidates, &candidate_count, left, shared); |
no test coverage detected