From 8f6fd1980669c8e1609768527a1c99513308bb62 Mon Sep 17 00:00:00 2001 From: "Cindy Wang (CindyLinz)" Date: Sat, 17 May 2014 00:35:56 +0800 Subject: [PATCH] self provided strndup & strdup --- include/r3_str.h | 4 ++++ src/edge.c | 2 +- src/node.c | 8 ++++---- src/str.c | 32 +++++++++++++++++++++++++++++--- src/token.c | 10 +++++----- 5 files changed, 43 insertions(+), 13 deletions(-) diff --git a/include/r3_str.h b/include/r3_str.h index f4d585e..9750cd1 100644 --- a/include/r3_str.h +++ b/include/r3_str.h @@ -28,6 +28,10 @@ void str_repeat(char *s, char *c, int len); void print_indent(int level); +char *my_strdup(const char *s); + +char *my_strndup(const char *s, size_t n); + #endif /* !STR_H */ diff --git a/src/edge.c b/src/edge.c index a1b4e72..cccd531 100644 --- a/src/edge.c +++ b/src/edge.c @@ -41,7 +41,7 @@ void r3_edge_branch(edge *e, int dl) { // the suffix edge of the leaf c1 = r3_tree_create(3); s1_len = e->pattern_len - dl; - e1 = r3_edge_create(strndup(s1, s1_len), s1_len, c1); + e1 = r3_edge_create(my_strndup(s1, s1_len), s1_len, c1); // printf("edge left: %s\n", e1->pattern); // Migrate the child edges to the new edge we just created. diff --git a/src/node.c b/src/node.c index 02caa39..100bd0e 100644 --- a/src/node.c +++ b/src/node.c @@ -274,7 +274,7 @@ node * r3_tree_match(node * n, char * path, int path_len, match_entry * entry) { 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 , my_strndup(substring_start, substring_length)); } if (restlen == 0) { return e->child; @@ -378,7 +378,7 @@ node * r3_tree_insert_pathn(node *tree, char *route, int route_len, void * route if ( dl == 0 ) { // not found, we should just insert a whole new edge node * child = r3_tree_create(3); - r3_tree_add_child(n, strndup(route, route_len) , child); + r3_tree_add_child(n, my_strndup(route, route_len) , child); info("edge not found, insert one: %s\n", route); child->route_ptr = route_ptr; child->endpoint++; @@ -415,13 +415,13 @@ node * r3_tree_insert_pathn(node *tree, char *route, int route_len, void * route // here is the new edge from. c2 = r3_tree_create(3); s2_len = route_len - dl; - e2 = r3_edge_create(strndup(s2, s2_len), s2_len, c2); + e2 = r3_edge_create(my_strndup(s2, s2_len), s2_len, c2); // printf("edge right: %s\n", e2->pattern); r3_tree_append_edge(e->child, e2); // truncate the original edge pattern free(e->pattern); - e->pattern = strndup(e->pattern, dl); + e->pattern = my_strndup(e->pattern, dl); e->pattern_len = dl; // move n->edges to c1 diff --git a/src/str.c b/src/str.c index ca86b17..5d0caf8 100644 --- a/src/str.c +++ b/src/str.c @@ -62,7 +62,7 @@ char * compile_slug(char * str, int len) s1 = strchr(str, '{'); if ( s1 == NULL ) { - return strdup(str); + return my_strdup(str); } if ( (s1 - str) > 0 ) { @@ -117,7 +117,7 @@ char * ltrim_slash(char* str) { char * p = str; while (*p == '/') p++; - return strdup(p); + return my_strdup(p); } char** str_split(char* a_str, const char a_delim) @@ -158,7 +158,7 @@ char** str_split(char* a_str, const char a_delim) while (token) { assert(idx < count); - *(result + idx++) = strdup(token); + *(result + idx++) = my_strdup(token); token = strtok(0, delim); } assert(idx == count - 1); @@ -180,3 +180,29 @@ void print_indent(int level) { printf(" "); } } + +char *my_strdup(const char *s) { + char *out; + size_t count = 0; + while( s[count] ) + ++count; + ++count; + out = malloc(sizeof(char*) * count); + out[--count] = 0; + while( --count >= 0 ) + out[count] = s[count]; + return out; +} + +char *my_strndup(const char *s, size_t n) { + char *out; + size_t count = 0; + while( count < n && s[count] ) + ++count; + ++count; + out = malloc(sizeof(char*) * count); + out[--count] = 0; + while( --count >= 0 ) + out[count] = s[count]; + return out; +} diff --git a/src/token.c b/src/token.c index 45ff273..fd47fe9 100644 --- a/src/token.c +++ b/src/token.c @@ -96,21 +96,21 @@ str_array * split_route_pattern(char *pattern, int pattern_len) { assert(p - pattern < pattern_len ); // throw exception } p++; // contains the '}' - // printf("==> %s\n", strndup(s1, p-s1) ); - str_array_append(str_array, strndup(s1, p-s1) ); + // printf("==> %s\n", my_strndup(s1, p-s1) ); + str_array_append(str_array, my_strndup(s1, p-s1) ); s1 = p; continue; } else if ( *p == '/' ) { - // printf("==> %s\n", strndup(s1, p-s1) ); - str_array_append(str_array, strndup(s1, p-s1) ); + // printf("==> %s\n", my_strndup(s1, p-s1) ); + str_array_append(str_array, my_strndup(s1, p-s1) ); s1 = p; } p++; } if ( p-s1 > 0 ) { - str_array_append(str_array, strndup(s1, p-s1) ); + str_array_append(str_array, my_strndup(s1, p-s1) ); } return str_array; }