Add LF-to-CRLF streaming conversion
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>
Junio C Hamano committed
May 20, 2011 at 16:47 UTC
e322ee38ad8d655f5a32b3482ae9ce813b73e4bc
1 file changed
+41
convert.c
+41
@@ -872,6 +872,43 @@ int is_null_stream_filter(struct stream_filter *filter)
872
return filter == &null_filter_singleton;
873
}
874
875
+static int lf_to_crlf_filter_fn(struct stream_filter *filter,
876
+ const char *input, size_t *isize_p,
877
+ char *output, size_t *osize_p)
878
+{
879
+ size_t count;
880
+
881
+ if (!input)
882
+ return 0; /* we do not keep any states */
883
+ count = *isize_p;
884
+ if (count) {
885
+ size_t i, o;
886
+ for (i = o = 0; o < *osize_p && i < count; i++) {
887
+ char ch = input[i];
888
+ if (ch == '\n') {
889
+ if (o + 1 < *osize_p)
890
+ output[o++] = '\r';
891
+ else
892
+ break;
893
+ }
894
+ output[o++] = ch;
895
+ }
896
+
897
+ *osize_p -= o;
898
+ *isize_p -= i;
899
+ }
900
+ return 0;
901
+}
902
+
903
+static struct stream_filter_vtbl lf_to_crlf_vtbl = {
904
+ lf_to_crlf_filter_fn,
905
+ null_free_fn,
906
+};
907
+
908
+static struct stream_filter lf_to_crlf_filter_singleton = {
909
+ &lf_to_crlf_vtbl,
910
+};
911
+
912
/*
913
* Return an appropriately constructed filter for the path, or NULL if
914
* the contents cannot be filtered without reading the whole thing
@@ -896,6 +933,10 @@ struct stream_filter *get_stream_filter(const char *path, const unsigned char *s
933
(crlf_action == CRLF_GUESS && auto_crlf == AUTO_CRLF_FALSE))
934
return &null_filter_singleton;
935
936
+ if (output_eol(crlf_action) == EOL_CRLF &&
937
+ !(crlf_action == CRLF_AUTO || crlf_action == CRLF_GUESS))
938
+ return &lf_to_crlf_filter_singleton;
939
+
940
return NULL;
941
}
942