git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v4 09/21] strbuf: give strbuf_getline() to the "most text friendly" variant

From
Junio C Hamano <gitster@pobox.com>
Date
Jan 14, 2016, 23:58 UTC
Message-ID
<1452815916-6447-10-git-send-email-gitster@pobox.com>
In-Reply-To
<1452815916-6447-1-git-send-email-gitster@pobox.com>

Now there is no direct caller to strbuf_getline(), we can demote it to file-scope static private to strbuf.c implementation 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>
---
 builtin/am.c       |  8 ++++----
 strbuf.c           |  8 ++++----
 strbuf.h           | 25 ++++++++++++++-----------
 transport-helper.c |  3 ++-
 4 files changed, 24 insertions(+), 20 deletions(-)
diff --git a/builtin/am.c b/builtin/am.c
index 9063a4a..7b8351d 100644
--- a/builtin/am.c
+++ b/builtin/am.c
@@ -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
diff --git a/strbuf.c b/strbuf.c
index 2ff898c..47ac045 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)
diff --git a/strbuf.h b/strbuf.h
index 5501743..220c541 100644
--- a/strbuf.h
+++ b/strbuf.h
@@ -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 *);
 
@@ -380,26 +380,29 @@ 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'`.
+ * of the strbuf.  There are three public functions with this
+ * function signature, with different line termination convention.
  * 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);
-
 
 typedef int (*strbuf_getline_fn)(struct strbuf *, FILE *);
 
+/* Uses LF as the line terminator */
 extern int strbuf_getline_lf(struct strbuf *sb, FILE *fp);
+
+/* Uses NUL as the line terminator */
 extern int strbuf_getline_nul(struct strbuf *sb, FILE *fp);
 
-/*
- * Similar to strbuf_getline(), but uses '\n' as the terminator,
- * and additionally treats a '\r' that comes immediately before '\n'
- * as part of the terminator.
+/**
+ * Similar to strbuf_getline_lf(), but additionally treats
+ * a '\r' that comes immediately before '\n' 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 *);
 
 
 /**
diff --git a/transport-helper.c b/transport-helper.c
index 163e4b1..e45d88f 100644
--- a/transport-helper.c
+++ b/transport-helper.c
@@ -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.
 	 */
