]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
split: auto set suffix len for --numeric-suffixes=<N --number=N
authorPádraig Brady <P@draigBrady.com>
Wed, 6 May 2015 00:48:40 +0000 (01:48 +0100)
committerPádraig Brady <P@draigBrady.com>
Wed, 13 May 2015 11:43:40 +0000 (12:43 +0100)
Supporting `split --numeric-suffixes=1 -n100` for example.

* doc/coreutils.texi (split invocation): Mention the two
use cases for the FROM parameter, and the consequences on
the suffix length determination.
* src/split.c (set_suffix_length): Use the --numeric-suffixes
FROM parameter in the suffix width calculation, when it's
less than the number of files specified in --number.
* tests/split/suffix-auto-length.sh: Add test cases.
Fixes http://bugs.gnu.org/20511

doc/coreutils.texi
src/split.c
tests/split/suffix-auto-length.sh

index 6a69b75cab44a780ab3eaec9688fe63dc3eea462..4ae06c30b973f2a5b4f046fafa985f107cc1639e 100644 (file)
@@ -3181,9 +3181,14 @@ specified, will auto increase the length by 2 as required.
 @opindex --numeric-suffixes
 Use digits in suffixes rather than lower-case letters.  The numerical
 suffix counts from @var{from} if specified, 0 otherwise.
-Note specifying a @var{from} value also disables the default
-auto suffix length expansion described above, and so you may also
-want to specify @option{-a} to allow suffixes beyond @samp{99}.
+
+@var{from} is used to either set the initial suffix for a single run,
+or to set the suffix offset for independently split inputs, and consequently
+the auto suffix length expansion described above is disabled.  Therefore
+you may also want to use option @option{-a} to allow suffixes beyond @samp{99}.
+Note if option @option{--number} is specified and the number of files is less
+than @var{from}, a single run is assumed and the minimum suffix length
+required is automatically determined.
 
 @item --additional-suffix=@var{suffix}
 @opindex --additional-suffix
index 5d6043f70b58bb03fea7b05500818a51dcffa97f..b6fe2ddefcd887b59e14da1d05c032249d44590f 100644 (file)
@@ -39,6 +39,7 @@
 #include "sig2str.h"
 #include "xfreopen.h"
 #include "xdectoint.h"
+#include "xstrtol.h"
 
 /* The official name of this program (e.g., no 'g' prefix).  */
 #define PROGRAM_NAME "split"
@@ -173,9 +174,26 @@ set_suffix_length (uintmax_t n_units, enum Split_type split_type)
   if (split_type == type_chunk_bytes || split_type == type_chunk_lines
       || split_type == type_rr)
     {
+      uintmax_t n_units_end = n_units;
+      if (numeric_suffix_start)
+        {
+          uintmax_t n_start;
+          strtol_error e = xstrtoumax (numeric_suffix_start, NULL, 10,
+                                       &n_start, "");
+          if (e == LONGINT_OK && n_start <= UINTMAX_MAX - n_units)
+            {
+              /* Restrict auto adjustment so we don't keep
+                 incrementing a suffix size arbitrarily,
+                 as that would break sort order for files
+                 generated from multiple split runs.  */
+              if (n_start < n_units)
+                n_units_end += n_start;
+            }
+
+        }
       size_t alphabet_len = strlen (suffix_alphabet);
-      bool alphabet_slop = (n_units % alphabet_len) != 0;
-      while (n_units /= alphabet_len)
+      bool alphabet_slop = (n_units_end % alphabet_len) != 0;
+      while (n_units_end /= alphabet_len)
         suffix_needed++;
       suffix_needed += alphabet_slop;
       suffix_auto = false;
index 6ee220066f6624438ad2c4dea691b3fa9abc0a42..6ef221a1686d2a448ed5aab3fc68f8cff893e849 100755 (executable)
@@ -1,5 +1,5 @@
 #!/bin/sh
-# Test the suffix auto widening functionality
+# Test the suffix auto width functionality
 
 # Copyright (C) 2012-2015 Free Software Foundation, Inc.
 
 print_ver_ split
 
 
-# ensure this feature is off when start number specified
-truncate -s12 file.in
+# ensure auto widening is off when start number specified
+truncate -s12 file.in || framework_failure_
 returns_ 1 split file.in -b1 --numeric=89 || fail=1
 test "$(ls -1 x* | wc -l)" = 11 || fail=1
 rm -f x*
 
-# ensure this feature works when no start num specified
-truncate -s91 file.in
+# ensure auto widening works when no start num specified
+truncate -s91 file.in || framework_failure_
 for prefix in 'x' 'xx' ''; do
     for add_suffix in '.txt' ''; do
       split file.in "$prefix" -b1 --numeric --additional-suffix="$add_suffix" \
@@ -39,4 +39,15 @@ for prefix in 'x' 'xx' ''; do
     done
 done
 
+# ensure auto width with --number and start num < number of files
+# That's the single run use case which is valid to adjust suffix len
+truncate -s100 file.in || framework_failure_
+split --numeric-suffixes=1 --number=r/100 file.in || fail=1
+rm -f x*
+
+# ensure no auto width with --number and start num >= number of files
+# That's the multi run use case which is invalid to adjust suffix len
+# as that would result in an incorrect order for the total output file set
+returns_ 1 split --numeric-suffixes=100 --number=r/100 file.in || fail=1
+
 Exit $fail