]> git.ipfire.org Git - thirdparty/git.git/commitdiff
skip_prefix: add case-insensitive variant
authorJeff King <peff@peff.net>
Sun, 13 May 2018 16:57:14 +0000 (12:57 -0400)
committerJeff King <peff@peff.net>
Tue, 22 May 2018 03:50:11 +0000 (23:50 -0400)
We have the convenient skip_prefix() helper, but if you want
to do case-insensitive matching, you're stuck doing it by
hand. We could add an extra parameter to the function to
let callers ask for this, but the function is small and
somewhat performance-critical. Let's just re-implement it
for the case-insensitive version.

Signed-off-by: Jeff King <peff@peff.net>
git-compat-util.h

index 59866d72fa3bb5aff109632b42c7178b24f528bc..4be15e5eb233ffdfbce17826c20e841e56f14630 100644 (file)
@@ -956,6 +956,23 @@ static inline int sane_iscase(int x, int is_lower)
                return (x & 0x20) == 0;
 }
 
+/*
+ * Like skip_prefix, but compare case-insensitively. Note that the comparison
+ * is done via tolower(), so it is strictly ASCII (no multi-byte characters or
+ * locale-specific conversions).
+ */
+static inline int skip_iprefix(const char *str, const char *prefix,
+                              const char **out)
+{
+       do {
+               if (!*prefix) {
+                       *out = str;
+                       return 1;
+               }
+       } while (tolower(*str++) == tolower(*prefix++));
+       return 0;
+}
+
 static inline int strtoul_ui(char const *s, int base, unsigned int *result)
 {
        unsigned long ul;