diff options
| author | Junio C Hamano <gitster@pobox.com> | 2026-02-27 15:11:50 -0800 |
|---|---|---|
| committer | Junio C Hamano <gitster@pobox.com> | 2026-02-27 15:11:51 -0800 |
| commit | ce4530ac10eecfcd80bd54c6993f2279a377e193 (patch) | |
| tree | 05f6ea104030c7bc939555e0f8d406dd2d451a67 | |
| parent | bb9c781f4f4be2e6bf6285149ba7007fdaa735e7 (diff) | |
| parent | 8b0061b5c5008375ef0986b7aafedbd7d79da0f6 (diff) | |
| download | git-ce4530ac10eecfcd80bd54c6993f2279a377e193.tar.gz git-ce4530ac10eecfcd80bd54c6993f2279a377e193.zip | |
Merge branch 'jk/ref-filter-lrstrip-optim'
Code clean-up.
* jk/ref-filter-lrstrip-optim:
ref-filter: clarify lstrip/rstrip component counting
ref-filter: avoid strrchr() in rstrip_ref_components()
ref-filter: simplify rstrip_ref_components() memory handling
ref-filter: simplify lstrip_ref_components() memory handling
ref-filter: factor out refname component counting
| -rw-r--r-- | ref-filter.c | 65 |
1 files changed, 23 insertions, 42 deletions
diff --git a/ref-filter.c b/ref-filter.c index 3917c4ccd9..6bbb6fac18 100644 --- a/ref-filter.c +++ b/ref-filter.c @@ -2173,32 +2173,34 @@ static inline char *copy_advance(char *dst, const char *src) return dst; } -static const char *lstrip_ref_components(const char *refname, int len) +static int normalize_component_count(const char *refname, int len) { - long remaining = len; - const char *start = xstrdup(refname); - const char *to_free = start; - if (len < 0) { - int i; - const char *p = refname; + int slashes = 0; + + for (const char *p = refname; *p; p++) { + if (*p == '/') + slashes++; + } - /* Find total no of '/' separated path-components */ - for (i = 0; p[i]; p[i] == '/' ? i++ : *p++) - ; /* * The number of components we need to strip is now * the total minus the components to be left (Plus one * because we count the number of '/', but the number * of components is one more than the no of '/'). */ - remaining = i + len + 1; + len = slashes + len + 1; } + return len; +} + +static const char *lstrip_ref_components(const char *refname, int len) +{ + int remaining = normalize_component_count(refname, len); while (remaining > 0) { - switch (*start++) { + switch (*refname++) { case '\0': - free((char *)to_free); return xstrdup(""); case '/': remaining--; @@ -2206,42 +2208,21 @@ static const char *lstrip_ref_components(const char *refname, int len) } } - start = xstrdup(start); - free((char *)to_free); - return start; + return xstrdup(refname); } static const char *rstrip_ref_components(const char *refname, int len) { - long remaining = len; - const char *start = xstrdup(refname); - const char *to_free = start; - - if (len < 0) { - int i; - const char *p = refname; + int remaining = normalize_component_count(refname, len); + const char *end = refname + strlen(refname); - /* Find total no of '/' separated path-components */ - for (i = 0; p[i]; p[i] == '/' ? i++ : *p++) - ; - /* - * The number of components we need to strip is now - * the total minus the components to be left (Plus one - * because we count the number of '/', but the number - * of components is one more than the no of '/'). - */ - remaining = i + len + 1; - } - - while (remaining-- > 0) { - char *p = strrchr(start, '/'); - if (!p) { - free((char *)to_free); + while (remaining > 0) { + if (end == refname) return xstrdup(""); - } else - p[0] = '\0'; + if (*--end == '/') + remaining--; } - return start; + return xmemdupz(refname, end - refname); } static const char *show_ref(struct refname_atom *atom, const char *refname) |
