Lines Matching refs:sb
20 void strbuf_init(struct strbuf *sb, ssize_t hint) in strbuf_init() argument
22 sb->alloc = sb->len = 0; in strbuf_init()
23 sb->buf = strbuf_slopbuf; in strbuf_init()
25 strbuf_grow(sb, hint); in strbuf_init()
28 void strbuf_release(struct strbuf *sb) in strbuf_release() argument
30 if (sb->alloc) { in strbuf_release()
31 zfree(&sb->buf); in strbuf_release()
32 strbuf_init(sb, 0); in strbuf_release()
36 char *strbuf_detach(struct strbuf *sb, size_t *sz) in strbuf_detach() argument
38 char *res = sb->alloc ? sb->buf : NULL; in strbuf_detach()
40 *sz = sb->len; in strbuf_detach()
41 strbuf_init(sb, 0); in strbuf_detach()
45 void strbuf_grow(struct strbuf *sb, size_t extra) in strbuf_grow() argument
47 if (sb->len + extra + 1 <= sb->len) in strbuf_grow()
49 if (!sb->alloc) in strbuf_grow()
50 sb->buf = NULL; in strbuf_grow()
51 ALLOC_GROW(sb->buf, sb->len + extra + 1, sb->alloc); in strbuf_grow()
54 static void strbuf_splice(struct strbuf *sb, size_t pos, size_t len, in strbuf_splice() argument
59 if (pos > sb->len) in strbuf_splice()
61 if (pos + len > sb->len) in strbuf_splice()
65 strbuf_grow(sb, dlen - len); in strbuf_splice()
66 memmove(sb->buf + pos + dlen, in strbuf_splice()
67 sb->buf + pos + len, in strbuf_splice()
68 sb->len - pos - len); in strbuf_splice()
69 memcpy(sb->buf + pos, data, dlen); in strbuf_splice()
70 strbuf_setlen(sb, sb->len + dlen - len); in strbuf_splice()
73 void strbuf_remove(struct strbuf *sb, size_t pos, size_t len) in strbuf_remove() argument
75 strbuf_splice(sb, pos, len, NULL, 0); in strbuf_remove()
78 void strbuf_add(struct strbuf *sb, const void *data, size_t len) in strbuf_add() argument
80 strbuf_grow(sb, len); in strbuf_add()
81 memcpy(sb->buf + sb->len, data, len); in strbuf_add()
82 strbuf_setlen(sb, sb->len + len); in strbuf_add()
85 void strbuf_addf(struct strbuf *sb, const char *fmt, ...) in strbuf_addf() argument
90 if (!strbuf_avail(sb)) in strbuf_addf()
91 strbuf_grow(sb, 64); in strbuf_addf()
93 len = vsnprintf(sb->buf + sb->len, sb->alloc - sb->len, fmt, ap); in strbuf_addf()
97 if (len > strbuf_avail(sb)) { in strbuf_addf()
98 strbuf_grow(sb, len); in strbuf_addf()
100 len = vsnprintf(sb->buf + sb->len, sb->alloc - sb->len, fmt, ap); in strbuf_addf()
102 if (len > strbuf_avail(sb)) { in strbuf_addf()
106 strbuf_setlen(sb, sb->len + len); in strbuf_addf()
109 ssize_t strbuf_read(struct strbuf *sb, int fd, ssize_t hint) in strbuf_read() argument
111 size_t oldlen = sb->len; in strbuf_read()
112 size_t oldalloc = sb->alloc; in strbuf_read()
114 strbuf_grow(sb, hint ? hint : 8192); in strbuf_read()
118 cnt = read(fd, sb->buf + sb->len, sb->alloc - sb->len - 1); in strbuf_read()
121 strbuf_release(sb); in strbuf_read()
123 strbuf_setlen(sb, oldlen); in strbuf_read()
128 sb->len += cnt; in strbuf_read()
129 strbuf_grow(sb, 8192); in strbuf_read()
132 sb->buf[sb->len] = '\0'; in strbuf_read()
133 return sb->len - oldlen; in strbuf_read()