strbuf.h: integrate api-strbuf.txt documentation

Some of strbuf is documented as comments above functions, and some is separate in Documentation/technical/api-strbuf.txt. This makes it annoying to find the appropriate documentation. We'd rather have it all in one place, which means all in the text document, or all in the header. Let's choose the header as that place. Even though the formatting is not quite as pretty, this keeps the documentation close to the related code. The hope is that this makes it easier to find what you want (human-readable comments are right next to the C declarations), and easier for writers to keep the documentation up to date. This is more or less a straight import of the text from api-strbuf.txt into C comments, complete with asciidoc formatting. The exceptions are: 1. All comments created in this way are started with "/**" to indicate they are part of the API documentation. This may help later with extracting the text to pretty-print it. 2. Function descriptions do not repeat the function name, as it is available in the context directly below. So: `strbuf_add`:: Add data of given length to the buffer. from api-strbuf.txt becomes: /** * Add data of given length to the buffer. */ void strbuf_add(struct strbuf *sb, const void *, size_t); As a result, any block-continuation required in asciidoc for that list item was dropped in favor of straight blank-line paragraph (since it is not necessary when we are not in a list item). 3. There is minor re-wording to integrate existing comments and api-strbuf text. In each case, I took whichever version was more descriptive, and eliminated any redundancies. In one case, for strbuf_addstr, the api documentation gave its inline definition; I eliminated this as redundant with the actual definition, which can be seen directly below the comment. 4. The functions in the header file are re-ordered to match the ordering of the API documentation, under the assumption that more thought went into the grouping there. Helped-by: Jonathan Nieder <jrnieder@gmail.com> Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Jan 16, 2015 at 04:04 UTC bdfdaa4978dd92992737e662f25adc01d32d0774
2 files changed +390 -418
Documentation/technical/api-strbuf.txt deleted
-351
@@ -1,351 +0,0 @@
1 -strbuf API
2 -==========
3 -
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.
9 -
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.
12 -
13 -strbufs have 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
16 -string operations safely. strbuf's _have_ to be initialized either by
17 -`strbuf_init()` or by `= STRBUF_INIT` before the invariants, though.
18 -+
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()`.
23 -+
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).
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 -+
32 -NOTE: It is OK to "play" with the buffer directly if you work it this
33 - way:
34 -+
35 -----
36 -strbuf_grow(sb, SOME_SIZE); <1>
37 -strbuf_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 -+
43 -NOTE: `SOME_OTHER_SIZE` must be smaller or equal to `strbuf_avail(sb)`.
44 -+
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.
47 -+
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()`
51 -instead.
52 -
53 -Data structures
54 ----------------
55 -
56 -* `struct strbuf`
57 -
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.
61 -
62 -Functions
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_reencode`::
138 -
139 - Replace the contents of the strbuf with a reencoded form. Returns -1
140 - on error, 0 on success.
141 -
142 -`strbuf_tolower`::
143 -
144 - Lowercase each character in the buffer using `tolower`.
145 -
146 -`strbuf_cmp`::
147 -
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.
151 -
152 -* Adding data to the buffer
153 -
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.
158 -
159 -`strbuf_addch`::
160 -
161 - Add a single character to the buffer.
162 -
163 -`strbuf_addchars`::
164 -
165 - Add a character the specified number of times to the buffer.
166 -
167 -`strbuf_insert`::
168 -
169 - Insert data to the given position of the buffer. The remaining contents
170 - will be shifted, not overwritten.
171 -
172 -`strbuf_remove`::
173 -
174 - Remove given amount of data from a given position of the buffer.
175 -
176 -`strbuf_splice`::
177 -
178 - Remove the bytes between `pos..pos+len` and replace it with the given
179 - data.
180 -
181 -`strbuf_add_commented_lines`::
182 -
183 - Add a NUL-terminated string to the buffer. Each line will be prepended
184 - by a comment character and a blank.
185 -
186 -`strbuf_add`::
187 -
188 - Add data of given length to the buffer.
189 -
190 -`strbuf_addstr`::
191 -
192 -Add a NUL-terminated string to the buffer.
193 -+
194 -NOTE: This function will *always* be implemented as an inline or a macro
195 -that expands to:
196 -+
197 -----
198 -strbuf_add(..., s, strlen(s));
199 -----
200 -+
201 -Meaning that this is efficient to write things like:
202 -+
203 -----
204 -strbuf_addstr(sb, "immediate string");
205 -----
206 -
207 -`strbuf_addbuf`::
208 -
209 - Copy the contents of another buffer at the end of the current one.
210 -
211 -`strbuf_adddup`::
212 -
213 - Copy part of the buffer from a given position till a given length to the
214 - end of the buffer.
215 -
216 -`strbuf_expand`::
217 -
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.
221 -+
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
227 -over it.
228 -+
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.
232 -+
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.
236 -+
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.
240 -
241 -`strbuf_expand_dict_cb`::
242 -
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.
247 -
248 -`strbuf_addbuf_percentquote`::
249 -
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.
254 -
255 -`strbuf_humanise_bytes`::
256 -
257 - Append the given byte size as a human-readable string (i.e. 12.23 KiB,
258 - 3.50 MiB).
259 -
260 -`strbuf_addf`::
261 -
262 - Add a formatted string to the buffer.
263 -
264 -`strbuf_commented_addf`::
265 -
266 - Add a formatted string prepended by a comment character and a
267 - blank to the buffer.
268 -
269 -`strbuf_fread`::
270 -
271 - Read a given size of data from a FILE* pointer to the buffer.
272 -+
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.
277 -
278 -`strbuf_read`::
279 -
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.
282 -
283 -`strbuf_read_file`::
284 -
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.
287 -
288 -`strbuf_readlink`::
289 -
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.
292 -
293 -`strbuf_getline`::
294 -
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`.
301 -
302 -`strbuf_getwholeline`::
303 -
304 - Like `strbuf_getline`, but keeps the trailing terminator (if
305 - any) in the buffer.
306 -
307 -`strbuf_getwholeline_fd`::
308 -
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
312 - descriptor.
313 -
314 -`strbuf_getcwd`::
315 -
316 - Set the buffer to the path of the current working directory.
317 -
318 -`strbuf_add_absolute_path`
319 -
320 - Add a path to a buffer, converting a relative path to an
321 - absolute one in the process. Symbolic links are not
322 - resolved.
323 -
324 -`stripspace`::
325 -
326 - Strip whitespace from a buffer. The second parameter controls if
327 - comments are considered contents to be removed or not.
328 -
329 -`strbuf_split_buf`::
330 -`strbuf_split_str`::
331 -`strbuf_split_max`::
332 -`strbuf_split`::
333 -
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.
339 -
340 -`strbuf_list_free`::
341 -
342 - Free a list of strbufs (for example, the return values of the
343 - `strbuf_split()` functions).
344 -
345 -`launch_editor`::
346 -
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.
strbuf.h
+390 -67
@@ -1,22 +1,117 @@
1 #ifndef STRBUF_H
2 #define STRBUF_H
3
4 -/* See Documentation/technical/api-strbuf.txt */
4 +/**
5 + * strbuf's are meant to be used with all the usual C string and memory
6 + * APIs. Given that the length of the buffer is known, it's often better to
7 + * use the mem* functions than a str* one (memchr vs. strchr e.g.).
8 + * Though, one has to be careful about the fact that str* functions often
9 + * stop on NULs and that strbufs may have embedded NULs.
10 + *
11 + * A strbuf is NUL terminated for convenience, but no function in the
12 + * strbuf API actually relies on the string being free of NULs.
13 + *
14 + * strbufs have some invariants that are very important to keep in mind:
15 + *
16 + * . The `buf` member is never NULL, so it can be used in any usual C
17 + * string operations safely. strbuf's _have_ to be initialized either by
18 + * `strbuf_init()` or by `= STRBUF_INIT` before the invariants, though.
19 + * +
20 + * Do *not* assume anything on what `buf` really is (e.g. if it is
21 + * allocated memory or not), use `strbuf_detach()` to unwrap a memory
22 + * buffer from its strbuf shell in a safe way. That is the sole supported
23 + * way. This will give you a malloced buffer that you can later `free()`.
24 + * +
25 + * However, it is totally safe to modify anything in the string pointed by
26 + * the `buf` member, between the indices `0` and `len-1` (inclusive).
27 + *
28 + * . The `buf` member is a byte array that has at least `len + 1` bytes
29 + * allocated. The extra byte is used to store a `'\0'`, allowing the
30 + * `buf` member to be a valid C-string. Every strbuf function ensure this
31 + * invariant is preserved.
32 + * +
33 + * NOTE: It is OK to "play" with the buffer directly if you work it this
34 + * way:
35 + * +
36 + * ----
37 + * strbuf_grow(sb, SOME_SIZE); <1>
38 + * strbuf_setlen(sb, sb->len + SOME_OTHER_SIZE);
39 + * ----
40 + * <1> Here, the memory array starting at `sb->buf`, and of length
41 + * `strbuf_avail(sb)` is all yours, and you can be sure that
42 + * `strbuf_avail(sb)` is at least `SOME_SIZE`.
43 + * +
44 + * NOTE: `SOME_OTHER_SIZE` must be smaller or equal to `strbuf_avail(sb)`.
45 + * +
46 + * Doing so is safe, though if it has to be done in many places, adding the
47 + * missing API to the strbuf module is the way to go.
48 + * +
49 + * WARNING: Do _not_ assume that the area that is yours is of size `alloc
50 + * - 1` even if it's true in the current implementation. Alloc is somehow a
51 + * "private" member that should not be messed with. Use `strbuf_avail()`
52 + * instead.
53 + */
54
6 -extern char strbuf_slopbuf[];
55 +/**
56 + * Data Structures
57 + * ---------------
58 + */
59 +
60 +/**
61 + * This is the string buffer structure. The `len` member can be used to
62 + * determine the current length of the string, and `buf` member provides
63 + * access to the string itself.
64 + */
65 struct strbuf {
66 size_t alloc;
67 size_t len;
68 char *buf;
69 };
70
71 +extern char strbuf_slopbuf[];
72 #define STRBUF_INIT { 0, 0, strbuf_slopbuf }
73
15 -/*----- strbuf life cycle -----*/
74 +/**
75 + * Functions
76 + * ---------
77 + */
78 +
79 +/**
80 + * * Life Cycle
81 + */
82 +
83 +/**
84 + * Initialize the structure. The second parameter can be zero or a bigger
85 + * number to allocate memory, in case you want to prevent further reallocs.
86 + */
87 extern void strbuf_init(struct strbuf *, size_t);
88 +
89 +/**
90 + * Release a string buffer and the memory it used. You should not use the
91 + * string buffer after using this function, unless you initialize it again.
92 + */
93 extern void strbuf_release(struct strbuf *);
94 +
95 +/**
96 + * Detach the string from the strbuf and returns it; you now own the
97 + * storage the string occupies and it is your responsibility from then on
98 + * to release it with `free(3)` when you are done with it.
99 + */
100 extern char *strbuf_detach(struct strbuf *, size_t *);
101 +
102 +/**
103 + * Attach a string to a buffer. You should specify the string to attach,
104 + * the current length of the string and the amount of allocated memory.
105 + * The amount must be larger than the string length, because the string you
106 + * pass is supposed to be a NUL-terminated string. This string _must_ be
107 + * malloc()ed, and after attaching, the pointer cannot be relied upon
108 + * anymore, and neither be free()d directly.
109 + */
110 extern void strbuf_attach(struct strbuf *, void *, size_t, size_t);
111 +
112 +/**
113 + * Swap the contents of two string buffers.
114 + */
115 static inline void strbuf_swap(struct strbuf *a, struct strbuf *b)
116 {
117 struct strbuf tmp = *a;
@@ -24,14 +119,35 @@ static inline void strbuf_swap(struct strbuf *a, struct strbuf *b)
119 *b = tmp;
120 }
121
27 -/*----- strbuf size related -----*/
122 +
123 +/**
124 + * * Related to the size of the buffer
125 + */
126 +
127 +/**
128 + * Determine the amount of allocated but unused memory.
129 + */
130 static inline size_t strbuf_avail(const struct strbuf *sb)
131 {
132 return sb->alloc ? sb->alloc - sb->len - 1 : 0;
133 }
134
135 +/**
136 + * Ensure that at least this amount of unused memory is available after
137 + * `len`. This is used when you know a typical size for what you will add
138 + * and want to avoid repetitive automatic resizing of the underlying buffer.
139 + * This is never a needed operation, but can be critical for performance in
140 + * some cases.
141 + */
142 extern void strbuf_grow(struct strbuf *, size_t);
143
144 +/**
145 + * Set the length of the buffer to a given value. This function does *not*
146 + * allocate new memory, so you should not perform a `strbuf_setlen()` to a
147 + * length that is larger than `len + strbuf_avail()`. `strbuf_setlen()` is
148 + * just meant as a 'please fix invariants from this strbuf I just messed
149 + * with'.
150 + */
151 static inline void strbuf_setlen(struct strbuf *sb, size_t len)
152 {
153 if (len > (sb->alloc ? sb->alloc - 1 : 0))
@@ -39,16 +155,277 @@ static inline void strbuf_setlen(struct strbuf *sb, size_t len)
155 sb->len = len;
156 sb->buf[len] = '\0';
157 }
158 +
159 +/**
160 + * Empty the buffer by setting the size of it to zero.
161 + */
162 #define strbuf_reset(sb) strbuf_setlen(sb, 0)
163
44 -/*----- content related -----*/
164 +
165 +/**
166 + * * Related to the contents of the buffer
167 + */
168 +
169 +/**
170 + * Strip whitespace from the beginning and end of a string.
171 + * Equivalent to performing `strbuf_rtrim()` followed by `strbuf_ltrim()`.
172 + */
173 extern void strbuf_trim(struct strbuf *);
174 +
175 +/**
176 + * Strip whitespace from the end of a string.
177 + */
178 extern void strbuf_rtrim(struct strbuf *);
179 +
180 +/**
181 + * Strip whitespace from the beginning of a string.
182 + */
183 extern void strbuf_ltrim(struct strbuf *);
184 +
185 +/**
186 + * Replace the contents of the strbuf with a reencoded form. Returns -1
187 + * on error, 0 on success.
188 + */
189 extern int strbuf_reencode(struct strbuf *sb, const char *from, const char *to);
190 +
191 +/**
192 + * Lowercase each character in the buffer using `tolower`.
193 + */
194 extern void strbuf_tolower(struct strbuf *sb);
195 +
196 +/**
197 + * Compare two buffers. Returns an integer less than, equal to, or greater
198 + * than zero if the first buffer is found, respectively, to be less than,
199 + * to match, or be greater than the second buffer.
200 + */
201 extern int strbuf_cmp(const struct strbuf *, const struct strbuf *);
202
203 +
204 +/**
205 + * * Adding data to the buffer
206 + *
207 + * NOTE: All of the functions in this section will grow the buffer as
208 + * necessary. If they fail for some reason other than memory shortage and the
209 + * buffer hadn't been allocated before (i.e. the `struct strbuf` was set to
210 + * `STRBUF_INIT`), then they will free() it.
211 + */
212 +
213 +/**
214 + * Add a single character to the buffer.
215 + */
216 +static inline void strbuf_addch(struct strbuf *sb, int c)
217 +{
218 + strbuf_grow(sb, 1);
219 + sb->buf[sb->len++] = c;
220 + sb->buf[sb->len] = '\0';
221 +}
222 +
223 +/**
224 + * Add a character the specified number of times to the buffer.
225 + */
226 +extern void strbuf_addchars(struct strbuf *sb, int c, size_t n);
227 +
228 +/**
229 + * Insert data to the given position of the buffer. The remaining contents
230 + * will be shifted, not overwritten.
231 + */
232 +extern void strbuf_insert(struct strbuf *, size_t pos, const void *, size_t);
233 +
234 +/**
235 + * Remove given amount of data from a given position of the buffer.
236 + */
237 +extern void strbuf_remove(struct strbuf *, size_t pos, size_t len);
238 +
239 +/**
240 + * Remove the bytes between `pos..pos+len` and replace it with the given
241 + * data.
242 + */
243 +extern void strbuf_splice(struct strbuf *, size_t pos, size_t len,
244 + const void *, size_t);
245 +
246 +/**
247 + * Add a NUL-terminated string to the buffer. Each line will be prepended
248 + * by a comment character and a blank.
249 + */
250 +extern void strbuf_add_commented_lines(struct strbuf *out, const char *buf, size_t size);
251 +
252 +
253 +/**
254 + * Add data of given length to the buffer.
255 + */
256 +extern void strbuf_add(struct strbuf *, const void *, size_t);
257 +
258 +/**
259 + * Add a NUL-terminated string to the buffer.
260 + *
261 + * NOTE: This function will *always* be implemented as an inline or a macro
262 + * using strlen, meaning that this is efficient to write things like:
263 + *
264 + * ----
265 + * strbuf_addstr(sb, "immediate string");
266 + * ----
267 + *
268 + */
269 +static inline void strbuf_addstr(struct strbuf *sb, const char *s)
270 +{
271 + strbuf_add(sb, s, strlen(s));
272 +}
273 +
274 +/**
275 + * Copy the contents of another buffer at the end of the current one.
276 + */
277 +static inline void strbuf_addbuf(struct strbuf *sb, const struct strbuf *sb2)
278 +{
279 + strbuf_grow(sb, sb2->len);
280 + strbuf_add(sb, sb2->buf, sb2->len);
281 +}
282 +
283 +/**
284 + * Copy part of the buffer from a given position till a given length to the
285 + * end of the buffer.
286 + */
287 +extern void strbuf_adddup(struct strbuf *sb, size_t pos, size_t len);
288 +
289 +/**
290 + * This function can be used to expand a format string containing
291 + * placeholders. To that end, it parses the string and calls the specified
292 + * function for every percent sign found.
293 + *
294 + * The callback function is given a pointer to the character after the `%`
295 + * and a pointer to the struct strbuf. It is expected to add the expanded
296 + * version of the placeholder to the strbuf, e.g. to add a newline
297 + * character if the letter `n` appears after a `%`. The function returns
298 + * the length of the placeholder recognized and `strbuf_expand()` skips
299 + * over it.
300 + *
301 + * The format `%%` is automatically expanded to a single `%` as a quoting
302 + * mechanism; callers do not need to handle the `%` placeholder themselves,
303 + * and the callback function will not be invoked for this placeholder.
304 + *
305 + * All other characters (non-percent and not skipped ones) are copied
306 + * verbatim to the strbuf. If the callback returned zero, meaning that the
307 + * placeholder is unknown, then the percent sign is copied, too.
308 + *
309 + * In order to facilitate caching and to make it possible to give
310 + * parameters to the callback, `strbuf_expand()` passes a context pointer,
311 + * which can be used by the programmer of the callback as she sees fit.
312 + */
313 +typedef size_t (*expand_fn_t) (struct strbuf *sb, const char *placeholder, void *context);
314 +extern void strbuf_expand(struct strbuf *sb, const char *format, expand_fn_t fn, void *context);
315 +
316 +/**
317 + * Used as callback for `strbuf_expand()`, expects an array of
318 + * struct strbuf_expand_dict_entry as context, i.e. pairs of
319 + * placeholder and replacement string. The array needs to be
320 + * terminated by an entry with placeholder set to NULL.
321 + */
322 +struct strbuf_expand_dict_entry {
323 + const char *placeholder;
324 + const char *value;
325 +};
326 +extern size_t strbuf_expand_dict_cb(struct strbuf *sb, const char *placeholder, void *context);
327 +
328 +/**
329 + * Append the contents of one strbuf to another, quoting any
330 + * percent signs ("%") into double-percents ("%%") in the
331 + * destination. This is useful for literal data to be fed to either
332 + * strbuf_expand or to the *printf family of functions.
333 + */
334 +extern void strbuf_addbuf_percentquote(struct strbuf *dst, const struct strbuf *src);
335 +
336 +/**
337 + * Append the given byte size as a human-readable string (i.e. 12.23 KiB,
338 + * 3.50 MiB).
339 + */
340 +extern void strbuf_humanise_bytes(struct strbuf *buf, off_t bytes);
341 +
342 +/**
343 + * Add a formatted string to the buffer.
344 + */
345 +__attribute__((format (printf,2,3)))
346 +extern void strbuf_addf(struct strbuf *sb, const char *fmt, ...);
347 +
348 +/**
349 + * Add a formatted string prepended by a comment character and a
350 + * blank to the buffer.
351 + */
352 +__attribute__((format (printf, 2, 3)))
353 +extern void strbuf_commented_addf(struct strbuf *sb, const char *fmt, ...);
354 +
355 +__attribute__((format (printf,2,0)))
356 +extern void strbuf_vaddf(struct strbuf *sb, const char *fmt, va_list ap);
357 +
358 +/**
359 + * Read a given size of data from a FILE* pointer to the buffer.
360 + *
361 + * NOTE: The buffer is rewound if the read fails. If -1 is returned,
362 + * `errno` must be consulted, like you would do for `read(3)`.
363 + * `strbuf_read()`, `strbuf_read_file()` and `strbuf_getline()` has the
364 + * same behaviour as well.
365 + */
366 +extern size_t strbuf_fread(struct strbuf *, size_t, FILE *);
367 +
368 +/**
369 + * Read the contents of a given file descriptor. The third argument can be
370 + * used to give a hint about the file size, to avoid reallocs. If read fails,
371 + * any partial read is undone.
372 + */
373 +extern ssize_t strbuf_read(struct strbuf *, int fd, size_t hint);
374 +
375 +/**
376 + * Read the contents of a file, specified by its path. The third argument
377 + * can be used to give a hint about the file size, to avoid reallocs.
378 + */
379 +extern int strbuf_read_file(struct strbuf *sb, const char *path, size_t hint);
380 +
381 +/**
382 + * Read the target of a symbolic link, specified by its path. The third
383 + * argument can be used to give a hint about the size, to avoid reallocs.
384 + */
385 +extern int strbuf_readlink(struct strbuf *sb, const char *path, size_t hint);
386 +
387 +/**
388 + * Read a line from a FILE *, overwriting the existing contents
389 + * of the strbuf. The second argument specifies the line
390 + * terminator character, typically `'\n'`.
391 + * Reading stops after the terminator or at EOF. The terminator
392 + * is removed from the buffer before returning. Returns 0 unless
393 + * there was nothing left before EOF, in which case it returns `EOF`.
394 + */
395 +extern int strbuf_getline(struct strbuf *, FILE *, int);
396 +
397 +/**
398 + * Like `strbuf_getline`, but keeps the trailing terminator (if
399 + * any) in the buffer.
400 + */
401 +extern int strbuf_getwholeline(struct strbuf *, FILE *, int);
402 +
403 +/**
404 + * Like `strbuf_getwholeline`, but operates on a file descriptor.
405 + * It reads one character at a time, so it is very slow. Do not
406 + * use it unless you need the correct position in the file
407 + * descriptor.
408 + */
409 +extern int strbuf_getwholeline_fd(struct strbuf *, int, int);
410 +
411 +/**
412 + * Set the buffer to the path of the current working directory.
413 + */
414 +extern int strbuf_getcwd(struct strbuf *sb);
415 +
416 +/**
417 + * Add a path to a buffer, converting a relative path to an
418 + * absolute one in the process. Symbolic links are not
419 + * resolved.
420 + */
421 +extern void strbuf_add_absolute_path(struct strbuf *sb, const char *path);
422 +
423 +/**
424 + * Strip whitespace from a buffer. The second parameter controls if
425 + * comments are considered contents to be removed or not.
426 + */
427 +extern void stripspace(struct strbuf *buf, int skip_comments);
428 +
429 static inline int strbuf_strip_suffix(struct strbuf *sb, const char *suffix)
430 {
431 if (strip_suffix_mem(sb->buf, &sb->len, suffix)) {
@@ -110,51 +487,14 @@ static inline struct strbuf **strbuf_split(const struct strbuf *sb,
487 */
488 extern void strbuf_list_free(struct strbuf **);
489
113 -/*----- add data in your buffer -----*/
114 -static inline void strbuf_addch(struct strbuf *sb, int c)
115 -{
116 - strbuf_grow(sb, 1);
117 - sb->buf[sb->len++] = c;
118 - sb->buf[sb->len] = '\0';
119 -}
120 -
121 -extern void strbuf_insert(struct strbuf *, size_t pos, const void *, size_t);
122 -extern void strbuf_remove(struct strbuf *, size_t pos, size_t len);
123 -
124 -/* splice pos..pos+len with given data */
125 -extern void strbuf_splice(struct strbuf *, size_t pos, size_t len,
126 - const void *, size_t);
127 -
128 -extern void strbuf_add_commented_lines(struct strbuf *out, const char *buf, size_t size);
129 -
130 -extern void strbuf_add(struct strbuf *, const void *, size_t);
131 -static inline void strbuf_addstr(struct strbuf *sb, const char *s)
132 -{
133 - strbuf_add(sb, s, strlen(s));
134 -}
135 -static inline void strbuf_addbuf(struct strbuf *sb, const struct strbuf *sb2)
136 -{
137 - strbuf_grow(sb, sb2->len);
138 - strbuf_add(sb, sb2->buf, sb2->len);
139 -}
140 -extern void strbuf_adddup(struct strbuf *sb, size_t pos, size_t len);
141 -extern void strbuf_addchars(struct strbuf *sb, int c, size_t n);
142 -
143 -typedef size_t (*expand_fn_t) (struct strbuf *sb, const char *placeholder, void *context);
144 -extern void strbuf_expand(struct strbuf *sb, const char *format, expand_fn_t fn, void *context);
145 -struct strbuf_expand_dict_entry {
146 - const char *placeholder;
147 - const char *value;
148 -};
149 -extern size_t strbuf_expand_dict_cb(struct strbuf *sb, const char *placeholder, void *context);
150 -extern void strbuf_addbuf_percentquote(struct strbuf *dst, const struct strbuf *src);
151 -
152 -__attribute__((format (printf,2,3)))
153 -extern void strbuf_addf(struct strbuf *sb, const char *fmt, ...);
154 -__attribute__((format (printf, 2, 3)))
155 -extern void strbuf_commented_addf(struct strbuf *sb, const char *fmt, ...);
156 -__attribute__((format (printf,2,0)))
157 -extern void strbuf_vaddf(struct strbuf *sb, const char *fmt, va_list ap);
490 +/**
491 + * Launch the user preferred editor to edit a file and fill the buffer
492 + * with the file's contents upon the user completing their editing. The
493 + * third argument can be used to set the environment which the editor is
494 + * run in. If the buffer is NULL the editor is launched as usual but the
495 + * file's contents are not read into the buffer upon completion.
496 + */
497 +extern int launch_editor(const char *path, struct strbuf *buffer, const char *const *env);
498
499 extern void strbuf_add_lines(struct strbuf *sb, const char *prefix, const char *buf, size_t size);
500
@@ -170,28 +510,11 @@ static inline void strbuf_complete_line(struct strbuf *sb)
510 strbuf_addch(sb, '\n');
511 }
512
173 -extern size_t strbuf_fread(struct strbuf *, size_t, FILE *);
174 -/* XXX: if read fails, any partial read is undone */
175 -extern ssize_t strbuf_read(struct strbuf *, int fd, size_t hint);
176 -extern int strbuf_read_file(struct strbuf *sb, const char *path, size_t hint);
177 -extern int strbuf_readlink(struct strbuf *sb, const char *path, size_t hint);
178 -extern int strbuf_getcwd(struct strbuf *sb);
179 -
180 -extern int strbuf_getwholeline(struct strbuf *, FILE *, int);
181 -extern int strbuf_getline(struct strbuf *, FILE *, int);
182 -extern int strbuf_getwholeline_fd(struct strbuf *, int, int);
183 -
184 -extern void stripspace(struct strbuf *buf, int skip_comments);
185 -extern int launch_editor(const char *path, struct strbuf *buffer, const char *const *env);
186 -
513 extern int strbuf_branchname(struct strbuf *sb, const char *name);
514 extern int strbuf_check_branch_ref(struct strbuf *sb, const char *name);
515
516 extern void strbuf_addstr_urlencode(struct strbuf *, const char *,
517 int reserved);
192 -extern void strbuf_humanise_bytes(struct strbuf *buf, off_t bytes);
193 -
194 -extern void strbuf_add_absolute_path(struct strbuf *sb, const char *path);
518
519 __attribute__((format (printf,1,2)))
520 extern int printf_ln(const char *fmt, ...);