coreutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH 2/2] cut: fix -f to work with the -d$'\n' edge case


From: Pádraig Brady
Subject: [PATCH 2/2] cut: fix -f to work with the -d$'\n' edge case
Date: Tue, 22 Jan 2013 01:47:51 +0000

* src/cut.c (cut_fields): Handle the edge case where '\n' is
the delimiter, which could be used for example to suppress
the last line if it doesn't contain a '\n'.
* test/misc/cut.pl: Add tests for this edge case.
---
 src/cut.c         |   14 ++++++++------
 tests/misc/cut.pl |   10 ++++++++++
 2 files changed, 18 insertions(+), 6 deletions(-)

diff --git a/src/cut.c b/src/cut.c
index 88a2f1b..c48eef7 100644
--- a/src/cut.c
+++ b/src/cut.c
@@ -618,6 +618,7 @@ cut_fields (FILE *stream)
         {
           ssize_t len;
           size_t n_bytes;
+          bool got_line;
 
           len = getndelim2 (&field_1_buffer, &field_1_bufsize, 0,
                             GETNLINE_NO_LIMIT, delim, '\n', stream);
@@ -634,13 +635,14 @@ cut_fields (FILE *stream)
           assert (n_bytes != 0);
 
           c = 0;
+          got_line = field_1_buffer[n_bytes - 1] == '\n';
 
           /* If the first field extends to the end of line (it is not
              delimited) and we are printing all non-delimited lines,
              print this one.  */
-          if (to_uchar (field_1_buffer[n_bytes - 1]) != delim)
+          if (to_uchar (field_1_buffer[n_bytes - 1]) != delim || got_line)
             {
-              if (suppress_non_delimited)
+              if (suppress_non_delimited && !(got_line && delim == '\n'))
                 {
                   /* Empty.  */
                 }
@@ -648,7 +650,7 @@ cut_fields (FILE *stream)
                 {
                   fwrite (field_1_buffer, sizeof (char), n_bytes, stdout);
                   /* Make sure the output line is newline terminated.  */
-                  if (field_1_buffer[n_bytes - 1] != '\n')
+                  if (! got_line)
                     putchar ('\n');
                   c = '\n';
                 }
@@ -688,9 +690,7 @@ cut_fields (FILE *stream)
             }
         }
 
-      if (c == delim)
-        ++field_idx;
-      else if (c == '\n' || c == EOF)
+      if (c == '\n' || c == EOF)
         {
           if (found_any_selected_field
               || !(suppress_non_delimited && field_idx == 1))
@@ -703,6 +703,8 @@ cut_fields (FILE *stream)
           field_idx = 1;
           found_any_selected_field = false;
         }
+      else if (c == delim)
+        field_idx++;
     }
 }
 
diff --git a/tests/misc/cut.pl b/tests/misc/cut.pl
index 3ce09bb..874c169 100755
--- a/tests/misc/cut.pl
+++ b/tests/misc/cut.pl
@@ -143,6 +143,16 @@ my @Tests =
   ['newline-11', '-s', '-d:', '-f1,2', {IN=>"a:1\nb:2\n"}, 
{OUT=>"a:1\nb:2\n"}],
   ['newline-12', '-s', '-d:', '-f1', {IN=>"a:1\nb:"}, {OUT=>"a\nb\n"}],
   ['newline-13', '-d:', '-f1-', {IN=>"a1:\n:"}, {OUT=>"a1:\n:\n"}],
+  # newline processing for fields when -d == '\n'
+  ['newline-14', "-d'\n'", '-f1', {IN=>"a:1\nb:"}, {OUT=>"a:1\nb:\n"}],
+  ['newline-15', '-s', "-d'\n'", '-f1', {IN=>"a:1\nb:"}, {OUT=>"a:1\n"}],
+  ['newline-16', '-s', "-d'\n'", '-f2', {IN=>"\nb"}, {OUT=>""}],
+  ['newline-17', '-s', "-d'\n'", '-f1', {IN=>"\nb"}, {OUT=>"\n"}],
+  ['newline-18', "-d'\n'", '-f2', {IN=>"\nb"}, {OUT=>"\nb\n"}],
+  ['newline-19', "-d'\n'", '-f1', {IN=>"\nb"}, {OUT=>"\nb\n"}],
+  ['newline-20', '-s', "-d'\n'", '-f1-', {IN=>"\n"}, {OUT=>"\n"}],
+  ['newline-21', '-s', "-d'\n'", '-f1-', {IN=>"\nb"}, {OUT=>"\n"}],
+  ['newline-22', "-d'\n'", '-f1-', {IN=>"\nb"}, {OUT=>"\nb\n"}],
 
   # New functionality:
   ['out-delim1', '-c1-3,5-', '--output-d=:', {IN=>"abcdefg\n"},
-- 
1.7.6.4




reply via email to

[Prev in Thread] Current Thread [Next in Thread]