Introduce the function strip_path_suffix()
The function strip_path_suffix() will try to strip a given suffix from a given path. The suffix must start at a directory boundary (i.e. "core" is not a path suffix of "libexec/git-core", but "git-core" is). Arbitrary runs of directory separators ("slashes") are assumed identical. Example: strip_path_suffix("C:\\msysgit/\\libexec\\git-core", "libexec///git-core", &prefix) will set prefix to "C:\\msysgit" and return 0. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Acked-by: Johannes Sixt <j6t@kdbg.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Johannes Schindelin committed
Feb 19, 2009 at 20:10 UTC
4fcc86b07db26da17f17af0fd777cab78fcaaf4c
4 files changed
+47
cache.h
+1
@@ -626,6 +626,7 @@ const char *make_nonrelative_path(const char *path);
626
const char *make_relative_path(const char *abs, const char *base);
627
int normalize_path_copy(char *dst, const char *src);
628
int longest_ancestor_length(const char *path, const char *prefix_list);
629
+char *strip_path_suffix(const char *path, const char *suffix);
630
631
/* Read and unpack a sha1 file into memory, write memory to a sha1 file */
632
extern int sha1_object_info(const unsigned char *, unsigned long *);
path.c
+36
@@ -499,3 +499,39 @@ int longest_ancestor_length(const char *path, const char *prefix_list)
499
500
return max_len;
501
}
502
+
503
+/* strip arbitrary amount of directory separators at end of path */
504
+static inline int chomp_trailing_dir_sep(const char *path, int len)
505
+{
506
+ while (len && is_dir_sep(path[len - 1]))
507
+ len--;
508
+ return len;
509
+}
510
+
511
+/*
512
+ * If path ends with suffix (complete path components), returns the
513
+ * part before suffix (sans trailing directory separators).
514
+ * Otherwise returns NULL.
515
+ */
516
+char *strip_path_suffix(const char *path, const char *suffix)
517
+{
518
+ int path_len = strlen(path), suffix_len = strlen(suffix);
519
+
520
+ while (suffix_len) {
521
+ if (!path_len)
522
+ return NULL;
523
+
524
+ if (is_dir_sep(path[path_len - 1])) {
525
+ if (!is_dir_sep(suffix[suffix_len - 1]))
526
+ return NULL;
527
+ path_len = chomp_trailing_dir_sep(path, path_len);
528
+ suffix_len = chomp_trailing_dir_sep(suffix, suffix_len);
529
+ }
530
+ else if (path[--path_len] != suffix[--suffix_len])
531
+ return NULL;
532
+ }
533
+
534
+ if (path_len && !is_dir_sep(path[path_len - 1]))
535
+ return NULL;
536
+ return xstrndup(path, chomp_trailing_dir_sep(path, path_len));
537
+}
t/t0060-path-utils.sh
+4
@@ -85,4 +85,8 @@ ancestor /foo/bar :://foo/.:: 4
85
ancestor /foo/bar //foo/./::/bar 4
86
ancestor /foo/bar ::/bar -1
87
88
+test_expect_success 'strip_path_suffix' '
89
+ test c:/msysgit = $(test-path-utils strip_path_suffix \
90
+ c:/msysgit/libexec//git-core libexec/git-core)
91
+'
92
test_done
test-path-utils.c
+6
@@ -26,6 +26,12 @@ int main(int argc, char **argv)
26
return 0;
27
}
28
29
+ if (argc == 4 && !strcmp(argv[1], "strip_path_suffix")) {
30
+ char *prefix = strip_path_suffix(argv[2], argv[3]);
31
+ printf("%s\n", prefix ? prefix : "(null)");
32
+ return 0;
33
+ }
34
+
35
fprintf(stderr, "%s: unknown function name: %s\n", argv[0],
36
argv[1] ? argv[1] : "(there was none)");
37
return 1;