Implemented zstrndup.

This commit is contained in:
Omer Katz 2014-05-21 11:36:50 +03:00
parent e34759c1aa
commit 3ea9354e0b
5 changed files with 22 additions and 5 deletions

View file

@ -70,6 +70,7 @@ void *zcalloc(size_t size);
void *zrealloc(void *ptr, size_t size);
void zfree(void *ptr);
char *zstrdup(const char *s);
char *zstrndup(const char *s, size_t n);
size_t zmalloc_used_memory(void);
void zmalloc_enable_thread_safeness(void);
void zmalloc_set_oom_handler(void (*oom_handler)(size_t));

View file

@ -55,7 +55,7 @@ node * r3_edge_branch(edge *e, int dl) {
// the suffix edge of the leaf
new_child = r3_tree_create(3);
s1_len = e->pattern_len - dl;
e1 = r3_edge_create(strndup(s1, s1_len), s1_len, new_child);
e1 = r3_edge_create(zstrndup(s1, s1_len), s1_len, new_child);
// Migrate the child edges to the new edge we just created.
for ( int i = 0 ; i < tmp_edge_len ; i++ ) {

View file

@ -306,7 +306,7 @@ node * r3_tree_matchl(const node * n, char * path, int path_len, match_entry * e
if (entry && e->has_slug) {
// append captured token to entry
str_array_append(entry->vars , strndup(substring_start, substring_length));
str_array_append(entry->vars , zstrndup(substring_start, substring_length));
}
if (restlen == 0 ) {
return e->child && e->child->endpoint > 0 ? e->child : NULL;
@ -459,14 +459,14 @@ node * r3_tree_insert_pathl_(node *tree, char *path, int path_len, route * route
// insert the first one edge, and break at "p"
node * child = r3_tree_create(3);
r3_node_add_child(n, strndup(path, (int)(p - path)), child);
r3_node_add_child(n, zstrndup(path, (int)(p - path)), child);
child->endpoint = 0;
// and insert the rest part to the child
return r3_tree_insert_pathl_(child, p, path_len - (int)(p - path), route, data);
} else {
node * child = r3_tree_create(3);
r3_node_add_child(n, strndup(path, path_len) , child);
r3_node_add_child(n, zstrndup(path, path_len) , child);
// info("edge not found, insert one: %s\n", path);
child->data = data;
child->endpoint++;

View file

@ -204,7 +204,7 @@ char *zstrdup(const char *s) {
#endif
#ifndef HAVE_STRNDUP
char *strndup(const char *s, int n) {
char *zstrndup(const char *s, int n) {
char *out;
int count = 0;
while( count < n && s[count] )

View file

@ -215,6 +215,22 @@ char *zstrdup(const char *s) {
return p;
}
char * zstrndup (const char *s, size_t n)
{
char *result;
size_t len = strlen (s);
if (n < len)
len = n;
result = (char *) zmalloc (len + 1);
if (!result)
return 0;
result[len] = '\0';
return (char *) memcpy (result, s, len);
}
size_t zmalloc_used_memory(void) {
size_t um;