Skip to content
Permalink
Browse files
Added stringcache_iscached().
  • Loading branch information
icculus committed Oct 11, 2012
1 parent f4e4575 commit de4220bcad6c528a5562c1f7c5f1565bcaad37d6
Showing with 22 additions and 3 deletions.
  1. +21 −3 mojoshader_common.c
  2. +1 −0 mojoshader_internal.h
@@ -328,13 +328,16 @@ struct StringCache
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 @@ const char *stringcache_len(StringCache *cache, const char *str,
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 @@ const char *stringcache_len(StringCache *cache, const char *str,
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.
@@ -217,6 +217,7 @@ const char *stringcache(StringCache *cache, const char *str);
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);


0 comments on commit de4220b

Please sign in to comment.