From 24ee5649494cfcb94fbc4027bc347ad18f65428c Mon Sep 17 00:00:00 2001 From: Gustavo Sverzut Barbieri Date: Tue, 28 Oct 2008 00:31:09 +0000 Subject: [PATCH] Cheap way to get strlen of a shared string. it's not safe, but it's faster and can help for large strings, maximum cost is 4 comparisons plus one pointer access. SVN revision: 37257 --- legacy/eina/src/include/eina_stringshare.h | 1 + legacy/eina/src/lib/eina_stringshare.c | 30 ++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/legacy/eina/src/include/eina_stringshare.h b/legacy/eina/src/include/eina_stringshare.h index d5e21654ca..0ad23f5fcb 100644 --- a/legacy/eina/src/include/eina_stringshare.h +++ b/legacy/eina/src/include/eina_stringshare.h @@ -69,6 +69,7 @@ EAPI int eina_stringshare_init(void); EAPI int eina_stringshare_shutdown(void); EAPI const char *eina_stringshare_add(const char *str); EAPI void eina_stringshare_del(const char *str); +EAPI int eina_stringshare_strlen(const char *str); EAPI void eina_stringshare_dump(void); /** diff --git a/legacy/eina/src/lib/eina_stringshare.c b/legacy/eina/src/lib/eina_stringshare.c index 9d70be7d41..2830613bef 100644 --- a/legacy/eina/src/lib/eina_stringshare.c +++ b/legacy/eina/src/lib/eina_stringshare.c @@ -891,6 +891,36 @@ eina_stringshare_del(const char *str) if (getenv("EINA_ERROR_ABORT")) abort(); } +/** + * @brief Note that the given string @b must be shared. + * + * @param str the shared string to know the length. It is safe to + * give NULL, in that case -1 is returned. + * + * This function is a cheap way to known the length of a shared + * string. Note that if the given pointer is not shared or NULL, bad + * things will happen, likely a segmentation fault. If in doubt, try + * strlen(). + */ +EAPI int +eina_stringshare_strlen(const char *str) +{ + const Eina_Stringshare_Node *node; + + if (!str) + return -1; + + /* special cases */ + if (str[0] == '\0') return 0; + if (str[1] == '\0') return 1; + if (str[2] == '\0') return 2; + if (str[3] == '\0') return 3; + + node = (void *)(str - sizeof(Eina_Stringshare_Node)); + EINA_MAGIC_CHECK_STRINGSHARE_NODE(node); + return node->length; +} + struct dumpinfo { int used, saved, dups, unique;