strbuf.h: format according to coding guidelines

The previous patch suggested the strbuf header to be the leading example of how we would want our APIs to be documented. This may lead to some scrutiny of that code and the coding style (which is different from the API documentation style) and hence might be taken as an example on how to format code as well. So let's format strbuf.h in a way that we'd like to see: * omit the extern keyword from function declarations * name all parameters (usually the parameters are obvious from its type, but consider exceptions like `int strbuf_getwholeline_fd(struct strbuf *, int, int);` * break overly long lines Signed-off-by: Stefan Beller <sbeller@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Stefan Beller committed Sep 28, 2018 at 10:30 UTC c7e5fe79b985dbe9a9bfaca5a097a4d53ab77437
1 file changed +81 -67
strbuf.h
+81 -67
@@ -87,7 +87,7 @@ struct object_id;
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 */
90 -extern void strbuf_init(struct strbuf *, size_t);
90 +void strbuf_init(struct strbuf *sb, size_t alloc);
91
92 /**
93 * Release a string buffer and the memory it used. After this call, the
@@ -97,7 +97,7 @@ extern void strbuf_init(struct strbuf *, size_t);
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.
99 */
100 -extern void strbuf_release(struct strbuf *);
100 +void strbuf_release(struct strbuf *sb);
101
102 /**
103 * Detach the string from the strbuf and returns it; you now own the
@@ -107,7 +107,7 @@ extern void strbuf_release(struct strbuf *);
107 * The strbuf that previously held the string is reset to `STRBUF_INIT` so
108 * it can be reused after calling this function.
109 */
110 -extern char *strbuf_detach(struct strbuf *, size_t *);
110 +char *strbuf_detach(struct strbuf *sb, size_t *sz);
111
112 /**
113 * Attach a string to a buffer. You should specify the string to attach,
@@ -117,7 +117,7 @@ extern char *strbuf_detach(struct strbuf *, size_t *);
117 * malloc()ed, and after attaching, the pointer cannot be relied upon
118 * anymore, and neither be free()d directly.
119 */
120 -extern void strbuf_attach(struct strbuf *, void *, size_t, size_t);
120 +void strbuf_attach(struct strbuf *sb, void *str, size_t len, size_t mem);
121
122 /**
123 * Swap the contents of two string buffers.
@@ -148,7 +148,7 @@ static inline size_t strbuf_avail(const struct strbuf *sb)
148 * This is never a needed operation, but can be critical for performance in
149 * some cases.
150 */
151 -extern void strbuf_grow(struct strbuf *, size_t);
151 +void strbuf_grow(struct strbuf *sb, size_t amount);
152
153 /**
154 * Set the length of the buffer to a given value. This function does *not*
@@ -183,30 +183,30 @@ static inline void strbuf_setlen(struct strbuf *sb, size_t len)
183 * Strip whitespace from the beginning (`ltrim`), end (`rtrim`), or both side
184 * (`trim`) of a string.
185 */
186 -extern void strbuf_trim(struct strbuf *);
187 -extern void strbuf_rtrim(struct strbuf *);
188 -extern void strbuf_ltrim(struct strbuf *);
186 +void strbuf_trim(struct strbuf *sb);
187 +void strbuf_rtrim(struct strbuf *sb);
188 +void strbuf_ltrim(struct strbuf *sb);
189
190 /* Strip trailing directory separators */
191 -extern void strbuf_trim_trailing_dir_sep(struct strbuf *);
191 +void strbuf_trim_trailing_dir_sep(struct strbuf *sb);
192
193 /**
194 * Replace the contents of the strbuf with a reencoded form. Returns -1
195 * on error, 0 on success.
196 */
197 -extern int strbuf_reencode(struct strbuf *sb, const char *from, const char *to);
197 +int strbuf_reencode(struct strbuf *sb, const char *from, const char *to);
198
199 /**
200 * Lowercase each character in the buffer using `tolower`.
201 */
202 -extern void strbuf_tolower(struct strbuf *sb);
202 +void strbuf_tolower(struct strbuf *sb);
203
204 /**
205 * Compare two buffers. Returns an integer less than, equal to, or greater
206 * than zero if the first buffer is found, respectively, to be less than,
207 * to match, or be greater than the second buffer.
208 */
209 -extern int strbuf_cmp(const struct strbuf *, const struct strbuf *);
209 +int strbuf_cmp(const struct strbuf *first, const struct strbuf *second);
210
211
212 /**
@@ -233,37 +233,38 @@ static inline void strbuf_addch(struct strbuf *sb, int c)
233 /**
234 * Add a character the specified number of times to the buffer.
235 */
236 -extern void strbuf_addchars(struct strbuf *sb, int c, size_t n);
236 +void strbuf_addchars(struct strbuf *sb, int c, size_t n);
237
238 /**
239 * Insert data to the given position of the buffer. The remaining contents
240 * will be shifted, not overwritten.
241 */
242 -extern void strbuf_insert(struct strbuf *, size_t pos, const void *, size_t);
242 +void strbuf_insert(struct strbuf *sb, size_t pos, const void *, size_t);
243
244 /**
245 * Remove given amount of data from a given position of the buffer.
246 */
247 -extern void strbuf_remove(struct strbuf *, size_t pos, size_t len);
247 +void strbuf_remove(struct strbuf *sb, size_t pos, size_t len);
248
249 /**
250 * Remove the bytes between `pos..pos+len` and replace it with the given
251 * data.
252 */
253 -extern void strbuf_splice(struct strbuf *, size_t pos, size_t len,
254 - const void *, size_t);
253 +void strbuf_splice(struct strbuf *sb, size_t pos, size_t len,
254 + const void *data, size_t data_len);
255
256 /**
257 * Add a NUL-terminated string to the buffer. Each line will be prepended
258 * by a comment character and a blank.
259 */
260 -extern void strbuf_add_commented_lines(struct strbuf *out, const char *buf, size_t size);
260 +void strbuf_add_commented_lines(struct strbuf *out,
261 + const char *buf, size_t size);
262
263
264 /**
265 * Add data of given length to the buffer.
266 */
266 -extern void strbuf_add(struct strbuf *, const void *, size_t);
267 +void strbuf_add(struct strbuf *sb, const void *data, size_t len);
268
269 /**
270 * Add a NUL-terminated string to the buffer.
@@ -282,7 +283,7 @@ static inline void strbuf_addstr(struct strbuf *sb, const char *s)
283 /**
284 * Copy the contents of another buffer at the end of the current one.
285 */
285 -extern void strbuf_addbuf(struct strbuf *sb, const struct strbuf *sb2);
286 +void strbuf_addbuf(struct strbuf *sb, const struct strbuf *sb2);
287
288 /**
289 * This function can be used to expand a format string containing
@@ -308,8 +309,13 @@ extern void strbuf_addbuf(struct strbuf *sb, const struct strbuf *sb2);
309 * parameters to the callback, `strbuf_expand()` passes a context pointer,
310 * which can be used by the programmer of the callback as she sees fit.
311 */
311 -typedef size_t (*expand_fn_t) (struct strbuf *sb, const char *placeholder, void *context);
312 -extern void strbuf_expand(struct strbuf *sb, const char *format, expand_fn_t fn, void *context);
312 +typedef size_t (*expand_fn_t) (struct strbuf *sb,
313 + const char *placeholder,
314 + void *context);
315 +void strbuf_expand(struct strbuf *sb,
316 + const char *format,
317 + expand_fn_t fn,
318 + void *context);
319
320 /**
321 * Used as callback for `strbuf_expand()`, expects an array of
@@ -321,7 +327,9 @@ struct strbuf_expand_dict_entry {
327 const char *placeholder;
328 const char *value;
329 };
324 -extern size_t strbuf_expand_dict_cb(struct strbuf *sb, const char *placeholder, void *context);
330 +size_t strbuf_expand_dict_cb(struct strbuf *sb,
331 + const char *placeholder,
332 + void *context);
333
334 /**
335 * Append the contents of one strbuf to another, quoting any
@@ -329,29 +337,29 @@ extern size_t strbuf_expand_dict_cb(struct strbuf *sb, const char *placeholder,
337 * destination. This is useful for literal data to be fed to either
338 * strbuf_expand or to the *printf family of functions.
339 */
332 -extern void strbuf_addbuf_percentquote(struct strbuf *dst, const struct strbuf *src);
340 +void strbuf_addbuf_percentquote(struct strbuf *dst, const struct strbuf *src);
341
342 /**
343 * Append the given byte size as a human-readable string (i.e. 12.23 KiB,
344 * 3.50 MiB).
345 */
338 -extern void strbuf_humanise_bytes(struct strbuf *buf, off_t bytes);
346 +void strbuf_humanise_bytes(struct strbuf *buf, off_t bytes);
347
348 /**
349 * Add a formatted string to the buffer.
350 */
351 __attribute__((format (printf,2,3)))
344 -extern void strbuf_addf(struct strbuf *sb, const char *fmt, ...);
352 +void strbuf_addf(struct strbuf *sb, const char *fmt, ...);
353
354 /**
355 * Add a formatted string prepended by a comment character and a
356 * blank to the buffer.
357 */
358 __attribute__((format (printf, 2, 3)))
351 -extern void strbuf_commented_addf(struct strbuf *sb, const char *fmt, ...);
359 +void strbuf_commented_addf(struct strbuf *sb, const char *fmt, ...);
360
361 __attribute__((format (printf,2,0)))
354 -extern void strbuf_vaddf(struct strbuf *sb, const char *fmt, va_list ap);
362 +void strbuf_vaddf(struct strbuf *sb, const char *fmt, va_list ap);
363
364 /**
365 * Add the time specified by `tm`, as formatted by `strftime`.
@@ -361,9 +369,9 @@ extern void strbuf_vaddf(struct strbuf *sb, const char *fmt, va_list ap);
369 * `suppress_tz_name`, when set, expands %Z internally to the empty
370 * string rather than passing it to `strftime`.
371 */
364 -extern void strbuf_addftime(struct strbuf *sb, const char *fmt,
365 - const struct tm *tm, int tz_offset,
366 - int suppress_tz_name);
372 +void strbuf_addftime(struct strbuf *sb, const char *fmt,
373 + const struct tm *tm, int tz_offset,
374 + int suppress_tz_name);
375
376 /**
377 * Read a given size of data from a FILE* pointer to the buffer.
@@ -373,14 +381,14 @@ extern void strbuf_addftime(struct strbuf *sb, const char *fmt,
381 * `strbuf_read()`, `strbuf_read_file()` and `strbuf_getline_*()`
382 * family of functions have the same behaviour as well.
383 */
376 -extern size_t strbuf_fread(struct strbuf *, size_t, FILE *);
384 +size_t strbuf_fread(struct strbuf *sb, size_t size, FILE *file);
385
386 /**
387 * Read the contents of a given file descriptor. The third argument can be
388 * used to give a hint about the file size, to avoid reallocs. If read fails,
389 * any partial read is undone.
390 */
383 -extern ssize_t strbuf_read(struct strbuf *, int fd, size_t hint);
391 +ssize_t strbuf_read(struct strbuf *sb, int fd, size_t hint);
392
393 /**
394 * Read the contents of a given file descriptor partially by using only one
@@ -388,7 +396,7 @@ extern ssize_t strbuf_read(struct strbuf *, int fd, size_t hint);
396 * file size, to avoid reallocs. Returns the number of new bytes appended to
397 * the sb.
398 */
391 -extern ssize_t strbuf_read_once(struct strbuf *, int fd, size_t hint);
399 +ssize_t strbuf_read_once(struct strbuf *sb, int fd, size_t hint);
400
401 /**
402 * Read the contents of a file, specified by its path. The third argument
@@ -396,19 +404,19 @@ extern ssize_t strbuf_read_once(struct strbuf *, int fd, size_t hint);
404 * Return the number of bytes read or a negative value if some error
405 * occurred while opening or reading the file.
406 */
399 -extern ssize_t strbuf_read_file(struct strbuf *sb, const char *path, size_t hint);
407 +ssize_t strbuf_read_file(struct strbuf *sb, const char *path, size_t hint);
408
409 /**
410 * Read the target of a symbolic link, specified by its path. The third
411 * argument can be used to give a hint about the size, to avoid reallocs.
412 */
405 -extern int strbuf_readlink(struct strbuf *sb, const char *path, size_t hint);
413 +int strbuf_readlink(struct strbuf *sb, const char *path, size_t hint);
414
415 /**
416 * Write the whole content of the strbuf to the stream not stopping at
417 * NUL bytes.
418 */
411 -extern ssize_t strbuf_write(struct strbuf *sb, FILE *stream);
419 +ssize_t strbuf_write(struct strbuf *sb, FILE *stream);
420
421 /**
422 * Read a line from a FILE *, overwriting the existing contents of
@@ -422,10 +430,10 @@ extern ssize_t strbuf_write(struct strbuf *sb, FILE *stream);
430 typedef int (*strbuf_getline_fn)(struct strbuf *, FILE *);
431
432 /* Uses LF as the line terminator */
425 -extern int strbuf_getline_lf(struct strbuf *sb, FILE *fp);
433 +int strbuf_getline_lf(struct strbuf *sb, FILE *fp);
434
435 /* Uses NUL as the line terminator */
428 -extern int strbuf_getline_nul(struct strbuf *sb, FILE *fp);
436 +int strbuf_getline_nul(struct strbuf *sb, FILE *fp);
437
438 /*
439 * Similar to strbuf_getline_lf(), but additionally treats a CR that
@@ -434,14 +442,14 @@ extern int strbuf_getline_nul(struct strbuf *sb, FILE *fp);
442 * that can come from platforms whose native text format is CRLF
443 * terminated.
444 */
437 -extern int strbuf_getline(struct strbuf *, FILE *);
445 +int strbuf_getline(struct strbuf *sb, FILE *file);
446
447
448 /**
449 * Like `strbuf_getline`, but keeps the trailing terminator (if
450 * any) in the buffer.
451 */
444 -extern int strbuf_getwholeline(struct strbuf *, FILE *, int);
452 +int strbuf_getwholeline(struct strbuf *sb, FILE *file, int term);
453
454 /**
455 * Like `strbuf_getwholeline`, but operates on a file descriptor.
@@ -449,19 +457,19 @@ extern int strbuf_getwholeline(struct strbuf *, FILE *, int);
457 * use it unless you need the correct position in the file
458 * descriptor.
459 */
452 -extern int strbuf_getwholeline_fd(struct strbuf *, int, int);
460 +int strbuf_getwholeline_fd(struct strbuf *sb, int fd, int term);
461
462 /**
463 * Set the buffer to the path of the current working directory.
464 */
457 -extern int strbuf_getcwd(struct strbuf *sb);
465 +int strbuf_getcwd(struct strbuf *sb);
466
467 /**
468 * Add a path to a buffer, converting a relative path to an
469 * absolute one in the process. Symbolic links are not
470 * resolved.
471 */
464 -extern void strbuf_add_absolute_path(struct strbuf *sb, const char *path);
472 +void strbuf_add_absolute_path(struct strbuf *sb, const char *path);
473
474 /**
475 * Canonize `path` (make it absolute, resolve symlinks, remove extra
@@ -475,7 +483,7 @@ extern void strbuf_add_absolute_path(struct strbuf *sb, const char *path);
483 * Callers that don't mind links should use the more lightweight
484 * strbuf_add_absolute_path() instead.
485 */
478 -extern void strbuf_add_real_path(struct strbuf *sb, const char *path);
486 +void strbuf_add_real_path(struct strbuf *sb, const char *path);
487
488
489 /**
@@ -483,13 +491,13 @@ extern void strbuf_add_real_path(struct strbuf *sb, const char *path);
491 * normalize_path_copy() for details. If an error occurs, the contents of "sb"
492 * are left untouched, and -1 is returned.
493 */
486 -extern int strbuf_normalize_path(struct strbuf *sb);
494 +int strbuf_normalize_path(struct strbuf *sb);
495
496 /**
497 * Strip whitespace from a buffer. The second parameter controls if
498 * comments are considered contents to be removed or not.
499 */
492 -extern void strbuf_stripspace(struct strbuf *buf, int skip_comments);
500 +void strbuf_stripspace(struct strbuf *buf, int skip_comments);
501
502 static inline int strbuf_strip_suffix(struct strbuf *sb, const char *suffix)
503 {
@@ -518,8 +526,8 @@ static inline int strbuf_strip_suffix(struct strbuf *sb, const char *suffix)
526 * For lighter-weight alternatives, see string_list_split() and
527 * string_list_split_in_place().
528 */
521 -extern struct strbuf **strbuf_split_buf(const char *, size_t,
522 - int terminator, int max);
529 +struct strbuf **strbuf_split_buf(const char *str, size_t len,
530 + int terminator, int max);
531
532 static inline struct strbuf **strbuf_split_str(const char *str,
533 int terminator, int max)
@@ -528,7 +536,7 @@ static inline struct strbuf **strbuf_split_str(const char *str,
536 }
537
538 static inline struct strbuf **strbuf_split_max(const struct strbuf *sb,
531 - int terminator, int max)
539 + int terminator, int max)
540 {
541 return strbuf_split_buf(sb->buf, sb->len, terminator, max);
542 }
@@ -549,23 +557,23 @@ static inline struct strbuf **strbuf_split(const struct strbuf *sb,
557 * 'element1, element2, ..., elementN'
558 * to str. If only one element, just write "element1" to str.
559 */
552 -extern void strbuf_add_separated_string_list(struct strbuf *str,
553 - const char *sep,
554 - struct string_list *slist);
560 +void strbuf_add_separated_string_list(struct strbuf *str,
561 + const char *sep,
562 + struct string_list *slist);
563
564 /**
565 * Free a NULL-terminated list of strbufs (for example, the return
566 * values of the strbuf_split*() functions).
567 */
560 -extern void strbuf_list_free(struct strbuf **);
568 +void strbuf_list_free(struct strbuf **list);
569
570 /**
571 * Add the abbreviation, as generated by find_unique_abbrev, of `sha1` to
572 * the strbuf `sb`.
573 */
566 -extern void strbuf_add_unique_abbrev(struct strbuf *sb,
567 - const struct object_id *oid,
568 - int abbrev_len);
574 +void strbuf_add_unique_abbrev(struct strbuf *sb,
575 + const struct object_id *oid,
576 + int abbrev_len);
577
578 /**
579 * Launch the user preferred editor to edit a file and fill the buffer
@@ -574,15 +582,21 @@ extern void strbuf_add_unique_abbrev(struct strbuf *sb,
582 * run in. If the buffer is NULL the editor is launched as usual but the
583 * file's contents are not read into the buffer upon completion.
584 */
577 -extern int launch_editor(const char *path, struct strbuf *buffer, const char *const *env);
585 +int launch_editor(const char *path,
586 + struct strbuf *buffer,
587 + const char *const *env);
588
579 -extern void strbuf_add_lines(struct strbuf *sb, const char *prefix, const char *buf, size_t size);
589 +void strbuf_add_lines(struct strbuf *sb,
590 + const char *prefix,
591 + const char *buf,
592 + size_t size);
593
594 /**
595 * Append s to sb, with the characters '<', '>', '&' and '"' converted
596 * into XML entities.
597 */
585 -extern void strbuf_addstr_xml_quoted(struct strbuf *sb, const char *s);
598 +void strbuf_addstr_xml_quoted(struct strbuf *sb,
599 + const char *s);
600
601 /**
602 * "Complete" the contents of `sb` by ensuring that either it ends with the
@@ -612,8 +626,8 @@ static inline void strbuf_complete_line(struct strbuf *sb)
626 * If "allowed" is non-zero, restrict the set of allowed expansions. See
627 * interpret_branch_name() for details.
628 */
615 -extern void strbuf_branchname(struct strbuf *sb, const char *name,
616 - unsigned allowed);
629 +void strbuf_branchname(struct strbuf *sb, const char *name,
630 + unsigned allowed);
631
632 /*
633 * Like strbuf_branchname() above, but confirm that the result is
@@ -621,15 +635,15 @@ extern void strbuf_branchname(struct strbuf *sb, const char *name,
635 *
636 * The return value is "0" if the result is valid, and "-1" otherwise.
637 */
624 -extern int strbuf_check_branch_ref(struct strbuf *sb, const char *name);
638 +int strbuf_check_branch_ref(struct strbuf *sb, const char *name);
639
626 -extern void strbuf_addstr_urlencode(struct strbuf *, const char *,
627 - int reserved);
640 +void strbuf_addstr_urlencode(struct strbuf *sb, const char *name,
641 + int reserved);
642
643 __attribute__((format (printf,1,2)))
630 -extern int printf_ln(const char *fmt, ...);
644 +int printf_ln(const char *fmt, ...);
645 __attribute__((format (printf,2,3)))
632 -extern int fprintf_ln(FILE *fp, const char *fmt, ...);
646 +int fprintf_ln(FILE *fp, const char *fmt, ...);
647
648 char *xstrdup_tolower(const char *);
649 char *xstrdup_toupper(const char *);