1strbuf API 2========== 3 4strbuf's are meant to be used with all the usual C string and memory 5APIs. Given that the length of the buffer is known, it's often better to 6use the mem* functions than a str* one (memchr vs. strchr e.g.). 7Though, one has to be careful about the fact that str* functions often 8stop on NULs and that strbufs may have embedded NULs. 9 10An strbuf is NUL terminated for convenience, but no function in the 11strbuf API actually relies on the string being free of NULs. 12 13strbufs has some invariants that are very important to keep in mind: 14 15. The `buf` member is never NULL, so it can be used in any usual C 16string operations safely. strbuf's _have_ to be initialized either by 17`strbuf_init()` or by `= STRBUF_INIT` before the invariants, though. 18+ 19Do *not* assume anything on what `buf` really is (e.g. if it is 20allocated memory or not), use `strbuf_detach()` to unwrap a memory 21buffer from its strbuf shell in a safe way. That is the sole supported 22way. This will give you a malloced buffer that you can later `free()`. 23+ 24However, it is totally safe to modify anything in the string pointed by 25the `buf` member, between the indices `0` and `len-1` (inclusive). 26 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. 31+ 32NOTE: It is OK to "play" with the buffer directly if you work it this 33 way: 34+ 35---- 36strbuf_grow(sb, SOME_SIZE); <1> 37strbuf_setlen(sb, sb->len + SOME_OTHER_SIZE); 38---- 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`. 42+ 43NOTE: `SOME_OTHER_SIZE` must be smaller or equal to `strbuf_avail(sb)`. 44+ 45Doing so is safe, though if it has to be done in many places, adding the 46missing API to the strbuf module is the way to go. 47+ 48WARNING: 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()` 51instead. 52 53Data structures 54--------------- 55 56* `struct strbuf` 57 58This is the string buffer structure. The `len` member can be used to 59determine the current length of the string, and `buf` member provides access to 60the string itself. 61 62Functions 63--------- 64 65* Life cycle 66 67`strbuf_init`:: 68 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. 71 72`strbuf_release`:: 73 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. 76 77`strbuf_detach`:: 78 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. 82 83`strbuf_attach`:: 84 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. 91 92`strbuf_swap`:: 93 94 Swap the contents of two string buffers. 95 96* Related to the size of the buffer 97 98`strbuf_avail`:: 99 100 Determine the amount of allocated but unused memory. 101 102`strbuf_grow`:: 103 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 108 some cases. 109 110`strbuf_setlen`:: 111 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 116 with'. 117 118`strbuf_reset`:: 119 120 Empty the buffer by setting the size of it to zero. 121 122* Related to the contents of the buffer 123 124`strbuf_trim`:: 125 126 Strip whitespace from the beginning and end of a string. 127 Equivalent to performing `strbuf_rtrim()` followed by `strbuf_ltrim()`. 128 129`strbuf_rtrim`:: 130 131 Strip whitespace from the end of a string. 132 133`strbuf_ltrim`:: 134 135 Strip whitespace from the beginning of a string. 136 137`strbuf_cmp`:: 138 139 Compare two buffers. Returns an integer less than, equal to, or greater 140 than zero if the first buffer is found, respectively, to be less than, 141 to match, or be greater than the second buffer. 142 143* Adding data to the buffer 144 145NOTE: All of the functions in this section will grow the buffer as necessary. 146If they fail for some reason other than memory shortage and the buffer hadn't 147been allocated before (i.e. the `struct strbuf` was set to `STRBUF_INIT`), 148then they will free() it. 149 150`strbuf_addch`:: 151 152 Add a single character to the buffer. 153 154`strbuf_insert`:: 155 156 Insert data to the given position of the buffer. The remaining contents 157 will be shifted, not overwritten. 158 159`strbuf_remove`:: 160 161 Remove given amount of data from a given position of the buffer. 162 163`strbuf_splice`:: 164 165 Remove the bytes between `pos..pos+len` and replace it with the given 166 data. 167 168`strbuf_add_commented_lines`:: 169 170 Add a NUL-terminated string to the buffer. Each line will be prepended 171 by a comment character and a blank. 172 173`strbuf_add`:: 174 175 Add data of given length to the buffer. 176 177`strbuf_addstr`:: 178 179Add a NUL-terminated string to the buffer. 180+ 181NOTE: This function will *always* be implemented as an inline or a macro 182that expands to: 183+ 184---- 185strbuf_add(..., s, strlen(s)); 186---- 187+ 188Meaning that this is efficient to write things like: 189+ 190---- 191strbuf_addstr(sb, "immediate string"); 192---- 193 194`strbuf_addbuf`:: 195 196 Copy the contents of an other buffer at the end of the current one. 197 198`strbuf_adddup`:: 199 200 Copy part of the buffer from a given position till a given length to the 201 end of the buffer. 202 203`strbuf_expand`:: 204 205 This function can be used to expand a format string containing 206 placeholders. To that end, it parses the string and calls the specified 207 function for every percent sign found. 208+ 209The callback function is given a pointer to the character after the `%` 210and a pointer to the struct strbuf. It is expected to add the expanded 211version of the placeholder to the strbuf, e.g. to add a newline 212character if the letter `n` appears after a `%`. The function returns 213the length of the placeholder recognized and `strbuf_expand()` skips 214over it. 215+ 216The format `%%` is automatically expanded to a single `%` as a quoting 217mechanism; callers do not need to handle the `%` placeholder themselves, 218and the callback function will not be invoked for this placeholder. 219+ 220All other characters (non-percent and not skipped ones) are copied 221verbatim to the strbuf. If the callback returned zero, meaning that the 222placeholder is unknown, then the percent sign is copied, too. 223+ 224In order to facilitate caching and to make it possible to give 225parameters to the callback, `strbuf_expand()` passes a context pointer, 226which can be used by the programmer of the callback as she sees fit. 227 228`strbuf_expand_dict_cb`:: 229 230 Used as callback for `strbuf_expand()`, expects an array of 231 struct strbuf_expand_dict_entry as context, i.e. pairs of 232 placeholder and replacement string. The array needs to be 233 terminated by an entry with placeholder set to NULL. 234 235`strbuf_addbuf_percentquote`:: 236 237 Append the contents of one strbuf to another, quoting any 238 percent signs ("%") into double-percents ("%%") in the 239 destination. This is useful for literal data to be fed to either 240 strbuf_expand or to the *printf family of functions. 241 242`strbuf_humanise_bytes`:: 243 244 Append the given byte size as a human-readable string (i.e. 12.23 KiB, 245 3.50 MiB). 246 247`strbuf_addf`:: 248 249 Add a formatted string to the buffer. 250 251`strbuf_commented_addf`:: 252 253 Add a formatted string prepended by a comment character and a 254 blank to the buffer. 255 256`strbuf_fread`:: 257 258 Read a given size of data from a FILE* pointer to the buffer. 259+ 260NOTE: The buffer is rewound if the read fails. If -1 is returned, 261`errno` must be consulted, like you would do for `read(3)`. 262`strbuf_read()`, `strbuf_read_file()` and `strbuf_getline()` has the 263same behaviour as well. 264 265`strbuf_read`:: 266 267 Read the contents of a given file descriptor. The third argument can be 268 used to give a hint about the file size, to avoid reallocs. 269 270`strbuf_read_file`:: 271 272 Read the contents of a file, specified by its path. The third argument 273 can be used to give a hint about the file size, to avoid reallocs. 274 275`strbuf_readlink`:: 276 277 Read the target of a symbolic link, specified by its path. The third 278 argument can be used to give a hint about the size, to avoid reallocs. 279 280`strbuf_getline`:: 281 282 Read a line from a FILE *, overwriting the existing contents 283 of the strbuf. The second argument specifies the line 284 terminator character, typically `'\n'`. 285 Reading stops after the terminator or at EOF. The terminator 286 is removed from the buffer before returning. Returns 0 unless 287 there was nothing left before EOF, in which case it returns `EOF`. 288 289`strbuf_getwholeline`:: 290 291 Like `strbuf_getline`, but keeps the trailing terminator (if 292 any) in the buffer. 293 294`strbuf_getwholeline_fd`:: 295 296 Like `strbuf_getwholeline`, but operates on a file descriptor. 297 It reads one character at a time, so it is very slow. Do not 298 use it unless you need the correct position in the file 299 descriptor. 300 301`stripspace`:: 302 303 Strip whitespace from a buffer. The second parameter controls if 304 comments are considered contents to be removed or not. 305 306`strbuf_split_buf`:: 307`strbuf_split_str`:: 308`strbuf_split_max`:: 309`strbuf_split`:: 310 311 Split a string or strbuf into a list of strbufs at a specified 312 terminator character. The returned substrings include the 313 terminator characters. Some of these functions take a `max` 314 parameter, which, if positive, limits the output to that 315 number of substrings. 316 317`strbuf_list_free`:: 318 319 Free a list of strbufs (for example, the return values of the 320 `strbuf_split()` functions). 321 322`launch_editor`:: 323 324 Launch the user preferred editor to edit a file and fill the buffer 325 with the file's contents upon the user completing their editing. The 326 third argument can be used to set the environment which the editor is 327 run in. If the buffer is NULL the editor is launched as usual but the 328 file's contents are not read into the buffer upon completion.