Of the declarations [lo, hi) -- one class, so in path order -- the one a * reference written in file `src` binds (a choice of presentation among the * parts of one type, made without a scan): the one in `src` itself; else the * first one in the directory that shares the longest leading path with it; * else the first. */
| 2784 | * first one in the directory that shares the longest leading path with it; |
| 2785 | * else the first. */ |
| 2786 | static int nearest_bind(const cs_index_t *ix, const cs_bind_t *arr, int lo, int hi, int src) { |
| 2787 | int a = lo; |
| 2788 | int b = hi; |
| 2789 | while (a < b) { |
| 2790 | int mid = a + ((b - a) / PAIR_LEN); |
| 2791 | if (arr[mid].file < src) { |
| 2792 | a = mid + SKIP_ONE; |
| 2793 | } else { |
| 2794 | b = mid; |
| 2795 | } |
| 2796 | } |
| 2797 | if (a < hi && arr[a].file == src) { |
| 2798 | return a; |
| 2799 | } |
| 2800 | const char *sp = ix->files[src].rel_path; |
| 2801 | int left = a > lo ? common_dir_prefix(ix->files[arr[a - SKIP_ONE].file].rel_path, sp) : 0; |
| 2802 | int right = a < hi ? common_dir_prefix(ix->files[arr[a].file].rel_path, sp) : 0; |
| 2803 | int best = left > right ? left : right; |
| 2804 | if (best == 0) { |
| 2805 | return lo; |
| 2806 | } |
| 2807 | /* the entries before `a` that share those directories are the last ones |
| 2808 | * before it: the first of them */ |
| 2809 | size_t plen = dir_prefix_len(sp, best); |
| 2810 | int x = lo; |
| 2811 | int y = a; |
| 2812 | while (x < y) { |
| 2813 | int mid = x + ((y - x) / PAIR_LEN); |
| 2814 | if (strncmp(ix->files[arr[mid].file].rel_path, sp, plen) < 0) { |
| 2815 | x = mid + SKIP_ONE; |
| 2816 | } else { |
| 2817 | y = mid; |
| 2818 | } |
| 2819 | } |
| 2820 | return x; |
| 2821 | } |
| 2822 | |
| 2823 | /* [return, *hi): the declarations of class `cls` among arr[0, n), which is |
| 2824 | * sorted by class first. */ |
no test coverage detected