]> git.ipfire.org Git - thirdparty/git.git/blame - strbuf.h
abspath: move related functions to abspath
[thirdparty/git.git] / strbuf.h
CommitLineData
d1df5743
JH
1#ifndef STRBUF_H
2#define STRBUF_H
b449f4cf 3
45577796
CW
4/*
5 * NOTE FOR STRBUF DEVELOPERS
6 *
7 * strbuf is a low-level primitive; as such it should interact only
8 * with other low-level primitives. Do not introduce new functions
9 * which interact with higher-level APIs.
10 */
11
f6f77559
EN
12struct string_list;
13
bdfdaa49
JK
14/**
15 * strbuf's are meant to be used with all the usual C string and memory
16 * APIs. Given that the length of the buffer is known, it's often better to
17 * use the mem* functions than a str* one (memchr vs. strchr e.g.).
18 * Though, one has to be careful about the fact that str* functions often
19 * stop on NULs and that strbufs may have embedded NULs.
20 *
21 * A strbuf is NUL terminated for convenience, but no function in the
22 * strbuf API actually relies on the string being free of NULs.
23 *
24 * strbufs have some invariants that are very important to keep in mind:
25 *
aa07cac4
JK
26 * - The `buf` member is never NULL, so it can be used in any usual C
27 * string operations safely. strbuf's _have_ to be initialized either by
28 * `strbuf_init()` or by `= STRBUF_INIT` before the invariants, though.
bdfdaa49 29 *
aa07cac4
JK
30 * Do *not* assume anything on what `buf` really is (e.g. if it is
31 * allocated memory or not), use `strbuf_detach()` to unwrap a memory
32 * buffer from its strbuf shell in a safe way. That is the sole supported
33 * way. This will give you a malloced buffer that you can later `free()`.
34 *
35 * However, it is totally safe to modify anything in the string pointed by
36 * the `buf` member, between the indices `0` and `len-1` (inclusive).
37 *
38 * - The `buf` member is a byte array that has at least `len + 1` bytes
39 * allocated. The extra byte is used to store a `'\0'`, allowing the
40 * `buf` member to be a valid C-string. Every strbuf function ensure this
41 * invariant is preserved.
42 *
43 * NOTE: It is OK to "play" with the buffer directly if you work it this
44 * way:
45 *
088c9a86
JK
46 * strbuf_grow(sb, SOME_SIZE); <1>
47 * strbuf_setlen(sb, sb->len + SOME_OTHER_SIZE);
48 *
aa07cac4
JK
49 * <1> Here, the memory array starting at `sb->buf`, and of length
50 * `strbuf_avail(sb)` is all yours, and you can be sure that
51 * `strbuf_avail(sb)` is at least `SOME_SIZE`.
52 *
53 * NOTE: `SOME_OTHER_SIZE` must be smaller or equal to `strbuf_avail(sb)`.
54 *
55 * Doing so is safe, though if it has to be done in many places, adding the
56 * missing API to the strbuf module is the way to go.
57 *
58 * WARNING: Do _not_ assume that the area that is yours is of size `alloc
59 * - 1` even if it's true in the current implementation. Alloc is somehow a
60 * "private" member that should not be messed with. Use `strbuf_avail()`
61 * instead.
62*/
b449f4cf 63
bdfdaa49
JK
64/**
65 * Data Structures
66 * ---------------
67 */
68
69/**
70 * This is the string buffer structure. The `len` member can be used to
71 * determine the current length of the string, and `buf` member provides
72 * access to the string itself.
73 */
d1df5743 74struct strbuf {
b449f4cf
PH
75 size_t alloc;
76 size_t len;
bf0f910d 77 char *buf;
d1df5743
JH
78};
79
bdfdaa49 80extern char strbuf_slopbuf[];
608cfd31 81#define STRBUF_INIT { .buf = strbuf_slopbuf }
b449f4cf 82
30e677e0 83/*
84 * Predeclare this here, since cache.h includes this file before it defines the
85 * struct.
86 */
87struct object_id;
88
bdfdaa49 89/**
14e2177a
JK
90 * Life Cycle Functions
91 * --------------------
bdfdaa49
JK
92 */
93
94/**
95 * Initialize the structure. The second parameter can be zero or a bigger
96 * number to allocate memory, in case you want to prevent further reallocs.
97 */
c7e5fe79 98void strbuf_init(struct strbuf *sb, size_t alloc);
bdfdaa49
JK
99
100/**
e0222159
JN
101 * Release a string buffer and the memory it used. After this call, the
102 * strbuf points to an empty string that does not need to be free()ed, as
103 * if it had been set to `STRBUF_INIT` and never modified.
104 *
105 * To clear a strbuf in preparation for further use without the overhead
106 * of free()ing and malloc()ing again, use strbuf_reset() instead.
bdfdaa49 107 */
c7e5fe79 108void strbuf_release(struct strbuf *sb);
bdfdaa49
JK
109
110/**
111 * Detach the string from the strbuf and returns it; you now own the
112 * storage the string occupies and it is your responsibility from then on
113 * to release it with `free(3)` when you are done with it.
e0222159
JN
114 *
115 * The strbuf that previously held the string is reset to `STRBUF_INIT` so
116 * it can be reused after calling this function.
bdfdaa49 117 */
c7e5fe79 118char *strbuf_detach(struct strbuf *sb, size_t *sz);
bdfdaa49
JK
119
120/**
121 * Attach a string to a buffer. You should specify the string to attach,
122 * the current length of the string and the amount of allocated memory.
123 * The amount must be larger than the string length, because the string you
124 * pass is supposed to be a NUL-terminated string. This string _must_ be
125 * malloc()ed, and after attaching, the pointer cannot be relied upon
126 * anymore, and neither be free()d directly.
127 */
c7e5fe79 128void strbuf_attach(struct strbuf *sb, void *str, size_t len, size_t mem);
bdfdaa49
JK
129
130/**
131 * Swap the contents of two string buffers.
132 */
187e290a
NTND
133static inline void strbuf_swap(struct strbuf *a, struct strbuf *b)
134{
35d803bc 135 SWAP(*a, *b);
c76689df 136}
b449f4cf 137
bdfdaa49
JK
138
139/**
14e2177a
JK
140 * Functions related to the size of the buffer
141 * -------------------------------------------
bdfdaa49
JK
142 */
143
144/**
145 * Determine the amount of allocated but unused memory.
146 */
187e290a
NTND
147static inline size_t strbuf_avail(const struct strbuf *sb)
148{
c76689df 149 return sb->alloc ? sb->alloc - sb->len - 1 : 0;
b449f4cf 150}
a8f3e221 151
bdfdaa49
JK
152/**
153 * Ensure that at least this amount of unused memory is available after
154 * `len`. This is used when you know a typical size for what you will add
155 * and want to avoid repetitive automatic resizing of the underlying buffer.
156 * This is never a needed operation, but can be critical for performance in
157 * some cases.
158 */
c7e5fe79 159void strbuf_grow(struct strbuf *sb, size_t amount);
a8f3e221 160
bdfdaa49
JK
161/**
162 * Set the length of the buffer to a given value. This function does *not*
163 * allocate new memory, so you should not perform a `strbuf_setlen()` to a
164 * length that is larger than `len + strbuf_avail()`. `strbuf_setlen()` is
165 * just meant as a 'please fix invariants from this strbuf I just messed
166 * with'.
167 */
187e290a
NTND
168static inline void strbuf_setlen(struct strbuf *sb, size_t len)
169{
7141efab 170 if (len > (sb->alloc ? sb->alloc - 1 : 0))
46d699f4 171 BUG("strbuf_setlen() beyond buffer");
c76689df 172 sb->len = len;
65961d5a
173 if (sb->buf != strbuf_slopbuf)
174 sb->buf[len] = '\0';
175 else
176 assert(!strbuf_slopbuf[0]);
b449f4cf 177}
bdfdaa49
JK
178
179/**
180 * Empty the buffer by setting the size of it to zero.
181 */
b315c5c0 182#define strbuf_reset(sb) strbuf_setlen(sb, 0)
b449f4cf 183
bdfdaa49
JK
184
185/**
14e2177a
JK
186 * Functions related to the contents of the buffer
187 * -----------------------------------------------
bdfdaa49
JK
188 */
189
190/**
d468fa27
JK
191 * Strip whitespace from the beginning (`ltrim`), end (`rtrim`), or both side
192 * (`trim`) of a string.
bdfdaa49 193 */
c7e5fe79
SB
194void strbuf_trim(struct strbuf *sb);
195void strbuf_rtrim(struct strbuf *sb);
196void strbuf_ltrim(struct strbuf *sb);
bdfdaa49 197
c64a8d20 198/* Strip trailing directory separators */
c7e5fe79 199void strbuf_trim_trailing_dir_sep(struct strbuf *sb);
c64a8d20 200
f9573628 201/* Strip trailing LF or CR/LF */
39f73315 202void strbuf_trim_trailing_newline(struct strbuf *sb);
f9573628 203
bdfdaa49
JK
204/**
205 * Replace the contents of the strbuf with a reencoded form. Returns -1
206 * on error, 0 on success.
207 */
c7e5fe79 208int strbuf_reencode(struct strbuf *sb, const char *from, const char *to);
bdfdaa49
JK
209
210/**
211 * Lowercase each character in the buffer using `tolower`.
212 */
c7e5fe79 213void strbuf_tolower(struct strbuf *sb);
bdfdaa49
JK
214
215/**
216 * Compare two buffers. Returns an integer less than, equal to, or greater
217 * than zero if the first buffer is found, respectively, to be less than,
218 * to match, or be greater than the second buffer.
219 */
c7e5fe79 220int strbuf_cmp(const struct strbuf *first, const struct strbuf *second);
eacd6dc5 221
bdfdaa49
JK
222
223/**
14e2177a
JK
224 * Adding data to the buffer
225 * -------------------------
bdfdaa49
JK
226 *
227 * NOTE: All of the functions in this section will grow the buffer as
228 * necessary. If they fail for some reason other than memory shortage and the
229 * buffer hadn't been allocated before (i.e. the `struct strbuf` was set to
230 * `STRBUF_INIT`), then they will free() it.
231 */
232
233/**
234 * Add a single character to the buffer.
235 */
236static inline void strbuf_addch(struct strbuf *sb, int c)
237{
fec501da
JK
238 if (!strbuf_avail(sb))
239 strbuf_grow(sb, 1);
bdfdaa49
JK
240 sb->buf[sb->len++] = c;
241 sb->buf[sb->len] = '\0';
242}
243
244/**
245 * Add a character the specified number of times to the buffer.
246 */
c7e5fe79 247void strbuf_addchars(struct strbuf *sb, int c, size_t n);
bdfdaa49
JK
248
249/**
250 * Insert data to the given position of the buffer. The remaining contents
251 * will be shifted, not overwritten.
252 */
c7e5fe79 253void strbuf_insert(struct strbuf *sb, size_t pos, const void *, size_t);
bdfdaa49 254
a91cc7fa
RS
255/**
256 * Insert a NUL-terminated string to the given position of the buffer.
257 * The remaining contents will be shifted, not overwritten. It's an
258 * inline function to allow the compiler to resolve strlen() calls on
259 * constants at compile time.
260 */
261static inline void strbuf_insertstr(struct strbuf *sb, size_t pos,
262 const char *s)
263{
264 strbuf_insert(sb, pos, s, strlen(s));
265}
266
5ef264db
PSU
267/**
268 * Insert data to the given position of the buffer giving a printf format
269 * string. The contents will be shifted, not overwritten.
270 */
271void strbuf_vinsertf(struct strbuf *sb, size_t pos, const char *fmt,
272 va_list ap);
273
75d31cee 274__attribute__((format (printf, 3, 4)))
5ef264db
PSU
275void strbuf_insertf(struct strbuf *sb, size_t pos, const char *fmt, ...);
276
bdfdaa49
JK
277/**
278 * Remove given amount of data from a given position of the buffer.
279 */
c7e5fe79 280void strbuf_remove(struct strbuf *sb, size_t pos, size_t len);
bdfdaa49
JK
281
282/**
283 * Remove the bytes between `pos..pos+len` and replace it with the given
284 * data.
285 */
c7e5fe79
SB
286void strbuf_splice(struct strbuf *sb, size_t pos, size_t len,
287 const void *data, size_t data_len);
bdfdaa49
JK
288
289/**
290 * Add a NUL-terminated string to the buffer. Each line will be prepended
291 * by a comment character and a blank.
292 */
c7e5fe79
SB
293void strbuf_add_commented_lines(struct strbuf *out,
294 const char *buf, size_t size);
bdfdaa49
JK
295
296
297/**
298 * Add data of given length to the buffer.
299 */
c7e5fe79 300void strbuf_add(struct strbuf *sb, const void *data, size_t len);
bdfdaa49
JK
301
302/**
303 * Add a NUL-terminated string to the buffer.
304 *
305 * NOTE: This function will *always* be implemented as an inline or a macro
306 * using strlen, meaning that this is efficient to write things like:
307 *
088c9a86 308 * strbuf_addstr(sb, "immediate string");
bdfdaa49
JK
309 *
310 */
311static inline void strbuf_addstr(struct strbuf *sb, const char *s)
312{
313 strbuf_add(sb, s, strlen(s));
314}
315
316/**
317 * Copy the contents of another buffer at the end of the current one.
318 */
c7e5fe79 319void strbuf_addbuf(struct strbuf *sb, const struct strbuf *sb2);
bdfdaa49 320
e71c4a88
PSU
321/**
322 * Join the arguments into a buffer. `delim` is put between every
323 * two arguments.
324 */
325const char *strbuf_join_argv(struct strbuf *buf, int argc,
326 const char **argv, char delim);
327
bdfdaa49
JK
328/**
329 * This function can be used to expand a format string containing
330 * placeholders. To that end, it parses the string and calls the specified
331 * function for every percent sign found.
332 *
333 * The callback function is given a pointer to the character after the `%`
334 * and a pointer to the struct strbuf. It is expected to add the expanded
335 * version of the placeholder to the strbuf, e.g. to add a newline
336 * character if the letter `n` appears after a `%`. The function returns
337 * the length of the placeholder recognized and `strbuf_expand()` skips
338 * over it.
339 *
340 * The format `%%` is automatically expanded to a single `%` as a quoting
341 * mechanism; callers do not need to handle the `%` placeholder themselves,
342 * and the callback function will not be invoked for this placeholder.
343 *
344 * All other characters (non-percent and not skipped ones) are copied
345 * verbatim to the strbuf. If the callback returned zero, meaning that the
346 * placeholder is unknown, then the percent sign is copied, too.
347 *
348 * In order to facilitate caching and to make it possible to give
0e20b229
FC
349 * parameters to the callback, `strbuf_expand()` passes a context
350 * pointer with any kind of data.
bdfdaa49 351 */
c7e5fe79
SB
352typedef size_t (*expand_fn_t) (struct strbuf *sb,
353 const char *placeholder,
354 void *context);
355void strbuf_expand(struct strbuf *sb,
356 const char *format,
357 expand_fn_t fn,
358 void *context);
bdfdaa49 359
fd2015b3
AW
360/**
361 * Used as callback for `strbuf_expand` to only expand literals
362 * (i.e. %n and %xNN). The context argument is ignored.
363 */
364size_t strbuf_expand_literal_cb(struct strbuf *sb,
365 const char *placeholder,
366 void *context);
367
bdfdaa49
JK
368/**
369 * Used as callback for `strbuf_expand()`, expects an array of
370 * struct strbuf_expand_dict_entry as context, i.e. pairs of
371 * placeholder and replacement string. The array needs to be
372 * terminated by an entry with placeholder set to NULL.
373 */
374struct strbuf_expand_dict_entry {
375 const char *placeholder;
376 const char *value;
377};
c7e5fe79
SB
378size_t strbuf_expand_dict_cb(struct strbuf *sb,
379 const char *placeholder,
380 void *context);
bdfdaa49
JK
381
382/**
383 * Append the contents of one strbuf to another, quoting any
384 * percent signs ("%") into double-percents ("%%") in the
385 * destination. This is useful for literal data to be fed to either
386 * strbuf_expand or to the *printf family of functions.
387 */
c7e5fe79 388void strbuf_addbuf_percentquote(struct strbuf *dst, const struct strbuf *src);
bdfdaa49 389
b44d0118 390#define STRBUF_ENCODE_SLASH 1
391
46fd7b39 392/**
393 * Append the contents of a string to a strbuf, percent-encoding any characters
394 * that are needed to be encoded for a URL.
b44d0118 395 *
396 * If STRBUF_ENCODE_SLASH is set in flags, percent-encode slashes. Otherwise,
397 * slashes are not percent-encoded.
46fd7b39 398 */
b44d0118 399void strbuf_add_percentencode(struct strbuf *dst, const char *src, int flags);
46fd7b39 400
bdfdaa49
JK
401/**
402 * Append the given byte size as a human-readable string (i.e. 12.23 KiB,
403 * 3.50 MiB).
404 */
c7e5fe79 405void strbuf_humanise_bytes(struct strbuf *buf, off_t bytes);
bdfdaa49 406
8f354a1f
DR
407/**
408 * Append the given byte rate as a human-readable string (i.e. 12.23 KiB/s,
409 * 3.50 MiB/s).
410 */
411void strbuf_humanise_rate(struct strbuf *buf, off_t bytes);
412
bdfdaa49
JK
413/**
414 * Add a formatted string to the buffer.
415 */
416__attribute__((format (printf,2,3)))
c7e5fe79 417void strbuf_addf(struct strbuf *sb, const char *fmt, ...);
bdfdaa49
JK
418
419/**
420 * Add a formatted string prepended by a comment character and a
421 * blank to the buffer.
422 */
423__attribute__((format (printf, 2, 3)))
c7e5fe79 424void strbuf_commented_addf(struct strbuf *sb, const char *fmt, ...);
bdfdaa49
JK
425
426__attribute__((format (printf,2,0)))
c7e5fe79 427void strbuf_vaddf(struct strbuf *sb, const char *fmt, va_list ap);
bdfdaa49 428
aa1462cc
JK
429/**
430 * Add the time specified by `tm`, as formatted by `strftime`.
c3fbf81a
RS
431 * `tz_offset` is in decimal hhmm format, e.g. -600 means six hours west
432 * of Greenwich, and it's used to expand %z internally. However, tokens
433 * with modifiers (e.g. %Ez) are passed to `strftime`.
3b702239
ÆAB
434 * `suppress_tz_name`, when set, expands %Z internally to the empty
435 * string rather than passing it to `strftime`.
c3fbf81a 436 */
c7e5fe79
SB
437void strbuf_addftime(struct strbuf *sb, const char *fmt,
438 const struct tm *tm, int tz_offset,
439 int suppress_tz_name);
aa1462cc 440
bdfdaa49
JK
441/**
442 * Read a given size of data from a FILE* pointer to the buffer.
443 *
444 * NOTE: The buffer is rewound if the read fails. If -1 is returned,
445 * `errno` must be consulted, like you would do for `read(3)`.
1a0c8dfd
JH
446 * `strbuf_read()`, `strbuf_read_file()` and `strbuf_getline_*()`
447 * family of functions have the same behaviour as well.
bdfdaa49 448 */
c7e5fe79 449size_t strbuf_fread(struct strbuf *sb, size_t size, FILE *file);
bdfdaa49
JK
450
451/**
452 * Read the contents of a given file descriptor. The third argument can be
453 * used to give a hint about the file size, to avoid reallocs. If read fails,
454 * any partial read is undone.
455 */
c7e5fe79 456ssize_t strbuf_read(struct strbuf *sb, int fd, size_t hint);
bdfdaa49 457
b4e04fb6
SB
458/**
459 * Read the contents of a given file descriptor partially by using only one
460 * attempt of xread. The third argument can be used to give a hint about the
461 * file size, to avoid reallocs. Returns the number of new bytes appended to
462 * the sb.
463 */
c7e5fe79 464ssize_t strbuf_read_once(struct strbuf *sb, int fd, size_t hint);
b4e04fb6 465
bdfdaa49
JK
466/**
467 * Read the contents of a file, specified by its path. The third argument
468 * can be used to give a hint about the file size, to avoid reallocs.
ed008d7b
PB
469 * Return the number of bytes read or a negative value if some error
470 * occurred while opening or reading the file.
bdfdaa49 471 */
c7e5fe79 472ssize_t strbuf_read_file(struct strbuf *sb, const char *path, size_t hint);
bdfdaa49
JK
473
474/**
475 * Read the target of a symbolic link, specified by its path. The third
476 * argument can be used to give a hint about the size, to avoid reallocs.
477 */
c7e5fe79 478int strbuf_readlink(struct strbuf *sb, const char *path, size_t hint);
bdfdaa49 479
2dac9b56
SB
480/**
481 * Write the whole content of the strbuf to the stream not stopping at
482 * NUL bytes.
483 */
c7e5fe79 484ssize_t strbuf_write(struct strbuf *sb, FILE *stream);
2dac9b56 485
bdfdaa49 486/**
1a0c8dfd
JH
487 * Read a line from a FILE *, overwriting the existing contents of
488 * the strbuf. The strbuf_getline*() family of functions share
489 * this signature, but have different line termination conventions.
490 *
bdfdaa49
JK
491 * Reading stops after the terminator or at EOF. The terminator
492 * is removed from the buffer before returning. Returns 0 unless
493 * there was nothing left before EOF, in which case it returns `EOF`.
494 */
8f309aeb
JH
495typedef int (*strbuf_getline_fn)(struct strbuf *, FILE *);
496
497/* Uses LF as the line terminator */
c7e5fe79 498int strbuf_getline_lf(struct strbuf *sb, FILE *fp);
8f309aeb
JH
499
500/* Uses NUL as the line terminator */
c7e5fe79 501int strbuf_getline_nul(struct strbuf *sb, FILE *fp);
8f309aeb 502
c8aa9fdf 503/*
8f309aeb
JH
504 * Similar to strbuf_getline_lf(), but additionally treats a CR that
505 * comes immediately before the LF as part of the terminator.
1a0c8dfd
JH
506 * This is the most friendly version to be used to read "text" files
507 * that can come from platforms whose native text format is CRLF
508 * terminated.
c8aa9fdf 509 */
c7e5fe79 510int strbuf_getline(struct strbuf *sb, FILE *file);
c8aa9fdf 511
bdfdaa49
JK
512
513/**
514 * Like `strbuf_getline`, but keeps the trailing terminator (if
515 * any) in the buffer.
516 */
c7e5fe79 517int strbuf_getwholeline(struct strbuf *sb, FILE *file, int term);
bdfdaa49 518
bd021f39
PS
519/**
520 * Like `strbuf_getwholeline`, but appends the line instead of
521 * resetting the buffer first.
522 */
523int strbuf_appendwholeline(struct strbuf *sb, FILE *file, int term);
524
bdfdaa49
JK
525/**
526 * Like `strbuf_getwholeline`, but operates on a file descriptor.
527 * It reads one character at a time, so it is very slow. Do not
528 * use it unless you need the correct position in the file
529 * descriptor.
530 */
c7e5fe79 531int strbuf_getwholeline_fd(struct strbuf *sb, int fd, int term);
bdfdaa49
JK
532
533/**
534 * Set the buffer to the path of the current working directory.
535 */
c7e5fe79 536int strbuf_getcwd(struct strbuf *sb);
bdfdaa49 537
670c359d
JK
538/**
539 * Normalize in-place the path contained in the strbuf. See
540 * normalize_path_copy() for details. If an error occurs, the contents of "sb"
541 * are left untouched, and -1 is returned.
542 */
c7e5fe79 543int strbuf_normalize_path(struct strbuf *sb);
670c359d 544
bdfdaa49
JK
545/**
546 * Strip whitespace from a buffer. The second parameter controls if
547 * comments are considered contents to be removed or not.
548 */
c7e5fe79 549void strbuf_stripspace(struct strbuf *buf, int skip_comments);
63af4a84 550
6dda4e60
JK
551static inline int strbuf_strip_suffix(struct strbuf *sb, const char *suffix)
552{
553 if (strip_suffix_mem(sb->buf, &sb->len, suffix)) {
554 strbuf_setlen(sb, sb->len);
555 return 1;
556 } else
557 return 0;
558}
559
6afbbdda 560/**
06379a65
MH
561 * Split str (of length slen) at the specified terminator character.
562 * Return a null-terminated array of pointers to strbuf objects
563 * holding the substrings. The substrings include the terminator,
564 * except for the last substring, which might be unterminated if the
565 * original string did not end with a terminator. If max is positive,
566 * then split the string into at most max substrings (with the last
567 * substring containing everything following the (max-1)th terminator
568 * character).
569 *
f20e56e2
JK
570 * The most generic form is `strbuf_split_buf`, which takes an arbitrary
571 * pointer/len buffer. The `_str` variant takes a NUL-terminated string,
572 * the `_max` variant takes a strbuf, and just `strbuf_split` is a convenience
573 * wrapper to drop the `max` parameter.
574 *
06379a65
MH
575 * For lighter-weight alternatives, see string_list_split() and
576 * string_list_split_in_place().
577 */
c7e5fe79
SB
578struct strbuf **strbuf_split_buf(const char *str, size_t len,
579 int terminator, int max);
06379a65 580
2f1d9e2b 581static inline struct strbuf **strbuf_split_str(const char *str,
17b73dc6 582 int terminator, int max)
2f1d9e2b 583{
17b73dc6 584 return strbuf_split_buf(str, strlen(str), terminator, max);
2f1d9e2b 585}
06379a65 586
2f1d9e2b 587static inline struct strbuf **strbuf_split_max(const struct strbuf *sb,
c7e5fe79 588 int terminator, int max)
2f1d9e2b 589{
17b73dc6 590 return strbuf_split_buf(sb->buf, sb->len, terminator, max);
2f1d9e2b 591}
06379a65 592
17b73dc6
MH
593static inline struct strbuf **strbuf_split(const struct strbuf *sb,
594 int terminator)
28fc3a68 595{
17b73dc6 596 return strbuf_split_max(sb, terminator, 0);
28fc3a68 597}
06379a65 598
f6f77559
EN
599/*
600 * Adds all strings of a string list to the strbuf, separated by the given
601 * separator. For example, if sep is
602 * ', '
603 * and slist contains
604 * ['element1', 'element2', ..., 'elementN'],
605 * then write:
606 * 'element1, element2, ..., elementN'
607 * to str. If only one element, just write "element1" to str.
608 */
c7e5fe79
SB
609void strbuf_add_separated_string_list(struct strbuf *str,
610 const char *sep,
611 struct string_list *slist);
f6f77559 612
6afbbdda 613/**
06379a65
MH
614 * Free a NULL-terminated list of strbufs (for example, the return
615 * values of the strbuf_split*() functions).
616 */
c7e5fe79 617void strbuf_list_free(struct strbuf **list);
f1696ee3 618
af49c6d0 619/**
c7c33f50 620 * Add the abbreviation, as generated by repo_find_unique_abbrev(), of `sha1` to
af49c6d0
JK
621 * the strbuf `sb`.
622 */
155b517d
JT
623struct repository;
624void strbuf_repo_add_unique_abbrev(struct strbuf *sb, struct repository *repo,
625 const struct object_id *oid, int abbrev_len);
626void strbuf_add_unique_abbrev(struct strbuf *sb, const struct object_id *oid,
c7e5fe79 627 int abbrev_len);
af49c6d0 628
9ea57964
DS
629/*
630 * Remove the filename from the provided path string. If the path
631 * contains a trailing separator, then the path is considered a directory
632 * and nothing is modified.
633 *
634 * Examples:
635 * - "/path/to/file" -> "/path/to/"
636 * - "/path/to/dir/" -> "/path/to/dir/"
637 */
638void strbuf_strip_file_from_path(struct strbuf *sb);
639
c7e5fe79
SB
640void strbuf_add_lines(struct strbuf *sb,
641 const char *prefix,
642 const char *buf,
643 size_t size);
895680f0 644
6afbbdda 645/**
5963c036
MH
646 * Append s to sb, with the characters '<', '>', '&' and '"' converted
647 * into XML entities.
648 */
c7e5fe79
SB
649void strbuf_addstr_xml_quoted(struct strbuf *sb,
650 const char *s);
5963c036 651
399ad553
JK
652/**
653 * "Complete" the contents of `sb` by ensuring that either it ends with the
654 * character `term`, or it is empty. This can be used, for example,
655 * to ensure that text ends with a newline, but without creating an empty
656 * blank line if there is no content in the first place.
657 */
658static inline void strbuf_complete(struct strbuf *sb, char term)
659{
660 if (sb->len && sb->buf[sb->len - 1] != term)
661 strbuf_addch(sb, term);
662}
663
895680f0
JH
664static inline void strbuf_complete_line(struct strbuf *sb)
665{
399ad553 666 strbuf_complete(sb, '\n');
895680f0
JH
667}
668
0705fe20
JK
669/*
670 * Copy "name" to "sb", expanding any special @-marks as handled by
c7c33f50 671 * repo_interpret_branch_name(). The result is a non-qualified branch name
0705fe20
JK
672 * (so "foo" or "origin/master" instead of "refs/heads/foo" or
673 * "refs/remotes/origin/master").
674 *
675 * Note that the resulting name may not be a syntactically valid refname.
0e9f62da
JK
676 *
677 * If "allowed" is non-zero, restrict the set of allowed expansions. See
c7c33f50 678 * repo_interpret_branch_name() for details.
0705fe20 679 */
c7e5fe79
SB
680void strbuf_branchname(struct strbuf *sb, const char *name,
681 unsigned allowed);
0705fe20
JK
682
683/*
684 * Like strbuf_branchname() above, but confirm that the result is
685 * syntactically valid to be used as a local branch name in refs/heads/.
686 *
687 * The return value is "0" if the result is valid, and "-1" otherwise.
688 */
c7e5fe79 689int strbuf_check_branch_ref(struct strbuf *sb, const char *name);
a552de75 690
c2694952
MD
691typedef int (*char_predicate)(char ch);
692
693int is_rfc3986_unreserved(char ch);
694int is_rfc3986_reserved_or_unreserved(char ch);
695
c7e5fe79 696void strbuf_addstr_urlencode(struct strbuf *sb, const char *name,
c2694952 697 char_predicate allow_unencoded_fn);
679eebe2 698
9a0a30aa 699__attribute__((format (printf,1,2)))
c7e5fe79 700int printf_ln(const char *fmt, ...);
9a0a30aa 701__attribute__((format (printf,2,3)))
c7e5fe79 702int fprintf_ln(FILE *fp, const char *fmt, ...);
9a0a30aa 703
88d5a6f6 704char *xstrdup_tolower(const char *);
13ecb463 705char *xstrdup_toupper(const char *);
88d5a6f6 706
6afbbdda 707/**
30a0ddb7
JK
708 * Create a newly allocated string using printf format. You can do this easily
709 * with a strbuf, but this provides a shortcut to save a few lines.
710 */
711__attribute__((format (printf, 1, 0)))
712char *xstrvfmt(const char *fmt, va_list ap);
713__attribute__((format (printf, 1, 2)))
714char *xstrfmt(const char *fmt, ...);
715
d1df5743 716#endif /* STRBUF_H */