84b277
From 31ad188e3cec2909ce0041aa43ca816d0eee6ed1 Mon Sep 17 00:00:00 2001
84b277
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
84b277
Date: Wed, 3 Sep 2014 22:55:52 -0400
84b277
Subject: [PATCH] localed: introduce helper function to simplify matching
84b277
84b277
Conflicts:
84b277
        src/locale/localed.c
84b277
84b277
(cherry picked from commit 81fd105a5f9762fa2f2e42bc949876e32b3a126f)
84b277
84b277
Related: #1109145
84b277
---
84b277
 src/locale/localed.c | 22 ++++++++++------------
84b277
 1 file changed, 10 insertions(+), 12 deletions(-)
84b277
84b277
diff --git a/src/locale/localed.c b/src/locale/localed.c
84b277
index c22e12d..56900a6 100644
84b277
--- a/src/locale/localed.c
84b277
+++ b/src/locale/localed.c
84b277
@@ -145,6 +145,12 @@ static int free_and_set(char **s, const char *v) {
84b277
         return 0;
84b277
 }
84b277
 
84b277
+static bool startswith_comma(const char *s, const char *prefix) {
84b277
+        const char *t;
84b277
+
84b277
+        return s && (t = startswith(s, prefix)) && (*t == ',');
84b277
+}
84b277
+
84b277
 static void free_data_locale(void) {
84b277
         int p;
84b277
 
84b277
@@ -861,26 +867,18 @@ static int find_legacy_keymap(char **new_keymap) {
84b277
                         /* If we got an exact match, this is best */
84b277
                         matching = 10;
84b277
                 else {
84b277
-                        size_t x;
84b277
-
84b277
-                        x = strcspn(state.x11_layout, ",");
84b277
-
84b277
                         /* We have multiple X layouts, look for an
84b277
                          * entry that matches our key with everything
84b277
                          * but the first layout stripped off. */
84b277
-                        if (x > 0 &&
84b277
-                            strlen(a[1]) == x &&
84b277
-                            strneq(state.x11_layout, a[1], x))
84b277
+                        if (startswith_comma(state.x11_layout, a[1]))
84b277
                                 matching = 5;
84b277
                         else  {
84b277
-                                size_t w;
84b277
+                                char *x;
84b277
 
84b277
                                 /* If that didn't work, strip off the
84b277
                                  * other layouts from the entry, too */
84b277
-                                w = strcspn(a[1], ",");
84b277
-
84b277
-                                if (x > 0 && x == w &&
84b277
-                                    memcmp(state.x11_layout, a[1], x) == 0)
84b277
+                                x = strndupa(a[1], strcspn(a[1], ","));
84b277
+                                if (startswith_comma(state.x11_layout, x))
84b277
                                         matching = 1;
84b277
                         }
84b277
                 }