nasm.c: getkw -- use string helpers
authorCyrill Gorcunov <gorcunov@gmail.com>
Fri, 18 Sep 2009 15:23:53 +0000 (19:23 +0400)
committerCyrill Gorcunov <gorcunov@gmail.com>
Tue, 13 Oct 2009 15:42:05 +0000 (19:42 +0400)
This allow us to shrink code a bit and
make it easy to read.

Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
nasm.c

diff --git a/nasm.c b/nasm.c
index 7bf7c57..2004965 100644 (file)
--- a/nasm.c
+++ b/nasm.c
@@ -1772,47 +1772,38 @@ static enum directives getkw(char **directive, char **value)
 {
     char *p, *q, *buf;
 
-    buf = *directive;
-
-    /*  allow leading spaces or tabs */
-    while (*buf == ' ' || *buf == '\t')
-        buf++;
+    buf = nasm_skip_spaces(*directive);
 
+    /* it should be enclosed in [ ] */
     if (*buf != '[')
-        return 0;
-
-    p = buf;
-
-    while (*p && *p != ']')
-        p++;
+        return D_NONE;
+    q = strchr(buf, ']');
+    if (!q)
+        return D_NONE;
+
+    /* stip off the comments */
+    p = strchr(buf, ';');
+    if (p) {
+        if (p < q) /* ouch! somwhere inside */
+            return D_NONE;
+        *p = '\0';
+    }
 
-    if (!*p)
-        return 0;
+    /* no brace, no trailing spaces */
+    *q = '\0';
+    nasm_zap_spaces_rev(--q);
 
-    q = p++;
+    /* directive */
+    p = nasm_skip_spaces(++buf);
+    q = nasm_skip_word(p);
+    if (!q)
+        return D_NONE; /* sigh... no value there */
+    *q = '\0';
+    *directive = p;
 
-    while (*p && *p != ';') {
-        if (!nasm_isspace(*p))
-            return 0;
-        p++;
-    }
-    q[1] = '\0';
-
-    *directive = p = buf + 1;
-    while (*buf && *buf != ' ' && *buf != ']' && *buf != '\t')
-        buf++;
-    if (*buf == ']') {
-        *buf = '\0';
-        *value = buf;
-    } else {
-        *buf++ = '\0';
-        while (nasm_isspace(*buf))
-            buf++;              /* beppu - skip leading whitespace */
-        *value = buf;
-        while (*buf != ']')
-            buf++;
-        *buf++ = '\0';
-    }
+    /* and value finally */
+    p = nasm_skip_spaces(++q);
+    *value = p;
 
     return find_directive(*directive);
 }