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 <gitster@pobox.com>
This commit is contained in:
Junio C Hamano 2016-01-13 18:32:23 -08:00
parent a392f57daf
commit 1a0c8dfd89
4 changed files with 20 additions and 21 deletions

View File

@ -613,7 +613,7 @@ static int is_mail(FILE *fp)
if (regcomp(&regex, header_regex, REG_NOSUB | REG_EXTENDED))
die("invalid pattern: %s", header_regex);
while (!strbuf_getline_crlf(&sb, fp)) {
while (!strbuf_getline(&sb, fp)) {
if (!sb.len)
break; /* End of header */
@ -660,7 +660,7 @@ static int detect_patch_format(const char **paths)
fp = xfopen(*paths, "r");
while (!strbuf_getline_crlf(&l1, fp)) {
while (!strbuf_getline(&l1, fp)) {
if (l1.len)
break;
}
@ -681,9 +681,9 @@ static int detect_patch_format(const char **paths)
}
strbuf_reset(&l2);
strbuf_getline_crlf(&l2, fp);
strbuf_getline(&l2, fp);
strbuf_reset(&l3);
strbuf_getline_crlf(&l3, fp);
strbuf_getline(&l3, fp);
/*
* If the second line is empty and the third is a From, Author or Date

View File

@ -501,7 +501,7 @@ int strbuf_getwholeline(struct strbuf *sb, FILE *fp, int term)
}
#endif
int strbuf_getline(struct strbuf *sb, FILE *fp, int term)
static int strbuf_getdelim(struct strbuf *sb, FILE *fp, int term)
{
if (strbuf_getwholeline(sb, fp, term))
return EOF;
@ -510,7 +510,7 @@ int strbuf_getline(struct strbuf *sb, FILE *fp, int term)
return 0;
}
int strbuf_getline_crlf(struct strbuf *sb, FILE *fp)
int strbuf_getline(struct strbuf *sb, FILE *fp)
{
if (strbuf_getwholeline(sb, fp, '\n'))
return EOF;
@ -524,12 +524,12 @@ int strbuf_getline_crlf(struct strbuf *sb, FILE *fp)
int strbuf_getline_lf(struct strbuf *sb, FILE *fp)
{
return strbuf_getline(sb, fp, '\n');
return strbuf_getdelim(sb, fp, '\n');
}
int strbuf_getline_nul(struct strbuf *sb, FILE *fp)
{
return strbuf_getline(sb, fp, '\0');
return strbuf_getdelim(sb, fp, '\0');
}
int strbuf_getwholeline_fd(struct strbuf *sb, int fd, int term)

View File

@ -354,8 +354,8 @@ extern void strbuf_addftime(struct strbuf *sb, const char *fmt, const struct tm
*
* NOTE: The buffer is rewound if the read fails. If -1 is returned,
* `errno` must be consulted, like you would do for `read(3)`.
* `strbuf_read()`, `strbuf_read_file()` and `strbuf_getline()` has the
* same behaviour as well.
* `strbuf_read()`, `strbuf_read_file()` and `strbuf_getline_*()`
* family of functions have the same behaviour as well.
*/
extern size_t strbuf_fread(struct strbuf *, size_t, FILE *);
@ -379,19 +379,14 @@ extern ssize_t strbuf_read_file(struct strbuf *sb, const char *path, size_t hint
extern int strbuf_readlink(struct strbuf *sb, const char *path, size_t hint);
/**
* Read a line from a FILE *, overwriting the existing contents
* of the strbuf. The second argument specifies the line
* terminator character, typically `'\n'`.
* Read a line from a FILE *, overwriting the existing contents of
* the strbuf. The strbuf_getline*() family of functions share
* this signature, but have different line termination conventions.
*
* Reading stops after the terminator or at EOF. The terminator
* is removed from the buffer before returning. Returns 0 unless
* there was nothing left before EOF, in which case it returns `EOF`.
*/
extern int strbuf_getline(struct strbuf *, FILE *, int);
/**
* The strbuf_getline*() family of functions share this signature, but
* have different line termination conventions.
*/
typedef int (*strbuf_getline_fn)(struct strbuf *, FILE *);
/* Uses LF as the line terminator */
@ -403,8 +398,11 @@ extern int strbuf_getline_nul(struct strbuf *sb, FILE *fp);
/*
* Similar to strbuf_getline_lf(), but additionally treats a CR that
* comes immediately before the LF as part of the terminator.
* This is the most friendly version to be used to read "text" files
* that can come from platforms whose native text format is CRLF
* terminated.
*/
extern int strbuf_getline_crlf(struct strbuf *, FILE *);
extern int strbuf_getline(struct strbuf *, FILE *);
/**

View File

@ -137,7 +137,8 @@ static struct child_process *get_helper(struct transport *transport)
data->no_disconnect_req = 0;
/*
* Open the output as FILE* so strbuf_getline() can be used.
* Open the output as FILE* so strbuf_getline_*() family of
* functions can be used.
* Do this with duped fd because fclose() will close the fd,
* and stuff like taking over will require the fd to remain.
*/