blob: 5b1113abf8fccd49c936ed3fb5fe5d9e5dc774aa [file] [log] [blame]
Junio C Hamanod1df5742005-04-25 18:26:45 -07001#ifndef STRBUF_H
2#define STRBUF_H
Pierre Habouzitb449f4c2007-09-06 13:20:05 +02003
Elijah Newrenf6f77552018-04-19 10:58:08 -07004struct string_list;
5
Jeff Kingbdfdaa42015-01-16 04:04:04 -05006/**
7 * strbuf's are meant to be used with all the usual C string and memory
8 * APIs. Given that the length of the buffer is known, it's often better to
9 * use the mem* functions than a str* one (memchr vs. strchr e.g.).
10 * Though, one has to be careful about the fact that str* functions often
11 * stop on NULs and that strbufs may have embedded NULs.
12 *
13 * A strbuf is NUL terminated for convenience, but no function in the
14 * strbuf API actually relies on the string being free of NULs.
15 *
16 * strbufs have some invariants that are very important to keep in mind:
17 *
Jeff Kingaa07cac2015-01-16 04:05:10 -050018 * - The `buf` member is never NULL, so it can be used in any usual C
19 * string operations safely. strbuf's _have_ to be initialized either by
20 * `strbuf_init()` or by `= STRBUF_INIT` before the invariants, though.
Jeff Kingbdfdaa42015-01-16 04:04:04 -050021 *
Jeff Kingaa07cac2015-01-16 04:05:10 -050022 * Do *not* assume anything on what `buf` really is (e.g. if it is
23 * allocated memory or not), use `strbuf_detach()` to unwrap a memory
24 * buffer from its strbuf shell in a safe way. That is the sole supported
25 * way. This will give you a malloced buffer that you can later `free()`.
26 *
27 * However, it is totally safe to modify anything in the string pointed by
28 * the `buf` member, between the indices `0` and `len-1` (inclusive).
29 *
30 * - The `buf` member is a byte array that has at least `len + 1` bytes
31 * allocated. The extra byte is used to store a `'\0'`, allowing the
32 * `buf` member to be a valid C-string. Every strbuf function ensure this
33 * invariant is preserved.
34 *
35 * NOTE: It is OK to "play" with the buffer directly if you work it this
36 * way:
37 *
Jeff King088c9a82015-01-16 04:05:16 -050038 * strbuf_grow(sb, SOME_SIZE); <1>
39 * strbuf_setlen(sb, sb->len + SOME_OTHER_SIZE);
40 *
Jeff Kingaa07cac2015-01-16 04:05:10 -050041 * <1> Here, the memory array starting at `sb->buf`, and of length
42 * `strbuf_avail(sb)` is all yours, and you can be sure that
43 * `strbuf_avail(sb)` is at least `SOME_SIZE`.
44 *
45 * NOTE: `SOME_OTHER_SIZE` must be smaller or equal to `strbuf_avail(sb)`.
46 *
47 * Doing so is safe, though if it has to be done in many places, adding the
48 * missing API to the strbuf module is the way to go.
49 *
50 * WARNING: Do _not_ assume that the area that is yours is of size `alloc
51 * - 1` even if it's true in the current implementation. Alloc is somehow a
52 * "private" member that should not be messed with. Use `strbuf_avail()`
53 * instead.
54*/
Pierre Habouzitb449f4c2007-09-06 13:20:05 +020055
Jeff Kingbdfdaa42015-01-16 04:04:04 -050056/**
57 * Data Structures
58 * ---------------
59 */
60
61/**
62 * This is the string buffer structure. The `len` member can be used to
63 * determine the current length of the string, and `buf` member provides
64 * access to the string itself.
65 */
Junio C Hamanod1df5742005-04-25 18:26:45 -070066struct strbuf {
Pierre Habouzitb449f4c2007-09-06 13:20:05 +020067 size_t alloc;
68 size_t len;
Brian Gerstbf0f9102005-05-18 08:14:09 -040069 char *buf;
Junio C Hamanod1df5742005-04-25 18:26:45 -070070};
71
Jeff Kingbdfdaa42015-01-16 04:04:04 -050072extern char strbuf_slopbuf[];
Jeff Kingcbc0f812017-07-10 03:03:42 -040073#define STRBUF_INIT { .alloc = 0, .len = 0, .buf = strbuf_slopbuf }
Pierre Habouzitb449f4c2007-09-06 13:20:05 +020074
brian m. carlson30e677e2018-03-12 02:27:28 +000075/*
76 * Predeclare this here, since cache.h includes this file before it defines the
77 * struct.
78 */
79struct object_id;
80
Jeff Kingbdfdaa42015-01-16 04:04:04 -050081/**
Jeff King14e21772015-01-16 04:05:28 -050082 * Life Cycle Functions
83 * --------------------
Jeff Kingbdfdaa42015-01-16 04:04:04 -050084 */
85
86/**
87 * Initialize the structure. The second parameter can be zero or a bigger
88 * number to allocate memory, in case you want to prevent further reallocs.
89 */
Stefan Bellerc7e5fe72018-09-28 10:30:33 -070090void strbuf_init(struct strbuf *sb, size_t alloc);
Jeff Kingbdfdaa42015-01-16 04:04:04 -050091
92/**
Jonathan Niedere0222152017-10-03 19:39:54 -070093 * Release a string buffer and the memory it used. After this call, the
94 * strbuf points to an empty string that does not need to be free()ed, as
95 * if it had been set to `STRBUF_INIT` and never modified.
96 *
97 * To clear a strbuf in preparation for further use without the overhead
98 * of free()ing and malloc()ing again, use strbuf_reset() instead.
Jeff Kingbdfdaa42015-01-16 04:04:04 -050099 */
Stefan Bellerc7e5fe72018-09-28 10:30:33 -0700100void strbuf_release(struct strbuf *sb);
Jeff Kingbdfdaa42015-01-16 04:04:04 -0500101
102/**
103 * Detach the string from the strbuf and returns it; you now own the
104 * storage the string occupies and it is your responsibility from then on
105 * to release it with `free(3)` when you are done with it.
Jonathan Niedere0222152017-10-03 19:39:54 -0700106 *
107 * The strbuf that previously held the string is reset to `STRBUF_INIT` so
108 * it can be reused after calling this function.
Jeff Kingbdfdaa42015-01-16 04:04:04 -0500109 */
Stefan Bellerc7e5fe72018-09-28 10:30:33 -0700110char *strbuf_detach(struct strbuf *sb, size_t *sz);
Jeff Kingbdfdaa42015-01-16 04:04:04 -0500111
112/**
113 * Attach a string to a buffer. You should specify the string to attach,
114 * the current length of the string and the amount of allocated memory.
115 * The amount must be larger than the string length, because the string you
116 * pass is supposed to be a NUL-terminated string. This string _must_ be
117 * malloc()ed, and after attaching, the pointer cannot be relied upon
118 * anymore, and neither be free()d directly.
119 */
Stefan Bellerc7e5fe72018-09-28 10:30:33 -0700120void strbuf_attach(struct strbuf *sb, void *str, size_t len, size_t mem);
Jeff Kingbdfdaa42015-01-16 04:04:04 -0500121
122/**
123 * Swap the contents of two string buffers.
124 */
Nguyễn Thái Ngọc Duy187e2902014-03-01 09:50:55 +0700125static inline void strbuf_swap(struct strbuf *a, struct strbuf *b)
126{
René Scharfe35d803b2017-01-28 22:40:58 +0100127 SWAP(*a, *b);
Pierre Habouzitc76689d2007-09-20 00:42:12 +0200128}
Pierre Habouzitb449f4c2007-09-06 13:20:05 +0200129
Jeff Kingbdfdaa42015-01-16 04:04:04 -0500130
131/**
Jeff King14e21772015-01-16 04:05:28 -0500132 * Functions related to the size of the buffer
133 * -------------------------------------------
Jeff Kingbdfdaa42015-01-16 04:04:04 -0500134 */
135
136/**
137 * Determine the amount of allocated but unused memory.
138 */
Nguyễn Thái Ngọc Duy187e2902014-03-01 09:50:55 +0700139static inline size_t strbuf_avail(const struct strbuf *sb)
140{
Pierre Habouzitc76689d2007-09-20 00:42:12 +0200141 return sb->alloc ? sb->alloc - sb->len - 1 : 0;
Pierre Habouzitb449f4c2007-09-06 13:20:05 +0200142}
Junio C Hamanoa8f3e222007-09-26 02:26:06 -0700143
Jeff Kingbdfdaa42015-01-16 04:04:04 -0500144/**
145 * Ensure that at least this amount of unused memory is available after
146 * `len`. This is used when you know a typical size for what you will add
147 * and want to avoid repetitive automatic resizing of the underlying buffer.
148 * This is never a needed operation, but can be critical for performance in
149 * some cases.
150 */
Stefan Bellerc7e5fe72018-09-28 10:30:33 -0700151void strbuf_grow(struct strbuf *sb, size_t amount);
Junio C Hamanoa8f3e222007-09-26 02:26:06 -0700152
Jeff Kingbdfdaa42015-01-16 04:04:04 -0500153/**
154 * Set the length of the buffer to a given value. This function does *not*
155 * allocate new memory, so you should not perform a `strbuf_setlen()` to a
156 * length that is larger than `len + strbuf_avail()`. `strbuf_setlen()` is
157 * just meant as a 'please fix invariants from this strbuf I just messed
158 * with'.
159 */
Nguyễn Thái Ngọc Duy187e2902014-03-01 09:50:55 +0700160static inline void strbuf_setlen(struct strbuf *sb, size_t len)
161{
René Scharfe7141efa2011-04-27 19:24:50 +0200162 if (len > (sb->alloc ? sb->alloc - 1 : 0))
163 die("BUG: strbuf_setlen() beyond buffer");
Pierre Habouzitc76689d2007-09-20 00:42:12 +0200164 sb->len = len;
Martin Ågren65961d52017-08-21 19:43:47 +0200165 if (sb->buf != strbuf_slopbuf)
166 sb->buf[len] = '\0';
167 else
168 assert(!strbuf_slopbuf[0]);
Pierre Habouzitb449f4c2007-09-06 13:20:05 +0200169}
Jeff Kingbdfdaa42015-01-16 04:04:04 -0500170
171/**
172 * Empty the buffer by setting the size of it to zero.
173 */
Pierre Habouzitb315c5c2007-09-27 12:58:23 +0200174#define strbuf_reset(sb) strbuf_setlen(sb, 0)
Pierre Habouzitb449f4c2007-09-06 13:20:05 +0200175
Jeff Kingbdfdaa42015-01-16 04:04:04 -0500176
177/**
Jeff King14e21772015-01-16 04:05:28 -0500178 * Functions related to the contents of the buffer
179 * -----------------------------------------------
Jeff Kingbdfdaa42015-01-16 04:04:04 -0500180 */
181
182/**
Jeff Kingd468fa22015-01-16 04:06:04 -0500183 * Strip whitespace from the beginning (`ltrim`), end (`rtrim`), or both side
184 * (`trim`) of a string.
Jeff Kingbdfdaa42015-01-16 04:04:04 -0500185 */
Stefan Bellerc7e5fe72018-09-28 10:30:33 -0700186void strbuf_trim(struct strbuf *sb);
187void strbuf_rtrim(struct strbuf *sb);
188void strbuf_ltrim(struct strbuf *sb);
Jeff Kingbdfdaa42015-01-16 04:04:04 -0500189
Nguyễn Thái Ngọc Duyc64a8d22018-02-12 16:49:37 +0700190/* Strip trailing directory separators */
Stefan Bellerc7e5fe72018-09-28 10:30:33 -0700191void strbuf_trim_trailing_dir_sep(struct strbuf *sb);
Nguyễn Thái Ngọc Duyc64a8d22018-02-12 16:49:37 +0700192
Pratik Karkif9573622018-08-08 20:51:16 +0545193/* Strip trailing LF or CR/LF */
Junio C Hamano39f73312018-11-02 11:04:54 +0900194void strbuf_trim_trailing_newline(struct strbuf *sb);
Pratik Karkif9573622018-08-08 20:51:16 +0545195
Jeff Kingbdfdaa42015-01-16 04:04:04 -0500196/**
197 * Replace the contents of the strbuf with a reencoded form. Returns -1
198 * on error, 0 on success.
199 */
Stefan Bellerc7e5fe72018-09-28 10:30:33 -0700200int strbuf_reencode(struct strbuf *sb, const char *from, const char *to);
Jeff Kingbdfdaa42015-01-16 04:04:04 -0500201
202/**
203 * Lowercase each character in the buffer using `tolower`.
204 */
Stefan Bellerc7e5fe72018-09-28 10:30:33 -0700205void strbuf_tolower(struct strbuf *sb);
Jeff Kingbdfdaa42015-01-16 04:04:04 -0500206
207/**
208 * Compare two buffers. Returns an integer less than, equal to, or greater
209 * than zero if the first buffer is found, respectively, to be less than,
210 * to match, or be greater than the second buffer.
211 */
Stefan Bellerc7e5fe72018-09-28 10:30:33 -0700212int strbuf_cmp(const struct strbuf *first, const struct strbuf *second);
Lukas Sandströmeacd6dc2008-07-13 20:29:18 +0200213
Jeff Kingbdfdaa42015-01-16 04:04:04 -0500214
215/**
Jeff King14e21772015-01-16 04:05:28 -0500216 * Adding data to the buffer
217 * -------------------------
Jeff Kingbdfdaa42015-01-16 04:04:04 -0500218 *
219 * NOTE: All of the functions in this section will grow the buffer as
220 * necessary. If they fail for some reason other than memory shortage and the
221 * buffer hadn't been allocated before (i.e. the `struct strbuf` was set to
222 * `STRBUF_INIT`), then they will free() it.
223 */
224
225/**
226 * Add a single character to the buffer.
227 */
228static inline void strbuf_addch(struct strbuf *sb, int c)
229{
Jeff Kingfec501d2015-04-16 04:53:56 -0400230 if (!strbuf_avail(sb))
231 strbuf_grow(sb, 1);
Jeff Kingbdfdaa42015-01-16 04:04:04 -0500232 sb->buf[sb->len++] = c;
233 sb->buf[sb->len] = '\0';
234}
235
236/**
237 * Add a character the specified number of times to the buffer.
238 */
Stefan Bellerc7e5fe72018-09-28 10:30:33 -0700239void strbuf_addchars(struct strbuf *sb, int c, size_t n);
Jeff Kingbdfdaa42015-01-16 04:04:04 -0500240
241/**
242 * Insert data to the given position of the buffer. The remaining contents
243 * will be shifted, not overwritten.
244 */
Stefan Bellerc7e5fe72018-09-28 10:30:33 -0700245void strbuf_insert(struct strbuf *sb, size_t pos, const void *, size_t);
Jeff Kingbdfdaa42015-01-16 04:04:04 -0500246
247/**
René Scharfea91cc7f2020-02-09 14:44:23 +0100248 * Insert a NUL-terminated string to the given position of the buffer.
249 * The remaining contents will be shifted, not overwritten. It's an
250 * inline function to allow the compiler to resolve strlen() calls on
251 * constants at compile time.
252 */
253static inline void strbuf_insertstr(struct strbuf *sb, size_t pos,
254 const char *s)
255{
256 strbuf_insert(sb, pos, s, strlen(s));
257}
258
259/**
Paul-Sebastian Ungureanu5ef264d2019-02-25 23:16:07 +0000260 * Insert data to the given position of the buffer giving a printf format
261 * string. The contents will be shifted, not overwritten.
262 */
263void strbuf_vinsertf(struct strbuf *sb, size_t pos, const char *fmt,
264 va_list ap);
265
Ævar Arnfjörð Bjarmason75d31ce2021-07-13 10:05:19 +0200266__attribute__((format (printf, 3, 4)))
Paul-Sebastian Ungureanu5ef264d2019-02-25 23:16:07 +0000267void strbuf_insertf(struct strbuf *sb, size_t pos, const char *fmt, ...);
268
269/**
Jeff Kingbdfdaa42015-01-16 04:04:04 -0500270 * Remove given amount of data from a given position of the buffer.
271 */
Stefan Bellerc7e5fe72018-09-28 10:30:33 -0700272void strbuf_remove(struct strbuf *sb, size_t pos, size_t len);
Jeff Kingbdfdaa42015-01-16 04:04:04 -0500273
274/**
275 * Remove the bytes between `pos..pos+len` and replace it with the given
276 * data.
277 */
Stefan Bellerc7e5fe72018-09-28 10:30:33 -0700278void strbuf_splice(struct strbuf *sb, size_t pos, size_t len,
279 const void *data, size_t data_len);
Jeff Kingbdfdaa42015-01-16 04:04:04 -0500280
281/**
282 * Add a NUL-terminated string to the buffer. Each line will be prepended
283 * by a comment character and a blank.
284 */
Stefan Bellerc7e5fe72018-09-28 10:30:33 -0700285void strbuf_add_commented_lines(struct strbuf *out,
286 const char *buf, size_t size);
Jeff Kingbdfdaa42015-01-16 04:04:04 -0500287
288
289/**
290 * Add data of given length to the buffer.
291 */
Stefan Bellerc7e5fe72018-09-28 10:30:33 -0700292void strbuf_add(struct strbuf *sb, const void *data, size_t len);
Jeff Kingbdfdaa42015-01-16 04:04:04 -0500293
294/**
295 * Add a NUL-terminated string to the buffer.
296 *
297 * NOTE: This function will *always* be implemented as an inline or a macro
298 * using strlen, meaning that this is efficient to write things like:
299 *
Jeff King088c9a82015-01-16 04:05:16 -0500300 * strbuf_addstr(sb, "immediate string");
Jeff Kingbdfdaa42015-01-16 04:04:04 -0500301 *
302 */
303static inline void strbuf_addstr(struct strbuf *sb, const char *s)
304{
305 strbuf_add(sb, s, strlen(s));
306}
307
308/**
309 * Copy the contents of another buffer at the end of the current one.
310 */
Stefan Bellerc7e5fe72018-09-28 10:30:33 -0700311void strbuf_addbuf(struct strbuf *sb, const struct strbuf *sb2);
Jeff Kingbdfdaa42015-01-16 04:04:04 -0500312
313/**
Paul-Sebastian Ungureanue71c4a82019-02-25 23:16:06 +0000314 * Join the arguments into a buffer. `delim` is put between every
315 * two arguments.
316 */
317const char *strbuf_join_argv(struct strbuf *buf, int argc,
318 const char **argv, char delim);
319
320/**
Jeff Kingbdfdaa42015-01-16 04:04:04 -0500321 * This function can be used to expand a format string containing
322 * placeholders. To that end, it parses the string and calls the specified
323 * function for every percent sign found.
324 *
325 * The callback function is given a pointer to the character after the `%`
326 * and a pointer to the struct strbuf. It is expected to add the expanded
327 * version of the placeholder to the strbuf, e.g. to add a newline
328 * character if the letter `n` appears after a `%`. The function returns
329 * the length of the placeholder recognized and `strbuf_expand()` skips
330 * over it.
331 *
332 * The format `%%` is automatically expanded to a single `%` as a quoting
333 * mechanism; callers do not need to handle the `%` placeholder themselves,
334 * and the callback function will not be invoked for this placeholder.
335 *
336 * All other characters (non-percent and not skipped ones) are copied
337 * verbatim to the strbuf. If the callback returned zero, meaning that the
338 * placeholder is unknown, then the percent sign is copied, too.
339 *
340 * In order to facilitate caching and to make it possible to give
Felipe Contreras0e20b222021-06-15 14:11:10 +0000341 * parameters to the callback, `strbuf_expand()` passes a context
342 * pointer with any kind of data.
Jeff Kingbdfdaa42015-01-16 04:04:04 -0500343 */
Stefan Bellerc7e5fe72018-09-28 10:30:33 -0700344typedef size_t (*expand_fn_t) (struct strbuf *sb,
345 const char *placeholder,
346 void *context);
347void strbuf_expand(struct strbuf *sb,
348 const char *format,
349 expand_fn_t fn,
350 void *context);
Jeff Kingbdfdaa42015-01-16 04:04:04 -0500351
352/**
Anders Waldenborgfd2015b2019-01-28 22:33:36 +0100353 * Used as callback for `strbuf_expand` to only expand literals
354 * (i.e. %n and %xNN). The context argument is ignored.
355 */
356size_t strbuf_expand_literal_cb(struct strbuf *sb,
357 const char *placeholder,
358 void *context);
359
360/**
Jeff Kingbdfdaa42015-01-16 04:04:04 -0500361 * Used as callback for `strbuf_expand()`, expects an array of
362 * struct strbuf_expand_dict_entry as context, i.e. pairs of
363 * placeholder and replacement string. The array needs to be
364 * terminated by an entry with placeholder set to NULL.
365 */
366struct strbuf_expand_dict_entry {
367 const char *placeholder;
368 const char *value;
369};
Stefan Bellerc7e5fe72018-09-28 10:30:33 -0700370size_t strbuf_expand_dict_cb(struct strbuf *sb,
371 const char *placeholder,
372 void *context);
Jeff Kingbdfdaa42015-01-16 04:04:04 -0500373
374/**
375 * Append the contents of one strbuf to another, quoting any
376 * percent signs ("%") into double-percents ("%%") in the
377 * destination. This is useful for literal data to be fed to either
378 * strbuf_expand or to the *printf family of functions.
379 */
Stefan Bellerc7e5fe72018-09-28 10:30:33 -0700380void strbuf_addbuf_percentquote(struct strbuf *dst, const struct strbuf *src);
Jeff Kingbdfdaa42015-01-16 04:04:04 -0500381
brian m. carlsonb44d0112020-04-27 01:18:08 +0000382#define STRBUF_ENCODE_SLASH 1
383
Jeff Kingbdfdaa42015-01-16 04:04:04 -0500384/**
brian m. carlson46fd7b32020-02-20 02:24:13 +0000385 * Append the contents of a string to a strbuf, percent-encoding any characters
386 * that are needed to be encoded for a URL.
brian m. carlsonb44d0112020-04-27 01:18:08 +0000387 *
388 * If STRBUF_ENCODE_SLASH is set in flags, percent-encode slashes. Otherwise,
389 * slashes are not percent-encoded.
brian m. carlson46fd7b32020-02-20 02:24:13 +0000390 */
brian m. carlsonb44d0112020-04-27 01:18:08 +0000391void strbuf_add_percentencode(struct strbuf *dst, const char *src, int flags);
brian m. carlson46fd7b32020-02-20 02:24:13 +0000392
393/**
Jeff Kingbdfdaa42015-01-16 04:04:04 -0500394 * Append the given byte size as a human-readable string (i.e. 12.23 KiB,
395 * 3.50 MiB).
396 */
Stefan Bellerc7e5fe72018-09-28 10:30:33 -0700397void strbuf_humanise_bytes(struct strbuf *buf, off_t bytes);
Jeff Kingbdfdaa42015-01-16 04:04:04 -0500398
399/**
Dimitriy Ryazantcev8f354a12019-07-02 21:22:48 +0300400 * Append the given byte rate as a human-readable string (i.e. 12.23 KiB/s,
401 * 3.50 MiB/s).
402 */
403void strbuf_humanise_rate(struct strbuf *buf, off_t bytes);
404
405/**
Jeff Kingbdfdaa42015-01-16 04:04:04 -0500406 * Add a formatted string to the buffer.
407 */
408__attribute__((format (printf,2,3)))
Stefan Bellerc7e5fe72018-09-28 10:30:33 -0700409void strbuf_addf(struct strbuf *sb, const char *fmt, ...);
Jeff Kingbdfdaa42015-01-16 04:04:04 -0500410
411/**
412 * Add a formatted string prepended by a comment character and a
413 * blank to the buffer.
414 */
415__attribute__((format (printf, 2, 3)))
Stefan Bellerc7e5fe72018-09-28 10:30:33 -0700416void strbuf_commented_addf(struct strbuf *sb, const char *fmt, ...);
Jeff Kingbdfdaa42015-01-16 04:04:04 -0500417
418__attribute__((format (printf,2,0)))
Stefan Bellerc7e5fe72018-09-28 10:30:33 -0700419void strbuf_vaddf(struct strbuf *sb, const char *fmt, va_list ap);
Jeff Kingbdfdaa42015-01-16 04:04:04 -0500420
421/**
Jeff Kingaa1462c2015-06-25 12:55:45 -0400422 * Add the time specified by `tm`, as formatted by `strftime`.
René Scharfec3fbf812017-06-15 14:29:53 +0200423 * `tz_offset` is in decimal hhmm format, e.g. -600 means six hours west
424 * of Greenwich, and it's used to expand %z internally. However, tokens
425 * with modifiers (e.g. %Ez) are passed to `strftime`.
Ævar Arnfjörð Bjarmason3b702232017-07-01 13:15:47 +0000426 * `suppress_tz_name`, when set, expands %Z internally to the empty
427 * string rather than passing it to `strftime`.
Jeff Kingaa1462c2015-06-25 12:55:45 -0400428 */
Stefan Bellerc7e5fe72018-09-28 10:30:33 -0700429void strbuf_addftime(struct strbuf *sb, const char *fmt,
430 const struct tm *tm, int tz_offset,
431 int suppress_tz_name);
Jeff Kingaa1462c2015-06-25 12:55:45 -0400432
433/**
Jeff Kingbdfdaa42015-01-16 04:04:04 -0500434 * Read a given size of data from a FILE* pointer to the buffer.
435 *
436 * NOTE: The buffer is rewound if the read fails. If -1 is returned,
437 * `errno` must be consulted, like you would do for `read(3)`.
Junio C Hamano1a0c8df2016-01-13 18:32:23 -0800438 * `strbuf_read()`, `strbuf_read_file()` and `strbuf_getline_*()`
439 * family of functions have the same behaviour as well.
Jeff Kingbdfdaa42015-01-16 04:04:04 -0500440 */
Stefan Bellerc7e5fe72018-09-28 10:30:33 -0700441size_t strbuf_fread(struct strbuf *sb, size_t size, FILE *file);
Jeff Kingbdfdaa42015-01-16 04:04:04 -0500442
443/**
444 * Read the contents of a given file descriptor. The third argument can be
445 * used to give a hint about the file size, to avoid reallocs. If read fails,
446 * any partial read is undone.
447 */
Stefan Bellerc7e5fe72018-09-28 10:30:33 -0700448ssize_t strbuf_read(struct strbuf *sb, int fd, size_t hint);
Jeff Kingbdfdaa42015-01-16 04:04:04 -0500449
450/**
Stefan Bellerb4e04fb2015-12-15 16:04:08 -0800451 * Read the contents of a given file descriptor partially by using only one
452 * attempt of xread. The third argument can be used to give a hint about the
453 * file size, to avoid reallocs. Returns the number of new bytes appended to
454 * the sb.
455 */
Stefan Bellerc7e5fe72018-09-28 10:30:33 -0700456ssize_t strbuf_read_once(struct strbuf *sb, int fd, size_t hint);
Stefan Bellerb4e04fb2015-12-15 16:04:08 -0800457
458/**
Jeff Kingbdfdaa42015-01-16 04:04:04 -0500459 * Read the contents of a file, specified by its path. The third argument
460 * can be used to give a hint about the file size, to avoid reallocs.
Pranit Bauvaed008d72016-06-14 11:44:11 +0530461 * Return the number of bytes read or a negative value if some error
462 * occurred while opening or reading the file.
Jeff Kingbdfdaa42015-01-16 04:04:04 -0500463 */
Stefan Bellerc7e5fe72018-09-28 10:30:33 -0700464ssize_t strbuf_read_file(struct strbuf *sb, const char *path, size_t hint);
Jeff Kingbdfdaa42015-01-16 04:04:04 -0500465
466/**
467 * Read the target of a symbolic link, specified by its path. The third
468 * argument can be used to give a hint about the size, to avoid reallocs.
469 */
Stefan Bellerc7e5fe72018-09-28 10:30:33 -0700470int strbuf_readlink(struct strbuf *sb, const char *path, size_t hint);
Jeff Kingbdfdaa42015-01-16 04:04:04 -0500471
472/**
Stefan Beller2dac9b52016-02-29 18:07:15 -0800473 * Write the whole content of the strbuf to the stream not stopping at
474 * NUL bytes.
475 */
Stefan Bellerc7e5fe72018-09-28 10:30:33 -0700476ssize_t strbuf_write(struct strbuf *sb, FILE *stream);
Stefan Beller2dac9b52016-02-29 18:07:15 -0800477
478/**
Junio C Hamano1a0c8df2016-01-13 18:32:23 -0800479 * Read a line from a FILE *, overwriting the existing contents of
480 * the strbuf. The strbuf_getline*() family of functions share
481 * this signature, but have different line termination conventions.
482 *
Jeff Kingbdfdaa42015-01-16 04:04:04 -0500483 * Reading stops after the terminator or at EOF. The terminator
484 * is removed from the buffer before returning. Returns 0 unless
485 * there was nothing left before EOF, in which case it returns `EOF`.
486 */
Junio C Hamano8f309ae2016-01-13 15:31:17 -0800487typedef int (*strbuf_getline_fn)(struct strbuf *, FILE *);
488
489/* Uses LF as the line terminator */
Stefan Bellerc7e5fe72018-09-28 10:30:33 -0700490int strbuf_getline_lf(struct strbuf *sb, FILE *fp);
Junio C Hamano8f309ae2016-01-13 15:31:17 -0800491
492/* Uses NUL as the line terminator */
Stefan Bellerc7e5fe72018-09-28 10:30:33 -0700493int strbuf_getline_nul(struct strbuf *sb, FILE *fp);
Junio C Hamano8f309ae2016-01-13 15:31:17 -0800494
Junio C Hamanoc8aa9fd2015-10-28 13:17:29 -0700495/*
Junio C Hamano8f309ae2016-01-13 15:31:17 -0800496 * Similar to strbuf_getline_lf(), but additionally treats a CR that
497 * comes immediately before the LF as part of the terminator.
Junio C Hamano1a0c8df2016-01-13 18:32:23 -0800498 * This is the most friendly version to be used to read "text" files
499 * that can come from platforms whose native text format is CRLF
500 * terminated.
Junio C Hamanoc8aa9fd2015-10-28 13:17:29 -0700501 */
Stefan Bellerc7e5fe72018-09-28 10:30:33 -0700502int strbuf_getline(struct strbuf *sb, FILE *file);
Junio C Hamanoc8aa9fd2015-10-28 13:17:29 -0700503
Jeff Kingbdfdaa42015-01-16 04:04:04 -0500504
505/**
506 * Like `strbuf_getline`, but keeps the trailing terminator (if
507 * any) in the buffer.
508 */
Stefan Bellerc7e5fe72018-09-28 10:30:33 -0700509int strbuf_getwholeline(struct strbuf *sb, FILE *file, int term);
Jeff Kingbdfdaa42015-01-16 04:04:04 -0500510
511/**
Patrick Steinhardtbd021f32020-03-30 15:46:27 +0200512 * Like `strbuf_getwholeline`, but appends the line instead of
513 * resetting the buffer first.
514 */
515int strbuf_appendwholeline(struct strbuf *sb, FILE *file, int term);
516
517/**
Jeff Kingbdfdaa42015-01-16 04:04:04 -0500518 * Like `strbuf_getwholeline`, but operates on a file descriptor.
519 * It reads one character at a time, so it is very slow. Do not
520 * use it unless you need the correct position in the file
521 * descriptor.
522 */
Stefan Bellerc7e5fe72018-09-28 10:30:33 -0700523int strbuf_getwholeline_fd(struct strbuf *sb, int fd, int term);
Jeff Kingbdfdaa42015-01-16 04:04:04 -0500524
525/**
526 * Set the buffer to the path of the current working directory.
527 */
Stefan Bellerc7e5fe72018-09-28 10:30:33 -0700528int strbuf_getcwd(struct strbuf *sb);
Jeff Kingbdfdaa42015-01-16 04:04:04 -0500529
530/**
531 * Add a path to a buffer, converting a relative path to an
532 * absolute one in the process. Symbolic links are not
533 * resolved.
534 */
Stefan Bellerc7e5fe72018-09-28 10:30:33 -0700535void strbuf_add_absolute_path(struct strbuf *sb, const char *path);
Jeff Kingbdfdaa42015-01-16 04:04:04 -0500536
René Scharfe33ad9dd2017-02-25 17:00:33 +0100537/**
538 * Canonize `path` (make it absolute, resolve symlinks, remove extra
539 * slashes) and append it to `sb`. Die with an informative error
540 * message if there is a problem.
541 *
542 * The directory part of `path` (i.e., everything up to the last
543 * dir_sep) must denote a valid, existing directory, but the last
544 * component need not exist.
545 *
546 * Callers that don't mind links should use the more lightweight
547 * strbuf_add_absolute_path() instead.
548 */
Stefan Bellerc7e5fe72018-09-28 10:30:33 -0700549void strbuf_add_real_path(struct strbuf *sb, const char *path);
René Scharfe33ad9dd2017-02-25 17:00:33 +0100550
Jeff King670c3592016-10-03 16:34:17 -0400551
552/**
553 * Normalize in-place the path contained in the strbuf. See
554 * normalize_path_copy() for details. If an error occurs, the contents of "sb"
555 * are left untouched, and -1 is returned.
556 */
Stefan Bellerc7e5fe72018-09-28 10:30:33 -0700557int strbuf_normalize_path(struct strbuf *sb);
Jeff King670c3592016-10-03 16:34:17 -0400558
Jeff Kingbdfdaa42015-01-16 04:04:04 -0500559/**
560 * Strip whitespace from a buffer. The second parameter controls if
561 * comments are considered contents to be removed or not.
562 */
Stefan Bellerc7e5fe72018-09-28 10:30:33 -0700563void strbuf_stripspace(struct strbuf *buf, int skip_comments);
Tobias Klauser63af4a82015-10-16 17:16:42 +0200564
Jeff King6dda4e62014-06-30 13:01:51 -0400565static inline int strbuf_strip_suffix(struct strbuf *sb, const char *suffix)
566{
567 if (strip_suffix_mem(sb->buf, &sb->len, suffix)) {
568 strbuf_setlen(sb, sb->len);
569 return 1;
570 } else
571 return 0;
572}
573
Stefan Beller6afbbdd2015-01-16 04:04:51 -0500574/**
Michael Haggerty06379a62012-11-04 07:46:54 +0100575 * Split str (of length slen) at the specified terminator character.
576 * Return a null-terminated array of pointers to strbuf objects
577 * holding the substrings. The substrings include the terminator,
578 * except for the last substring, which might be unterminated if the
579 * original string did not end with a terminator. If max is positive,
580 * then split the string into at most max substrings (with the last
581 * substring containing everything following the (max-1)th terminator
582 * character).
583 *
Jeff Kingf20e56e2015-01-16 04:05:57 -0500584 * The most generic form is `strbuf_split_buf`, which takes an arbitrary
585 * pointer/len buffer. The `_str` variant takes a NUL-terminated string,
586 * the `_max` variant takes a strbuf, and just `strbuf_split` is a convenience
587 * wrapper to drop the `max` parameter.
588 *
Michael Haggerty06379a62012-11-04 07:46:54 +0100589 * For lighter-weight alternatives, see string_list_split() and
590 * string_list_split_in_place().
591 */
Stefan Bellerc7e5fe72018-09-28 10:30:33 -0700592struct strbuf **strbuf_split_buf(const char *str, size_t len,
593 int terminator, int max);
Michael Haggerty06379a62012-11-04 07:46:54 +0100594
Jeff King2f1d9e22011-06-09 11:54:58 -0400595static inline struct strbuf **strbuf_split_str(const char *str,
Michael Haggerty17b73dc2012-11-04 07:46:53 +0100596 int terminator, int max)
Jeff King2f1d9e22011-06-09 11:54:58 -0400597{
Michael Haggerty17b73dc2012-11-04 07:46:53 +0100598 return strbuf_split_buf(str, strlen(str), terminator, max);
Jeff King2f1d9e22011-06-09 11:54:58 -0400599}
Michael Haggerty06379a62012-11-04 07:46:54 +0100600
Jeff King2f1d9e22011-06-09 11:54:58 -0400601static inline struct strbuf **strbuf_split_max(const struct strbuf *sb,
Stefan Bellerc7e5fe72018-09-28 10:30:33 -0700602 int terminator, int max)
Jeff King2f1d9e22011-06-09 11:54:58 -0400603{
Michael Haggerty17b73dc2012-11-04 07:46:53 +0100604 return strbuf_split_buf(sb->buf, sb->len, terminator, max);
Jeff King2f1d9e22011-06-09 11:54:58 -0400605}
Michael Haggerty06379a62012-11-04 07:46:54 +0100606
Michael Haggerty17b73dc2012-11-04 07:46:53 +0100607static inline struct strbuf **strbuf_split(const struct strbuf *sb,
608 int terminator)
Jeff King28fc3a62011-06-09 11:51:22 -0400609{
Michael Haggerty17b73dc2012-11-04 07:46:53 +0100610 return strbuf_split_max(sb, terminator, 0);
Jeff King28fc3a62011-06-09 11:51:22 -0400611}
Michael Haggerty06379a62012-11-04 07:46:54 +0100612
Elijah Newrenf6f77552018-04-19 10:58:08 -0700613/*
614 * Adds all strings of a string list to the strbuf, separated by the given
615 * separator. For example, if sep is
616 * ', '
617 * and slist contains
618 * ['element1', 'element2', ..., 'elementN'],
619 * then write:
620 * 'element1, element2, ..., elementN'
621 * to str. If only one element, just write "element1" to str.
622 */
Stefan Bellerc7e5fe72018-09-28 10:30:33 -0700623void strbuf_add_separated_string_list(struct strbuf *str,
624 const char *sep,
625 struct string_list *slist);
Elijah Newrenf6f77552018-04-19 10:58:08 -0700626
Stefan Beller6afbbdd2015-01-16 04:04:51 -0500627/**
Michael Haggerty06379a62012-11-04 07:46:54 +0100628 * Free a NULL-terminated list of strbufs (for example, the return
629 * values of the strbuf_split*() functions).
630 */
Stefan Bellerc7e5fe72018-09-28 10:30:33 -0700631void strbuf_list_free(struct strbuf **list);
Pierre Habouzitf1696ee2007-09-10 12:35:04 +0200632
Jeff Kingbdfdaa42015-01-16 04:04:04 -0500633/**
Jeff Kingaf49c6d2015-09-24 17:05:45 -0400634 * Add the abbreviation, as generated by find_unique_abbrev, of `sha1` to
635 * the strbuf `sb`.
636 */
Stefan Bellerc7e5fe72018-09-28 10:30:33 -0700637void strbuf_add_unique_abbrev(struct strbuf *sb,
638 const struct object_id *oid,
639 int abbrev_len);
Jeff Kingaf49c6d2015-09-24 17:05:45 -0400640
641/**
Jeff Kingbdfdaa42015-01-16 04:04:04 -0500642 * Launch the user preferred editor to edit a file and fill the buffer
643 * with the file's contents upon the user completing their editing. The
644 * third argument can be used to set the environment which the editor is
645 * run in. If the buffer is NULL the editor is launched as usual but the
646 * file's contents are not read into the buffer upon completion.
647 */
Junio C Hamanob49ef562018-11-02 11:04:53 +0900648int launch_editor(const char *path, struct strbuf *buffer,
Stefan Bellerc7e5fe72018-09-28 10:30:33 -0700649 const char *const *env);
Pierre Habouzitb449f4c2007-09-06 13:20:05 +0200650
Junio C Hamanob49ef562018-11-02 11:04:53 +0900651int launch_sequence_editor(const char *path, struct strbuf *buffer,
652 const char *const *env);
653
Johannes Schindelinb38dd9e2019-12-13 08:08:00 +0000654/*
655 * In contrast to `launch_editor()`, this function writes out the contents
656 * of the specified file first, then clears the `buffer`, then launches
657 * the editor and reads back in the file contents into the `buffer`.
658 * Finally, it deletes the temporary file.
659 *
660 * If `path` is relative, it refers to a file in the `.git` directory.
661 */
662int strbuf_edit_interactively(struct strbuf *buffer, const char *path,
663 const char *const *env);
664
Stefan Bellerc7e5fe72018-09-28 10:30:33 -0700665void strbuf_add_lines(struct strbuf *sb,
666 const char *prefix,
667 const char *buf,
668 size_t size);
Junio C Hamano895680f2011-11-04 21:06:30 -0700669
Stefan Beller6afbbdd2015-01-16 04:04:51 -0500670/**
Michael Haggerty5963c032012-11-25 12:08:34 +0100671 * Append s to sb, with the characters '<', '>', '&' and '"' converted
672 * into XML entities.
673 */
Stefan Bellerc7e5fe72018-09-28 10:30:33 -0700674void strbuf_addstr_xml_quoted(struct strbuf *sb,
675 const char *s);
Michael Haggerty5963c032012-11-25 12:08:34 +0100676
Jeff King399ad552015-09-24 17:05:43 -0400677/**
678 * "Complete" the contents of `sb` by ensuring that either it ends with the
679 * character `term`, or it is empty. This can be used, for example,
680 * to ensure that text ends with a newline, but without creating an empty
681 * blank line if there is no content in the first place.
682 */
683static inline void strbuf_complete(struct strbuf *sb, char term)
684{
685 if (sb->len && sb->buf[sb->len - 1] != term)
686 strbuf_addch(sb, term);
687}
688
Junio C Hamano895680f2011-11-04 21:06:30 -0700689static inline void strbuf_complete_line(struct strbuf *sb)
690{
Jeff King399ad552015-09-24 17:05:43 -0400691 strbuf_complete(sb, '\n');
Junio C Hamano895680f2011-11-04 21:06:30 -0700692}
693
Jeff King0705fe22017-03-02 03:21:30 -0500694/*
695 * Copy "name" to "sb", expanding any special @-marks as handled by
696 * interpret_branch_name(). The result is a non-qualified branch name
697 * (so "foo" or "origin/master" instead of "refs/heads/foo" or
698 * "refs/remotes/origin/master").
699 *
700 * Note that the resulting name may not be a syntactically valid refname.
Jeff King0e9f62d2017-03-02 03:23:01 -0500701 *
702 * If "allowed" is non-zero, restrict the set of allowed expansions. See
703 * interpret_branch_name() for details.
Jeff King0705fe22017-03-02 03:21:30 -0500704 */
Stefan Bellerc7e5fe72018-09-28 10:30:33 -0700705void strbuf_branchname(struct strbuf *sb, const char *name,
706 unsigned allowed);
Jeff King0705fe22017-03-02 03:21:30 -0500707
708/*
709 * Like strbuf_branchname() above, but confirm that the result is
710 * syntactically valid to be used as a local branch name in refs/heads/.
711 *
712 * The return value is "0" if the result is valid, and "-1" otherwise.
713 */
Stefan Bellerc7e5fe72018-09-28 10:30:33 -0700714int strbuf_check_branch_ref(struct strbuf *sb, const char *name);
Junio C Hamanoa552de72009-03-21 13:17:30 -0700715
Matthew DeVorec2694952019-06-27 15:54:11 -0700716typedef int (*char_predicate)(char ch);
717
718int is_rfc3986_unreserved(char ch);
719int is_rfc3986_reserved_or_unreserved(char ch);
720
Stefan Bellerc7e5fe72018-09-28 10:30:33 -0700721void strbuf_addstr_urlencode(struct strbuf *sb, const char *name,
Matthew DeVorec2694952019-06-27 15:54:11 -0700722 char_predicate allow_unencoded_fn);
René Scharfe679eebe2014-07-28 20:33:55 +0200723
Nguyễn Thái Ngọc Duy9a0a30a2012-04-23 19:30:22 +0700724__attribute__((format (printf,1,2)))
Stefan Bellerc7e5fe72018-09-28 10:30:33 -0700725int printf_ln(const char *fmt, ...);
Nguyễn Thái Ngọc Duy9a0a30a2012-04-23 19:30:22 +0700726__attribute__((format (printf,2,3)))
Stefan Bellerc7e5fe72018-09-28 10:30:33 -0700727int fprintf_ln(FILE *fp, const char *fmt, ...);
Nguyễn Thái Ngọc Duy9a0a30a2012-04-23 19:30:22 +0700728
Jeff King88d5a6f2014-05-22 05:44:09 -0400729char *xstrdup_tolower(const char *);
Lars Schneider13ecb4632018-02-15 16:27:06 +0100730char *xstrdup_toupper(const char *);
Jeff King88d5a6f2014-05-22 05:44:09 -0400731
Stefan Beller6afbbdd2015-01-16 04:04:51 -0500732/**
Jeff King30a0ddb2014-06-18 16:01:34 -0400733 * Create a newly allocated string using printf format. You can do this easily
734 * with a strbuf, but this provides a shortcut to save a few lines.
735 */
736__attribute__((format (printf, 1, 0)))
737char *xstrvfmt(const char *fmt, va_list ap);
738__attribute__((format (printf, 1, 2)))
739char *xstrfmt(const char *fmt, ...);
740
Junio C Hamanod1df5742005-04-25 18:26:45 -0700741#endif /* STRBUF_H */