-- 
2.7.0-250-ge1b5ba3
Previous: Junio C HamanoNext: Eric Sunshine
Message 90 of 114 in “revisions --stdin: accept CRLF line terminators”
  1. revisions --stdin: accept CRLF line terminatorsJohannes Sixt, Aug 11, 2015
  2. Junio C HamanoAug 11, 2015
  3. Junio C HamanoAug 11, 2015
  4. Johannes SixtAug 12, 2015
  5. 00/17 Peace with CRLFJunio C Hamano, Oct 28, 2015
  6. 01/17 strbuf: add strbuf_gets()Junio C Hamano, Oct 28, 2015
  7. 02/17 check-attr, check-ignore, checkout-index: read paths with strbuf_gets()Junio C Hamano, Oct 28, 2015
  8. 03/17 update-index: read --index-info with strbuf_gets()Junio C Hamano, Oct 28, 2015
  9. 04/17 update-index: read list of paths with strbuf_gets() under --stdinJunio C Hamano, Oct 28, 2015
  10. 05/17 mktree: read textual tree representation with strbuf_gets()Junio C Hamano, Oct 28, 2015
  11. 06/17 hash-object: read --stdin-paths with strbuf_gets()Junio C Hamano, Oct 28, 2015
  12. 07/17 revision: read --stdin with strbuf_gets()Junio C Hamano, Oct 28, 2015
  13. 08/17 rev-parse: read parseopt spec with strbuf_gets()Junio C Hamano, Oct 28, 2015
  14. 09/17 ident.c: read /etc/mailname with strbuf_gets()Junio C Hamano, Oct 28, 2015
  15. 10/17 remote.c: read $GIT_DIR/remotes/* with strbuf_gets()Junio C Hamano, Oct 28, 2015
  16. 11/17 clone/sha1_file: read info/alternates with strbuf_gets()Junio C Hamano, Oct 28, 2015
  17. 12/17 transport-helper: read helper response with strbuf_gets()Junio C Hamano, Oct 28, 2015
  18. 13/17 cat-file: read batch stream with strbuf_gets()Junio C Hamano, Oct 28, 2015
  19. 14/17 column: read lines with strbuf_gets()Junio C Hamano, Oct 28, 2015
  20. 15/17 send-pack: read list of refs with strbuf_gets()Junio C Hamano, Oct 28, 2015
  21. 16/17 grep: read -f file with strbuf_gets()Junio C Hamano, Oct 28, 2015
  22. 17/17 test-sha1-array: read command stream with strbuf_gets()Junio C Hamano, Oct 28, 2015
  23. 00/17 Peace with CRLFJunio C Hamano, Dec 16, 2015
  24. 01/17 strbuf: make strbuf_getline_crlf() globalJunio C Hamano, Dec 16, 2015
  25. Johannes SchindelinJan 4, 2016
  26. Junio C HamanoJan 4, 2016
  27. 02/17 check-attr, check-ignore, checkout-index: read paths with strbuf_getline_crlf()Junio C Hamano, Dec 16, 2015
  28. Johannes SchindelinJan 4, 2016
  29. Johannes SchindelinJan 4, 2016
  30. Junio C HamanoJan 4, 2016
  31. 03/17 update-index: read --index-info with strbuf_getline_crlf()Junio C Hamano, Dec 16, 2015
  32. Johannes SchindelinJan 4, 2016
  33. Junio C HamanoJan 4, 2016
  34. 04/17 update-index: read list of paths with strbuf_getline_crlf() under --stdinJunio C Hamano, Dec 16, 2015
  35. Johannes SchindelinJan 4, 2016
  36. 05/17 mktree: read textual tree representation with strbuf_getline_crlf()Junio C Hamano, Dec 16, 2015
  37. Johannes SchindelinJan 4, 2016
  38. 06/17 hash-object: read --stdin-paths with strbuf_getline_crlf()Junio C Hamano, Dec 16, 2015
  39. 07/17 revision: read --stdin with strbuf_getline_crlf()Junio C Hamano, Dec 16, 2015
  40. 08/17 rev-parse: read parseopt spec with strbuf_getline_crlf()Junio C Hamano, Dec 16, 2015
  41. 09/17 ident.c: read /etc/mailname with strbuf_getline_crlf()Junio C Hamano, Dec 16, 2015
  42. 10/17 remote.c: read $GIT_DIR/remotes/* with strbuf_getline_crlf()Junio C Hamano, Dec 16, 2015
  43. 11/17 clone/sha1_file: read info/alternates with strbuf_getline_crlf()Junio C Hamano, Dec 16, 2015
  44. 12/17 transport-helper: read helper response with strbuf_getline_crlf()Junio C Hamano, Dec 16, 2015
  45. 13/17 cat-file: read batch stream with strbuf_getline_crlf()Junio C Hamano, Dec 16, 2015
  46. 14/17 column: read lines with strbuf_getline_crlf()Junio C Hamano, Dec 16, 2015
  47. 15/17 send-pack: read list of refs with strbuf_getline_crlf()Junio C Hamano, Dec 16, 2015
  48. 16/17 grep: read -f file with strbuf_getline_crlf()Junio C Hamano, Dec 16, 2015
  49. Johannes SchindelinJan 4, 2016
  50. Junio C HamanoJan 4, 2016
  51. 17/17 test-sha1-array: read command stream with strbuf_getline_crlf()Junio C Hamano, Dec 16, 2015
  52. Johannes SchindelinJan 4, 2016
  53. Johannes SchindelinJan 4, 2016
  54. 0/9 Preview of "Peace with CRLF" rerolledJunio C Hamano, Jan 14, 2016
  55. 1/9 strbuf: miniscule style fixJunio C Hamano, Jan 14, 2016
  56. 2/9 strbuf: make strbuf_getline_crlf() globalJunio C Hamano, Jan 14, 2016
  57. 3/9 strbuf: introduce strbuf_getline_{lf,nul}()Junio C Hamano, Jan 14, 2016
  58. 4/9 mktree: there are only two line terminatorsJunio C Hamano, Jan 14, 2016
  59. Jeff KingJan 14, 2016
  60. 5/9 check-attr: there are only two line terminatorsJunio C Hamano, Jan 14, 2016
  61. 6/9 check-ignore: there are only two line terminatorsJunio C Hamano, Jan 14, 2016
  62. 7/9 update-index: there are only two line terminatorsJunio C Hamano, Jan 14, 2016
  63. Jeff KingJan 14, 2016
  64. 8/9 checkout-index: there are only two line terminatorsJunio C Hamano, Jan 14, 2016
  65. Jeff KingJan 14, 2016
  66. Junio C HamanoJan 14, 2016
  67. Jeff KingJan 14, 2016
  68. 9/9 strbuf: give strbuf_getline() to the "most text friendly" variantJunio C Hamano, Jan 14, 2016
  69. 9/9 strbuf: retire strbuf_getline() for nowJunio C Hamano, Jan 14, 2016
  70. Junio C HamanoJan 14, 2016
  71. Jeff KingJan 14, 2016
  72. 00/21 Peace with CRLFJunio C Hamano, Jan 14, 2016
  73. 01/21 strbuf: miniscule style fixJunio C Hamano, Jan 14, 2016
  74. 02/21 strbuf: make strbuf_getline_crlf() globalJunio C Hamano, Jan 14, 2016
  75. 03/21 strbuf: introduce strbuf_getline_{lf,nul}()Junio C Hamano, Jan 14, 2016
  76. 04/21 mktree: there are only two possible line terminationsJunio C Hamano, Jan 14, 2016
  77. 05/21 check-attr: there are only two possible line terminationsJunio C Hamano, Jan 14, 2016
  78. Jeff KingJan 15, 2016
  79. Jeff KingJan 15, 2016
  80. Junio C HamanoJan 15, 2016
  81. Jeff KingJan 15, 2016
  82. Junio C HamanoJan 15, 2016
  83. 06/21 check-ignore: there are only two possible line terminationsJunio C Hamano, Jan 14, 2016
  84. 07/21 update-index: there are only two possible line terminationsJunio C Hamano, Jan 14, 2016
  85. 08/21 checkout-index: there are only two possible line terminationsJunio C Hamano, Jan 14, 2016
  86. Jeff KingJan 15, 2016
  87. Jeff KingJan 15, 2016
  88. Junio C HamanoJan 15, 2016
  89. Junio C HamanoJan 15, 2016
  90. 09/21 strbuf: give strbuf_getline() to the "most text friendly" variantJunio C Hamano, Jan 14, 2016
  91. Eric SunshineJan 15, 2016
  92. Junio C HamanoJan 15, 2016
  93. 10/21 hash-object: read --stdin-paths with strbuf_getline()Junio C Hamano, Jan 14, 2016
  94. Jeff KingJan 15, 2016
  95. 11/21 revision: read --stdin with strbuf_getline()Junio C Hamano, Jan 14, 2016
  96. Eric SunshineJan 15, 2016
  97. Junio C HamanoJan 15, 2016
  98. Jeff KingJan 15, 2016
  99. 12/21 rev-parse: read parseopt spec with strbuf_getline()Junio C Hamano, Jan 14, 2016
  100. 13/21 ident.c: read /etc/mailname with strbuf_getline()Junio C Hamano, Jan 14, 2016
  101. 14/21 remote.c: read $GIT_DIR/remotes/* with strbuf_getline()Junio C Hamano, Jan 14, 2016
  102. 15/21 clone/sha1_file: read info/alternates with strbuf_getline()Junio C Hamano, Jan 14, 2016
  103. Eric SunshineJan 15, 2016
  104. 16/21 transport-helper: read helper response with strbuf_getline()Junio C Hamano, Jan 14, 2016
  105. Jeff KingJan 15, 2016
  106. 17/21 cat-file: read batch stream with strbuf_getline()Junio C Hamano, Jan 14, 2016
  107. Jeff KingJan 15, 2016
  108. 18/21 column: read lines with strbuf_getline()Junio C Hamano, Jan 14, 2016
  109. Jeff KingJan 15, 2016
  110. 19/21 send-pack: read list of refs with strbuf_getline()Junio C Hamano, Jan 14, 2016
  111. 20/21 grep: read -f file with strbuf_getline()Junio C Hamano, Jan 14, 2016
  112. 21/21 test-sha1-array: read command stream with strbuf_getline()Junio C Hamano, Jan 14, 2016
  113. Jeff KingJan 15, 2016
  114. Jeff KingJan 15, 2016

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.