# HG changeset patch # User Tuomo Valkonen # Date 1169752555 -3600 # Node ID de53cada1423bb99156f030eea50089b938358a2 # Parent abbac137bdac3976dd70534e664b07eaa557a22a Added stringstore_find/alloc_n diff -r abbac137bdac -r de53cada1423 stringstore.c --- a/stringstore.c Sun Jan 21 21:02:55 2007 +0100 +++ b/stringstore.c Thu Jan 25 20:15:55 2007 +0100 @@ -8,6 +8,7 @@ */ #include +#include #include "misc.h" #include "output.h" @@ -23,16 +24,37 @@ return (const char*)(((Rb_node)id)->k.key); } + +typedef struct{ + const char *key; + uint len; +} D; + +static int cmp(const void *d_, const char *nodekey) +{ + D *d=(D*)d_; -StringId stringstore_find(const char *str) + int res=strncmp(d->key, nodekey, d->len); + + return (res!=0 + ? res + : (nodekey[d->len]=='\0' ? 0 : 1)); +} + + +StringId stringstore_find_n(const char *str, uint l) { Rb_node node; int found=0; + D d; if(stringstore==NULL) return STRINGID_NONE; - node=rb_find_key_n(stringstore, str, &found); + d.key=str; + d.len=l; + + node=rb_find_gkey_n(stringstore, &d, (Rb_compfn*)cmp, &found); if(!found) return STRINGID_NONE; @@ -41,9 +63,15 @@ } -StringId stringstore_alloc(const char *str) +StringId stringstore_find(const char *str) { - Rb_node node=(Rb_node)stringstore_find(str); + return stringstore_find_n(str, strlen(str)); +} + + +StringId stringstore_alloc_n(const char *str, uint l) +{ + Rb_node node=(Rb_node)stringstore_find_n(str, l); char *s; if(node!=NULL){ @@ -57,7 +85,7 @@ return STRINGID_NONE; } - s=scopy(str); + s=scopyn(str, l); if(s==NULL) return STRINGID_NONE; @@ -73,6 +101,12 @@ } +StringId stringstore_alloc(const char *str) +{ + return stringstore_alloc_n(str, strlen(str)); +} + + void stringstore_free(StringId id) { Rb_node node=(Rb_node)id; diff -r abbac137bdac -r de53cada1423 stringstore.h --- a/stringstore.h Sun Jan 21 21:02:55 2007 +0100 +++ b/stringstore.h Thu Jan 25 20:15:55 2007 +0100 @@ -17,6 +17,8 @@ extern const char *stringstore_get(StringId id); extern StringId stringstore_find(const char *str); extern StringId stringstore_alloc(const char *str); +extern StringId stringstore_find_n(const char *str, uint l); +extern StringId stringstore_alloc_n(const char *str, uint l); extern void stringstore_free(StringId id); extern void stringstore_ref(StringId id);