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

[PATCH 3/4] Add LF-to-CRLF streaming conversion

From
Junio C Hamano <gitster@pobox.com>
Date
May 21, 2011, 06:58 UTC
Message-ID
<1305961127-26540-4-git-send-email-gitster@pobox.com>
In-Reply-To
<1305961127-26540-1-git-send-email-gitster@pobox.com>

If we do not have to guess or validate by scanning the input, we can just stream this through.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 convert.c |   37 +++++++++++++++++++++++++++++++++++++
 1 files changed, 37 insertions(+), 0 deletions(-)
diff --git a/convert.c b/convert.c
index 1ec91a3..82fb75b 100644
--- a/convert.c
+++ b/convert.c
@@ -868,6 +868,39 @@ int is_null_stream_filter(struct stream_filter *filter)
 	return filter == &null_filter_singleton;
 }
 
+static int lf_to_crlf_filter_fn(struct stream_filter *filter,
+				const char *input, size_t *isize_p,
+				char *output, size_t *osize_p)
+{
+	size_t count = *isize_p;
+	if (count) {
+		size_t i, o;
+		for (i = o = 0; o < *osize_p && i < count; i++) {
+			char ch = input[i];
+			if (ch == '\n') {
+				if (o + 1 < *osize_p)
+					output[o++] = '\r';
+				else
+					break;
+			}
+			output[o++] = ch;
+		}
+
+		*osize_p -= o;
+		*isize_p -= i;
+	}
+	return 0;
+}
+
+static struct stream_filter_vtbl lf_to_crlf_vtbl = {
+	lf_to_crlf_filter_fn,
+	null_free_fn,
+};
+
+static struct stream_filter lf_to_crlf_filter_singleton = {
+	&lf_to_crlf_vtbl,
+};
+
 /*
  * Return an appropriately constructed filter for the path, or NULL if
  * the contents cannot be filtered without reading the whole thing
@@ -892,6 +925,10 @@ struct stream_filter *get_stream_filter(const char *path, const unsigned char *s
 	    (crlf_action == CRLF_GUESS && auto_crlf == AUTO_CRLF_FALSE))
 		return &null_filter_singleton;
 
+	if (output_eol(crlf_action) == EOL_CRLF &&
+	    !(crlf_action == CRLF_AUTO || crlf_action == CRLF_GUESS))
+		return &lf_to_crlf_filter_singleton;
+
 	return NULL;
 }
 
-- 
1.7.5.2.369.g8fc017
Previous: Junio C HamanoNext: Junio C Hamano
Message 4 of 12 in “streaming conversion API”
  1. 0/4 streaming conversion APIJunio C Hamano, May 21, 2011
  2. convert.h: move declarations for conversion from cache.hJunio C Hamano, May 21, 2011
  3. 2/4 Add streaming filter APIJunio C Hamano, May 21, 2011
  4. 3/4 Add LF-to-CRLF streaming conversionJunio C Hamano, May 21, 2011
  5. 4/4 streaming filter: ident filter and filter cascadingJunio C Hamano, May 21, 2011
  6. René ScharfeMay 21, 2011
  7. René ScharfeMay 21, 2011
  8. Junio C HamanoMay 25, 2011
  9. t0021: test application of both crlf and identJunio C Hamano, May 25, 2011
  10. streaming: filter cascadingJunio C Hamano, May 25, 2011
  11. René ScharfeMay 21, 2011
  12. Junio C HamanoMay 22, 2011

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.