Commit c79c80335be8a60e36b5ad21a05ff351d5bfa3f9

Ran Benita 2019-11-09T21:25:01

atom: combine atom_intern() and atom_lookup() Use an "add" bool parameter instead. This simplifies the code a bit. Signed-off-by: Ran Benita <ran@unusedvar.com>

diff --git a/src/atom.c b/src/atom.c
index e78c68f..86d02cb 100644
--- a/src/atom.c
+++ b/src/atom.c
@@ -149,9 +149,8 @@ atom_text(struct atom_table *table, xkb_atom_t atom)
     return darray_item(table->table, atom).string;
 }
 
-static bool
-find_atom_pointer(struct atom_table *table, const char *string, size_t len,
-                  xkb_atom_t **atomp_out, uint32_t *fingerprint_out)
+xkb_atom_t
+atom_intern(struct atom_table *table, const char *string, size_t len, bool add)
 {
     uint32_t fingerprint = hash_buf(string, len);
 
@@ -169,7 +168,7 @@ find_atom_pointer(struct atom_table *table, const char *string, size_t len,
             /* Now start testing the strings. */
             const int cmp = strncmp(string, node->string, len);
             if (cmp == 0 && node->string[len] == '\0') {
-                break;
+                return *atomp;
             }
             else if (cmp < 0) {
                 atomp = &node->left;
@@ -180,31 +179,9 @@ find_atom_pointer(struct atom_table *table, const char *string, size_t len,
         }
     }
 
-    if (fingerprint_out)
-        *fingerprint_out = fingerprint;
-    if (atomp_out)
-        *atomp_out = atomp;
-    return *atomp != XKB_ATOM_NONE;
-}
-
-xkb_atom_t
-atom_lookup(struct atom_table *table, const char *string, size_t len)
-{
-    xkb_atom_t *atomp;
-    if (!find_atom_pointer(table, string, len, &atomp, NULL))
+    if (!add)
         return XKB_ATOM_NONE;
 
-    return *atomp;
-}
-
-xkb_atom_t
-atom_intern(struct atom_table *table, const char *string, size_t len)
-{
-    xkb_atom_t *atomp;
-    uint32_t fingerprint;
-    if (find_atom_pointer(table, string, len, &atomp, &fingerprint))
-        return *atomp;
-
     struct atom_node node;
     node.string = strndup(string, len);
     assert(node.string != NULL);
@@ -214,6 +191,5 @@ atom_intern(struct atom_table *table, const char *string, size_t len)
     /* Do this before the append, as it may realloc and change the offsets. */
     *atomp = atom;
     darray_append(table->table, node);
-
     return atom;
 }
diff --git a/src/atom.h b/src/atom.h
index f936911..49478db 100644
--- a/src/atom.h
+++ b/src/atom.h
@@ -37,10 +37,7 @@ void
 atom_table_free(struct atom_table *table);
 
 xkb_atom_t
-atom_lookup(struct atom_table *table, const char *string, size_t len);
-
-xkb_atom_t
-atom_intern(struct atom_table *table, const char *string, size_t len);
+atom_intern(struct atom_table *table, const char *string, size_t len, bool add);
 
 const char *
 atom_text(struct atom_table *table, xkb_atom_t atom);
diff --git a/src/context-priv.c b/src/context-priv.c
index e3ba32d..03324fd 100644
--- a/src/context-priv.c
+++ b/src/context-priv.c
@@ -52,13 +52,13 @@ xkb_context_failed_include_path_get(struct xkb_context *ctx,
 xkb_atom_t
 xkb_atom_lookup(struct xkb_context *ctx, const char *string)
 {
-    return atom_lookup(ctx->atom_table, string, strlen(string));
+    return atom_intern(ctx->atom_table, string, strlen(string), false);
 }
 
 xkb_atom_t
 xkb_atom_intern(struct xkb_context *ctx, const char *string, size_t len)
 {
-    return atom_intern(ctx->atom_table, string, len);
+    return atom_intern(ctx->atom_table, string, len, true);
 }
 
 const char *
diff --git a/test/atom.c b/test/atom.c
index 592eca6..f196946 100644
--- a/test/atom.c
+++ b/test/atom.c
@@ -27,10 +27,10 @@
 #include "atom.h"
 
 #define INTERN_LITERAL(table, literal) \
-    atom_intern(table, literal, sizeof(literal) - 1)
+    atom_intern(table, literal, sizeof(literal) - 1, true)
 
 #define LOOKUP_LITERAL(table, literal) \
-    atom_lookup(table, literal, sizeof(literal) - 1)
+    atom_intern(table, literal, sizeof(literal) - 1, false)
 
 static void
 random_string(char **str_out, size_t *len_out)
@@ -85,7 +85,7 @@ test_random_strings(void)
     for (int i = 0; i < N; i++) {
         random_string(&arr[i].string, &arr[i].len);
 
-        atom = atom_lookup(table, arr[i].string, arr[i].len);
+        atom = atom_intern(table, arr[i].string, arr[i].len, false);
         if (atom != XKB_ATOM_NONE) {
             string = atom_text(table, atom);
             assert(string);
@@ -107,7 +107,7 @@ test_random_strings(void)
             continue;
         }
 
-        arr[i].atom = atom_intern(table, arr[i].string, arr[i].len);
+        arr[i].atom = atom_intern(table, arr[i].string, arr[i].len, true);
         if (arr[i].atom == XKB_ATOM_NONE) {
             fprintf(stderr, "failed to intern! len: %lu, string: %.*s\n",
                     arr[i].len, (int) arr[i].len, arr[i].string);
@@ -158,14 +158,14 @@ main(void)
     assert(table);
 
     assert(atom_text(table, XKB_ATOM_NONE) == NULL);
-    assert(atom_lookup(table, NULL, 0) == XKB_ATOM_NONE);
+    assert(atom_intern(table, NULL, 0, false) == XKB_ATOM_NONE);
 
     atom1 = INTERN_LITERAL(table, "hello");
     assert(atom1 != XKB_ATOM_NONE);
     assert(atom1 == LOOKUP_LITERAL(table, "hello"));
     assert(streq(atom_text(table, atom1), "hello"));
 
-    atom2 = atom_intern(table, "hello", 3);
+    atom2 = atom_intern(table, "hello", 3, true);
     assert(atom2 != XKB_ATOM_NONE);
     assert(atom1 != atom2);
     assert(streq(atom_text(table, atom2), "hel"));
@@ -173,7 +173,7 @@ main(void)
     assert(LOOKUP_LITERAL(table, "hell") == XKB_ATOM_NONE);
     assert(LOOKUP_LITERAL(table, "hello") == atom1);
 
-    atom3 = atom_intern(table, "", 0);
+    atom3 = atom_intern(table, "", 0, true);
     assert(atom3 != XKB_ATOM_NONE);
     assert(LOOKUP_LITERAL(table, "") == atom3);