4 * Used as the default ->buf value, so that people can always assume
5 * buf is non NULL and ->buf is NUL terminated even for a freshly
8 char strbuf_slopbuf
[1];
10 void strbuf_init(struct strbuf
*sb
, size_t hint
)
12 sb
->alloc
= sb
->len
= 0;
13 sb
->buf
= strbuf_slopbuf
;
15 strbuf_grow(sb
, hint
);
18 void strbuf_release(struct strbuf
*sb
)
26 char *strbuf_detach(struct strbuf
*sb
, size_t *sz
)
28 char *res
= sb
->alloc
? sb
->buf
: NULL
;
35 void strbuf_attach(struct strbuf
*sb
, void *buf
, size_t len
, size_t alloc
)
42 sb
->buf
[sb
->len
] = '\0';
45 void strbuf_grow(struct strbuf
*sb
, size_t extra
)
47 if (sb
->len
+ extra
+ 1 <= sb
->len
)
48 die("you want to use way too much memory");
51 ALLOC_GROW(sb
->buf
, sb
->len
+ extra
+ 1, sb
->alloc
);
54 void strbuf_rtrim(struct strbuf
*sb
)
56 while (sb
->len
> 0 && isspace((unsigned char)sb
->buf
[sb
->len
- 1]))
58 sb
->buf
[sb
->len
] = '\0';
61 int strbuf_cmp(struct strbuf
*a
, struct strbuf
*b
)
64 if (a
->len
< b
->len
) {
65 cmp
= memcmp(a
->buf
, b
->buf
, a
->len
);
66 return cmp
? cmp
: -1;
68 cmp
= memcmp(a
->buf
, b
->buf
, b
->len
);
69 return cmp
? cmp
: a
->len
!= b
->len
;
73 void strbuf_splice(struct strbuf
*sb
, size_t pos
, size_t len
,
74 const void *data
, size_t dlen
)
77 die("you want to use way too much memory");
79 die("`pos' is too far after the end of the buffer");
80 if (pos
+ len
> sb
->len
)
81 die("`pos + len' is too far after the end of the buffer");
84 strbuf_grow(sb
, dlen
- len
);
85 memmove(sb
->buf
+ pos
+ dlen
,
88 memcpy(sb
->buf
+ pos
, data
, dlen
);
89 strbuf_setlen(sb
, sb
->len
+ dlen
- len
);
92 void strbuf_insert(struct strbuf
*sb
, size_t pos
, const void *data
, size_t len
)
94 strbuf_splice(sb
, pos
, 0, data
, len
);
97 void strbuf_remove(struct strbuf
*sb
, size_t pos
, size_t len
)
99 strbuf_splice(sb
, pos
, len
, NULL
, 0);
102 void strbuf_add(struct strbuf
*sb
, const void *data
, size_t len
)
104 strbuf_grow(sb
, len
);
105 memcpy(sb
->buf
+ sb
->len
, data
, len
);
106 strbuf_setlen(sb
, sb
->len
+ len
);
109 void strbuf_adddup(struct strbuf
*sb
, size_t pos
, size_t len
)
111 strbuf_grow(sb
, len
);
112 memcpy(sb
->buf
+ sb
->len
, sb
->buf
+ pos
, len
);
113 strbuf_setlen(sb
, sb
->len
+ len
);
116 void strbuf_addf(struct strbuf
*sb
, const char *fmt
, ...)
121 if (!strbuf_avail(sb
))
124 len
= vsnprintf(sb
->buf
+ sb
->len
, sb
->alloc
- sb
->len
, fmt
, ap
);
127 die("your vsnprintf is broken");
128 if (len
> strbuf_avail(sb
)) {
129 strbuf_grow(sb
, len
);
131 len
= vsnprintf(sb
->buf
+ sb
->len
, sb
->alloc
- sb
->len
, fmt
, ap
);
133 if (len
> strbuf_avail(sb
)) {
134 die("this should not happen, your snprintf is broken");
137 strbuf_setlen(sb
, sb
->len
+ len
);
140 void strbuf_expand(struct strbuf
*sb
, const char *format
,
141 const char **placeholders
, expand_fn_t fn
, void *context
)
144 const char *percent
, **p
;
146 percent
= strchrnul(format
, '%');
147 strbuf_add(sb
, format
, percent
- format
);
150 format
= percent
+ 1;
152 for (p
= placeholders
; *p
; p
++) {
153 if (!prefixcmp(format
, *p
))
158 format
+= strlen(*p
);
160 strbuf_addch(sb
, '%');
164 size_t strbuf_fread(struct strbuf
*sb
, size_t size
, FILE *f
)
168 strbuf_grow(sb
, size
);
169 res
= fread(sb
->buf
+ sb
->len
, 1, size
, f
);
171 strbuf_setlen(sb
, sb
->len
+ res
);
176 ssize_t
strbuf_read(struct strbuf
*sb
, int fd
, size_t hint
)
178 size_t oldlen
= sb
->len
;
180 strbuf_grow(sb
, hint
? hint
: 8192);
184 cnt
= xread(fd
, sb
->buf
+ sb
->len
, sb
->alloc
- sb
->len
- 1);
186 strbuf_setlen(sb
, oldlen
);
192 strbuf_grow(sb
, 8192);
195 sb
->buf
[sb
->len
] = '\0';
196 return sb
->len
- oldlen
;
199 int strbuf_getline(struct strbuf
*sb
, FILE *fp
, int term
)
208 while ((ch
= fgetc(fp
)) != EOF
) {
212 sb
->buf
[sb
->len
++] = ch
;
214 if (ch
== EOF
&& sb
->len
== 0)
217 sb
->buf
[sb
->len
] = '\0';
221 int strbuf_read_file(struct strbuf
*sb
, const char *path
, size_t hint
)
225 fd
= open(path
, O_RDONLY
);
228 len
= strbuf_read(sb
, fd
, hint
);