]> git.ipfire.org Git - thirdparty/git.git/blobdiff - strbuf.c
"log --cherry-pick" documentation regression fix
[thirdparty/git.git] / strbuf.c
index a6153dca278abe957254fa091fdcd8eb13b90b25..07e8883ceb297bdde79f06ffa2d0519581ee60b9 100644 (file)
--- a/strbuf.c
+++ b/strbuf.c
@@ -10,6 +10,15 @@ int prefixcmp(const char *str, const char *prefix)
                        return (unsigned char)*prefix - (unsigned char)*str;
 }
 
+int suffixcmp(const char *str, const char *suffix)
+{
+       int len = strlen(str), suflen = strlen(suffix);
+       if (len < suflen)
+               return -1;
+       else
+               return strcmp(str + len - suflen, suffix);
+}
+
 /*
  * Used as the default ->buf value, so that people can always assume
  * buf is non NULL and ->buf is NUL terminated even for a freshly
@@ -54,7 +63,8 @@ void strbuf_attach(struct strbuf *sb, void *buf, size_t len, size_t alloc)
 
 void strbuf_grow(struct strbuf *sb, size_t extra)
 {
-       if (sb->len + extra + 1 <= sb->len)
+       if (unsigned_add_overflows(extra, 1) ||
+           unsigned_add_overflows(sb->len, extra + 1))
                die("you want to use way too much memory");
        if (!sb->alloc)
                sb->buf = NULL;
@@ -91,13 +101,6 @@ void strbuf_ltrim(struct strbuf *sb)
        sb->buf[sb->len] = '\0';
 }
 
-void strbuf_tolower(struct strbuf *sb)
-{
-       int i;
-       for (i = 0; i < sb->len; i++)
-               sb->buf[i] = tolower(sb->buf[i]);
-}
-
 struct strbuf **strbuf_split(const struct strbuf *sb, int delim)
 {
        int alloc = 2, pos = 0;
@@ -150,7 +153,7 @@ int strbuf_cmp(const struct strbuf *a, const struct strbuf *b)
 void strbuf_splice(struct strbuf *sb, size_t pos, size_t len,
                                   const void *data, size_t dlen)
 {
-       if (pos + len < pos)
+       if (unsigned_add_overflows(pos, len))
                die("you want to use way too much memory");
        if (pos > sb->len)
                die("`pos' is too far after the end of the buffer");
@@ -227,6 +230,12 @@ void strbuf_expand(struct strbuf *sb, const char *format, expand_fn_t fn,
                        break;
                format = percent + 1;
 
+               if (*format == '%') {
+                       strbuf_addch(sb, '%');
+                       format++;
+                       continue;
+               }
+
                consumed = fn(sb, format, context);
                if (consumed)
                        format += consumed;
@@ -251,6 +260,17 @@ size_t strbuf_expand_dict_cb(struct strbuf *sb, const char *placeholder,
        return 0;
 }
 
+void strbuf_addbuf_percentquote(struct strbuf *dst, const struct strbuf *src)
+{
+       int i, len = src->len;
+
+       for (i = 0; i < len; i++) {
+               if (src->buf[i] == '%')
+                       strbuf_addch(dst, '%');
+               strbuf_addch(dst, src->buf[i]);
+       }
+}
+
 size_t strbuf_fread(struct strbuf *sb, size_t size, FILE *f)
 {
        size_t res;
@@ -367,19 +387,3 @@ int strbuf_read_file(struct strbuf *sb, const char *path, size_t hint)
 
        return len;
 }
-
-int strbuf_branchname(struct strbuf *sb, const char *name)
-{
-       int len = strlen(name);
-       if (interpret_branch_name(name, sb) == len)
-               return 0;
-       strbuf_add(sb, name, len);
-       return len;
-}
-
-int strbuf_check_branch_ref(struct strbuf *sb, const char *name)
-{
-       strbuf_branchname(sb, name);
-       strbuf_splice(sb, 0, 0, "refs/heads/", 11);
-       return check_ref_format(sb->buf);
-}