]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
cut: optimize UTF-8 input with 0xF5-0xFF delimiters
authorPádraig Brady <P@draigBrady.com>
Fri, 27 Mar 2026 18:59:01 +0000 (18:59 +0000)
committerPádraig Brady <P@draigBrady.com>
Sun, 5 Apr 2026 12:15:56 +0000 (13:15 +0100)
* src/cut.c (bytesearch_field_delim_ok): Expand the range
of bytes that can be simply searched for. 0xF5-0xFF can't
appear in valid UTF-8 characters, and so may be used as
delimiters in UTF-8 input, so it's worth optimizing for.
* tests/cut/cut.pl: Add a test case (mainly as documentation).

src/cut.c
tests/cut/cut.pl

index 2d7797b1b19320f5f606c8df5fcc795fc27a2152..3a7b7ce0a790f2f114dc626898937417b4412db7 100644 (file)
--- a/src/cut.c
+++ b/src/cut.c
@@ -311,6 +311,9 @@ mcel_isblank (mcel_t g)
   return (g.len == 1 && c_isblank (g.ch)) || (g.len > 1 && c32issep (g.ch));
 }
 
+/* Return TRUE if it's valid to do a simple byte search
+   for the delimiter bytes.  */
+
 static inline bool
 bytesearch_field_delim_ok (void)
 {
@@ -318,7 +321,8 @@ bytesearch_field_delim_ok (void)
 
   return (delim_length == 1
           ? (MB_CUR_MAX <= 1
-             || (is_utf8_charset () ? delim_0 < 0x80 : delim_0 < 0x30))
+             || (is_utf8_charset ()
+                 ? (delim_0 < 0x80 || delim_0 > 0xF4) : delim_0 < 0x30))
           : utf8_field_delim_ok ());
 }
 
index bfccdbe85f05e52c7dcc7f07ccd8051ed6c31f5a..a34c9d345bce2e711242d6c1132528b8743661dd 100755 (executable)
@@ -337,6 +337,9 @@ if ($mb_locale ne 'C')
       ['mb-delim-7', '-d', "\xc3\xa9", '-f2',
        {IN=>"a\0b\xc3\xa9c\n"}, {OUT=>"c\n"},
        {ENV => "LC_ALL=$mb_locale"}],
+      ['mb-delim-8', '-d', "\xff", '-f2',  # Note 0xF5-0xFF is efficient
+       {IN=>"a\xffb\n"}, {OUT=>"b\n"},
+       {ENV => "LC_ALL=$mb_locale"}],
       ['mb-w-delim-1', '-w', '-f2', {IN=>"a\xe2\x80\x83b\n"}, {OUT=>"b\n"},
        {ENV => "LC_ALL=$mb_locale"}],
       ['mb-w-delim-2', '-sw', '-f2', {IN=>"a\xc2\xa0b\n"}, {OUT=>""},