From dce80bd18cae17b2d5645e3a9c40e76f905fd643 Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Wed, 13 Jan 2016 15:10:45 -0800 Subject: strbuf: miniscule style fix We write one SP on each side of an operator, even inside an [] pair that computes the array index. Signed-off-by: Junio C Hamano --- strbuf.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'strbuf.c') diff --git a/strbuf.c b/strbuf.c index d76f0aed8..b165d044a 100644 --- a/strbuf.c +++ b/strbuf.c @@ -505,8 +505,8 @@ int strbuf_getline(struct strbuf *sb, FILE *fp, int term) { if (strbuf_getwholeline(sb, fp, term)) return EOF; - if (sb->buf[sb->len-1] == term) - strbuf_setlen(sb, sb->len-1); + if (sb->buf[sb->len - 1] == term) + strbuf_setlen(sb, sb->len - 1); return 0; } -- cgit v1.2.1 From c8aa9fdf5dc15e2c508acb22df03d431983569ed Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Wed, 28 Oct 2015 13:17:29 -0700 Subject: strbuf: make strbuf_getline_crlf() global Often we read "text" files that are supplied by the end user (e.g. commit log message that was edited with $GIT_EDITOR upon 'git commit -e'), and in some environments lines in a text file are terminated with CRLF. Existing strbuf_getline() knows to read a single line and then strip the terminating byte from the result, but it is handy to have a version that is more tailored for a "text" input that takes both '\n' and '\r\n' as line terminator (aka in POSIX lingo) and returns the body of the line after stripping . Recently reimplemented "git am" uses such a function implemented privately; move it to strbuf.[ch] and make it available for others. Note that we do not blindly replace calls to strbuf_getline() that uses LF as the line terminator with calls to strbuf_getline_crlf() and this is very much deliberate. Some callers may want to treat an incoming line that ends with CR (and terminated with LF) to have a payload that includes the final CR, and such a blind replacement will result in misconversion when done without code audit. Signed-off-by: Junio C Hamano --- strbuf.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'strbuf.c') diff --git a/strbuf.c b/strbuf.c index b165d044a..7ad5ea414 100644 --- a/strbuf.c +++ b/strbuf.c @@ -510,6 +510,18 @@ int strbuf_getline(struct strbuf *sb, FILE *fp, int term) return 0; } +int strbuf_getline_crlf(struct strbuf *sb, FILE *fp) +{ + if (strbuf_getwholeline(sb, fp, '\n')) + return EOF; + if (sb->buf[sb->len - 1] == '\n') { + strbuf_setlen(sb, sb->len - 1); + if (sb->len && sb->buf[sb->len - 1] == '\r') + strbuf_setlen(sb, sb->len - 1); + } + return 0; +} + int strbuf_getwholeline_fd(struct strbuf *sb, int fd, int term) { strbuf_reset(sb); -- cgit v1.2.1 From 8f309aeb8225a9c26f20c0dbc031f1ea8df75d49 Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Wed, 13 Jan 2016 15:31:17 -0800 Subject: strbuf: introduce strbuf_getline_{lf,nul}() The strbuf_getline() interface allows a byte other than LF or NUL as the line terminator, but this is only because I wrote these codepaths anticipating that there might be a value other than NUL and LF that could be useful when I introduced line_termination long time ago. No useful caller that uses other value has emerged. By now, it is clear that the interface is overly broad without a good reason. Many codepaths have hardcoded preference to read either LF terminated or NUL terminated records from their input, and then call strbuf_getline() with LF or NUL as the third parameter. This step introduces two thin wrappers around strbuf_getline(), namely, strbuf_getline_lf() and strbuf_getline_nul(), and mechanically rewrites these call sites to call either one of them. The changes contained in this patch are: * introduction of these two functions in strbuf.[ch] * mechanical conversion of all callers to strbuf_getline() with either '\n' or '\0' as the third parameter to instead call the respective thin wrapper. After this step, output from "git grep 'strbuf_getline('" would become a lot smaller. An interim goal of this series is to make this an empty set, so that we can have strbuf_getline_crlf() take over the shorter name strbuf_getline(). Signed-off-by: Junio C Hamano --- strbuf.c | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'strbuf.c') diff --git a/strbuf.c b/strbuf.c index 7ad5ea414..2ff898c8c 100644 --- a/strbuf.c +++ b/strbuf.c @@ -522,6 +522,16 @@ int strbuf_getline_crlf(struct strbuf *sb, FILE *fp) return 0; } +int strbuf_getline_lf(struct strbuf *sb, FILE *fp) +{ + return strbuf_getline(sb, fp, '\n'); +} + +int strbuf_getline_nul(struct strbuf *sb, FILE *fp) +{ + return strbuf_getline(sb, fp, '\0'); +} + int strbuf_getwholeline_fd(struct strbuf *sb, int fd, int term) { strbuf_reset(sb); -- cgit v1.2.1 From 1a0c8dfd89475d6bb09ddee8c019cf0ae5b3bdc2 Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Wed, 13 Jan 2016 18:32:23 -0800 Subject: 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 --- strbuf.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'strbuf.c') diff --git a/strbuf.c b/strbuf.c index 2ff898c8c..47ac0457f 100644 --- a/strbuf.c +++ b/strbuf.c @@ -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) -- cgit v1.2.1