Skip to content

Commit

Permalink
Added stringcache_iscached().
Browse files Browse the repository at this point in the history
  • Loading branch information
icculus committed Oct 11, 2012
1 parent f4e4575 commit de4220b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
24 changes: 21 additions & 3 deletions mojoshader_common.c
Expand Up @@ -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];
Expand All @@ -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;
Expand All @@ -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.
Expand Down
1 change: 1 addition & 0 deletions mojoshader_internal.h
Expand Up @@ -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);


Expand Down

0 comments on commit de4220b

Please sign in to comment.