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>master
parent
adbd9c6f08
commit
c79c80335b
32
src/atom.c
32
src/atom.c
|
@ -149,9 +149,8 @@ atom_text(struct atom_table *table, xkb_atom_t atom)
|
||||||
return darray_item(table->table, atom).string;
|
return darray_item(table->table, atom).string;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool
|
xkb_atom_t
|
||||||
find_atom_pointer(struct atom_table *table, const char *string, size_t len,
|
atom_intern(struct atom_table *table, const char *string, size_t len, bool add)
|
||||||
xkb_atom_t **atomp_out, uint32_t *fingerprint_out)
|
|
||||||
{
|
{
|
||||||
uint32_t fingerprint = hash_buf(string, len);
|
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. */
|
/* Now start testing the strings. */
|
||||||
const int cmp = strncmp(string, node->string, len);
|
const int cmp = strncmp(string, node->string, len);
|
||||||
if (cmp == 0 && node->string[len] == '\0') {
|
if (cmp == 0 && node->string[len] == '\0') {
|
||||||
break;
|
return *atomp;
|
||||||
}
|
}
|
||||||
else if (cmp < 0) {
|
else if (cmp < 0) {
|
||||||
atomp = &node->left;
|
atomp = &node->left;
|
||||||
|
@ -180,31 +179,9 @@ find_atom_pointer(struct atom_table *table, const char *string, size_t len,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (fingerprint_out)
|
if (!add)
|
||||||
*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))
|
|
||||||
return XKB_ATOM_NONE;
|
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;
|
struct atom_node node;
|
||||||
node.string = strndup(string, len);
|
node.string = strndup(string, len);
|
||||||
assert(node.string != NULL);
|
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. */
|
/* Do this before the append, as it may realloc and change the offsets. */
|
||||||
*atomp = atom;
|
*atomp = atom;
|
||||||
darray_append(table->table, node);
|
darray_append(table->table, node);
|
||||||
|
|
||||||
return atom;
|
return atom;
|
||||||
}
|
}
|
||||||
|
|
|
@ -37,10 +37,7 @@ void
|
||||||
atom_table_free(struct atom_table *table);
|
atom_table_free(struct atom_table *table);
|
||||||
|
|
||||||
xkb_atom_t
|
xkb_atom_t
|
||||||
atom_lookup(struct atom_table *table, const char *string, size_t len);
|
atom_intern(struct atom_table *table, const char *string, size_t len, bool add);
|
||||||
|
|
||||||
xkb_atom_t
|
|
||||||
atom_intern(struct atom_table *table, const char *string, size_t len);
|
|
||||||
|
|
||||||
const char *
|
const char *
|
||||||
atom_text(struct atom_table *table, xkb_atom_t atom);
|
atom_text(struct atom_table *table, xkb_atom_t atom);
|
||||||
|
|
|
@ -52,13 +52,13 @@ xkb_context_failed_include_path_get(struct xkb_context *ctx,
|
||||||
xkb_atom_t
|
xkb_atom_t
|
||||||
xkb_atom_lookup(struct xkb_context *ctx, const char *string)
|
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_t
|
||||||
xkb_atom_intern(struct xkb_context *ctx, const char *string, size_t len)
|
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 *
|
const char *
|
||||||
|
|
14
test/atom.c
14
test/atom.c
|
@ -27,10 +27,10 @@
|
||||||
#include "atom.h"
|
#include "atom.h"
|
||||||
|
|
||||||
#define INTERN_LITERAL(table, literal) \
|
#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) \
|
#define LOOKUP_LITERAL(table, literal) \
|
||||||
atom_lookup(table, literal, sizeof(literal) - 1)
|
atom_intern(table, literal, sizeof(literal) - 1, false)
|
||||||
|
|
||||||
static void
|
static void
|
||||||
random_string(char **str_out, size_t *len_out)
|
random_string(char **str_out, size_t *len_out)
|
||||||
|
@ -85,7 +85,7 @@ test_random_strings(void)
|
||||||
for (int i = 0; i < N; i++) {
|
for (int i = 0; i < N; i++) {
|
||||||
random_string(&arr[i].string, &arr[i].len);
|
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) {
|
if (atom != XKB_ATOM_NONE) {
|
||||||
string = atom_text(table, atom);
|
string = atom_text(table, atom);
|
||||||
assert(string);
|
assert(string);
|
||||||
|
@ -107,7 +107,7 @@ test_random_strings(void)
|
||||||
continue;
|
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) {
|
if (arr[i].atom == XKB_ATOM_NONE) {
|
||||||
fprintf(stderr, "failed to intern! len: %lu, string: %.*s\n",
|
fprintf(stderr, "failed to intern! len: %lu, string: %.*s\n",
|
||||||
arr[i].len, (int) arr[i].len, arr[i].string);
|
arr[i].len, (int) arr[i].len, arr[i].string);
|
||||||
|
@ -158,14 +158,14 @@ main(void)
|
||||||
assert(table);
|
assert(table);
|
||||||
|
|
||||||
assert(atom_text(table, XKB_ATOM_NONE) == NULL);
|
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");
|
atom1 = INTERN_LITERAL(table, "hello");
|
||||||
assert(atom1 != XKB_ATOM_NONE);
|
assert(atom1 != XKB_ATOM_NONE);
|
||||||
assert(atom1 == LOOKUP_LITERAL(table, "hello"));
|
assert(atom1 == LOOKUP_LITERAL(table, "hello"));
|
||||||
assert(streq(atom_text(table, atom1), "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(atom2 != XKB_ATOM_NONE);
|
||||||
assert(atom1 != atom2);
|
assert(atom1 != atom2);
|
||||||
assert(streq(atom_text(table, atom2), "hel"));
|
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, "hell") == XKB_ATOM_NONE);
|
||||||
assert(LOOKUP_LITERAL(table, "hello") == atom1);
|
assert(LOOKUP_LITERAL(table, "hello") == atom1);
|
||||||
|
|
||||||
atom3 = atom_intern(table, "", 0);
|
atom3 = atom_intern(table, "", 0, true);
|
||||||
assert(atom3 != XKB_ATOM_NONE);
|
assert(atom3 != XKB_ATOM_NONE);
|
||||||
assert(LOOKUP_LITERAL(table, "") == atom3);
|
assert(LOOKUP_LITERAL(table, "") == atom3);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue