Skip to content

Commit

Permalink
Only use unsigned characters
Browse files Browse the repository at this point in the history
  • Loading branch information
armon committed Feb 6, 2014
1 parent 363bd1d commit 6153ec0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 19 deletions.
26 changes: 13 additions & 13 deletions src/art.c
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ static inline int min(int a, int b) {
* Returns the number of prefix characters shared between
* the key and node.
*/
static int check_prefix(art_node *n, char *key, int key_len, int depth) {
static int check_prefix(art_node *n, unsigned char *key, int key_len, int depth) {
int max_cmp = min(min(n->partial_len, MAX_PREFIX_LEN), key_len - depth);
int idx;
for (idx=0; idx < max_cmp; idx++) {
Expand All @@ -202,7 +202,7 @@ static int check_prefix(art_node *n, char *key, int key_len, int depth) {
* Checks if a leaf matches
* @return 0 on success.
*/
static int leaf_matches(art_leaf *n, char *key, int key_len, int depth) {
static int leaf_matches(art_leaf *n, unsigned char *key, int key_len, int depth) {
(void)depth;
// Fail if the key lengths are different
if (n->key_len != (uint32_t)key_len) return 1;
Expand All @@ -219,7 +219,7 @@ static int leaf_matches(art_leaf *n, char *key, int key_len, int depth) {
* @return NULL if the item was not found, otherwise
* the value pointer is returned.
*/
void* art_search(art_tree *t, char *key, int key_len) {
void* art_search(art_tree *t, unsigned char *key, int key_len) {
art_node **child;
art_node *n = t->root;
int prefix_len, depth = 0;
Expand Down Expand Up @@ -316,7 +316,7 @@ art_leaf* art_maximum(art_tree *t) {
return maximum((art_node*)t->root);
}

static art_leaf* make_leaf(char *key, int key_len, void *value) {
static art_leaf* make_leaf(unsigned char *key, int key_len, void *value) {
art_leaf *l = malloc(sizeof(art_leaf)+key_len);
l->value = value;
l->key_len = key_len;
Expand Down Expand Up @@ -460,7 +460,7 @@ static void add_child(art_node *n, art_node **ref, unsigned char c, void *child)
/**
* Calculates the index at which the prefixes mismatch
*/
static int prefix_mismatch(art_node *n, char *key, int key_len, int depth) {
static int prefix_mismatch(art_node *n, unsigned char *key, int key_len, int depth) {
int max_cmp = min(min(MAX_PREFIX_LEN, n->partial_len), key_len - depth);
int idx;
for (idx=0; idx < max_cmp; idx++) {
Expand All @@ -481,7 +481,7 @@ static int prefix_mismatch(art_node *n, char *key, int key_len, int depth) {
return idx;
}

static void* recursive_insert(art_node *n, art_node **ref, char *key, int key_len, void *value, int depth, int *old) {
static void* recursive_insert(art_node *n, art_node **ref, unsigned char *key, int key_len, void *value, int depth, int *old) {
// If we are at a NULL node, inject a leaf
if (!n) {
*ref = (art_node*)SET_LEAF(make_leaf(key, key_len, value));
Expand Down Expand Up @@ -575,7 +575,7 @@ RECURSE_SEARCH:;
* @return NULL if the item was newly inserted, otherwise
* the old value pointer is returned.
*/
void* art_insert(art_tree *t, char *key, int key_len, void *value) {
void* art_insert(art_tree *t, unsigned char *key, int key_len, void *value) {
int old_val = 0;
void *old = recursive_insert(t->root, &t->root, key, key_len, value, 0, &old_val);
if (!old_val) t->size++;
Expand Down Expand Up @@ -691,7 +691,7 @@ static void remove_child(art_node *n, art_node **ref, unsigned char c, art_node
}
}

static art_leaf* recursive_delete(art_node *n, art_node **ref, char *key, int key_len, int depth) {
static art_leaf* recursive_delete(art_node *n, art_node **ref, unsigned char *key, int key_len, int depth) {
// Search terminated
if (!n) return NULL;

Expand Down Expand Up @@ -741,7 +741,7 @@ static art_leaf* recursive_delete(art_node *n, art_node **ref, char *key, int ke
* @return NULL if the item was not found, otherwise
* the value pointer is returned.
*/
void* art_delete(art_tree *t, char *key, int key_len) {
void* art_delete(art_tree *t, unsigned char *key, int key_len) {
art_leaf *l = recursive_delete(t->root, &t->root, key, key_len, 0);
if (l) {
t->size--;
Expand All @@ -758,7 +758,7 @@ static int recursive_iter(art_node *n, art_callback cb, void *data) {
if (!n) return 0;
if (IS_LEAF(n)) {
art_leaf *l = LEAF_RAW(n);
return cb(data, (const char*)l->key, l->key_len, l->value);
return cb(data, (const unsigned char*)l->key, l->key_len, l->value);
}

int idx, res;
Expand Down Expand Up @@ -819,7 +819,7 @@ int art_iter(art_tree *t, art_callback cb, void *data) {
* Checks if a leaf prefix matches
* @return 0 on success.
*/
static int leaf_prefix_matches(art_leaf *n, char *prefix, int prefix_len) {
static int leaf_prefix_matches(art_leaf *n, unsigned char *prefix, int prefix_len) {
// Fail if the key length is too short
if (n->key_len < (uint32_t)prefix_len) return 1;

Expand All @@ -839,7 +839,7 @@ static int leaf_prefix_matches(art_leaf *n, char *prefix, int prefix_len) {
* @arg data Opaque handle passed to the callback
* @return 0 on success, or the return of the callback.
*/
int art_iter_prefix(art_tree *t, char *key, int key_len, art_callback cb, void *data) {
int art_iter_prefix(art_tree *t, unsigned char *key, int key_len, art_callback cb, void *data) {
art_node **child;
art_node *n = t->root;
int prefix_len, depth = 0;
Expand All @@ -850,7 +850,7 @@ int art_iter_prefix(art_tree *t, char *key, int key_len, art_callback cb, void *
// Check if the expanded path matches
if (!leaf_prefix_matches((art_leaf*)n, key, key_len)) {
art_leaf *l = (art_leaf*)n;
return cb(data, (const char*)l->key, l->key_len, l->value);
return cb(data, (const unsigned char*)l->key, l->key_len, l->value);
}
return 0;
}
Expand Down
12 changes: 6 additions & 6 deletions src/art.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

#define MAX_PREFIX_LEN 10

typedef int(*art_callback)(void *data, const char *key, uint32_t key_len, void *value);
typedef int(*art_callback)(void *data, const unsigned char *key, uint32_t key_len, void *value);

/**
* This struct is included as part
Expand All @@ -19,7 +19,7 @@ typedef struct {
uint8_t type;
uint8_t num_children;
uint32_t partial_len;
char partial[MAX_PREFIX_LEN];
unsigned char partial[MAX_PREFIX_LEN];
} art_node;

/**
Expand Down Expand Up @@ -104,7 +104,7 @@ inline uint64_t art_size(art_tree *t) {
* @return NULL if the item was newly inserted, otherwise
* the old value pointer is returned.
*/
void* art_insert(art_tree *t, char *key, int key_len, void *value);
void* art_insert(art_tree *t, unsigned char *key, int key_len, void *value);

/**
* Deletes a value from the ART tree
Expand All @@ -114,7 +114,7 @@ void* art_insert(art_tree *t, char *key, int key_len, void *value);
* @return NULL if the item was not found, otherwise
* the value pointer is returned.
*/
void* art_delete(art_tree *t, char *key, int key_len);
void* art_delete(art_tree *t, unsigned char *key, int key_len);

/**
* Searches for a value in the ART tree
Expand All @@ -124,7 +124,7 @@ void* art_delete(art_tree *t, char *key, int key_len);
* @return NULL if the item was not found, otherwise
* the value pointer is returned.
*/
void* art_search(art_tree *t, char *key, int key_len);
void* art_search(art_tree *t, unsigned char *key, int key_len);

/**
* Returns the minimum valued leaf
Expand Down Expand Up @@ -162,6 +162,6 @@ int art_iter(art_tree *t, art_callback cb, void *data);
* @arg data Opaque handle passed to the callback
* @return 0 on success, or the return of the callback.
*/
int art_iter_prefix(art_tree *t, char *prefix, int prefix_len, art_callback cb, void *data);
int art_iter_prefix(art_tree *t, unsigned char *prefix, int prefix_len, art_callback cb, void *data);

#endif

0 comments on commit 6153ec0

Please sign in to comment.