Used by some ART tests and also some LLVM tests. (The motivating example
is the latter, but I noticed the former when looking for other users.)

Bug: http://b/137298656
---
 tests/diff.test     |  5 +++++
 toys/pending/diff.c | 50 ++++++++++++++++++++++++++++-----------------
 2 files changed, 36 insertions(+), 19 deletions(-)
From 7e9e594b262b1faf7a16826624749c74e190e57e Mon Sep 17 00:00:00 2001
From: Elliott Hughes <[email protected]>
Date: Thu, 11 Jul 2019 15:23:31 -0700
Subject: [PATCH] diff: implement --strip-trailing-cr.

Used by some ART tests and also some LLVM tests. (The motivating example
is the latter, but I noticed the former when looking for other users.)

Bug: http://b/137298656
---
 tests/diff.test     |  5 +++++
 toys/pending/diff.c | 50 ++++++++++++++++++++++++++++-----------------
 2 files changed, 36 insertions(+), 19 deletions(-)

diff --git a/tests/diff.test b/tests/diff.test
index 98477587..f78eaa66 100644
--- a/tests/diff.test
+++ b/tests/diff.test
@@ -33,3 +33,8 @@ echo foo > tree1/file
 echo food > tree2/file
 
 testing "-r" "diff -r -L tree1/file -L tree2/file tree1 tree2 |tee out" "$expected" "" ""
+
+echo -e "hello\r\nworld\r\n"> a
+echo -e "hello\nworld\n"> b
+testing "--strip-trailing-cr off" "diff -q a b" "Files a and b differ\n" "" ""
+testing "--strip-trailing-cr on" "diff -u --strip-trailing-cr a b" "" "" ""
diff --git a/toys/pending/diff.c b/toys/pending/diff.c
index d865e8df..2d13d977 100644
--- a/toys/pending/diff.c
+++ b/toys/pending/diff.c
@@ -5,7 +5,7 @@
  *
  * See: http://cm.bell-labs.com/cm/cs/cstr/41.pdf
 
-USE_DIFF(NEWTOY(diff, "<2>2(color)B(ignore-blank-lines)d(minimal)b(ignore-space-change)ut(expand-tabs)w(ignore-all-space)i(ignore-case)T(initial-tab)s(report-identical-files)q(brief)a(text)L(label)*S(starting-file):N(new-file)r(recursive)U(unified)#<0=3", TOYFLAG_USR|TOYFLAG_BIN|TOYFLAG_ARGFAIL(2)))
+USE_DIFF(NEWTOY(diff, "<2>2(color)(strip-trailing-cr)B(ignore-blank-lines)d(minimal)b(ignore-space-change)ut(expand-tabs)w(ignore-all-space)i(ignore-case)T(initial-tab)s(report-identical-files)q(brief)a(text)L(label)*S(starting-file):N(new-file)r(recursive)U(unified)#<0=3", TOYFLAG_USR|TOYFLAG_BIN|TOYFLAG_ARGFAIL(2)))
 
 config DIFF
   bool "diff"
@@ -13,23 +13,25 @@ config DIFF
   help
   usage: diff [-abBdiNqrTstw] [-L LABEL] [-S FILE] [-U LINES] FILE1 FILE2
 
-  -a  Treat all files as text
-  -b  Ignore changes in the amount of whitespace
-  -B  Ignore changes whose lines are all blank
-  -d  Try hard to find a smaller set of changes
-  -i  Ignore case differences
-  -L  Use LABEL instead of the filename in the unified header
-  -N  Treat absent files as empty
-  -q  Output only whether files differ
-  -r  Recurse
-  -S  Start with FILE when comparing directories
-  -T  Make tabs line up by prefixing a tab when necessary
-  -s  Report when two files are the same
-  -t  Expand tabs to spaces in output
-  -U  Output LINES lines of context
-  -w  Ignore all whitespace
-
-  --color  Colored output
+  -a	Treat all files as text
+  -b	Ignore changes in the amount of whitespace
+  -B	Ignore changes whose lines are all blank
+  -d	Try hard to find a smaller set of changes
+  -i	Ignore case differences
+  -L	Use LABEL instead of the filename in the unified header
+  -N	Treat absent files as empty
+  -q	Output only whether files differ
+  -r	Recurse
+  -S	Start with FILE when comparing directories
+  -T	Make tabs line up by prefixing a tab when necessary
+  -s	Report when two files are the same
+  -t	Expand tabs to spaces in output
+  -u	Unified diff
+  -U	Output LINES lines of context
+  -w	Ignore all whitespace
+
+  --color              Colored output
+  --strip-trailing-cr  Strip trailing '\r's from input lines
 */
 
 #define FOR_diff
@@ -196,8 +198,18 @@ static int read_tok(FILE *fp, off_t *off, int tok)
 
   tok |= empty;
   while (!(tok & eol)) {
-
     t = fgetc(fp);
+
+    if (FLAG(strip_trailing_cr) && t == '\r') {
+      int t2 = fgetc(fp);
+      if (t2 == '\n') {
+        t = t2;
+        if (off) (*off)++;
+      } else {
+        ungetc(t2, fp);
+      }
+    }
+
     if (off && t != EOF) *off += 1;
     is_space = isspace(t) || (t == EOF);
     tok |= (t & (eof + eol)); //set tok eof+eol when t is eof
-- 
2.22.0.410.gd8fdbe21b5-goog

_______________________________________________
Toybox mailing list
[email protected]
http://lists.landley.net/listinfo.cgi/toybox-landley.net

Reply via email to