Subject: bug#33144: Replace some loops with str functions

From: "Kapus, Timotej" <[email-masked]>
Date: Wed, 24 Oct 2018 23:06:41 +0000

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 diff has a couple of these loops and wrote a patch (pasted below) that changes this.

I find strspn easier to read, communicate intention and has less LOC, but would be interested to hear why you would like to keep the loops to.

Cheers,
Timotej

>From 38e7030f127f567fd2d02f27663168dde4472d57 Mon Sep 17 00:00:00 2001
From: Timotej Kapus <[email-masked]>
Date: Wed, 24 Oct 2018 23:47:22 +0100
Subject: [PATCH] maint: change 3 loops to strspn calls

  * src/ifdef.c: Change 3 loops to equivalent calls to strspn
---
 src/ifdef.c | 11 ++++-------
 1 file changed, 4 insertions(+), 7 deletions(-)

diff --git a/src/ifdef.c b/src/ifdef.c
index 0ecc2c0..8046ac5 100644
--- a/src/ifdef.c
+++ b/src/ifdef.c
@@ -313,13 +313,10 @@ do_printf_spec (FILE *out, char const *spec,
   /* Scan printf-style SPEC of the form %[-'0]*[0-9]*(.[0-9]*)?[cdoxX].  */
   /* assert (*f == '%'); */
   f++;
-  while ((c = *f++) == '-' || c == '\'' || c == '0')
-    continue;
-  while (ISDIGIT (c))
-    c = *f++;
-  if (c == '.')
-    while (ISDIGIT (c = *f++))
-      continue;
+  f += strspn(f, "-\'0") + 1;
+  f += strspn(f, "0123456789") + 1;
+  if ((c = *f++) == '.')
+    f += strspn(f, "0123456789") + 1;
   c1 = *f++;

   switch (c)
--
2.7.4