rules: rewrite get_index to use sscanf
authorRan Benita <ran234@gmail.com>
Thu, 17 May 2012 10:55:38 +0000 (13:55 +0300)
committerRan Benita <ran234@gmail.com>
Sun, 20 May 2012 17:31:48 +0000 (20:31 +0300)
Signed-off-by: Ran Benita <ran234@gmail.com>
src/xkbcomp/rules.c

index a5200a8..00667c9 100644 (file)
@@ -275,34 +275,28 @@ struct rules {
     struct group *groups;
 };
 
-#define NDX_BUFF_SIZE  4
-
 /***====================================================================***/
 
-static char*
+/*
+ * Resolve numeric index, such as "[4]" in layout[4]. Missing index
+ * means zero.
+ */
+static char *
 get_index(char *str, int *ndx)
 {
-   char ndx_buf[NDX_BUFF_SIZE];
-   char *end;
+    int empty = 0, consumed = 0, num;
+
+    sscanf(str, "[%n%d]%n", &empty, &num, &consumed);
+    if (consumed > 0) {
+        *ndx = num;
+        str += consumed;
+    } else if (empty > 0) {
+        *ndx = -1;
+    } else {
+        *ndx = 0;
+    }
 
-   if (*str != '[') {
-       *ndx = 0;
-       return str;
-   }
-   str++;
-   end = strchr(str, ']');
-   if (end == NULL) {
-       *ndx = -1;
-       return str - 1;
-   }
-   if ( (end - str) >= NDX_BUFF_SIZE) {
-       *ndx = -1;
-       return end + 1;
-   }
-   strncpy(ndx_buf, str, end - str);
-   ndx_buf[end - str] = '\0';
-   *ndx = atoi(ndx_buf);
-   return end + 1;
+    return str;
 }
 
 static void