From: Junio C Hamano Date: Wed, 16 Jul 2014 18:25:59 +0000 (-0700) Subject: Merge branch 'jk/strip-suffix' X-Git-Tag: v2.1.0-rc0~40 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=6e4094731acee5207595a8416d19508107ea475d;p=git Merge branch 'jk/strip-suffix' * jk/strip-suffix: prepare_packed_git_one: refactor duplicate-pack check verify-pack: use strbuf_strip_suffix strbuf: implement strbuf_strip_suffix index-pack: use strip_suffix to avoid magic numbers use strip_suffix instead of ends_with in simple cases replace has_extension with ends_with implement ends_with via strip_suffix add strip_suffix function sha1_file: replace PATH_MAX buffer with strbuf in prepare_packed_git_one() --- 6e4094731acee5207595a8416d19508107ea475d diff --cc git-compat-util.h index 9de3180710,47a49c355d..0b53c9a4af --- a/git-compat-util.h +++ b/git-compat-util.h @@@ -347,36 -339,49 +347,68 @@@ extern void set_error_routine(void (*ro extern void set_die_is_recursing_routine(int (*routine)(void)); extern int starts_with(const char *str, const char *prefix); - extern int ends_with(const char *str, const char *suffix); -static inline const char *skip_prefix(const char *str, const char *prefix) +/* + * If the string "str" begins with the string found in "prefix", return 1. + * The "out" parameter is set to "str + strlen(prefix)" (i.e., to the point in + * the string right after the prefix). + * + * Otherwise, return 0 and leave "out" untouched. + * + * Examples: + * + * [extract branch name, fail if not a branch] + * if (!skip_prefix(ref, "refs/heads/", &branch) + * return -1; + * + * [skip prefix if present, otherwise use whole string] + * skip_prefix(name, "refs/heads/", &name); + */ +static inline int skip_prefix(const char *str, const char *prefix, + const char **out) { do { - if (!*prefix) - return str; + if (!*prefix) { + *out = str; + return 1; + } } while (*str++ == *prefix++); - return NULL; + return 0; } + /* + * If buf ends with suffix, return 1 and subtract the length of the suffix + * from *len. Otherwise, return 0 and leave *len untouched. + */ + static inline int strip_suffix_mem(const char *buf, size_t *len, + const char *suffix) + { + size_t suflen = strlen(suffix); + if (*len < suflen || memcmp(buf + (*len - suflen), suffix, suflen)) + return 0; + *len -= suflen; + return 1; + } + + /* + * If str ends with suffix, return 1 and set *len to the size of the string + * without the suffix. Otherwise, return 0 and set *len to the size of the + * string. + * + * Note that we do _not_ NUL-terminate str to the new length. + */ + static inline int strip_suffix(const char *str, const char *suffix, size_t *len) + { + *len = strlen(str); + return strip_suffix_mem(str, len, suffix); + } + + static inline int ends_with(const char *str, const char *suffix) + { + size_t len; + return strip_suffix(str, suffix, &len); + } + #if defined(NO_MMAP) || defined(USE_WIN32_MMAP) #ifndef PROT_READ diff --cc help.c index f31f29ac42,97567c4523..7af65e205e --- a/help.c +++ b/help.c @@@ -143,10 -145,9 +143,10 @@@ static void list_commands_in_dir(struc len = buf.len; while ((de = readdir(dir)) != NULL) { + const char *ent; - int entlen; + size_t entlen; - if (!starts_with(de->d_name, prefix)) + if (!skip_prefix(de->d_name, prefix, &ent)) continue; strbuf_setlen(&buf, len); @@@ -154,11 -155,10 +154,10 @@@ if (!is_executable(buf.buf)) continue; - entlen = strlen(de->d_name) - prefix_len; - strip_suffix(de->d_name, ".exe", &entlen); + entlen = strlen(ent); - if (has_extension(ent, ".exe")) - entlen -= 4; ++ strip_suffix(ent, ".exe", &entlen); - add_cmdname(cmds, de->d_name + prefix_len, entlen); + add_cmdname(cmds, ent, entlen); } closedir(dir); strbuf_release(&buf); diff --cc strbuf.h index a594c24b2b,1d768c1124..a7c0192e9e --- a/strbuf.h +++ b/strbuf.h @@@ -45,10 -45,17 +45,19 @@@ static inline void strbuf_setlen(struc extern void strbuf_trim(struct strbuf *); extern void strbuf_rtrim(struct strbuf *); extern void strbuf_ltrim(struct strbuf *); +extern int strbuf_reencode(struct strbuf *sb, const char *from, const char *to); +extern void strbuf_tolower(struct strbuf *sb); extern int strbuf_cmp(const struct strbuf *, const struct strbuf *); + static inline int strbuf_strip_suffix(struct strbuf *sb, const char *suffix) + { + if (strip_suffix_mem(sb->buf, &sb->len, suffix)) { + strbuf_setlen(sb, sb->len); + return 1; + } else + return 0; + } + /* * Split str (of length slen) at the specified terminator character. * Return a null-terminated array of pointers to strbuf objects