added memmem to extensions
Minclude/cutils/dyn_string.h
| @@ -43,10 +43,13 @@ void string_remove_range(String* string, size_t index, size_t length); | |||
|---|---|---|---|
| 43 | 43 | size_t string_strip(String* string, char character); | |
| 44 | 44 | ||
| 45 | 45 | bool string_adjust_size(String* string, size_t size); | |
| 46 | - | /* TODO: rework find functions to take extra argument of result count | |
| 47 | - | * and then find that many and return them in a vector/linked list */ | |
| 48 | 46 | bool string_find_char(const String* haystack, const char needle, size_t* pos); | |
| 49 | - | bool string_find_str(const String* haystack, const String* needle, size_t* pos); | |
| 47 | + | HEDLEY_INLINE | |
| 48 | + | static bool string_find_str(const String* haystack, const String* needle, size_t* pos) | |
| 49 | + | { | |
| 50 | + | return cutil_memmem(haystack->chars, haystack->length, needle->chars, needle->length, pos); | |
| 51 | + | } | |
| 52 | + | ||
| 50 | 53 | size_t string_count(const String* string, char character); | |
| 51 | 54 | ||
| 52 | 55 | bool string_concat(String* string, const String* other); | |
Minclude/cutils/extensions.h
| @@ -9,6 +9,7 @@ | |||
|---|---|---|---|
| 9 | 9 | #include <ctype.h> | |
| 10 | 10 | #include <stdarg.h> | |
| 11 | 11 | #include <stdio.h> | |
| 12 | + | #include <cutils/common.h> | |
| 12 | 13 | ||
| 13 | 14 | int cutil_strcasecmp(const char* s1, const char* s2); | |
| 14 | 15 | int cutil_strncasecmp(const char* s1, const char* s2, size_t n); | |
| @@ -18,6 +19,8 @@ size_t cutil_strnlen(const char *s, size_t maxlen); | |||
|---|---|---|---|
| 18 | 19 | char* cutil_strdup(const char *s); | |
| 19 | 20 | char* cutil_strndup(const char *s, size_t n); | |
| 20 | 21 | ||
| 22 | + | bool cutil_memmem(const void* haystack, size_t haystacklen, const void* needle, size_t needlelen, size_t* pos); | |
| 23 | + | ||
| 21 | 24 | #if __STDC_VERSION__ >= 199901L | |
| 22 | 25 | int cutil_asprintf(char** strp, const char* format, ...); | |
| 23 | 26 | #endif | |
Msrc/dyn_string.c
| @@ -97,26 +97,6 @@ bool string_find_char(const String* haystack, const char needle, size_t* pos) | |||
|---|---|---|---|
| 97 | 97 | return false; | |
| 98 | 98 | } | |
| 99 | 99 | ||
| 100 | - | bool string_find_str(const String* haystack, const String* needle, size_t* pos) | |
| 101 | - | { | |
| 102 | - | size_t i, j, tmp; | |
| 103 | - | ||
| 104 | - | for(i = 0; i < haystack->length; i++) | |
| 105 | - | { | |
| 106 | - | for(j = 0, tmp = i; j < needle->length && i < haystack->length; j++, i++) | |
| 107 | - | { | |
| 108 | - | if(haystack->chars[i] != needle->chars[j]) | |
| 109 | - | { | |
| 110 | - | break; | |
| 111 | - | } else if(j == needle->length-1){ | |
| 112 | - | *pos = tmp; | |
| 113 | - | return true; | |
| 114 | - | } | |
| 115 | - | } | |
| 116 | - | } | |
| 117 | - | return false; | |
| 118 | - | } | |
| 119 | - | ||
| 120 | 100 | size_t string_count(const String* string, char character) | |
| 121 | 101 | { | |
| 122 | 102 | size_t i, ret = 0; | |
Msrc/extensions.c
| @@ -75,6 +75,33 @@ char* cutil_strndup(const char *s, size_t n) | |||
|---|---|---|---|
| 75 | 75 | return ret; | |
| 76 | 76 | } | |
| 77 | 77 | ||
| 78 | + | bool cutil_memmem(const void* haystack, size_t haystacklen, const void* needle, size_t needlelen, size_t* pos) | |
| 79 | + | { | |
| 80 | + | size_t i, j, tmp; | |
| 81 | + | const byte* haystackb = haystack; | |
| 82 | + | const byte* needleb = needle; | |
| 83 | + | ||
| 84 | + | ||
| 85 | + | if (haystacklen == 0 || needlelen == 0) | |
| 86 | + | return false; | |
| 87 | + | ||
| 88 | + | for(i = 0; i < haystacklen; i++) | |
| 89 | + | { | |
| 90 | + | for(j = 0, tmp = i; j < needlelen && i < haystacklen; j++, i++) | |
| 91 | + | { | |
| 92 | + | if(haystackb[i] != needleb[j]) | |
| 93 | + | { | |
| 94 | + | break; | |
| 95 | + | } else if(j == needlelen-1){ | |
| 96 | + | *pos = tmp; | |
| 97 | + | return true; | |
| 98 | + | } | |
| 99 | + | } | |
| 100 | + | } | |
| 101 | + | ||
| 102 | + | return false; | |
| 103 | + | } | |
| 104 | + | ||
| 78 | 105 | #if __STDC_VERSION__ >= 199901L | |
| 79 | 106 | int cutil_asprintf(char** strp, const char* format, ...) | |
| 80 | 107 | { | |
Msrc/linked-list.c
| @@ -306,7 +306,6 @@ void sll_swap(LinkedList* ll, size_t item1, size_t item2) | |||
|---|---|---|---|
| 306 | 306 | { | |
| 307 | 307 | size_t i, first_index, second_index; | |
| 308 | 308 | SLnode *first, *second; | |
| 309 | - | void* tmp; | |
| 310 | 309 | ||
| 311 | 310 | first_index = item1 <= item2 ? item1 : item2; | |
| 312 | 311 | second_index = item1 <= item2 ? item2 : item1; | |
| @@ -324,11 +323,7 @@ void sll_swap(LinkedList* ll, size_t item1, size_t item2) | |||
|---|---|---|---|
| 324 | 323 | if(!first) | |
| 325 | 324 | first = second; | |
| 326 | 325 | ||
| 327 | - | tmp = first->item; | |
| 328 | - | first->item = second->item; | |
| 329 | - | second->item = tmp; | |
| 330 | - | ||
| 331 | - | /*memswap(&first->item, &second->item, sizeof(first->item));*/ | |
| 326 | + | memswap(&first->item, &second->item, sizeof(first->item)); | |
| 332 | 327 | } | |
| 333 | 328 | ||
| 334 | 329 | void dll_swap(LinkedList* ll, size_t item1, size_t item2) | |
Msrc/test.c
| @@ -107,7 +107,7 @@ static void test_string(void) | |||
|---|---|---|---|
| 107 | 107 | ||
| 108 | 108 | string = from_cstring("abcd"); | |
| 109 | 109 | string2 = from_cstring("cd"); | |
| 110 | - | string_find_str(string, string2, &tmp); | |
| 110 | + | assert(string_find_str(string, string2, &tmp)); | |
| 111 | 111 | assert(tmp == 2); | |
| 112 | 112 | ||
| 113 | 113 | delete_string(string2); | |