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>
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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
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);