refactored dyn_string functions, more function to macros
Minclude/cutils/dyn_string.h
| @@ -14,21 +14,23 @@ typedef struct String | |||
|---|---|---|---|
| 14 | 14 | size_t length; | |
| 15 | 15 | } String; | |
| 16 | 16 | ||
| 17 | - | String* new_string(void); | |
| 17 | + | #define new_string() string_with_capacity(STRING_DEFAULT_SIZE); | |
| 18 | 18 | String* string_with_capacity(size_t capacity); | |
| 19 | 19 | void delete_string(String* string); | |
| 20 | 20 | ||
| 21 | 21 | char string_at(const String* string, size_t index); | |
| 22 | + | #define string_pop(string) string_pop_at(string, string->length-1); | |
| 23 | + | char string_pop_at(String* string, size_t index); | |
| 22 | 24 | ||
| 23 | - | bool string_append(String* string, char character); | |
| 24 | 25 | bool string_insert(String* string, size_t index, char character); | |
| 26 | + | #define string_push(string, character) string_insert(string, string->length, character); | |
| 25 | 27 | ||
| 26 | 28 | void string_remove(String* string, size_t index); | |
| 27 | 29 | ||
| 28 | 30 | bool string_adjust_size(String* string, size_t size); | |
| 29 | 31 | size_t* string_find_char(const String* haystack, const char needle); | |
| 30 | - | bool string_concat(String* string, const String* other); | |
| 31 | 32 | ||
| 33 | + | bool string_concat(String* string, const String* other); | |
| 32 | 34 | String* from_cstring(const char* cstring); | |
| 33 | 35 | char* to_cstring(const String* string); | |
| 34 | 36 | ||
Minclude/cutils/vector.h
| @@ -24,6 +24,7 @@ void* vector_pop_at(Vector* vector, size_t index); | |||
|---|---|---|---|
| 24 | 24 | ||
| 25 | 25 | bool vector_insert(Vector* vector, size_t index, void* item); | |
| 26 | 26 | #define vector_push(vector, item) vector_insert(vector, vector->length, item); | |
| 27 | + | ||
| 27 | 28 | void vector_remove(Vector* vector, size_t index, void (*rmv)(void*)); | |
| 28 | 29 | ||
| 29 | 30 | bool vector_adjust_size(Vector* vector, size_t size); | |
Msrc/dyn_string.c
| @@ -1,10 +1,5 @@ | |||
|---|---|---|---|
| 1 | 1 | #include <cutils/dyn_string.h> | |
| 2 | 2 | ||
| 3 | - | String* new_string(void) | |
| 4 | - | { | |
| 5 | - | return string_with_capacity(STRING_DEFAULT_SIZE); | |
| 6 | - | } | |
| 7 | - | ||
| 8 | 3 | String* string_with_capacity(size_t capacity) | |
| 9 | 4 | { | |
| 10 | 5 | String* string = malloc(sizeof(*string)); | |
| @@ -22,11 +17,6 @@ String* string_with_capacity(size_t capacity) | |||
|---|---|---|---|
| 22 | 17 | return string; | |
| 23 | 18 | } | |
| 24 | 19 | ||
| 25 | - | bool string_append(String* string, char character) | |
| 26 | - | { | |
| 27 | - | return string_insert(string, string->length, character); | |
| 28 | - | } | |
| 29 | - | ||
| 30 | 20 | void string_remove(String* string, size_t index) | |
| 31 | 21 | { | |
| 32 | 22 | if(index < string->length) | |
| @@ -55,6 +45,18 @@ char string_at(const String* string, size_t index) | |||
|---|---|---|---|
| 55 | 45 | return string->chars[index]; | |
| 56 | 46 | } | |
| 57 | 47 | ||
| 48 | + | char string_pop_at(String* string, size_t index) | |
| 49 | + | { | |
| 50 | + | char tmp = string_at(string, index); | |
| 51 | + | if(!tmp) | |
| 52 | + | { | |
| 53 | + | return '\0'; | |
| 54 | + | } else { | |
| 55 | + | string_remove(string, index); | |
| 56 | + | return tmp; | |
| 57 | + | } | |
| 58 | + | } | |
| 59 | + | ||
| 58 | 60 | bool string_concat(String* string, const String* other) | |
| 59 | 61 | { | |
| 60 | 62 | if(!string_adjust_size(string,string->length+other->length-1)) | |
| @@ -97,7 +99,6 @@ bool string_adjust_size(String* string, size_t size) | |||
|---|---|---|---|
| 97 | 99 | } | |
| 98 | 100 | ||
| 99 | 101 | ||
| 100 | - | ||
| 101 | 102 | void delete_string(String* string) | |
| 102 | 103 | { | |
| 103 | 104 | free(string->chars); | |
Msrc/test.c
| @@ -65,13 +65,16 @@ static void test1(void) | |||
|---|---|---|---|
| 65 | 65 | ||
| 66 | 66 | static void test2(void) | |
| 67 | 67 | { | |
| 68 | - | String* test = new_string(); | |
| 68 | + | String* test; | |
| 69 | 69 | String* test2; | |
| 70 | 70 | char* cstring; | |
| 71 | - | string_append(test, 'a'); | |
| 72 | - | string_append(test, 'b'); | |
| 73 | - | string_append(test, 'c'); | |
| 74 | - | string_append(test, 'd'); | |
| 71 | + | ||
| 72 | + | test = new_string(); | |
| 73 | + | ||
| 74 | + | string_push(test, 'a'); | |
| 75 | + | string_push(test, 'b'); | |
| 76 | + | string_push(test, 'c'); | |
| 77 | + | string_push(test, 'd'); | |
| 75 | 78 | printf("%ld\n", test->length); | |
| 76 | 79 | ||
| 77 | 80 | cstring = to_cstring(test); | |