Use strstart() instead of strncmp().
authorChristoph Mallon <christoph.mallon@gmx.de>
Wed, 14 Sep 2011 09:02:50 +0000 (11:02 +0200)
committerChristoph Mallon <christoph.mallon@gmx.de>
Wed, 14 Sep 2011 09:02:50 +0000 (11:02 +0200)
adt/strutil.c
builtins.c
driver/firm_opt.c
warning.c
wrappergen/write_jna.c

index fc494fe..93fec69 100644 (file)
@@ -3,12 +3,11 @@
 
 bool streq_underscore(const char *const s1, const char *const s2)
 {
-       if (s2[0] == '_' && s2[1] == '_') {
-               size_t len2 = strlen(s2);
-               size_t len1 = strlen(s1);
-               if (len1 == len2 - 4 && s2[len2 - 2] == '_' && s2[len2 - 1] == '_') {
-                       return strncmp(s1, s2 + 2, len2 - 4) == 0;
-               }
+       char const* const middle = strstart(s2, "__");
+       if (middle) {
+               char const* const rest = strstart(middle, s1);
+               if (rest && streq(rest, "__"))
+                       return true;
        }
 
        return streq(s1, s2);
index 924b14f..8236a4e 100644 (file)
@@ -19,6 +19,7 @@
  */
 #include "config.h"
 
+#include "adt/strutil.h"
 #include "type_t.h"
 #include "types.h"
 #include "entity_t.h"
@@ -234,7 +235,7 @@ void adapt_special_functions(function_t *function)
 
        /* Disregard prefix _, __, __x or __builtin_.  */
        if (name[0] == '_') {
-               if (!strncmp(name + 1, "_builtin_", 9))
+               if (strstart(name + 1, "_builtin_"))
                        name += 10;
                else if (name[1] == '_' && name[2] == 'x')
                        name += 3;
index f87b9ff..f1535ba 100644 (file)
@@ -868,11 +868,8 @@ static void disable_all_opts(void)
 
 static bool firm_opt_option(const char *opt)
 {
-       bool enable = true;
-       if (strncmp(opt, "no-", 3) == 0) {
-               enable = false;
-               opt = opt + 3;
-       }
+       char const* const rest   = strstart(opt, "no-");
+       bool        const enable = rest ? opt = rest, false : true;
 
        opt_config_t *config = get_opt(opt);
        if (config == NULL || (config->flags & OPT_FLAG_HIDE_OPTIONS))
index 6bffa2b..8caacbf 100644 (file)
--- a/warning.c
+++ b/warning.c
@@ -21,6 +21,7 @@
 #include <stdio.h>
 #include <string.h>
 
+#include "adt/strutil.h"
 #include "adt/util.h"
 #include "warning.h"
 #include "help.h"
@@ -99,8 +100,9 @@ void set_warning_opt(const char *const opt)
 {
        /* Process prefixes: -W[no-][error=] */
        char const *s     = opt;
-       bool const  no    = strncmp(s, "no-",    3) == 0 ? s += 3, true : false;
-       bool const  error = strncmp(s, "error=", 6) == 0 ? s += 6, true : false;
+       char const *rest;
+       bool const  no    = (rest = strstart(s, "no-"))    ? s = rest, true : false;
+       bool const  error = (rest = strstart(s, "error=")) ? s = rest, true : false;
 
        warn_state_t on  = WARN_STATE_NONE;
        warn_state_t off = WARN_STATE_NONE;
index 1739481..2cdadb9 100644 (file)
@@ -22,6 +22,7 @@
 #include <errno.h>
 #include <string.h>
 
+#include "adt/strutil.h"
 #include "write_jna.h"
 #include "symbol_t.h"
 #include "ast_t.h"
@@ -46,7 +47,7 @@ static const char    *libname;
 
 static bool is_system_header(const char *fname)
 {
-       if (strncmp(fname, "/usr/include", 12) == 0)
+       if (strstart(fname, "/usr/include"))
                return true;
        if (fname == builtin_source_position.input_name)
                return true;