4 strbuf's are meant to be used with all the usual C string and memory
5 APIs. Given that the length of the buffer is known, it's often better to
6 use the mem* functions than a str* one (memchr vs. strchr e.g.).
7 Though, one has to be careful about the fact that str* functions often
8 stop on NULs and that strbufs may have embedded NULs.
10 A strbuf is NUL terminated for convenience, but no function in the
11 strbuf API actually relies on the string being free of NULs.
13 strbufs have some invariants that are very important to keep in mind:
15 . The `buf` member is never NULL, so it can be used in any usual C
16 string operations safely. strbuf's _have_ to be initialized either by
17 `strbuf_init()` or by `= STRBUF_INIT` before the invariants, though.
19 Do *not* assume anything on what `buf` really is (e.g. if it is
20 allocated memory or not), use `strbuf_detach()` to unwrap a memory
21 buffer from its strbuf shell in a safe way. That is the sole supported
22 way. This will give you a malloced buffer that you can later `free()`.
24 However, it is totally safe to modify anything in the string pointed by
25 the `buf` member, between the indices `0` and `len-1` (inclusive).
27 . The `buf` member is a byte array that has at least `len + 1` bytes
28 allocated. The extra byte is used to store a `'\0'`, allowing the
29 `buf` member to be a valid C-string. Every strbuf function ensure this
30 invariant is preserved.
32 NOTE: It is OK to "play" with the buffer directly if you work it this
36 strbuf_grow(sb, SOME_SIZE); <1>
37 strbuf_setlen(sb, sb->len + SOME_OTHER_SIZE);
39 <1> Here, the memory array starting at `sb->buf`, and of length
40 `strbuf_avail(sb)` is all yours, and you can be sure that
41 `strbuf_avail(sb)` is at least `SOME_SIZE`.
43 NOTE: `SOME_OTHER_SIZE` must be smaller or equal to `strbuf_avail(sb)`.
45 Doing so is safe, though if it has to be done in many places, adding the
46 missing API to the strbuf module is the way to go.
48 WARNING: Do _not_ assume that the area that is yours is of size `alloc
49 - 1` even if it's true in the current implementation. Alloc is somehow a
50 "private" member that should not be messed with. Use `strbuf_avail()`
58 This is the string buffer structure. The `len` member can be used to
59 determine the current length of the string, and `buf` member provides
60 access to the string itself.
69 Initialize the structure. The second parameter can be zero or a bigger
70 number to allocate memory, in case you want to prevent further reallocs.
74 Release a string buffer and the memory it used. You should not use the
75 string buffer after using this function, unless you initialize it again.
79 Detach the string from the strbuf and returns it; you now own the
80 storage the string occupies and it is your responsibility from then on
81 to release it with `free(3)` when you are done with it.
85 Attach a string to a buffer. You should specify the string to attach,
86 the current length of the string and the amount of allocated memory.
87 The amount must be larger than the string length, because the string you
88 pass is supposed to be a NUL-terminated string. This string _must_ be
89 malloc()ed, and after attaching, the pointer cannot be relied upon
90 anymore, and neither be free()d directly.
94 Swap the contents of two string buffers.
96 * Related to the size of the buffer
100 Determine the amount of allocated but unused memory.
104 Ensure that at least this amount of unused memory is available after
105 `len`. This is used when you know a typical size for what you will add
106 and want to avoid repetitive automatic resizing of the underlying buffer.
107 This is never a needed operation, but can be critical for performance in
112 Set the length of the buffer to a given value. This function does *not*
113 allocate new memory, so you should not perform a `strbuf_setlen()` to a
114 length that is larger than `len + strbuf_avail()`. `strbuf_setlen()` is
115 just meant as a 'please fix invariants from this strbuf I just messed
120 Empty the buffer by setting the size of it to zero.
122 * Related to the contents of the buffer
126 Strip whitespace from the beginning and end of a string.
127 Equivalent to performing `strbuf_rtrim()` followed by `strbuf_ltrim()`.
131 Strip whitespace from the end of a string.
135 Strip whitespace from the beginning of a string.
139 Replace the contents of the strbuf with a reencoded form. Returns -1
140 on error, 0 on success.
144 Lowercase each character in the buffer using `tolower`.
148 Compare two buffers. Returns an integer less than, equal to, or greater
149 than zero if the first buffer is found, respectively, to be less than,
150 to match, or be greater than the second buffer.
152 * Adding data to the buffer
154 NOTE: All of the functions in this section will grow the buffer as necessary.
155 If they fail for some reason other than memory shortage and the buffer hadn't
156 been allocated before (i.e. the `struct strbuf` was set to `STRBUF_INIT`),
157 then they will free() it.
161 Add a single character to the buffer.
165 Add a character the specified number of times to the buffer.
169 Insert data to the given position of the buffer. The remaining contents
170 will be shifted, not overwritten.
174 Remove given amount of data from a given position of the buffer.
178 Remove the bytes between `pos..pos+len` and replace it with the given
181 `strbuf_add_commented_lines`::
183 Add a NUL-terminated string to the buffer. Each line will be prepended
184 by a comment character and a blank.
188 Add data of given length to the buffer.
192 Add a NUL-terminated string to the buffer.
194 NOTE: This function will *always* be implemented as an inline or a macro
198 strbuf_add(..., s, strlen(s));
201 Meaning that this is efficient to write things like:
204 strbuf_addstr(sb, "immediate string");
209 Copy the contents of another buffer at the end of the current one.
213 Copy part of the buffer from a given position till a given length to the
218 This function can be used to expand a format string containing
219 placeholders. To that end, it parses the string and calls the specified
220 function for every percent sign found.
222 The callback function is given a pointer to the character after the `%`
223 and a pointer to the struct strbuf. It is expected to add the expanded
224 version of the placeholder to the strbuf, e.g. to add a newline
225 character if the letter `n` appears after a `%`. The function returns
226 the length of the placeholder recognized and `strbuf_expand()` skips
229 The format `%%` is automatically expanded to a single `%` as a quoting
230 mechanism; callers do not need to handle the `%` placeholder themselves,
231 and the callback function will not be invoked for this placeholder.
233 All other characters (non-percent and not skipped ones) are copied
234 verbatim to the strbuf. If the callback returned zero, meaning that the
235 placeholder is unknown, then the percent sign is copied, too.
237 In order to facilitate caching and to make it possible to give
238 parameters to the callback, `strbuf_expand()` passes a context pointer,
239 which can be used by the programmer of the callback as she sees fit.
241 `strbuf_expand_dict_cb`::
243 Used as callback for `strbuf_expand()`, expects an array of
244 struct strbuf_expand_dict_entry as context, i.e. pairs of
245 placeholder and replacement string. The array needs to be
246 terminated by an entry with placeholder set to NULL.
248 `strbuf_addbuf_percentquote`::
250 Append the contents of one strbuf to another, quoting any
251 percent signs ("%") into double-percents ("%%") in the
252 destination. This is useful for literal data to be fed to either
253 strbuf_expand or to the *printf family of functions.
255 `strbuf_humanise_bytes`::
257 Append the given byte size as a human-readable string (i.e. 12.23 KiB,
262 Add a formatted string to the buffer.
264 `strbuf_commented_addf`::
266 Add a formatted string prepended by a comment character and a
271 Read a given size of data from a FILE* pointer to the buffer.
273 NOTE: The buffer is rewound if the read fails. If -1 is returned,
274 `errno` must be consulted, like you would do for `read(3)`.
275 `strbuf_read()`, `strbuf_read_file()` and `strbuf_getline()` has the
276 same behaviour as well.
280 Read the contents of a given file descriptor. The third argument can be
281 used to give a hint about the file size, to avoid reallocs.
285 Read the contents of a file, specified by its path. The third argument
286 can be used to give a hint about the file size, to avoid reallocs.
290 Read the target of a symbolic link, specified by its path. The third
291 argument can be used to give a hint about the size, to avoid reallocs.
295 Read a line from a FILE *, overwriting the existing contents
296 of the strbuf. The second argument specifies the line
297 terminator character, typically `'\n'`.
298 Reading stops after the terminator or at EOF. The terminator
299 is removed from the buffer before returning. Returns 0 unless
300 there was nothing left before EOF, in which case it returns `EOF`.
302 `strbuf_getwholeline`::
304 Like `strbuf_getline`, but keeps the trailing terminator (if
307 `strbuf_getwholeline_fd`::
309 Like `strbuf_getwholeline`, but operates on a file descriptor.
310 It reads one character at a time, so it is very slow. Do not
311 use it unless you need the correct position in the file
316 Set the buffer to the path of the current working directory.
318 `strbuf_add_absolute_path`
320 Add a path to a buffer, converting a relative path to an
321 absolute one in the process. Symbolic links are not
326 Strip whitespace from a buffer. The second parameter controls if
327 comments are considered contents to be removed or not.
334 Split a string or strbuf into a list of strbufs at a specified
335 terminator character. The returned substrings include the
336 terminator characters. Some of these functions take a `max`
337 parameter, which, if positive, limits the output to that
338 number of substrings.
342 Free a list of strbufs (for example, the return values of the
343 `strbuf_split()` functions).
347 Launch the user preferred editor to edit a file and fill the buffer
348 with the file's contents upon the user completing their editing. The
349 third argument can be used to set the environment which the editor is
350 run in. If the buffer is NULL the editor is launched as usual but the
351 file's contents are not read into the buffer upon completion.