[PATCH 01/12] Make starts_with() a wrapper of skip_prefix()
starts_with() started out as a copy of prefixcmp(). But if we don't care about the sorting order, the logic looks closer to skip_prefix(). This looks like a good thing to do. Signed-off-by: Nguyễn Thái Ngọc Duy pclo...@gmail.com --- git-compat-util.h | 6 +- strbuf.c | 9 - 2 files changed, 5 insertions(+), 10 deletions(-) diff --git a/git-compat-util.h b/git-compat-util.h index b73916b..84f1078 100644 --- a/git-compat-util.h +++ b/git-compat-util.h @@ -350,7 +350,6 @@ extern void set_die_routine(NORETURN_PTR void (*routine)(const char *err, va_lis extern void set_error_routine(void (*routine)(const char *err, va_list params)); extern void set_die_is_recursing_routine(int (*routine)(void)); -extern int starts_with(const char *str, const char *prefix); extern int prefixcmp(const char *str, const char *prefix); extern int ends_with(const char *str, const char *suffix); extern int suffixcmp(const char *str, const char *suffix); @@ -361,6 +360,11 @@ static inline const char *skip_prefix(const char *str, const char *prefix) return strncmp(str, prefix, len) ? NULL : str + len; } +static inline int starts_with(const char *str, const char *prefix) +{ + return skip_prefix(str, prefix) != NULL; +} + #if defined(NO_MMAP) || defined(USE_WIN32_MMAP) #ifndef PROT_READ diff --git a/strbuf.c b/strbuf.c index 83caf4a..bd4c0d8 100644 --- a/strbuf.c +++ b/strbuf.c @@ -1,15 +1,6 @@ #include cache.h #include refs.h -int starts_with(const char *str, const char *prefix) -{ - for (; ; str++, prefix++) - if (!*prefix) - return 1; - else if (*str != *prefix) - return 0; -} - int prefixcmp(const char *str, const char *prefix) { for (; ; str++, prefix++) -- 1.8.5.1.208.g019362e -- To unsubscribe from this list: send the line unsubscribe git in the body of a message to majord...@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html
Re: [PATCH 01/12] Make starts_with() a wrapper of skip_prefix()
Nguyễn Thái Ngọc Duy pclo...@gmail.com writes: starts_with() started out as a copy of prefixcmp(). But if we don't care about the sorting order, the logic looks closer to skip_prefix(). This looks like a good thing to do. Signed-off-by: Nguyễn Thái Ngọc Duy pclo...@gmail.com --- Sure, but the implementation of skip_prefix() scans the prefix string twice, while prefixcmp() aka starts_with() does it only once. I'd expect a later step in this series would rectify this micro regression in the performance, though ;-) git-compat-util.h | 6 +- strbuf.c | 9 - 2 files changed, 5 insertions(+), 10 deletions(-) diff --git a/git-compat-util.h b/git-compat-util.h index b73916b..84f1078 100644 --- a/git-compat-util.h +++ b/git-compat-util.h @@ -350,7 +350,6 @@ extern void set_die_routine(NORETURN_PTR void (*routine)(const char *err, va_lis extern void set_error_routine(void (*routine)(const char *err, va_list params)); extern void set_die_is_recursing_routine(int (*routine)(void)); -extern int starts_with(const char *str, const char *prefix); extern int prefixcmp(const char *str, const char *prefix); extern int ends_with(const char *str, const char *suffix); extern int suffixcmp(const char *str, const char *suffix); @@ -361,6 +360,11 @@ static inline const char *skip_prefix(const char *str, const char *prefix) return strncmp(str, prefix, len) ? NULL : str + len; } +static inline int starts_with(const char *str, const char *prefix) +{ + return skip_prefix(str, prefix) != NULL; +} + #if defined(NO_MMAP) || defined(USE_WIN32_MMAP) #ifndef PROT_READ diff --git a/strbuf.c b/strbuf.c index 83caf4a..bd4c0d8 100644 --- a/strbuf.c +++ b/strbuf.c @@ -1,15 +1,6 @@ #include cache.h #include refs.h -int starts_with(const char *str, const char *prefix) -{ - for (; ; str++, prefix++) - if (!*prefix) - return 1; - else if (*str != *prefix) - return 0; -} - int prefixcmp(const char *str, const char *prefix) { for (; ; str++, prefix++) -- To unsubscribe from this list: send the line unsubscribe git in the body of a message to majord...@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html
Re: [PATCH 01/12] Make starts_with() a wrapper of skip_prefix()
Junio C Hamano gits...@pobox.com writes: Nguyễn Thái Ngọc Duy pclo...@gmail.com writes: starts_with() started out as a copy of prefixcmp(). But if we don't care about the sorting order, the logic looks closer to skip_prefix(). This looks like a good thing to do. Signed-off-by: Nguyễn Thái Ngọc Duy pclo...@gmail.com --- Sure, but the implementation of skip_prefix() scans the prefix string twice, while prefixcmp() aka starts_with() does it only once. I'd expect a later step in this series would rectify this micro regression in the performance, though ;-) ... and I did not see one, but it would be trivial on top. git-compat-util.h | 7 +-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/git-compat-util.h b/git-compat-util.h index b72a80d..59265af 100644 --- a/git-compat-util.h +++ b/git-compat-util.h @@ -356,8 +356,11 @@ extern int suffixcmp(const char *str, const char *suffix); static inline const char *skip_prefix_defval(const char *str, const char *prefix, const char *defval) { - size_t len = strlen(prefix); - return strncmp(str, prefix, len) ? defval : str + len; + for ( ; ; str++, prefix++) + if (!*prefix) + return str; + else if (*str != *prefix) + return defval; } static inline const char *skip_prefix(const char *str, const char *prefix) -- To unsubscribe from this list: send the line unsubscribe git in the body of a message to majord...@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html