]> git.ipfire.org Git - thirdparty/git.git/blobdiff - strbuf.c
Git 2.45
[thirdparty/git.git] / strbuf.c
index a33aed6c07a1e7e1cd3204d012bed12ed9f2e06b..0d929e4e195fa3db65257142e558938471e7c141 100644 (file)
--- a/strbuf.c
+++ b/strbuf.c
@@ -24,6 +24,17 @@ int istarts_with(const char *str, const char *prefix)
                        return 0;
 }
 
+int starts_with_mem(const char *str, size_t len, const char *prefix)
+{
+       const char *end = str + len;
+       for (; ; str++, prefix++) {
+               if (!*prefix)
+                       return 1;
+               else if (str == end || *str != *prefix)
+                       return 0;
+       }
+}
+
 int skip_to_optional_arg_default(const char *str, const char *prefix,
                                 const char **arg, const char *def)
 {
@@ -266,7 +277,7 @@ void strbuf_vinsertf(struct strbuf *sb, size_t pos, const char *fmt, va_list ap)
        len = vsnprintf(sb->buf + sb->len, 0, fmt, cp);
        va_end(cp);
        if (len < 0)
-               BUG("your vsnprintf is broken (returned %d)", len);
+               die(_("unable to format message: %s"), fmt);
        if (!len)
                return; /* nothing to do */
        if (unsigned_add_overflows(sb->len, len))
@@ -359,16 +370,12 @@ static void add_lines(struct strbuf *out,
 }
 
 void strbuf_add_commented_lines(struct strbuf *out, const char *buf,
-                               size_t size, char comment_prefix)
+                               size_t size, const char *comment_prefix)
 {
-       char prefix[2];
-
-       prefix[0] = comment_prefix;
-       prefix[1] = '\0';
-       add_lines(out, prefix, buf, size, 1);
+       add_lines(out, comment_prefix, buf, size, 1);
 }
 
-void strbuf_commented_addf(struct strbuf *sb, char comment_prefix,
+void strbuf_commented_addf(struct strbuf *sb, const char *comment_prefix,
                           const char *fmt, ...)
 {
        va_list params;
@@ -397,7 +404,7 @@ void strbuf_vaddf(struct strbuf *sb, const char *fmt, va_list ap)
        len = vsnprintf(sb->buf + sb->len, sb->alloc - sb->len, fmt, cp);
        va_end(cp);
        if (len < 0)
-               BUG("your vsnprintf is broken (returned %d)", len);
+               die(_("unable to format message: %s"), fmt);
        if (len > strbuf_avail(sb)) {
                strbuf_grow(sb, len);
                len = vsnprintf(sb->buf + sb->len, sb->alloc - sb->len, fmt, ap);
@@ -438,6 +445,26 @@ size_t strbuf_expand_literal(struct strbuf *sb, const char *placeholder)
        return 0;
 }
 
+void strbuf_expand_bad_format(const char *format, const char *command)
+{
+       const char *end;
+
+       if (*format != '(')
+               /* TRANSLATORS: The first %s is a command like "ls-tree". */
+               die(_("bad %s format: element '%s' does not start with '('"),
+                   command, format);
+
+       end = strchr(format + 1, ')');
+       if (!end)
+               /* TRANSLATORS: The first %s is a command like "ls-tree". */
+               die(_("bad %s format: element '%s' does not end in ')'"),
+                   command, format);
+
+       /* TRANSLATORS: %s is a command like "ls-tree". */
+       die(_("bad %s format: %%%.*s"),
+           command, (int)(end - format + 1), format);
+}
+
 void strbuf_addbuf_percentquote(struct strbuf *dst, const struct strbuf *src)
 {
        size_t i, len = src->len;
@@ -1001,10 +1028,10 @@ static size_t cleanup(char *line, size_t len)
  *
  * If last line does not have a newline at the end, one is added.
  *
- * Pass a non-NUL comment_prefix to skip every line starting
+ * Pass a non-NULL comment_prefix to skip every line starting
  * with it.
  */
-void strbuf_stripspace(struct strbuf *sb, char comment_prefix)
+void strbuf_stripspace(struct strbuf *sb, const char *comment_prefix)
 {
        size_t empties = 0;
        size_t i, j, len, newlen;
@@ -1018,7 +1045,7 @@ void strbuf_stripspace(struct strbuf *sb, char comment_prefix)
                len = eol ? eol - (sb->buf + i) + 1 : sb->len - i;
 
                if (comment_prefix && len &&
-                   sb->buf[i] == comment_prefix) {
+                   starts_with(sb->buf + i, comment_prefix)) {
                        newlen = 0;
                        continue;
                }