rules: rewrite get_index to use sscanf Signed-off-by: Ran Benita <ran234@gmail.com>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
diff --git a/src/xkbcomp/rules.c b/src/xkbcomp/rules.c
index a5200a8..00667c9 100644
--- a/src/xkbcomp/rules.c
+++ b/src/xkbcomp/rules.c
@@ -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