atom: expand variable names

A bit easier to understand at a glance.

Signed-off-by: Ran Benita <ran234@gmail.com>
master
Ran Benita 2013-07-21 16:32:21 +03:00
parent 9ffe9dae1d
commit 9cd29453ae
2 changed files with 43 additions and 47 deletions

View File

@ -142,37 +142,35 @@ atom_strdup(struct atom_table *table, xkb_atom_t atom)
static bool static bool
find_node_pointer(struct atom_table *table, const char *string, find_node_pointer(struct atom_table *table, const char *string,
struct atom_node ***np_out, unsigned int *fingerprint_out) struct atom_node ***nodep_out, unsigned int *fingerprint_out)
{ {
struct atom_node **np; struct atom_node **nodep;
unsigned i; unsigned int fingerprint = 0;
int comp;
unsigned int fp = 0;
size_t len; size_t len;
bool found = false; bool found = false;
len = strlen(string); len = strlen(string);
np = &table->root; nodep = &table->root;
for (i = 0; i < (len + 1) / 2; i++) { for (size_t i = 0; i < (len + 1) / 2; i++) {
fp = fp * 27 + string[i]; fingerprint = fingerprint * 27 + string[i];
fp = fp * 27 + string[len - 1 - i]; fingerprint = fingerprint * 27 + string[len - 1 - i];
} }
while (*np) { while (*nodep) {
if (fp < (*np)->fingerprint) { if (fingerprint < (*nodep)->fingerprint) {
np = &((*np)->left); nodep = &((*nodep)->left);
} }
else if (fp > (*np)->fingerprint) { else if (fingerprint > (*nodep)->fingerprint) {
np = &((*np)->right); nodep = &((*nodep)->right);
} }
else { else {
/* now start testing the strings */ /* Now start testing the strings. */
comp = strncmp(string, (*np)->string, len); const int cmp = strncmp(string, (*nodep)->string, len);
if (comp < 0 || (comp == 0 && len < strlen((*np)->string))) { if (cmp < 0 || (cmp == 0 && len < strlen((*nodep)->string))) {
np = &((*np)->left); nodep = &((*nodep)->left);
} }
else if (comp > 0) { else if (cmp > 0) {
np = &((*np)->right); nodep = &((*nodep)->right);
} }
else { else {
found = true; found = true;
@ -181,24 +179,24 @@ find_node_pointer(struct atom_table *table, const char *string,
} }
} }
*fingerprint_out = fp; *fingerprint_out = fingerprint;
*np_out = np; *nodep_out = nodep;
return found; return found;
} }
xkb_atom_t xkb_atom_t
atom_lookup(struct atom_table *table, const char *string) atom_lookup(struct atom_table *table, const char *string)
{ {
struct atom_node **np; struct atom_node **nodep;
unsigned int fp; unsigned int fingerprint;
if (!string) if (!string)
return XKB_ATOM_NONE; return XKB_ATOM_NONE;
if (!find_node_pointer(table, string, &np, &fp)) if (!find_node_pointer(table, string, &nodep, &fingerprint))
return XKB_ATOM_NONE; return XKB_ATOM_NONE;
return (*np)->atom; return (*nodep)->atom;
} }
/* /*
@ -207,42 +205,41 @@ atom_lookup(struct atom_table *table, const char *string)
* afterwards. Use to avoid some redundant allocations. * afterwards. Use to avoid some redundant allocations.
*/ */
xkb_atom_t xkb_atom_t
atom_intern(struct atom_table *table, const char *string, atom_intern(struct atom_table *table, const char *string, bool steal)
bool steal)
{ {
struct atom_node **np; struct atom_node **nodep;
struct atom_node *nd; struct atom_node *node;
unsigned int fp; unsigned int fingerprint;
if (!string) if (!string)
return XKB_ATOM_NONE; return XKB_ATOM_NONE;
if (find_node_pointer(table, string, &np, &fp)) { if (find_node_pointer(table, string, &nodep, &fingerprint)) {
if (steal) if (steal)
free(UNCONSTIFY(string)); free(UNCONSTIFY(string));
return (*np)->atom; return (*nodep)->atom;
} }
nd = malloc(sizeof(*nd)); node = malloc(sizeof(*node));
if (!nd) if (!node)
return XKB_ATOM_NONE; return XKB_ATOM_NONE;
if (steal) { if (steal) {
nd->string = UNCONSTIFY(string); node->string = UNCONSTIFY(string);
} }
else { else {
nd->string = strdup(string); node->string = strdup(string);
if (!nd->string) { if (!node->string) {
free(nd); free(node);
return XKB_ATOM_NONE; return XKB_ATOM_NONE;
} }
} }
*np = nd; *nodep = node;
nd->left = nd->right = NULL; node->left = node->right = NULL;
nd->fingerprint = fp; node->fingerprint = fingerprint;
nd->atom = darray_size(table->table); node->atom = darray_size(table->table);
darray_append(table->table, nd); darray_append(table->table, node);
return nd->atom; return node->atom;
} }

View File

@ -40,8 +40,7 @@ xkb_atom_t
atom_lookup(struct atom_table *table, const char *string); atom_lookup(struct atom_table *table, const char *string);
xkb_atom_t xkb_atom_t
atom_intern(struct atom_table *table, const char *string, atom_intern(struct atom_table *table, const char *string, bool steal);
bool steal);
char * char *
atom_strdup(struct atom_table *table, xkb_atom_t atom); atom_strdup(struct atom_table *table, xkb_atom_t atom);