Commit 7e0ae4b4d5bfcebd7bf4cefcefe681ea7ecc5f61

Ran Benita 2013-07-21T16:41:27

atom: allow interning non-NUL-terminated strings We need this later. The strlen was calculated anyway, so no loss here. Signed-off-by: Ran Benita <ran234@gmail.com>

diff --git a/src/atom.c b/src/atom.c
index 8edf465..cbc7a14 100644
--- a/src/atom.c
+++ b/src/atom.c
@@ -141,15 +141,13 @@ atom_strdup(struct atom_table *table, xkb_atom_t atom)
 }
 
 static bool
-find_node_pointer(struct atom_table *table, const char *string,
+find_node_pointer(struct atom_table *table, const char *string, size_t len,
                   struct atom_node ***nodep_out, unsigned int *fingerprint_out)
 {
     struct atom_node **nodep;
     unsigned int fingerprint = 0;
-    size_t len;
     bool found = false;
 
-    len = strlen(string);
     nodep = &table->root;
     for (size_t i = 0; i < (len + 1) / 2; i++) {
         fingerprint = fingerprint * 27 + string[i];
@@ -185,7 +183,7 @@ find_node_pointer(struct atom_table *table, const char *string,
 }
 
 xkb_atom_t
-atom_lookup(struct atom_table *table, const char *string)
+atom_lookup(struct atom_table *table, const char *string, size_t len)
 {
     struct atom_node **nodep;
     unsigned int fingerprint;
@@ -193,7 +191,7 @@ atom_lookup(struct atom_table *table, const char *string)
     if (!string)
         return XKB_ATOM_NONE;
 
-    if (!find_node_pointer(table, string, &nodep, &fingerprint))
+    if (!find_node_pointer(table, string, len, &nodep, &fingerprint))
         return XKB_ATOM_NONE;
 
     return (*nodep)->atom;
@@ -201,11 +199,12 @@ atom_lookup(struct atom_table *table, const char *string)
 
 /*
  * If steal is true, we do not strdup @string; therefore it must be
- * dynamically allocated, not be free'd by the caller and not be used
- * afterwards. Use to avoid some redundant allocations.
+ * dynamically allocated, NUL-terminated, not be free'd by the caller
+ * and not be used afterwards. Use to avoid some redundant allocations.
  */
 xkb_atom_t
-atom_intern(struct atom_table *table, const char *string, bool steal)
+atom_intern(struct atom_table *table, const char *string, size_t len,
+            bool steal)
 {
     struct atom_node **nodep;
     struct atom_node *node;
@@ -214,7 +213,7 @@ atom_intern(struct atom_table *table, const char *string, bool steal)
     if (!string)
         return XKB_ATOM_NONE;
 
-    if (find_node_pointer(table, string, &nodep, &fingerprint)) {
+    if (find_node_pointer(table, string, len, &nodep, &fingerprint)) {
         if (steal)
             free(UNCONSTIFY(string));
         return (*nodep)->atom;
diff --git a/src/atom.h b/src/atom.h
index 7ee7acc..94c1494 100644
--- a/src/atom.h
+++ b/src/atom.h
@@ -37,10 +37,11 @@ void
 atom_table_free(struct atom_table *table);
 
 xkb_atom_t
-atom_lookup(struct atom_table *table, const char *string);
+atom_lookup(struct atom_table *table, const char *string, size_t len);
 
 xkb_atom_t
-atom_intern(struct atom_table *table, const char *string, bool steal);
+atom_intern(struct atom_table *table, const char *string, size_t len,
+            bool steal);
 
 char *
 atom_strdup(struct atom_table *table, xkb_atom_t atom);
diff --git a/src/context.c b/src/context.c
index 8d56487..923060e 100644
--- a/src/context.c
+++ b/src/context.c
@@ -331,19 +331,19 @@ xkb_context_new(enum xkb_context_flags flags)
 xkb_atom_t
 xkb_atom_lookup(struct xkb_context *ctx, const char *string)
 {
-    return atom_lookup(ctx->atom_table, string);
+    return atom_lookup(ctx->atom_table, string, strlen(string));
 }
 
 xkb_atom_t
 xkb_atom_intern(struct xkb_context *ctx, const char *string)
 {
-    return atom_intern(ctx->atom_table, string, false);
+    return atom_intern(ctx->atom_table, string, strlen(string), false);
 }
 
 xkb_atom_t
 xkb_atom_steal(struct xkb_context *ctx, char *string)
 {
-    return atom_intern(ctx->atom_table, string, true);
+    return atom_intern(ctx->atom_table, string, strlen(string), true);
 }
 
 char *