strbuf.con commit Documentation/submodule: add command references and update options (402e8a6)
   1#include "cache.h"
   2#include "refs.h"
   3
   4int prefixcmp(const char *str, const char *prefix)
   5{
   6        for (; ; str++, prefix++)
   7                if (!*prefix)
   8                        return 0;
   9                else if (*str != *prefix)
  10                        return (unsigned char)*prefix - (unsigned char)*str;
  11}
  12
  13int suffixcmp(const char *str, const char *suffix)
  14{
  15        int len = strlen(str), suflen = strlen(suffix);
  16        if (len < suflen)
  17                return -1;
  18        else
  19                return strcmp(str + len - suflen, suffix);
  20}
  21
  22/*
  23 * Used as the default ->buf value, so that people can always assume
  24 * buf is non NULL and ->buf is NUL terminated even for a freshly
  25 * initialized strbuf.
  26 */
  27char strbuf_slopbuf[1];
  28
  29void strbuf_init(struct strbuf *sb, size_t hint)
  30{
  31        sb->alloc = sb->len = 0;
  32        sb->buf = strbuf_slopbuf;
  33        if (hint) {
  34                strbuf_grow(sb, hint);
  35                sb->buf[0] = '\0';
  36        }
  37}
  38
  39void strbuf_release(struct strbuf *sb)
  40{
  41        if (sb->alloc) {
  42                free(sb->buf);
  43                strbuf_init(sb, 0);
  44        }
  45}
  46
  47char *strbuf_detach(struct strbuf *sb, size_t *sz)
  48{
  49        char *res = sb->alloc ? sb->buf : NULL;
  50        if (sz)
  51                *sz = sb->len;
  52        strbuf_init(sb, 0);
  53        return res;
  54}
  55
  56void strbuf_attach(struct strbuf *sb, void *buf, size_t len, size_t alloc)
  57{
  58        strbuf_release(sb);
  59        sb->buf   = buf;
  60        sb->len   = len;
  61        sb->alloc = alloc;
  62        strbuf_grow(sb, 0);
  63        sb->buf[sb->len] = '\0';
  64}
  65
  66void strbuf_grow(struct strbuf *sb, size_t extra)
  67{
  68        if (unsigned_add_overflows(extra, 1) ||
  69            unsigned_add_overflows(sb->len, extra + 1))
  70                die("you want to use way too much memory");
  71        if (!sb->alloc)
  72                sb->buf = NULL;
  73        ALLOC_GROW(sb->buf, sb->len + extra + 1, sb->alloc);
  74}
  75
  76void strbuf_trim(struct strbuf *sb)
  77{
  78        char *b = sb->buf;
  79        while (sb->len > 0 && isspace((unsigned char)sb->buf[sb->len - 1]))
  80                sb->len--;
  81        while (sb->len > 0 && isspace(*b)) {
  82                b++;
  83                sb->len--;
  84        }
  85        memmove(sb->buf, b, sb->len);
  86        sb->buf[sb->len] = '\0';
  87}
  88void strbuf_rtrim(struct strbuf *sb)
  89{
  90        while (sb->len > 0 && isspace((unsigned char)sb->buf[sb->len - 1]))
  91                sb->len--;
  92        sb->buf[sb->len] = '\0';
  93}
  94
  95void strbuf_ltrim(struct strbuf *sb)
  96{
  97        char *b = sb->buf;
  98        while (sb->len > 0 && isspace(*b)) {
  99                b++;
 100                sb->len--;
 101        }
 102        memmove(sb->buf, b, sb->len);
 103        sb->buf[sb->len] = '\0';
 104}
 105
 106struct strbuf **strbuf_split(const struct strbuf *sb, int delim)
 107{
 108        int alloc = 2, pos = 0;
 109        char *n, *p;
 110        struct strbuf **ret;
 111        struct strbuf *t;
 112
 113        ret = xcalloc(alloc, sizeof(struct strbuf *));
 114        p = n = sb->buf;
 115        while (n < sb->buf + sb->len) {
 116                int len;
 117                n = memchr(n, delim, sb->len - (n - sb->buf));
 118                if (pos + 1 >= alloc) {
 119                        alloc = alloc * 2;
 120                        ret = xrealloc(ret, sizeof(struct strbuf *) * alloc);
 121                }
 122                if (!n)
 123                        n = sb->buf + sb->len - 1;
 124                len = n - p + 1;
 125                t = xmalloc(sizeof(struct strbuf));
 126                strbuf_init(t, len);
 127                strbuf_add(t, p, len);
 128                ret[pos] = t;
 129                ret[++pos] = NULL;
 130                p = ++n;
 131        }
 132        return ret;
 133}
 134
 135void strbuf_list_free(struct strbuf **sbs)
 136{
 137        struct strbuf **s = sbs;
 138
 139        while (*s) {
 140                strbuf_release(*s);
 141                free(*s++);
 142        }
 143        free(sbs);
 144}
 145
 146int strbuf_cmp(const struct strbuf *a, const struct strbuf *b)
 147{
 148        int len = a->len < b->len ? a->len: b->len;
 149        int cmp = memcmp(a->buf, b->buf, len);
 150        if (cmp)
 151                return cmp;
 152        return a->len < b->len ? -1: a->len != b->len;
 153}
 154
 155void strbuf_splice(struct strbuf *sb, size_t pos, size_t len,
 156                                   const void *data, size_t dlen)
 157{
 158        if (unsigned_add_overflows(pos, len))
 159                die("you want to use way too much memory");
 160        if (pos > sb->len)
 161                die("`pos' is too far after the end of the buffer");
 162        if (pos + len > sb->len)
 163                die("`pos + len' is too far after the end of the buffer");
 164
 165        if (dlen >= len)
 166                strbuf_grow(sb, dlen - len);
 167        memmove(sb->buf + pos + dlen,
 168                        sb->buf + pos + len,
 169                        sb->len - pos - len);
 170        memcpy(sb->buf + pos, data, dlen);
 171        strbuf_setlen(sb, sb->len + dlen - len);
 172}
 173
 174void strbuf_insert(struct strbuf *sb, size_t pos, const void *data, size_t len)
 175{
 176        strbuf_splice(sb, pos, 0, data, len);
 177}
 178
 179void strbuf_remove(struct strbuf *sb, size_t pos, size_t len)
 180{
 181        strbuf_splice(sb, pos, len, NULL, 0);
 182}
 183
 184void strbuf_add(struct strbuf *sb, const void *data, size_t len)
 185{
 186        strbuf_grow(sb, len);
 187        memcpy(sb->buf + sb->len, data, len);
 188        strbuf_setlen(sb, sb->len + len);
 189}
 190
 191void strbuf_adddup(struct strbuf *sb, size_t pos, size_t len)
 192{
 193        strbuf_grow(sb, len);
 194        memcpy(sb->buf + sb->len, sb->buf + pos, len);
 195        strbuf_setlen(sb, sb->len + len);
 196}
 197
 198void strbuf_addf(struct strbuf *sb, const char *fmt, ...)
 199{
 200        va_list ap;
 201        va_start(ap, fmt);
 202        strbuf_vaddf(sb, fmt, ap);
 203        va_end(ap);
 204}
 205
 206void strbuf_vaddf(struct strbuf *sb, const char *fmt, va_list ap)
 207{
 208        int len;
 209        va_list cp;
 210
 211        if (!strbuf_avail(sb))
 212                strbuf_grow(sb, 64);
 213        va_copy(cp, ap);
 214        len = vsnprintf(sb->buf + sb->len, sb->alloc - sb->len, fmt, cp);
 215        va_end(cp);
 216        if (len < 0)
 217                die("BUG: your vsnprintf is broken (returned %d)", len);
 218        if (len > strbuf_avail(sb)) {
 219                strbuf_grow(sb, len);
 220                len = vsnprintf(sb->buf + sb->len, sb->alloc - sb->len, fmt, ap);
 221                if (len > strbuf_avail(sb))
 222                        die("BUG: your vsnprintf is broken (insatiable)");
 223        }
 224        strbuf_setlen(sb, sb->len + len);
 225}
 226
 227void strbuf_expand(struct strbuf *sb, const char *format, expand_fn_t fn,
 228                   void *context)
 229{
 230        for (;;) {
 231                const char *percent;
 232                size_t consumed;
 233
 234                percent = strchrnul(format, '%');
 235                strbuf_add(sb, format, percent - format);
 236                if (!*percent)
 237                        break;
 238                format = percent + 1;
 239
 240                if (*format == '%') {
 241                        strbuf_addch(sb, '%');
 242                        format++;
 243                        continue;
 244                }
 245
 246                consumed = fn(sb, format, context);
 247                if (consumed)
 248                        format += consumed;
 249                else
 250                        strbuf_addch(sb, '%');
 251        }
 252}
 253
 254size_t strbuf_expand_dict_cb(struct strbuf *sb, const char *placeholder,
 255                void *context)
 256{
 257        struct strbuf_expand_dict_entry *e = context;
 258        size_t len;
 259
 260        for (; e->placeholder && (len = strlen(e->placeholder)); e++) {
 261                if (!strncmp(placeholder, e->placeholder, len)) {
 262                        if (e->value)
 263                                strbuf_addstr(sb, e->value);
 264                        return len;
 265                }
 266        }
 267        return 0;
 268}
 269
 270void strbuf_addbuf_percentquote(struct strbuf *dst, const struct strbuf *src)
 271{
 272        int i, len = src->len;
 273
 274        for (i = 0; i < len; i++) {
 275                if (src->buf[i] == '%')
 276                        strbuf_addch(dst, '%');
 277                strbuf_addch(dst, src->buf[i]);
 278        }
 279}
 280
 281size_t strbuf_fread(struct strbuf *sb, size_t size, FILE *f)
 282{
 283        size_t res;
 284        size_t oldalloc = sb->alloc;
 285
 286        strbuf_grow(sb, size);
 287        res = fread(sb->buf + sb->len, 1, size, f);
 288        if (res > 0)
 289                strbuf_setlen(sb, sb->len + res);
 290        else if (oldalloc == 0)
 291                strbuf_release(sb);
 292        return res;
 293}
 294
 295ssize_t strbuf_read(struct strbuf *sb, int fd, size_t hint)
 296{
 297        size_t oldlen = sb->len;
 298        size_t oldalloc = sb->alloc;
 299
 300        strbuf_grow(sb, hint ? hint : 8192);
 301        for (;;) {
 302                ssize_t cnt;
 303
 304                cnt = xread(fd, sb->buf + sb->len, sb->alloc - sb->len - 1);
 305                if (cnt < 0) {
 306                        if (oldalloc == 0)
 307                                strbuf_release(sb);
 308                        else
 309                                strbuf_setlen(sb, oldlen);
 310                        return -1;
 311                }
 312                if (!cnt)
 313                        break;
 314                sb->len += cnt;
 315                strbuf_grow(sb, 8192);
 316        }
 317
 318        sb->buf[sb->len] = '\0';
 319        return sb->len - oldlen;
 320}
 321
 322#define STRBUF_MAXLINK (2*PATH_MAX)
 323
 324int strbuf_readlink(struct strbuf *sb, const char *path, size_t hint)
 325{
 326        size_t oldalloc = sb->alloc;
 327
 328        if (hint < 32)
 329                hint = 32;
 330
 331        while (hint < STRBUF_MAXLINK) {
 332                int len;
 333
 334                strbuf_grow(sb, hint);
 335                len = readlink(path, sb->buf, hint);
 336                if (len < 0) {
 337                        if (errno != ERANGE)
 338                                break;
 339                } else if (len < hint) {
 340                        strbuf_setlen(sb, len);
 341                        return 0;
 342                }
 343
 344                /* .. the buffer was too small - try again */
 345                hint *= 2;
 346        }
 347        if (oldalloc == 0)
 348                strbuf_release(sb);
 349        return -1;
 350}
 351
 352int strbuf_getwholeline(struct strbuf *sb, FILE *fp, int term)
 353{
 354        int ch;
 355
 356        strbuf_grow(sb, 0);
 357        if (feof(fp))
 358                return EOF;
 359
 360        strbuf_reset(sb);
 361        while ((ch = fgetc(fp)) != EOF) {
 362                strbuf_grow(sb, 1);
 363                sb->buf[sb->len++] = ch;
 364                if (ch == term)
 365                        break;
 366        }
 367        if (ch == EOF && sb->len == 0)
 368                return EOF;
 369
 370        sb->buf[sb->len] = '\0';
 371        return 0;
 372}
 373
 374int strbuf_getline(struct strbuf *sb, FILE *fp, int term)
 375{
 376        if (strbuf_getwholeline(sb, fp, term))
 377                return EOF;
 378        if (sb->buf[sb->len-1] == term)
 379                strbuf_setlen(sb, sb->len-1);
 380        return 0;
 381}
 382
 383int strbuf_read_file(struct strbuf *sb, const char *path, size_t hint)
 384{
 385        int fd, len;
 386
 387        fd = open(path, O_RDONLY);
 388        if (fd < 0)
 389                return -1;
 390        len = strbuf_read(sb, fd, hint);
 391        close(fd);
 392        if (len < 0)
 393                return -1;
 394
 395        return len;
 396}