tree-diff: allow diff_tree_sha1 to accept NULL sha1
[gitweb.git] / strbuf.c
index 48e9abb5c2069bfc02a32747004bf51799dda0f1..83caf4a9147e6788107fb5fd23e6d9e1d6f6267f 100644 (file)
--- a/strbuf.c
+++ b/strbuf.c
@@ -1,6 +1,15 @@
 #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++)
@@ -10,6 +19,15 @@ int prefixcmp(const char *str, const char *prefix)
                        return (unsigned char)*prefix - (unsigned char)*str;
 }
 
+int ends_with(const char *str, const char *suffix)
+{
+       int len = strlen(str), suflen = strlen(suffix);
+       if (len < suflen)
+               return 0;
+       else
+               return !strcmp(str + len - suflen, suffix);
+}
+
 int suffixcmp(const char *str, const char *suffix)
 {
        int len = strlen(str), suflen = strlen(suffix);
@@ -528,6 +546,25 @@ void strbuf_addstr_urlencode(struct strbuf *sb, const char *s,
        strbuf_add_urlencode(sb, s, strlen(s), reserved);
 }
 
+void strbuf_humanise_bytes(struct strbuf *buf, off_t bytes)
+{
+       if (bytes > 1 << 30) {
+               strbuf_addf(buf, "%u.%2.2u GiB",
+                           (int)(bytes >> 30),
+                           (int)(bytes & ((1 << 30) - 1)) / 10737419);
+       } else if (bytes > 1 << 20) {
+               int x = bytes + 5243;  /* for rounding */
+               strbuf_addf(buf, "%u.%2.2u MiB",
+                           x >> 20, ((x & ((1 << 20) - 1)) * 100) >> 20);
+       } else if (bytes > 1 << 10) {
+               int x = bytes + 5;  /* for rounding */
+               strbuf_addf(buf, "%u.%2.2u KiB",
+                           x >> 10, ((x & ((1 << 10) - 1)) * 100) >> 10);
+       } else {
+               strbuf_addf(buf, "%u bytes", (int)bytes);
+       }
+}
+
 int printf_ln(const char *fmt, ...)
 {
        int ret;