Commit 2c259f178229ec8b71f8a62d4e23b7dbdac007c7

Ran Benita 2014-09-25T21:55:52

symbols: improve FindKeyForSymbol() A bit more involved, but can short circuit. Signed-off-by: Ran Benita <ran234@gmail.com>

diff --git a/src/xkbcomp/symbols.c b/src/xkbcomp/symbols.c
index c60756b..aea2c8a 100644
--- a/src/xkbcomp/symbols.c
+++ b/src/xkbcomp/symbols.c
@@ -1243,38 +1243,32 @@ HandleSymbolsFile(SymbolsInfo *info, XkbFile *file, enum merge_mode merge)
 static struct xkb_key *
 FindKeyForSymbol(struct xkb_keymap *keymap, xkb_keysym_t sym)
 {
-    struct xkb_key *key, *ret = NULL;
-    xkb_layout_index_t group, min_group = UINT32_MAX;
-    xkb_level_index_t level, min_level = UINT32_MAX;
-
-    xkb_keys_foreach(key, keymap) {
-        for (group = 0; group < key->num_groups; group++) {
-            for (level = 0; level < XkbKeyGroupWidth(key, group); level++) {
-                if (key->groups[group].levels[level].num_syms != 1 ||
-                    key->groups[group].levels[level].u.sym != sym)
-                    continue;
-
-                /*
-                 * If the keysym was found in a group or level > 0, we must
-                 * keep looking since we might find a key in which the keysym
-                 * is in a lower group or level.
-                 */
-                if (group < min_group ||
-                    (group == min_group && level < min_level)) {
-                    ret = key;
-                    if (group == 0 && level == 0) {
-                        return ret;
-                    }
-                    else {
-                        min_group = group;
-                        min_level = level;
-                    }
+    struct xkb_key *key;
+    xkb_layout_index_t group;
+    xkb_level_index_t level;
+    bool got_one_group, got_one_level;
+
+    group = 0;
+    do {
+        got_one_group = false;
+        level = 0;
+        do {
+            got_one_level = false;
+            xkb_keys_foreach(key, keymap) {
+                if (group < key->num_groups &&
+                    level < XkbKeyGroupWidth(key, group)) {
+                    got_one_group = got_one_level = true;
+                    if (key->groups[group].levels[level].num_syms == 1 &&
+                        key->groups[group].levels[level].u.sym == sym)
+                        return key;
                 }
             }
-        }
-    }
+            level++;
+        } while (got_one_level);
+        group++;
+    } while (got_one_group);
 
-    return ret;
+    return NULL;
 }
 
 /*