self provided strndup & strdup
This commit is contained in:
parent
dc91651782
commit
8f6fd19806
5 changed files with 43 additions and 13 deletions
|
@ -28,6 +28,10 @@ void str_repeat(char *s, char *c, int len);
|
||||||
|
|
||||||
void print_indent(int level);
|
void print_indent(int level);
|
||||||
|
|
||||||
|
char *my_strdup(const char *s);
|
||||||
|
|
||||||
|
char *my_strndup(const char *s, size_t n);
|
||||||
|
|
||||||
|
|
||||||
#endif /* !STR_H */
|
#endif /* !STR_H */
|
||||||
|
|
||||||
|
|
|
@ -41,7 +41,7 @@ void r3_edge_branch(edge *e, int dl) {
|
||||||
// the suffix edge of the leaf
|
// the suffix edge of the leaf
|
||||||
c1 = r3_tree_create(3);
|
c1 = r3_tree_create(3);
|
||||||
s1_len = e->pattern_len - dl;
|
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);
|
// printf("edge left: %s\n", e1->pattern);
|
||||||
|
|
||||||
// Migrate the child edges to the new edge we just created.
|
// Migrate the child edges to the new edge we just created.
|
||||||
|
|
|
@ -274,7 +274,7 @@ node * r3_tree_match(node * n, char * path, int path_len, match_entry * entry) {
|
||||||
|
|
||||||
if (entry && e->has_slug) {
|
if (entry && e->has_slug) {
|
||||||
// append captured token to entry
|
// 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) {
|
if (restlen == 0) {
|
||||||
return e->child;
|
return e->child;
|
||||||
|
@ -378,7 +378,7 @@ node * r3_tree_insert_pathn(node *tree, char *route, int route_len, void * route
|
||||||
if ( dl == 0 ) {
|
if ( dl == 0 ) {
|
||||||
// not found, we should just insert a whole new edge
|
// not found, we should just insert a whole new edge
|
||||||
node * child = r3_tree_create(3);
|
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);
|
info("edge not found, insert one: %s\n", route);
|
||||||
child->route_ptr = route_ptr;
|
child->route_ptr = route_ptr;
|
||||||
child->endpoint++;
|
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.
|
// here is the new edge from.
|
||||||
c2 = r3_tree_create(3);
|
c2 = r3_tree_create(3);
|
||||||
s2_len = route_len - dl;
|
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);
|
// printf("edge right: %s\n", e2->pattern);
|
||||||
r3_tree_append_edge(e->child, e2);
|
r3_tree_append_edge(e->child, e2);
|
||||||
|
|
||||||
// truncate the original edge pattern
|
// truncate the original edge pattern
|
||||||
free(e->pattern);
|
free(e->pattern);
|
||||||
e->pattern = strndup(e->pattern, dl);
|
e->pattern = my_strndup(e->pattern, dl);
|
||||||
e->pattern_len = dl;
|
e->pattern_len = dl;
|
||||||
|
|
||||||
// move n->edges to c1
|
// move n->edges to c1
|
||||||
|
|
32
src/str.c
32
src/str.c
|
@ -62,7 +62,7 @@ char * compile_slug(char * str, int len)
|
||||||
s1 = strchr(str, '{');
|
s1 = strchr(str, '{');
|
||||||
|
|
||||||
if ( s1 == NULL ) {
|
if ( s1 == NULL ) {
|
||||||
return strdup(str);
|
return my_strdup(str);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( (s1 - str) > 0 ) {
|
if ( (s1 - str) > 0 ) {
|
||||||
|
@ -117,7 +117,7 @@ char * ltrim_slash(char* str)
|
||||||
{
|
{
|
||||||
char * p = str;
|
char * p = str;
|
||||||
while (*p == '/') p++;
|
while (*p == '/') p++;
|
||||||
return strdup(p);
|
return my_strdup(p);
|
||||||
}
|
}
|
||||||
|
|
||||||
char** str_split(char* a_str, const char a_delim)
|
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)
|
while (token)
|
||||||
{
|
{
|
||||||
assert(idx < count);
|
assert(idx < count);
|
||||||
*(result + idx++) = strdup(token);
|
*(result + idx++) = my_strdup(token);
|
||||||
token = strtok(0, delim);
|
token = strtok(0, delim);
|
||||||
}
|
}
|
||||||
assert(idx == count - 1);
|
assert(idx == count - 1);
|
||||||
|
@ -180,3 +180,29 @@ void print_indent(int level) {
|
||||||
printf(" ");
|
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;
|
||||||
|
}
|
||||||
|
|
10
src/token.c
10
src/token.c
|
@ -96,21 +96,21 @@ str_array * split_route_pattern(char *pattern, int pattern_len) {
|
||||||
assert(p - pattern < pattern_len ); // throw exception
|
assert(p - pattern < pattern_len ); // throw exception
|
||||||
}
|
}
|
||||||
p++; // contains the '}'
|
p++; // contains the '}'
|
||||||
// printf("==> %s\n", strndup(s1, p-s1) );
|
// printf("==> %s\n", my_strndup(s1, p-s1) );
|
||||||
str_array_append(str_array, strndup(s1, p-s1) );
|
str_array_append(str_array, my_strndup(s1, p-s1) );
|
||||||
s1 = p;
|
s1 = p;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
else if ( *p == '/' ) {
|
else if ( *p == '/' ) {
|
||||||
// printf("==> %s\n", strndup(s1, p-s1) );
|
// printf("==> %s\n", my_strndup(s1, p-s1) );
|
||||||
str_array_append(str_array, strndup(s1, p-s1) );
|
str_array_append(str_array, my_strndup(s1, p-s1) );
|
||||||
s1 = p;
|
s1 = p;
|
||||||
}
|
}
|
||||||
p++;
|
p++;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( p-s1 > 0 ) {
|
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;
|
return str_array;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue