Added stringstore_find/alloc_n

Thu, 25 Jan 2007 20:15:55 +0100

author
Tuomo Valkonen <tuomov@iki.fi>
date
Thu, 25 Jan 2007 20:15:55 +0100
changeset 105
de53cada1423
parent 104
abbac137bdac
child 106
f1bb821008fc

Added stringstore_find/alloc_n

stringstore.c file | annotate | diff | comparison | revisions
stringstore.h file | annotate | diff | comparison | revisions
--- 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 <stdlib.h>
+#include <string.h>
 
 #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;
--- 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);
 

mercurial