Added stringcache_iscached().
authorRyan C. Gordon <icculus@icculus.org>
Wed, 10 Oct 2012 21:33:36 -0400
changeset 1116 0ef3d106dab9
parent 1115 ce5fbd601bc0
child 1117 f6712bf72c19
Added stringcache_iscached().
mojoshader_common.c
mojoshader_internal.h
--- a/mojoshader_common.c	Wed Oct 10 21:32:58 2012 -0400
+++ b/mojoshader_common.c	Wed Oct 10 21:33:36 2012 -0400
@@ -328,13 +328,16 @@
     void *d;
 };
 
+
 const char *stringcache(StringCache *cache, const char *str)
 {
     return stringcache_len(cache, str, strlen(str));
 } // stringcache
 
-const char *stringcache_len(StringCache *cache, const char *str,
-                             const unsigned int len)
+static const char *stringcache_len_internal(StringCache *cache,
+                                            const char *str,
+                                            const unsigned int len,
+                                            const int addmissing)
 {
     const uint8 hash = hash_string(str, len) & (cache->table_size-1);
     StringBucket *bucket = cache->hashtable[hash];
@@ -358,7 +361,11 @@
         bucket = bucket->next;
     } // while
 
-    // no match, add to the table.
+    // no match!
+    if (!addmissing)
+        return NULL;
+
+    // add to the table.
     bucket = (StringBucket *) cache->m(sizeof (StringBucket), cache->d);
     if (bucket == NULL)
         return NULL;
@@ -373,8 +380,19 @@
     bucket->next = cache->hashtable[hash];
     cache->hashtable[hash] = bucket;
     return bucket->string;
+} // stringcache_len_internal
+
+const char *stringcache_len(StringCache *cache, const char *str,
+                            const unsigned int len)
+{
+    return stringcache_len_internal(cache, str, len, 1);
 } // stringcache_len
 
+int stringcache_iscached(StringCache *cache, const char *str)
+{
+    return (stringcache_len_internal(cache, str, strlen(str), 0) != NULL);
+} // stringcache_iscached
+
 const char *stringcache_fmt(StringCache *cache, const char *fmt, ...)
 {
     char buf[128];  // use the stack if reasonable.
--- a/mojoshader_internal.h	Wed Oct 10 21:32:58 2012 -0400
+++ b/mojoshader_internal.h	Wed Oct 10 21:33:36 2012 -0400
@@ -217,6 +217,7 @@
 const char *stringcache_len(StringCache *cache, const char *str,
                             const unsigned int len);
 const char *stringcache_fmt(StringCache *cache, const char *fmt, ...);
+int stringcache_iscached(StringCache *cache, const char *str);
 void stringcache_destroy(StringCache *cache);