Branch data Line data Source code
1 : : // Copyright 2007-2025 David Robillard <d@drobilla.net>
2 : : // SPDX-License-Identifier: ISC
3 : :
4 : : #include <zix/path.h>
5 : :
6 : : #include "index_range.h"
7 : : #include "path_iter.h"
8 : : #include "qualifiers.h"
9 : :
10 : : #include <zix/allocator.h>
11 : : #include <zix/attributes.h>
12 : : #include <zix/string_view.h>
13 : :
14 : : #include <stdbool.h>
15 : : #include <string.h>
16 : :
17 : : static ZIX_CONSTEXPR ZixIndexRange two_range = {0U, 2U};
18 : : static ZIX_CONSTEXPR ZixIndexRange one_range = {0U, 1U};
19 : :
20 : : #ifdef _WIN32
21 : :
22 : : # define ZIX_DIR_SEP '\\' // Backslash is preferred on Windows
23 : :
24 : : static inline bool
25 : : is_dir_sep(const char c)
26 : : {
27 : : return c == '/' || c == '\\';
28 : : }
29 : :
30 : : static inline bool
31 : : is_any_sep(const char c)
32 : : {
33 : : return c == '/' || c == ':' || c == '\\';
34 : : }
35 : :
36 : : static inline bool
37 : : is_root_name_char(const char c)
38 : : {
39 : : return c && !is_dir_sep(c);
40 : : }
41 : :
42 : : static ZixIndexRange
43 : : zix_path_root_name_range(const char* const path)
44 : : {
45 : : ZixIndexRange result = {0U, 0U};
46 : :
47 : : if (path) {
48 : : if (((path[0] >= 'A' && path[0] <= 'Z') ||
49 : : (path[0] >= 'a' && path[0] <= 'z')) &&
50 : : path[1] == ':') {
51 : : // A root that starts with a letter then ':' has a name like "C:"
52 : : result.end = 2U;
53 : :
54 : : } else if (is_dir_sep(path[0]) && is_dir_sep(path[1]) &&
55 : : is_root_name_char(path[2])) {
56 : : // A root that starts with two separators has a name like "\\host"
57 : : result.end = 2U;
58 : : while (is_root_name_char(path[result.end])) {
59 : : ++result.end;
60 : : }
61 : : }
62 : : }
63 : :
64 : : return result;
65 : : }
66 : :
67 : : #else
68 : :
69 : : # define ZIX_DIR_SEP '/' // Slash is the only separator on other platforms
70 : :
71 : : static inline bool
72 : 28200 : is_dir_sep(const char c)
73 : : {
74 : 28200 : return c == '/';
75 : : }
76 : :
77 : : static inline bool
78 : 287 : is_any_sep(const char c)
79 : : {
80 : 287 : return c == '/';
81 : : }
82 : :
83 : : static inline ZixIndexRange
84 : 5562 : zix_path_root_name_range(const char* const path)
85 : : {
86 : : (void)path;
87 : 5562 : return zix_make_range(0U, 0U);
88 : : }
89 : :
90 : : #endif
91 : :
92 : : static inline char
93 : 3059 : normal_path_char(const char c)
94 : : {
95 [ + + ]: 3059 : return (char)(is_dir_sep(c) ? ZIX_DIR_SEP : c);
96 : : }
97 : :
98 : : static ZixStringView
99 : 2149 : string_view(const char* const path)
100 : : {
101 : 2149 : const ZixStringView view = {path, strlen(path)};
102 : 2149 : return view;
103 : : }
104 : :
105 : : typedef struct {
106 : : ZixIndexRange name;
107 : : ZixIndexRange dir;
108 : : } ZixRootSlices;
109 : :
110 : : ZIX_PURE_FUNC static ZixRootSlices
111 : 4933 : zix_path_root_slices(const char* const path)
112 : : {
113 : : // A root name not trailed by a separator has no root directory
114 : 4933 : const ZixIndexRange name = zix_path_root_name_range(path);
115 [ + + + + ]: 4933 : const size_t dir_len = (size_t)(path && is_dir_sep(path[name.end]));
116 [ + + ]: 4933 : if (!dir_len) {
117 : 3315 : const ZixRootSlices result = {name, {name.end, name.end}};
118 : 3315 : return result;
119 : : }
120 : :
121 : : // Skip redundant root directory separators
122 : 1618 : ZixIndexRange dir = {name.end, name.end + dir_len};
123 [ + + ]: 2411 : while (is_dir_sep(path[dir.end])) {
124 : 793 : dir.begin = dir.end++;
125 : : }
126 : :
127 : 1618 : const ZixRootSlices result = {name, dir};
128 : 1618 : return result;
129 : : }
130 : :
131 : : static bool
132 : 2477 : zix_string_ranges_equal(const char* const lhs,
133 : : const ZixIndexRange lhs_range,
134 : : const char* const rhs,
135 : : const ZixIndexRange rhs_range)
136 : : {
137 : 2477 : const size_t lhs_len = lhs_range.end - lhs_range.begin;
138 : 2477 : const size_t rhs_len = rhs_range.end - rhs_range.begin;
139 : :
140 [ + + + + ]: 3114 : return lhs_len == rhs_len &&
141 : 637 : (lhs_len == 0 ||
142 [ + + ]: 637 : !strncmp(lhs + lhs_range.begin, rhs + rhs_range.begin, lhs_len));
143 : : }
144 : :
145 : : ZIX_PURE_FUNC static ZixIndexRange
146 : 3670 : zix_path_root_path_range(const char* const path)
147 : : {
148 : 3670 : const ZixRootSlices root = zix_path_root_slices(path);
149 : 3670 : const size_t dir_len = (size_t)!zix_is_empty_range(root.dir);
150 : :
151 : 3670 : return zix_is_empty_range(root.name)
152 : : ? root.dir
153 [ + - ]: 3670 : : zix_make_range(root.name.begin, root.name.end + dir_len);
154 : : }
155 : :
156 : : ZIX_PURE_FUNC static ZixIndexRange
157 : 542 : zix_path_parent_path_range(const ZixStringView path)
158 : : {
159 [ + + ]: 542 : if (!path.length) {
160 : 4 : return zix_make_range(0U, 0U); // Empty paths have no parent
161 : : }
162 : :
163 : : // Find the first relevant character (skip leading root path if any)
164 : 538 : const ZixIndexRange root = zix_path_root_path_range(path.data);
165 : 538 : const size_t p = root.begin;
166 : :
167 : : // Scan backwards starting at the last character
168 : 538 : size_t l = path.length - 1U;
169 [ + + ]: 538 : if (is_dir_sep(path.data[l])) {
170 : : // Rewind to the last relevant separator (skip trailing redundant ones)
171 [ + + + + ]: 178 : while (l > p && is_dir_sep(path.data[l - 1U])) {
172 : 42 : --l;
173 : : }
174 : : } else {
175 : : // Rewind to the last relevant character (skip trailing name)
176 [ + + + + ]: 2049 : while (l > p && !is_dir_sep(path.data[l])) {
177 : 1647 : --l;
178 : : }
179 : : }
180 : :
181 [ + + ]: 538 : if (l <= root.end) {
182 : 162 : return root;
183 : : }
184 : :
185 : : // Drop trailing separators
186 [ + + + + ]: 769 : while (l > p && is_dir_sep(path.data[l])) {
187 : 393 : --l;
188 : : }
189 : :
190 : 376 : return zix_make_range(root.begin, root.begin + l + 1U - p);
191 : : }
192 : :
193 : : ZIX_PURE_FUNC static ZixIndexRange
194 : 1777 : zix_path_filename_range(const ZixStringView path)
195 : : {
196 [ + + ]: 1777 : if (!path.length) {
197 : 12 : return zix_make_range(0, 0);
198 : : }
199 : :
200 : : // Find the first filename character (skip leading root path if any)
201 : 1765 : const size_t begin = zix_path_root_path_range(path.data).end;
202 [ + + + + ]: 1765 : if (begin == path.length || is_dir_sep(path.data[path.length - 1U])) {
203 : 419 : return zix_make_range(0U, 0U);
204 : : }
205 : :
206 : : // Scan backwards to find the first character after the last separator
207 : 1346 : size_t f = path.length - 1U;
208 [ + + + + ]: 5852 : while (f > begin && !is_dir_sep(path.data[f - 1])) {
209 : 4506 : --f;
210 : : }
211 : :
212 : 1346 : return zix_make_range(f, path.length);
213 : : }
214 : :
215 : : ZIX_PURE_FUNC static ZixIndexRange
216 : 1063 : zix_path_stem_range(const ZixStringView path)
217 : : {
218 : 1063 : const ZixIndexRange name = zix_path_filename_range(path);
219 : :
220 : 1063 : ZixIndexRange stem = name;
221 [ + + ]: 1063 : if (!zix_is_empty_range(stem) &&
222 [ + + ]: 809 : !zix_string_ranges_equal(path.data, stem, ".", one_range) &&
223 [ + + ]: 759 : !zix_string_ranges_equal(path.data, stem, "..", two_range)) {
224 : : // Scan backwards to the last "." after the last directory separator
225 : 681 : --stem.end;
226 [ + + + + ]: 2753 : while (stem.end > stem.begin && path.data[stem.end] != '.') {
227 : 2072 : --stem.end;
228 : : }
229 : : }
230 : :
231 [ + + ]: 1063 : return zix_is_empty_range(stem) ? name : stem;
232 : : }
233 : :
234 : : ZIX_PURE_FUNC static ZixIndexRange
235 : 531 : zix_path_extension_range(const ZixStringView path)
236 : : {
237 : 531 : const ZixIndexRange stem = zix_path_stem_range(path);
238 : :
239 : 531 : return zix_is_empty_range(stem)
240 : : ? stem
241 [ + + ]: 531 : : zix_make_range(stem.end, stem.end + path.length - stem.end);
242 : : }
243 : :
244 : : char*
245 [ + + ]: 183 : zix_path_join(ZixAllocator* const allocator,
246 : : const char* const a,
247 : : const char* const b)
248 : : {
249 : : const ZixStringView b_view = zix_string(b);
250 [ + + + + ]: 183 : if (!a || !a[0]) {
251 : 18 : return zix_string_view_copy(allocator, b_view);
252 : : }
253 : :
254 : : const ZixStringView a_view = zix_string(a);
255 : 165 : const ZixRootSlices a_root = zix_path_root_slices(a);
256 : 165 : const ZixRootSlices b_root = zix_path_root_slices(b);
257 : :
258 : : #ifdef _WIN32
259 : : // If the RHS has a different root name, just copy it
260 : : if (b_root.name.end &&
261 : : !zix_string_ranges_equal(a, a_root.name, b, b_root.name)) {
262 : : return zix_string_view_copy(allocator, b_view);
263 : : }
264 : : #endif
265 : :
266 : : // Determine how to join paths
267 : 165 : const bool a_has_root_dir = !zix_is_empty_range(a_root.dir);
268 : 165 : const bool a_has_filename = zix_path_has_filename(a_view.data);
269 : 165 : size_t prefix_len = a_view.length;
270 : 165 : bool add_sep = false;
271 [ + + ]: 165 : if (!zix_is_empty_range(b_root.dir)) {
272 : 15 : prefix_len = a_root.name.end; // Omit any path from a
273 [ + + + + : 150 : } else if (a_has_filename || (!a_has_root_dir && zix_path_is_absolute(a))) {
- + ]
274 : 117 : add_sep = true; // Add a preferred separator after a
275 : : }
276 : :
277 : 165 : const size_t path_len = prefix_len + (size_t)add_sep + b_view.length;
278 : 165 : char* const path = (char*)zix_calloc(allocator, path_len + 1U, 1U);
279 : :
280 [ + + ]: 165 : if (path) {
281 : 164 : memcpy(path, a, prefix_len);
282 : :
283 : 164 : size_t p = prefix_len;
284 [ + + ]: 164 : if (add_sep) {
285 : 116 : path[p++] = ZIX_DIR_SEP;
286 : : }
287 : :
288 [ + + ]: 164 : if (b_view.length > b_root.name.end) {
289 : 126 : memcpy(path + p, b + b_root.name.end, b_view.length - b_root.name.end);
290 : 126 : path[p + b_view.length] = '\0';
291 : : }
292 : : }
293 : :
294 : 165 : return path;
295 : : }
296 : :
297 : : char*
298 : 422 : zix_path_preferred(ZixAllocator* const allocator, const char* const path)
299 : : {
300 : 422 : const ZixStringView path_view = string_view(path);
301 : 422 : char* const result = (char*)zix_calloc(allocator, path_view.length + 1U, 1U);
302 : :
303 [ + + ]: 422 : if (result) {
304 [ + + ]: 3371 : for (size_t i = 0U; i < path_view.length; ++i) {
305 : 2950 : result[i] = normal_path_char(path[i]);
306 : : }
307 : : }
308 : :
309 : 422 : return result;
310 : : }
311 : :
312 : : char*
313 : 331 : zix_path_lexically_normal(ZixAllocator* const allocator, const char* const path)
314 : : {
315 : 331 : ZIX_CONSTEXPR char sep = ZIX_DIR_SEP;
316 : :
317 : : /* Loosely following the normalization algorithm from
318 : : <https://en.cppreference.com/w/cpp/filesystem/path>, but in such a way
319 : : that only a single buffer (the result) is needed. This means that
320 : : dot-dot segments are relatively expensive, but it beats a stack in
321 : : everyday common cases. */
322 : :
323 [ + + ]: 331 : if (!path[0]) {
324 : 4 : return (char*)zix_calloc(allocator, 1U, 1U); // The empty path is normal
325 : : }
326 : :
327 : : // Allocate a result buffer, with space for one additional character
328 : 327 : const ZixStringView path_view = string_view(path);
329 : 327 : const size_t path_len = path_view.length;
330 : 327 : char* const result = (char*)zix_calloc(allocator, path_len + 2U, 1);
331 [ + + ]: 327 : if (!result) {
332 : 1 : return NULL;
333 : : }
334 : :
335 : : // Copy root, normalizing separators as we go
336 : 326 : const ZixIndexRange root = zix_path_root_path_range(path);
337 : 326 : const size_t root_len = root.end - root.begin;
338 : 326 : size_t r = 0U;
339 [ + + ]: 435 : for (size_t i = 0; i < root_len; ++i) {
340 : 109 : result[r++] = normal_path_char(path[i]);
341 : : }
342 : :
343 : : // Copy path, removing dot entries and collapsing separators as we go
344 [ + + ]: 2827 : for (size_t i = root.end; i < path_len; ++i) {
345 [ + + ]: 2501 : if (is_dir_sep(path[i])) {
346 [ + + + + ]: 489 : if (((r == root.end + 1U && result[r - 1] == '.') ||
347 [ + + + + ]: 477 : (r >= root.end + 2U && result[r - 2] == sep &&
348 [ + + ]: 96 : result[r - 1] == '.'))) {
349 : : // Remove dot entry and any immediately following separators
350 : 50 : result[--r] = '\0';
351 : :
352 : : } else {
353 : : // Replace separators with a single preferred separator
354 : 439 : result[r++] = sep;
355 : : }
356 : :
357 : : // Collapse redundant separators
358 [ + + ]: 551 : while (is_dir_sep(path[i + 1])) {
359 : 62 : ++i;
360 : : }
361 : :
362 : : } else {
363 : 2012 : result[r++] = path[i];
364 : : }
365 : : }
366 : :
367 : : // Collapse any dot-dot entries following a directory name
368 : 326 : size_t last = r;
369 : 326 : size_t next = 0;
370 [ + + ]: 2852 : for (size_t i = root_len; i < r;) {
371 [ + + + + : 2526 : if (last < r && i > 2U && result[i - 2U] == sep && result[i - 1U] == '.' &&
+ + + + ]
372 [ + + + + : 83 : result[i] == '.' && (!result[i + 1U] || is_dir_sep(result[i + 1U]))) {
+ + ]
373 [ + + ]: 71 : if (result[i + 1] == sep) {
374 : 52 : ++i;
375 : : }
376 : :
377 : 71 : const size_t suffix_len = r - i - 1U;
378 : 71 : memmove(result + last, result + i + 1, suffix_len);
379 : 71 : r = r - ((r - last) - suffix_len);
380 : 71 : result[r] = '\0';
381 : 71 : i = 0;
382 : 71 : last = r;
383 : 71 : next = 0;
384 : : } else {
385 [ + + + + ]: 2455 : if (i >= 1 && result[i - 1] == sep) {
386 : 462 : next = i;
387 : : }
388 : :
389 [ + + + + ]: 2455 : if (result[i] != sep && result[i] != '.') {
390 : 1735 : last = next;
391 : : }
392 : 2455 : ++i;
393 : : }
394 : : }
395 : :
396 : : // Remove any dot-dot entries following the root directory
397 [ + + + - ]: 326 : if (root_len && is_dir_sep(result[root_len - 1U])) {
398 : 109 : size_t start = root_len;
399 [ + + + + : 135 : while (start < r && result[start] == '.' && result[start + 1] == '.' &&
+ + ]
400 [ + + + - ]: 26 : (result[start + 2] == sep || result[start + 2] == '\0')) {
401 [ + + ]: 26 : start += (result[start + 2] == sep) ? 3U : 2U;
402 : : }
403 : :
404 [ + + ]: 109 : if (start > root_len) {
405 [ + + ]: 18 : if (start < r) {
406 : 3 : memmove(result + root_len, result + start, r - start);
407 : 3 : r = root_len + r - start;
408 : : } else {
409 : 15 : r = root_len;
410 : : }
411 : :
412 : 18 : result[r] = '\0';
413 : 18 : return result;
414 : : }
415 : : }
416 : :
417 : : // Remove trailing dot entry
418 [ + + + + : 308 : if (r >= 2U && is_any_sep(result[r - 2]) && result[r - 1] == '.') {
+ + ]
419 : 14 : result[r - 1] = '\0';
420 : : }
421 : :
422 : : // Remove trailing dot-dot entry
423 [ + + + + : 316 : if (r >= 3U && result[r - 3] == '.' && result[r - 2] == '.' &&
+ + + - ]
424 : 8 : is_any_sep(result[r - 1])) {
425 : 8 : result[r - 1] = '\0';
426 : : }
427 : :
428 : : // If the path is empty, add a dot
429 [ + + ]: 308 : if (!result[0]) {
430 : 12 : result[0] = '.';
431 : 12 : result[1] = '\0';
432 : : }
433 : :
434 : 308 : return result;
435 : : }
436 : :
437 : : static ZixPathIter
438 : 318 : make_iter(const ZixPathIterState state, const ZixIndexRange range)
439 : : {
440 : 318 : const ZixPathIter result = {range, state};
441 : 318 : return result;
442 : : }
443 : :
444 : : ZixPathIter
445 : 378 : zix_path_begin(const char* const path)
446 : : {
447 : 378 : const ZixPathIter iter = {zix_path_root_name_range(path), ZIX_PATH_ROOT_NAME};
448 : :
449 [ + - ]: 378 : return (iter.range.end > iter.range.begin) ? iter : zix_path_next(path, iter);
450 : : }
451 : :
452 : : ZixPathIter
453 : 1012 : zix_path_next(const char* const path, ZixPathIter iter)
454 : : {
455 [ + + + + ]: 1012 : if (iter.state == ZIX_PATH_ROOT_NAME && is_dir_sep(path[iter.range.end])) {
456 : 82 : return make_iter(ZIX_PATH_ROOT_DIRECTORY,
457 : 82 : zix_make_range(iter.range.end, iter.range.end + 1U));
458 : : }
459 : :
460 [ + + ]: 930 : if (iter.state <= ZIX_PATH_ROOT_DIRECTORY) {
461 : 378 : iter.range.begin = iter.range.end;
462 : 378 : iter.state = ZIX_PATH_FILE_NAME;
463 : : }
464 : :
465 [ + - ]: 930 : if (iter.state == ZIX_PATH_FILE_NAME) {
466 : : // If the last range end was the end of the path, we're done
467 : 930 : iter.range.begin = iter.range.end;
468 [ + + ]: 930 : if (!path[iter.range.begin]) {
469 : 236 : return make_iter(ZIX_PATH_END, iter.range);
470 : : }
471 : :
472 : : // Skip any leading separators
473 [ + + ]: 1055 : while (is_dir_sep(path[iter.range.begin])) {
474 : 361 : iter.range.begin = ++iter.range.end;
475 : : }
476 : :
477 : : // Advance end to the next separator or path end
478 [ + + + + ]: 2083 : while (path[iter.range.end] && !is_dir_sep(path[iter.range.end])) {
479 : 1389 : ++iter.range.end;
480 : : }
481 : : }
482 : :
483 : 694 : return iter;
484 : : }
485 : :
486 : : static size_t
487 : 297 : zix_path_append(char* const buf,
488 : : const size_t offset,
489 : : const char* const string,
490 : : const size_t length)
491 : : {
492 : 297 : size_t o = offset;
493 [ + + ]: 297 : if (offset) {
494 : 159 : buf[o++] = ZIX_DIR_SEP;
495 : : }
496 : :
497 : 297 : memcpy(buf + o, string, length);
498 : 297 : return o + length;
499 : : }
500 : :
501 : : char*
502 : 216 : zix_path_lexically_relative(ZixAllocator* const allocator,
503 : : const char* const path,
504 : : const char* const base)
505 : : {
506 : : static const ZixStringView dot = ZIX_STATIC_STRING(".");
507 : :
508 : : // Mismatched roots can't be expressed in relative form
509 : 216 : const ZixRootSlices path_root = zix_path_root_slices(path);
510 : 216 : const ZixRootSlices base_root = zix_path_root_slices(base);
511 : 216 : const bool path_has_root_dir = !zix_is_empty_range(path_root.dir);
512 : 216 : const bool base_has_root_dir = !zix_is_empty_range(base_root.dir);
513 [ + - ]: 216 : if (!zix_string_ranges_equal(path, path_root.name, base, base_root.name) ||
514 [ + + ]: 216 : (zix_path_is_absolute(path) != zix_path_is_absolute(base)) ||
515 [ + + - + ]: 188 : (!path_has_root_dir && base_has_root_dir)) {
516 : 28 : return NULL;
517 : : }
518 : :
519 : : // Find the first mismatching element in the paths (or the end)
520 : 188 : ZixPathIter a = zix_path_begin(path);
521 : 188 : ZixPathIter b = zix_path_begin(base);
522 [ + + + + : 701 : while (a.state != ZIX_PATH_END && a.state == b.state &&
+ + ]
523 : 323 : zix_string_ranges_equal(path, a.range, base, b.range)) {
524 : 190 : a = zix_path_next(path, a);
525 : 190 : b = zix_path_next(base, b);
526 : : }
527 : :
528 : : // Matching paths reduce to "."
529 [ + + + + : 341 : if ((a.state == ZIX_PATH_END && b.state == ZIX_PATH_END) ||
+ + ]
530 [ + + ]: 170 : (zix_is_empty_range(a.range) && b.state == ZIX_PATH_END)) {
531 : 37 : return zix_string_view_copy(allocator, dot);
532 : : }
533 : :
534 : : // Count the trailing non-matching entries in base
535 : 151 : size_t n_base_up = 0U;
536 : 151 : size_t n_non_empty = 0U;
537 [ + + ]: 396 : for (; b.state < ZIX_PATH_END; b = zix_path_next(base, b)) {
538 [ + + ]: 245 : if (!zix_is_empty_range(b.range)) {
539 [ + + ]: 191 : if (zix_string_ranges_equal(base, b.range, "..", two_range)) {
540 : 12 : ++n_base_up;
541 [ + + ]: 179 : } else if (!zix_string_ranges_equal(base, b.range, ".", one_range)) {
542 : 173 : ++n_non_empty;
543 : : }
544 : : }
545 : : }
546 : :
547 : : // Base can't have too many up-references
548 [ + + ]: 151 : if (n_base_up > n_non_empty) {
549 : 6 : return NULL;
550 : : }
551 : :
552 : 145 : const size_t n_up =
553 [ + - ]: 145 : (a.state == ZIX_PATH_ROOT_DIRECTORY) ? 0U : (n_non_empty - n_base_up);
554 : :
555 : : // A result with no up-references or names reduces to "."
556 [ + + + + ]: 145 : if (n_up == 0 && a.state == ZIX_PATH_END) {
557 : 6 : return zix_string_view_copy(allocator, dot);
558 : : }
559 : :
560 : : // Allocate buffer for relative path result
561 : 139 : const size_t path_len = strlen(path);
562 : 139 : const size_t rel_len = (n_up * 3U) + path_len - a.range.begin;
563 : 139 : char* const rel = (char*)zix_calloc(allocator, rel_len + 1U, 1U);
564 [ + + ]: 139 : if (!rel) {
565 : 1 : return NULL;
566 : : }
567 : :
568 : : // Write leading up-references
569 : 138 : size_t offset = 0U;
570 [ + + ]: 306 : for (size_t i = 0U; i < n_up; ++i) {
571 : 168 : offset = zix_path_append(rel, offset, "..", 2U);
572 : : }
573 : :
574 : 138 : const char path_last = path[path_len - 1U];
575 [ + + ]: 138 : if (a.range.begin < path_len) {
576 : : // Copy suffix from path (from `a` to the end)
577 : 129 : const size_t suffix_len = path_len - a.range.begin;
578 : 129 : offset = zix_path_append(rel, offset, path + a.range.begin, suffix_len);
579 [ + - + + : 9 : } else if (n_up && path_len > 1 && is_dir_sep(path_last)) {
+ + ]
580 : : // Copy trailing directory separator from path
581 : 4 : rel[offset++] = path_last;
582 : : }
583 : :
584 : 138 : rel[offset++] = '\0';
585 : 138 : return rel;
586 : : }
587 : :
588 : : // Decomposition
589 : :
590 : : static ZixStringView
591 : 1940 : range_string_view(const char* const path, const ZixIndexRange range)
592 : : {
593 : 1940 : return zix_substring(path + range.begin, range.end - range.begin);
594 : : }
595 : :
596 : : #ifdef _WIN32
597 : :
598 : : ZIX_NONBLOCKING ZixStringView
599 : : zix_path_root_name(const char* const path)
600 : : {
601 : : return range_string_view(path, zix_path_root_name_range(path));
602 : : }
603 : :
604 : : #else
605 : :
606 : : ZIX_NONBLOCKING ZixStringView
607 : 290 : zix_path_root_name(const char* const path)
608 : : {
609 : : (void)path;
610 : 290 : return zix_empty_string();
611 : : }
612 : :
613 : : #endif
614 : :
615 : : ZIX_NONBLOCKING ZixStringView
616 : 250 : zix_path_root_directory(const char* const path)
617 : : {
618 : 250 : return range_string_view(path, zix_path_root_slices(path).dir);
619 : : }
620 : :
621 : : ZIX_NONBLOCKING ZixStringView
622 : 290 : zix_path_root_path(const char* const path)
623 : : {
624 : 290 : return range_string_view(path, zix_path_root_path_range(path));
625 : : }
626 : :
627 : : ZIX_NONBLOCKING ZixStringView
628 : 250 : zix_path_relative_path(const char* const path)
629 : : {
630 : 250 : const ZixStringView view = string_view(path);
631 : 250 : const ZixIndexRange root = zix_path_root_path_range(path);
632 : :
633 : 250 : return range_string_view(path, zix_make_range(root.end, view.length));
634 : : }
635 : :
636 : : ZIX_NONBLOCKING ZixStringView
637 : 291 : zix_path_parent_path(const char* const path)
638 : : {
639 : 291 : return range_string_view(path, zix_path_parent_path_range(string_view(path)));
640 : : }
641 : :
642 : : ZIX_NONBLOCKING ZixStringView
643 : 298 : zix_path_filename(const char* const path)
644 : : {
645 : 298 : return range_string_view(path, zix_path_filename_range(string_view(path)));
646 : : }
647 : :
648 : : ZIX_NONBLOCKING ZixStringView
649 : 281 : zix_path_stem(const char* const path)
650 : : {
651 : 281 : return range_string_view(path, zix_path_stem_range(string_view(path)));
652 : : }
653 : :
654 : : ZIX_NONBLOCKING ZixStringView
655 : 280 : zix_path_extension(const char* const path)
656 : : {
657 : 280 : return range_string_view(path, zix_path_extension_range(string_view(path)));
658 : : }
659 : :
660 : : // Queries
661 : :
662 : : ZIX_NONBLOCKING bool
663 : 251 : zix_path_has_root_path(const char* const path)
664 : : {
665 : 251 : return !zix_is_empty_range(zix_path_root_path_range(path));
666 : : }
667 : :
668 : : ZIX_NONBLOCKING bool
669 : 251 : zix_path_has_root_name(const char* const path)
670 : : {
671 : 251 : return !zix_is_empty_range(zix_path_root_name_range(path));
672 : : }
673 : :
674 : : ZIX_NONBLOCKING bool
675 : 251 : zix_path_has_root_directory(const char* const path)
676 : : {
677 : 251 : return !zix_is_empty_range(zix_path_root_slices(path).dir);
678 : : }
679 : :
680 : : ZIX_NONBLOCKING bool
681 : 251 : zix_path_has_relative_path(const char* const path)
682 : : {
683 [ + + + + ]: 251 : return path && path[zix_path_root_path_range(path).end];
684 : : }
685 : :
686 : : ZIX_NONBLOCKING bool
687 [ + + ]: 251 : zix_path_has_parent_path(const char* const path)
688 : : {
689 : 251 : return !zix_is_empty_range(zix_path_parent_path_range(zix_string(path)));
690 : : }
691 : :
692 : : ZIX_NONBLOCKING bool
693 [ + + ]: 416 : zix_path_has_filename(const char* const path)
694 : : {
695 : 416 : return !zix_is_empty_range(zix_path_filename_range(zix_string(path)));
696 : : }
697 : :
698 : : ZIX_NONBLOCKING bool
699 [ + + ]: 251 : zix_path_has_stem(const char* const path)
700 : : {
701 : 251 : return !zix_is_empty_range(zix_path_stem_range(zix_string(path)));
702 : : }
703 : :
704 : : ZIX_NONBLOCKING bool
705 [ + + ]: 251 : zix_path_has_extension(const char* const path)
706 : : {
707 : 251 : return !zix_is_empty_range(zix_path_extension_range(zix_string(path)));
708 : : }
709 : :
710 : : ZIX_NONBLOCKING bool
711 : 976 : zix_path_is_absolute(const char* const path)
712 : : {
713 : : #ifdef _WIN32
714 : : const ZixRootSlices root = zix_path_root_slices(path);
715 : : return (!zix_is_empty_range(root.name) &&
716 : : (!zix_is_empty_range(root.dir) ||
717 : : (is_dir_sep(path[0]) && is_dir_sep(path[1]))));
718 : :
719 : : #else
720 [ + + + + ]: 976 : return path && is_dir_sep(path[0]);
721 : : #endif
722 : : }
723 : :
724 : : ZIX_NONBLOCKING bool
725 : 266 : zix_path_is_relative(const char* const path)
726 : : {
727 : 266 : return !zix_path_is_absolute(path);
728 : : }
|