Hi,

I'm writing a program analysis that tries to find and replace some loops with 
str* functions. I'm trying to see if these replacements are a useful 
refactoring or the loops are intentional. I noticed that patch has a couple of 
these loops and wrote a patch (pasted below) that changes this.

I replaced 3 loops with rawmemchr, which is easier to read, communicates the 
intention better, has less LOC and is probably faster with most libc 
implementations.

The other loops are replaced by strspn, which I also find easier to understand, 
but might be slower, depending on the excepted workload.

Cheers,
Timotej


>From d1e3c918d456485a2692d2dfd4de19c116e3115d Mon Sep 17 00:00:00 2001
From: Timotej Kapus <tk1...@ic.ac.uk>
Date: Fri, 26 Oct 2018 12:33:58 +0100
Subject: [PATCH] Replace loops with string.h functions

* src/inp.c Replace loop with rawmemchr
* src/pch.c Replace loops with rawmemchr and strspn
---
 src/inp.c |  3 +--
 src/pch.c | 22 +++++++---------------
 2 files changed, 8 insertions(+), 17 deletions(-)

diff --git a/src/inp.c b/src/inp.c
index 32d0919..0480e1d 100644
--- a/src/inp.c
+++ b/src/inp.c
@@ -488,8 +488,7 @@ ifetch (lin line, bool whichbuf, size_t *psize)
  if (line == input_lines)
      *psize = last_line_size;
  else {
-     for (q = p;  *q++ != '\n';  )
- /* do nothing */ ;
+     q = rawmemchr(p,'\n') + 1;
      *psize = q - p;
  }
  return p;
diff --git a/src/pch.c b/src/pch.c
index a500ad9..4c1f24f 100644
--- a/src/pch.c
+++ b/src/pch.c
@@ -369,9 +369,7 @@ sha1_says_nonexistent(char const *sha1)
   char const *s;

   /* Nonexisting files have an all-zero checksum.  */
-  for (s = sha1; *s; s++)
-    if (*s != '0')
-      break;
+  s = sha1 + strspn(sha1, "0");
   if (! *s)
     return 2;

@@ -1230,13 +1228,11 @@ another_hunk (enum diff difftype, bool rev)
      return chars_read == (size_t) -1 ? -1 : 0;
  }
  s = buf;
- while (*s == '*')
-     s++;
+ s += strspn(s, "*");
  if (*s == ' ')
    {
      p_c_function = s;
-     while (*s != '\n')
- s++;
+     s = rawmemchr(s,'\n');
      *s = '\0';
      p_c_function = savestr (p_c_function);
      if (! p_c_function)
@@ -1654,8 +1650,7 @@ another_hunk (enum diff difftype, bool rev)
  if (*s++ == '@' && *s == ' ')
    {
      p_c_function = s;
-     while (*s != '\n')
- s++;
+     s = rawmemchr(s, '\n');
      *s = '\0';
      p_c_function = savestr (p_c_function);
      if (! p_c_function)
@@ -2348,14 +2343,12 @@ get_ed_command_letter (char const *line)

   if (ISDIGIT (*p))
     {
-      while (ISDIGIT (*++p))
- /* do nothing */ ;
+      p += strspn(p + 1, "0123456789") + 1;
       if (*p == ',')
  {
    if (! ISDIGIT (*++p))
      return 0;
-   while (ISDIGIT (*++p))
-     /* do nothing */ ;
+   p += strspn(p + 1, "0123456789") + 1;
    pair = true;
  }
     }
@@ -2384,8 +2377,7 @@ get_ed_command_letter (char const *line)
       return 0;
     }

-  while (*p == ' ' || *p == '\t')
-    p++;
+  p += strspn(p, " \t");
   if (*p == '\n')
     return letter;
   return 0;
--
2.7.4


Reply via email to