]> git.ipfire.org Git - thirdparty/git.git/blobdiff - strbuf.c
Git 2.45
[thirdparty/git.git] / strbuf.c
index 7c8f58212758b120b759074d8c7fa5f7da239490..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))
@@ -393,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);
@@ -434,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;