[git commit] cut: fix -F n-m to match toybox

Denys Vlasenko vda.linux at googlemail.com
Sat Dec 14 12:58:49 UTC 2024


commit: https://git.busybox.net/busybox/commit/?id=b25ea3f156c6b5927b358f9b12b90f1852f30311
branch: https://git.busybox.net/busybox/commit/?id=refs/heads/master

function                                             old     new   delta
cut_main                                            1339    1391     +52

Signed-off-by: Denys Vlasenko <vda.linux at googlemail.com>
---
 coreutils/cut.c     | 10 ++++++++++
 testsuite/cut.tests | 21 ++++++++++++++++++---
 2 files changed, 28 insertions(+), 3 deletions(-)

diff --git a/coreutils/cut.c b/coreutils/cut.c
index f4cf5401b..3abebe7ad 100644
--- a/coreutils/cut.c
+++ b/coreutils/cut.c
@@ -243,6 +243,16 @@ static void cut_file(FILE *file, const char *delim, const char *odelim,
 						start = next;
 						continue;
 					}
+					/* -F N-M preserves intermediate delimiters: */
+					//printf "1 2  3  4  5  6  7\n" | toybox cut -O: -F2,4-6,7
+					//2:4  5  6:7
+					if (opt_REGEX && dcount <= cut_list[cl_pos].endpos)
+						continue;
+// NB: toybox does the above for -f too, but it's a compatibility bug:
+//printf "1 2 3 4 5 6 7 8\n" | toybox cut -d' ' -O: -f2,4-6,7
+//2:4 5 6:7  // WRONG!
+//printf "1 2 3 4 5 6 7 8\n" | cut -d' ' --output-delimiter=: -f2,4-6,7
+//2:4:5:6:7  // GNU coreutils 9.1
 				}
 #if ENABLE_FEATURE_CUT_REGEX
 				if (end != start || !opt_REGEX)
diff --git a/testsuite/cut.tests b/testsuite/cut.tests
index 52d05fde3..e57b028ac 100755
--- a/testsuite/cut.tests
+++ b/testsuite/cut.tests
@@ -104,6 +104,19 @@ Weather forecast for tonight : dark.
 Apple: you can buy better, but you can't pay more.
 Subcalifragilisticexpialidocious.
 Auntie Em: Hate you, hate Kansas. Took the dog. Dorothy."
+
+optional FEATURE_CUT_REGEX LONG_OPTS
+testing "cut -F preserves intermediate delimiters" \
+	"cut --output-delimiter=: -F2,4-6,7" \
+	"2:4		5  6:7\n" \
+	"" "1 2  3  4\t\t5  6  7 8\n"
+SKIP=
+
+optional LONG_OPTS
+testing "cut -f does not preserve intermediate delimiters" \
+	"cut --output-delimiter=: -d' ' -f2,4-6,7" \
+	"2:4:5:6:7\n" \
+	"" "1 2 3 4 5 6 7 8\n"
 SKIP=
 
 testing "cut empty field" "cut -d ':' -f 1-3" \
@@ -124,23 +137,25 @@ testing "cut -dNEWLINE" \
 	"2\n4\n6\n7\n" \
 	"" "1\n2\n3\n4\n5\n6\n7"
 
+optional LONG_OPTS
 testing "cut -dNEWLINE --output-delimiter" \
 	"cut -d'
-' -O@@ -f4,2,6-8" \
+' --output-delimiter=@@ -f4,2,6-8" \
 	"2@@4@@6@@7\n" \
 	"" "1\n2\n3\n4\n5\n6\n7"
 
 testing "cut -dNEWLINE --output-delimiter 2" \
 	"cut -d'
-' -O@@ -f4,2,6-8" \
+' --output-delimiter=@@ -f4,2,6-8" \
 	"2@@4@@6@@7\n" \
 	"" "1\n2\n3\n4\n5\n6\n7\n"
 
 testing "cut -dNEWLINE --output-delimiter EMPTY_INPUT" \
 	"cut -d'
-' -O@@ -f4,2,6-8" \
+' --output-delimiter=@@ -f4,2,6-8" \
 	"" \
 	"" ""
+SKIP=
 
 # This seems to work as if delimiter is never found.
 # We test here that -d '' does *not* operate as if there was no -d


More information about the busybox-cvs mailing list