From b8ae68c2a345934902abcce6ea15a46b39d3ff07 Mon Sep 17 00:00:00 2001 From: Ran Benita Date: Thu, 17 May 2012 13:55:38 +0300 Subject: [PATCH] rules: rewrite get_index to use sscanf Signed-off-by: Ran Benita --- src/xkbcomp/rules.c | 40 +++++++++++++++++----------------------- 1 file changed, 17 insertions(+), 23 deletions(-) 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 -- 2.7.4