Skip to contentSkip to conversation

kernel lib parser: cleaned up code and fixed redundancies

PATCH Jonathan Ho 3 participants 3 messages Dec 29, 2004 Library code

lore
Share & tools
lore
#0 lore raw

Just cleaned up code and fixed variable assignment redundancies.

Signed-off-by <[email protected]>

----------------------------------

lib/parser.c Tue Dec 28 12:35:38 2004 renamed from lib/parser.c.orig Fri Dec 24 13:34:33 2004 +0 -0
@@ -104,8 +104,7 @@ int match_token(char *s, match_table_t t
{
    struct match_token *p;

- for (p = table; !match_one(s, p->pattern, args) ; p++)
- ;
+ for (p = table; !match_one(s, p->pattern, args); p++);

    return p->token;
}

@@ -122,9 +121,8 @@ int match_token(char *s, match_table_t t
*/
static int match_number(substring_t *s, int *result, int base) {

-    char *endp;
-    char *buf;
-    int ret;
+    char *endp, *buf;
+    int ret = 0;
    buf = kmalloc(s->to - s->from + 1, GFP_KERNEL);
    if (!buf)
@@ -132,7 +130,6 @@ static int match_number(substring_t *s,
    memcpy(buf, s->from, s->to - s->from);
    buf[s->to - s->from] = '\0';
    *result = simple_strtol(buf, &endp, base);
-    ret = 0;
    if (endp == buf)
        ret = -EINVAL;
    kfree(buf);
#1 lore raw
Quoted 9 lines · Jonathan Ho

Just cleaned up code and fixed variable assignment redundancies.

Signed-off-by <[email protected]>

----------------------------------

lib/parser.c Tue Dec 28 12:35:38 2004 int match_token(char *s, match_table_t t · line 108

Quoted 7 lines
lib/parser.c Tue Dec 28 12:35:38 2004 renamed from lib/parser.c.orig Fri Dec 24 13:34:33 2004 +1 -2
@@ -104,8 +104,7 @@ int match_token(char *s, match_table_t t
 {
    struct match_token *p;
 
-    for (p = table; !match_one(s, p->pattern, args) ; p++)
-        ;
+    for (p = table; !match_one(s, p->pattern, args); p++);
 

Personally I prefer the ";" on a line by itself - make it more obvious that the for loop is supposed to be empty and thus more readable. If you move the semicolon it it looks more like an error on casual inspection.
Personally I'd probably have changed the existing code to something like this if at all :

for (p = table; !match_one(s, p->pattern, args); p++)

	; /* empty body */

Then it's really obvious.

Quoted 12 lines
lib/parser.c Tue Dec 28 12:35:38 2004 renamed from lib/parser.c.orig Fri Dec 24 13:34:33 2004 +2 -3
 static int match_number(substring_t *s, int *result, int base)
 {
-    char *endp;
-    char *buf;
-    int ret;
+    char *endp, *buf;
+    int ret = 0;
 
    buf = kmalloc(s->to - s->from + 1, GFP_KERNEL);
    if (!buf)

lib/parser.c Tue Dec 28 12:35:38 2004 static int match_number(substring_t *s, · line 135

Quoted 7 lines
lib/parser.c Tue Dec 28 12:35:38 2004 renamed from lib/parser.c.orig Fri Dec 24 13:34:33 2004 +0 -1
@@ -132,7 +130,6 @@ static int match_number(substring_t *s,
    memcpy(buf, s->from, s->to - s->from);
    buf[s->to - s->from] = '\0';
    *result = simple_strtol(buf, &endp, base);
-    ret = 0;
    if (endp == buf)
        ret = -EINVAL;
    kfree(buf);

Why move the assignment of ret? the way it was the assignment would only be done when nessesary (we can bail out of the function before the poing where ret was set to 0) - with your code we'll always set ret to 0 even when we bail out early with return -ENOMEM; and ret is not used at all.

Signature · 1 line
Jesper Juhl 
#2 lore raw
In reply to Jonathan Ho

Review

Quoted 3 lines
(quoted diff) +1 -2
-    for (p = table; !match_one(s, p->pattern, args) ; p++)
-        ;
+    for (p = table; !match_one(s, p->pattern, args); p++);

this is a regression in readability. Also your patch is whitespace-damaged.