]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
maint: prefer rawmemchr to memchr when easy
authorPaul Eggert <eggert@cs.ucla.edu>
Wed, 15 Sep 2021 21:09:03 +0000 (14:09 -0700)
committerPaul Eggert <eggert@cs.ucla.edu>
Wed, 15 Sep 2021 22:08:28 +0000 (15:08 -0700)
* bootstrap.conf (gnulib_modules): Add rawmemchr.
* src/csplit.c: Include idx.h.
* src/csplit.c (record_line_starts):
* src/head.c (elide_tail_lines_pipe):
* src/shuf.c (next_line):
* src/split.c (lines_split):
* src/tail.c (pipe_lines):
* src/wc.c (wc_lines):
Prefer rawmemchr to memchr when rawmemchr is easy.
* src/csplit.c (load_buffer):
* src/head.c (struct linebuffer):
Make room for a 1-byte sentinel.

bootstrap.conf
src/csplit.c
src/head.c
src/shuf.c
src/split.c
src/tail.c
src/wc.c

index 481a37e9c3aeec4d6385fbfe5084a7f637ac7bb4..bcfc6f0a0c998cc2471db24651d49ae2f2f912c7 100644 (file)
@@ -210,6 +210,7 @@ gnulib_modules="
   quotearg
   randint
   randperm
+  rawmemchr
   read-file
   readlink
   readtokens
index e1fb66ed247a9d578c96472e1c8d9f8dbadda442..a7191fedd2f019fdbc809e86ca1c0e6c964a3b25 100644 (file)
@@ -31,6 +31,7 @@
 #include "die.h"
 #include "error.h"
 #include "fd-reopen.h"
+#include "idx.h"
 #include "quote.h"
 #include "safe-read.h"
 #include "stdio--.h"
