summaryrefslogtreecommitdiff
path: root/st.c
diff options
context:
space:
mode:
authorksaito <ksaito@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2005-01-28 15:21:48 +0000
committerksaito <ksaito@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2005-01-28 15:21:48 +0000
commita19d6b33d7419ed3724ee6646fa9303d9542b5e5 (patch)
tree297425b4ec8a0690858d8a21f49797a0ddacaedf /st.c
parent7d765e5203d8c909c1956276ead32b1f3d8c351f (diff)
* ascii.c, euc_jp.c, hash.c, oniggnu.h, oniguruma.h, regcomp.c, regenc.c, regenc.h, regerror.c, regexec.c, reggnu.c, regint.h, regparse.c, regparse.h, sjis.c, st.c, st.h, utf8.c: imported Oni Guruma 3.5.4.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@7846 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'st.c')
-rw-r--r--st.c157
1 files changed, 145 insertions, 12 deletions
diff --git a/st.c b/st.c
index e4036f1025..cad73288f6 100644
--- a/st.c
+++ b/st.c
@@ -6,12 +6,29 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
-#include "st.h"
#ifdef _WIN32
#include <malloc.h>
#endif
+#ifdef NOT_RUBY
+#include "regint.h"
+#else
+#ifdef RUBY_PLATFORM
+#define xmalloc ruby_xmalloc
+#define xcalloc ruby_xcalloc
+#define xrealloc ruby_xrealloc
+#define xfree ruby_xfree
+
+void *xmalloc(long);
+void *xcalloc(long, long);
+void *xrealloc(void *, long);
+void xfree(void *);
+#endif
+#endif
+
+#include "st.h"
+
typedef struct st_table_entry st_table_entry;
struct st_table_entry {
@@ -33,11 +50,14 @@ struct st_table_entry {
* allocated initially
*
*/
+
static int numcmp(long, long);
static int numhash(long);
static struct st_hash_type type_numhash = {
numcmp,
numhash,
+ st_nothing_key_free,
+ st_nothing_key_clone
};
/* extern int strcmp(const char *, const char *); */
@@ -45,19 +65,21 @@ static int strhash(const char *);
static struct st_hash_type type_strhash = {
strcmp,
strhash,
+ st_nothing_key_free,
+ st_nothing_key_clone
};
-#ifdef RUBY_PLATFORM
-#define xmalloc ruby_xmalloc
-#define xcalloc ruby_xcalloc
-#define xrealloc ruby_xrealloc
-#define xfree ruby_xfree
+static int strend_cmp(st_strend_key*, st_strend_key*);
+static int strend_hash(st_strend_key*);
+static int strend_key_free(st_data_t key);
+static st_data_t strend_key_clone(st_data_t x);
-void *xmalloc(long);
-void *xcalloc(long, long);
-void *xrealloc(void *, long);
-void xfree(void *);
-#endif
+static struct st_hash_type type_strend_hash = {
+ strend_cmp,
+ strend_hash,
+ strend_key_free,
+ strend_key_clone
+};
static void rehash(st_table *);
@@ -125,7 +147,7 @@ new_size(size)
int newsize;
for (i = 0, newsize = MINSIZE;
- i < sizeof(primes)/sizeof(primes[0]);
+ i < (int )(sizeof(primes)/sizeof(primes[0]));
i++, newsize <<= 1)
{
if (newsize > size) return primes[i];
@@ -206,6 +228,13 @@ st_init_strtable_with_size(size)
return st_init_table_with_size(&type_strhash, size);
}
+st_table*
+st_init_strend_table_with_size(size)
+ int size;
+{
+ return st_init_table_with_size(&type_strend_hash, size);
+}
+
void
st_free_table(table)
st_table *table;
@@ -267,6 +296,21 @@ st_lookup(table, key, value)
}
}
+int
+st_lookup_strend(table, str_key, end_key, value)
+ st_table *table;
+ unsigned char* str_key;
+ unsigned char* end_key;
+ st_data_t *value;
+{
+ st_strend_key key;
+
+ key.s = (unsigned char* )str_key;
+ key.end = (unsigned char* )end_key;
+
+ return st_lookup(table, (st_data_t )(&key), value);
+}
+
#define ADD_DIRECT(table, key, value, hash_val, bin_pos)\
do {\
st_table_entry *entry;\
@@ -307,6 +351,22 @@ st_insert(table, key, value)
}
}
+int
+st_insert_strend(table, str_key, end_key, value)
+ st_table *table;
+ unsigned char* str_key;
+ unsigned char* end_key;
+ st_data_t value;
+{
+ st_strend_key* key;
+
+ key = alloc(st_strend_key);
+ key->s = (unsigned char* )str_key;
+ key->end = (unsigned char* )end_key;
+
+ return st_insert(table, (st_data_t )key, value);
+}
+
void
st_add_direct(table, key, value)
st_table *table;
@@ -320,6 +380,21 @@ st_add_direct(table, key, value)
ADD_DIRECT(table, key, value, hash_val, bin_pos);
}
+void
+st_add_direct_strend(table, str_key, end_key, value)
+ st_table *table;
+ unsigned char* str_key;
+ unsigned char* end_key;
+ st_data_t value;
+{
+ st_strend_key* key;
+
+ key = alloc(st_strend_key);
+ key->s = (unsigned char* )str_key;
+ key->end = (unsigned char* )end_key;
+ st_add_direct(table, (st_data_t )key, value);
+}
+
static void
rehash(table)
register st_table *table;
@@ -379,6 +454,7 @@ st_copy(old_table)
return 0;
}
*entry = *ptr;
+ entry->key = old_table->type->key_clone(ptr->key);
entry->next = new_table->bins[i];
new_table->bins[i] = entry;
ptr = ptr->next;
@@ -522,6 +598,7 @@ st_foreach(table, func, arg)
last->next = ptr->next;
}
ptr = ptr->next;
+ table->type->key_free(tmp->key);
free(tmp);
table->num_entries--;
}
@@ -581,3 +658,59 @@ numhash(n)
{
return n;
}
+
+extern int
+st_nothing_key_free(st_data_t key) { return 0; }
+
+extern st_data_t
+st_nothing_key_clone(st_data_t x) { return x; }
+
+static int strend_cmp(st_strend_key* x, st_strend_key* y)
+{
+ unsigned char *p, *q;
+ int c;
+
+ if ((x->end - x->s) != (y->end - y->s))
+ return 1;
+
+ p = x->s;
+ q = y->s;
+ while (p < x->end) {
+ c = (int )*p - (int )*q;
+ if (c != 0) return c;
+
+ p++; q++;
+ }
+
+ return 0;
+}
+
+static int strend_hash(st_strend_key* x)
+{
+ int val;
+ unsigned char *p;
+
+ val = 0;
+ p = x->s;
+ while (p < x->end) {
+ val = val * 997 + (int )*p++;
+ }
+
+ return val + (val >> 5);
+}
+
+static int strend_key_free(st_data_t x)
+{
+ xfree((void* )x);
+ return 0;
+}
+
+static st_data_t strend_key_clone(st_data_t x)
+{
+ st_strend_key* new_key;
+ st_strend_key* key = (st_strend_key* )x;
+
+ new_key = alloc(st_strend_key);
+ *new_key = *key;
+ return (st_data_t )new_key;
+}