/* PSPP - a program for statistical analysis.
- Copyright (C) 2010, 2011 Free Software Foundation, Inc.
+ Copyright (C) 2010, 2011, 2012 Free Software Foundation, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
{
struct hmap_node node; /* Node in hash table. */
size_t ref_cnt; /* Reference count. */
+ size_t length; /* strlen(string). */
char string[1]; /* Null-terminated string. */
};
/* All interned strings. */
static struct hmap interns = HMAP_INITIALIZER (interns);
-/* Searches the table of interned string for */
+/* Searches the table of interned strings for one equal to S, which has length
+ LENGTH and hash value HASH. */
static struct interned_string *
intern_lookup__ (const char *s, size_t length, unsigned int hash)
{
struct interned_string *is;
HMAP_FOR_EACH_WITH_HASH (is, struct interned_string, node, hash, &interns)
- if (!memcmp (s, is->string, length + 1))
+ if (is->length == length && !memcmp (s, is->string, length))
return is;
return NULL;
const char *
intern_new (const char *s)
{
- size_t length = strlen (s);
+ return intern_buffer (s, strlen (s));
+}
+
+const char *
+intern_buffer (const char *s, size_t length)
+{
unsigned int hash = hash_bytes (s, length, 0);
struct interned_string *is;
is = xmalloc (length + sizeof *is);
hmap_insert (&interns, &is->node, hash);
is->ref_cnt = 1;
+ is->length = length;
memcpy (is->string, s, length + 1);
}
return is->string;
}
static struct interned_string *
-interned_string_from_string (const char *s)
+interned_string_from_string (const char *s_)
{
+ char (*s)[1] = (char (*)[1]) s_;
struct interned_string *is = UP_CAST (s, struct interned_string, string);
assert (is->ref_cnt > 0);
return is;
}
}
+/* Returns the length of interned string S. */
+size_t
+intern_strlen (const char *s)
+{
+ return interned_string_from_string (s)->length;
+}
+
/* Given null-terminated string S, returns true if S is an interned string
returned by intern_string_new(), false otherwise.