Skip to content

Commit 1a0c8df

Browse files
committed
strbuf: give strbuf_getline() to the "most text friendly" variant
Now there is no direct caller to strbuf_getline(), we can demote it to file-scope static that is private to strbuf.c and rename it to strbuf_getdelim(). Rename strbuf_getline_crlf(), which is designed to be the most "text friendly" variant, and allow it to take over this simplest name, strbuf_getline(), so we can add more uses of it without having to type _crlf over and over again in the coming steps. Signed-off-by: Junio C Hamano <[email protected]>
1 parent a392f57 commit 1a0c8df

File tree

4 files changed

+20
-21
lines changed

4 files changed

+20
-21
lines changed

builtin/am.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -613,7 +613,7 @@ static int is_mail(FILE *fp)
613613
if (regcomp(&regex, header_regex, REG_NOSUB | REG_EXTENDED))
614614
die("invalid pattern: %s", header_regex);
615615

616-
while (!strbuf_getline_crlf(&sb, fp)) {
616+
while (!strbuf_getline(&sb, fp)) {
617617
if (!sb.len)
618618
break; /* End of header */
619619

@@ -660,7 +660,7 @@ static int detect_patch_format(const char **paths)
660660

661661
fp = xfopen(*paths, "r");
662662

663-
while (!strbuf_getline_crlf(&l1, fp)) {
663+
while (!strbuf_getline(&l1, fp)) {
664664
if (l1.len)
665665
break;
666666
}
@@ -681,9 +681,9 @@ static int detect_patch_format(const char **paths)
681681
}
682682

683683
strbuf_reset(&l2);
684-
strbuf_getline_crlf(&l2, fp);
684+
strbuf_getline(&l2, fp);
685685
strbuf_reset(&l3);
686-
strbuf_getline_crlf(&l3, fp);
686+
strbuf_getline(&l3, fp);
687687

688688
/*
689689
* If the second line is empty and the third is a From, Author or Date

strbuf.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,7 @@ int strbuf_getwholeline(struct strbuf *sb, FILE *fp, int term)
501501
}
502502
#endif
503503

504-
int strbuf_getline(struct strbuf *sb, FILE *fp, int term)
504+
static int strbuf_getdelim(struct strbuf *sb, FILE *fp, int term)
505505
{
506506
if (strbuf_getwholeline(sb, fp, term))
507507
return EOF;
@@ -510,7 +510,7 @@ int strbuf_getline(struct strbuf *sb, FILE *fp, int term)
510510
return 0;
511511
}
512512

513-
int strbuf_getline_crlf(struct strbuf *sb, FILE *fp)
513+
int strbuf_getline(struct strbuf *sb, FILE *fp)
514514
{
515515
if (strbuf_getwholeline(sb, fp, '\n'))
516516
return EOF;
@@ -524,12 +524,12 @@ int strbuf_getline_crlf(struct strbuf *sb, FILE *fp)
524524

525525
int strbuf_getline_lf(struct strbuf *sb, FILE *fp)
526526
{
527-
return strbuf_getline(sb, fp, '\n');
527+
return strbuf_getdelim(sb, fp, '\n');
528528
}
529529

530530
int strbuf_getline_nul(struct strbuf *sb, FILE *fp)
531531
{
532-
return strbuf_getline(sb, fp, '\0');
532+
return strbuf_getdelim(sb, fp, '\0');
533533
}
534534

535535
int strbuf_getwholeline_fd(struct strbuf *sb, int fd, int term)

strbuf.h

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -354,8 +354,8 @@ extern void strbuf_addftime(struct strbuf *sb, const char *fmt, const struct tm
354354
*
355355
* NOTE: The buffer is rewound if the read fails. If -1 is returned,
356356
* `errno` must be consulted, like you would do for `read(3)`.
357-
* `strbuf_read()`, `strbuf_read_file()` and `strbuf_getline()` has the
358-
* same behaviour as well.
357+
* `strbuf_read()`, `strbuf_read_file()` and `strbuf_getline_*()`
358+
* family of functions have the same behaviour as well.
359359
*/
360360
extern size_t strbuf_fread(struct strbuf *, size_t, FILE *);
361361

@@ -379,19 +379,14 @@ extern ssize_t strbuf_read_file(struct strbuf *sb, const char *path, size_t hint
379379
extern int strbuf_readlink(struct strbuf *sb, const char *path, size_t hint);
380380

381381
/**
382-
* Read a line from a FILE *, overwriting the existing contents
383-
* of the strbuf. The second argument specifies the line
384-
* terminator character, typically `'\n'`.
382+
* Read a line from a FILE *, overwriting the existing contents of
383+
* the strbuf. The strbuf_getline*() family of functions share
384+
* this signature, but have different line termination conventions.
385+
*
385386
* Reading stops after the terminator or at EOF. The terminator
386387
* is removed from the buffer before returning. Returns 0 unless
387388
* there was nothing left before EOF, in which case it returns `EOF`.
388389
*/
389-
extern int strbuf_getline(struct strbuf *, FILE *, int);
390-
391-
/**
392-
* The strbuf_getline*() family of functions share this signature, but
393-
* have different line termination conventions.
394-
*/
395390
typedef int (*strbuf_getline_fn)(struct strbuf *, FILE *);
396391

397392
/* Uses LF as the line terminator */
@@ -403,8 +398,11 @@ extern int strbuf_getline_nul(struct strbuf *sb, FILE *fp);
403398
/*
404399
* Similar to strbuf_getline_lf(), but additionally treats a CR that
405400
* comes immediately before the LF as part of the terminator.
401+
* This is the most friendly version to be used to read "text" files
402+
* that can come from platforms whose native text format is CRLF
403+
* terminated.
406404
*/
407-
extern int strbuf_getline_crlf(struct strbuf *, FILE *);
405+
extern int strbuf_getline(struct strbuf *, FILE *);
408406

409407

410408
/**

transport-helper.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,8 @@ static struct child_process *get_helper(struct transport *transport)
137137
data->no_disconnect_req = 0;
138138

139139
/*
140-
* Open the output as FILE* so strbuf_getline() can be used.
140+
* Open the output as FILE* so strbuf_getline_*() family of
141+
* functions can be used.
141142
* Do this with duped fd because fclose() will close the fd,
142143
* and stuff like taking over will require the fd to remain.
143144
*/

0 commit comments

Comments
 (0)