From b9c499c2d6893dd5a6b0ac6e9d405e5095fc79af Mon Sep 17 00:00:00 2001 From: Ben Pfaff Date: Tue, 12 Apr 2011 22:19:45 -0700 Subject: [PATCH] intern: New function intern_strlen(). --- src/libpspp/intern.c | 15 +++++++++++++-- src/libpspp/intern.h | 5 ++++- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/src/libpspp/intern.c b/src/libpspp/intern.c index 258e2829..fe0e784c 100644 --- a/src/libpspp/intern.c +++ b/src/libpspp/intern.c @@ -33,20 +33,22 @@ struct interned_string { 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; @@ -69,6 +71,7 @@ intern_new (const char *s) 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; @@ -119,3 +122,11 @@ is_interned_string (const char *s) unsigned int hash = hash_bytes (s, length, 0); return intern_lookup__ (s, length, hash) != NULL; } + +/* Returns the length of S, which must be an interned string returned by + intern_new(). */ +size_t +intern_strlen (const char *s) +{ + return interned_string_from_string (s)->length; +} diff --git a/src/libpspp/intern.h b/src/libpspp/intern.h index 147a69a8..2f07a9ef 100644 --- a/src/libpspp/intern.h +++ b/src/libpspp/intern.h @@ -1,5 +1,5 @@ /* PSPP - a program for statistical analysis. - Copyright (C) 2010 Free Software Foundation, Inc. + Copyright (C) 2010, 2011 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 @@ -31,11 +31,14 @@ See http://en.wikipedia.org/wiki/String_interning for more information. */ #include +#include const char *intern_new (const char *); const char *intern_ref (const char *); void intern_unref (const char *); +size_t intern_strlen (const char *); + bool is_interned_string (const char *); #endif /* libpspp/intern.h */ -- 2.30.2