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>
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
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 *