From 0c6afd03b7361d3052557dbd6579f82038bcb51d Mon Sep 17 00:00:00 2001 From: c9s Date: Sat, 17 May 2014 07:26:47 +0800 Subject: [PATCH] use HAVE_STR*DUP config to check function definitions --- include/r3_str.h | 9 +++++++-- src/edge.c | 2 +- src/node.c | 8 ++++---- src/str.c | 14 +++++++++----- src/token.c | 10 +++++----- tests/bench_str.csv | 1 + 6 files changed, 27 insertions(+), 17 deletions(-) diff --git a/include/r3_str.h b/include/r3_str.h index e167f24..6d821ad 100644 --- a/include/r3_str.h +++ b/include/r3_str.h @@ -8,6 +8,7 @@ #define STR_H #include "r3_define.h" +#include "config.h" int strndiff(char * d1, char * d2, unsigned int n); @@ -28,9 +29,13 @@ void str_repeat(char *s, char *c, int len); void print_indent(int level); -char *my_strdup(const char *s); +#ifndef HAVE_STRDUP +char *strdup(const char *s); +#endif -char *my_strndup(const char *s, int n); +#ifndef HAVE_STRNDUP +char *strndup(const char *s, int n); +#endif #endif /* !STR_H */ diff --git a/src/edge.c b/src/edge.c index ea71ad7..d6124c2 100644 --- a/src/edge.c +++ b/src/edge.c @@ -42,7 +42,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(my_strndup(s1, s1_len), s1_len, c1); + e1 = r3_edge_create(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 288192b..2213c0d 100644 --- a/src/node.c +++ b/src/node.c @@ -275,7 +275,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 , my_strndup(substring_start, substring_length)); + str_array_append(entry->vars , strndup(substring_start, substring_length)); } if (restlen == 0) { return e->child; @@ -379,7 +379,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, my_strndup(route, route_len) , child); + r3_tree_add_child(n, strndup(route, route_len) , child); info("edge not found, insert one: %s\n", route); child->route_ptr = route_ptr; child->endpoint++; @@ -416,13 +416,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(my_strndup(s2, s2_len), s2_len, c2); + e2 = r3_edge_create(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 = my_strndup(e->pattern, dl); + e->pattern = strndup(e->pattern, dl); e->pattern_len = dl; // move n->edges to c1 diff --git a/src/str.c b/src/str.c index 02087dd..77ecedb 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 my_strdup(str); + return strdup(str); } if ( (s1 - str) > 0 ) { @@ -117,7 +117,7 @@ char * ltrim_slash(char* str) { char * p = str; while (*p == '/') p++; - return my_strdup(p); + return 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++) = my_strdup(token); + *(result + idx++) = strdup(token); token = strtok(0, delim); } assert(idx == count - 1); @@ -181,7 +181,8 @@ void print_indent(int level) { } } -char *my_strdup(const char *s) { +#ifndef HAVE_STRDUP +char *strdup(const char *s) { char *out; int count = 0; while( s[count] ) @@ -193,8 +194,10 @@ char *my_strdup(const char *s) { out[count] = s[count]; return out; } +#endif -char *my_strndup(const char *s, int n) { +#ifndef HAVE_STRNDUP +char *strndup(const char *s, int n) { char *out; int count = 0; while( count < n && s[count] ) @@ -206,3 +209,4 @@ char *my_strndup(const char *s, int n) { out[count] = s[count]; return out; } +#endif diff --git a/src/token.c b/src/token.c index cebbce9..b7c7a2b 100644 --- a/src/token.c +++ b/src/token.c @@ -97,21 +97,21 @@ str_array * split_route_pattern(char *pattern, int pattern_len) { assert(p - pattern < pattern_len ); // throw exception } p++; // contains the '}' - // printf("==> %s\n", my_strndup(s1, p-s1) ); - str_array_append(str_array, my_strndup(s1, p-s1) ); + // printf("==> %s\n", strndup(s1, p-s1) ); + str_array_append(str_array, strndup(s1, p-s1) ); s1 = p; continue; } else if ( *p == '/' ) { - // printf("==> %s\n", my_strndup(s1, p-s1) ); - str_array_append(str_array, my_strndup(s1, p-s1) ); + // printf("==> %s\n", strndup(s1, p-s1) ); + str_array_append(str_array, strndup(s1, p-s1) ); s1 = p; } p++; } if ( p-s1 > 0 ) { - str_array_append(str_array, my_strndup(s1, p-s1) ); + str_array_append(str_array, strndup(s1, p-s1) ); } return str_array; } diff --git a/tests/bench_str.csv b/tests/bench_str.csv index f2102a1..4a47ea1 100644 --- a/tests/bench_str.csv +++ b/tests/bench_str.csv @@ -49,3 +49,4 @@ 1400278620,5733465.40 1400279249,6306920.57 1400282602,11775983.90 +1400282802,11627473.22