]> git.ipfire.org Git - thirdparty/make.git/commitdiff
Replace strcmp() with memcmp() where possible
authorNoah Goldstein <goldstein.w.n@gmail.com>
Sun, 24 Apr 2022 21:43:56 +0000 (17:43 -0400)
committerPaul Smith <psmith@gnu.org>
Sun, 24 Apr 2022 21:52:54 +0000 (17:52 -0400)
memcmp() is always faster than strcmp().  In places where we already
know that both buffers have sufficient size, replace strcmp() with
memcmp().

* src/main.c (main): Replace strncmp() with memcmp()==0.
* src/read.c (word1eq): Ditto.
* src/commands.c (set_file_variables): Ditto.
* src/function.c (func_filter_filterout): Ditto.
(a_word_hash_cmp): Use STRING_N_COMPARE since we know the length.
(func_sort): Replace strcmp() with memcmp().

src/commands.c
src/function.c
src/main.c
src/read.c

index 644fe43be6b73d4baaf356190b4c08fac7c1922e..cef51d359bce9eb96a0ff31e558a5aa09878f7b7 100644 (file)
@@ -121,8 +121,9 @@ set_file_variables (struct file *file, const char *stem)
 
       for (d = enter_file (strcache_add (".SUFFIXES"))->deps; d ; d = d->next)
         {
-          size_t slen = strlen (dep_name (d));
-          if (len > slen && strneq (dep_name (d), name + (len - slen), slen))
+          const char *dn = dep_name (d);
+          size_t slen = strlen (dn);
+          if (len > slen && memcmp (dn, name + (len - slen), slen) == 0)
             {
               file->stem = stem = strcache_add_len (name, len - slen);
               break;
index 38ab9667823dd6197c506d32109feeb795ac119e..89449a8fa4c07c17d6b27362435f2fabe331b1b0 100644 (file)
@@ -990,8 +990,9 @@ a_word_hash_cmp (const void *x, const void *y)
   int result = (int) ((struct a_word const *) x)->length - ((struct a_word const *) y)->length;
   if (result)
     return result;
-  return_STRING_COMPARE (((struct a_word const *) x)->str,
-                         ((struct a_word const *) y)->str);
+  return_STRING_N_COMPARE (((struct a_word const *) x)->str,
+                           ((struct a_word const *) y)->str,
+                           ((struct a_word const *) y)->length);
 }
 
 struct a_pattern
@@ -1110,7 +1111,7 @@ func_filter_filterout (char *o, char **argv, const char *funcname)
       else
         for (wp = words; wp < word_end; ++wp)
           wp->matched |= (wp->length == pp->length
-                          && strneq (pp->str, wp->str, wp->length));
+                          && memcmp (pp->str, wp->str, wp->length) == 0);
     }
 
   /* Output the words that matched (or didn't, for filter-out).  */
@@ -1245,7 +1246,7 @@ func_sort (char *o, char **argv, const char *funcname UNUSED)
         {
           len = strlen (words[i]);
           if (i == wordi - 1 || strlen (words[i + 1]) != len
-              || strcmp (words[i], words[i + 1]))
+              || memcmp (words[i], words[i + 1], len))
             {
               o = variable_buffer_output (o, words[i], len);
               o = variable_buffer_output (o, " ", 1);
index 9053935bc668e919e18ffad337165391aff4fdcd..829824bbd6f2714a704a2c8ce297207ff08b03a1 100644 (file)
@@ -1404,7 +1404,7 @@ main (int argc, char **argv, char **envp)
 
         /* If this is MAKE_RESTARTS, check to see if the "already printed
            the enter statement" flag is set.  */
-        if (len == 13 && strneq (envp[i], "MAKE_RESTARTS", 13))
+        if (len == 13 && memcmp (envp[i], "MAKE_RESTARTS", 13) == 0)
           {
             if (*ep == '-')
               {
index 6007273f67fcbeba0487677a01d2d7fba64586b5..97e71bac89db2bb858cc592382f7942a212ee6f5 100644 (file)
@@ -164,9 +164,8 @@ static char *unescape_char (char *string, int c);
 
 
 /* Compare a word, both length and contents.
-   P must point to the word to be tested, and WLEN must be the length.
-*/
-#define word1eq(s)      (wlen == CSTRLEN (s) && strneq (s, p, CSTRLEN (s)))
+   P must point to the word to be tested, and WLEN must be the length.  */
+#define word1eq(s)  (wlen == CSTRLEN (s) && memcmp (s, p, CSTRLEN (s)) == 0)
 
 \f
 /* Read in all the makefiles and return a chain of targets to rebuild.  */