@@ -344,8 +345,6 @@ static size_t
 record_line_starts (struct buffer_record *b)
 {
   char *line_start;            /* Start of current line. */
-  char *line_end;              /* End of each line found. */
-  size_t bytes_left;           /* Length of incomplete last line. */
   size_t lines;                        /* Number of lines found. */
   size_t line_length;          /* Length of each line found. */
 
@@ -354,21 +353,22 @@ record_line_starts (struct buffer_record *b)
 
   lines = 0;
   line_start = b->buffer;
-  bytes_left = b->bytes_used;
+  char *buffer_end = line_start + b->bytes_used;
+  *buffer_end = '\n';
 
   while (true)
     {
-      line_end = memchr (line_start, '\n', bytes_left);
-      if (line_end == NULL)
+      char *line_end = rawmemchr (line_start, '\n');
+      if (line_end == buffer_end)
         break;
       line_length = line_end - line_start + 1;
       keep_new_line (b, line_start, line_length);
-      bytes_left -= line_length;
       line_start = line_end + 1;
       lines++;
     }
 
   /* Check for an incomplete last line. */
+  idx_t bytes_left = buffer_end - line_start;
   if (bytes_left)
     {
       if (have_read_eof)
@@ -492,9 +492,10 @@ load_buffer (void)
     return false;
 
   /* We must make the buffer at least as large as the amount of data
-     in the partial line left over from the last call. */
-  if (bytes_wanted < hold_count)
-    bytes_wanted = hold_count;
+     in the partial line left over from the last call,
+     plus room for a sentinel '\n'. */
+  if (bytes_wanted <= hold_count)
+    bytes_wanted = hold_count + 1;
 
   while (true)
     {
@@ -512,7 +513,7 @@ load_buffer (void)
           hold_count = 0;
         }
 
-      b->bytes_used += read_input (p, bytes_avail);
+      b->bytes_used += read_input (p, bytes_avail - 1);
 
       lines_found = record_line_starts (b);
 
index 7b2a440418fe332cf04bec73b3d271a4cf0cc4c1..04d0cd8aa90893eafae6be9f349cc833a4f9619d 100644 (file)
@@ -500,7 +500,7 @@ elide_tail_lines_pipe (char const *filename, int fd, uintmax_t n_elide,
 {
   struct linebuffer
   {
-    char buffer[BUFSIZ];
+    char buffer[BUFSIZ + 1];
     size_t nbytes;
     size_t nlines;
     struct linebuffer *next;
@@ -539,9 +539,10 @@ elide_tail_lines_pipe (char const *filename, int fd, uintmax_t n_elide,
 
       /* Count the number of newlines just read.  */
       {
-        char const *buffer_end = tmp->buffer + n_read;
+        char *buffer_end = tmp->buffer + n_read;
+        *buffer_end = line_end;
         char const *p = tmp->buffer;
-        while ((p = memchr (p, line_end, buffer_end - p)))
+        while ((p = rawmemchr (p, line_end)) < buffer_end)
           {
             ++p;
             ++tmp->nlines;
index 1af1b533ade4fb3d07fc1e7b990f264fd2a087a3..553b293503cacd8c80b24af2081ba9a64cd646db 100644 (file)
@@ -134,13 +134,13 @@ input_from_argv (char **operand, int n_operands, char eolbyte)
   operand[n_operands] = p;
 }
 
-/* Return the start of the next line after LINE.  The current line
-   ends in EOLBYTE, and is guaranteed to end before LINE + N.  */
+/* Return the start of the next line after LINE, which is guaranteed
+   to end in EOLBYTE.  */
 
 static char *
-next_line (char *line, char eolbyte, size_t n)
+next_line (char *line, char eolbyte)
 {
-  char *p = memchr (line, eolbyte, n);
+  char *p = rawmemchr (line, eolbyte);
   return p + 1;
 }
 
@@ -284,14 +284,14 @@ read_input (FILE *in, char eolbyte, char ***pline)
   lim = buf + used;
 
   n_lines = 0;
-  for (p = buf; p < lim; p = next_line (p, eolbyte, lim - p))
+  for (p = buf; p < lim; p = next_line (p, eolbyte))
     n_lines++;
 
   *pline = line = xnmalloc (n_lines + 1, sizeof *line);
 
   line[0] = p = buf;
   for (size_t i = 1; i <= n_lines; i++)
-    line[i] = p = next_line (p, eolbyte, lim - p);
+    line[i] = p = next_line (p, eolbyte);
 
   return n_lines;
 }
index 6062f052a12e679b423aaec0f7a59dd35c908907..4b1b144d06fdf697c42bb555d30da2f1a1f361df 100644 (file)
@@ -716,7 +716,7 @@ lines_split (uintmax_t n_lines, char *buf, size_t bufsize)
       *eob = eolchar;
       while (true)
         {
-          bp = memchr (bp, eolchar, eob - bp + 1);
+          bp = rawmemchr (bp, eolchar);
           if (bp == eob)
             {
               if (eob != bp_out) /* do not write 0 bytes! */
index 99977afa7b30cbf131f9d6a728e45f4ce5bc65b4..eb15b933f331d74fbf67eaccaa2a26f2bda0f6e3 100644 (file)
@@ -713,8 +713,7 @@ pipe_lines (char const *pretty_filename, int fd, uintmax_t n_lines,
         size_t j;
         for (j = total_lines - n_lines; j; --j)
           {
-            beg = memchr (beg, line_end, buffer_end - beg);
-            assert (beg);
+            beg = rawmemchr (beg, line_end);
             ++beg;
           }
       }
index bdb51928d846aad92364b05e4facf7575d3e4464..ec2a4e18416ad92c1d116caa254c6664c6a34957 100644 (file)
--- a/src/wc.c
+++ b/src/wc.c
@@ -319,8 +319,9 @@ wc_lines (char const *file, int fd, uintmax_t *lines_out, uintmax_t *bytes_out)
         }
       else
         {
-          /* memchr is more efficient with longer lines.  */
-          while ((p = memchr (p, '\n', end - p)))
+          /* rawmemchr is more efficient with longer lines.  */
+          *end = '\n';
+          while ((p = rawmemchr (p, '\n')) < end)
             {
               ++p;
               ++lines;