changed naming form extension functions, added strnlen, str[n]dup and asprintf, added bitfuncs.h, changed find functions, added typedefs in misc.h
Minclude/cutils/backport.h
| @@ -1,5 +1,5 @@ | |||
|---|---|---|---|
| 1 | - | #ifndef BACKPORT_H | |
| 2 | - | #define BACKPORT_H | |
| 1 | + | #ifndef CUTILS_BACKPORT_H | |
| 2 | + | #define CUTILS_BACKPORT_H | |
| 3 | 3 | ||
| 4 | 4 | #include <limits.h> | |
| 5 | 5 | #if __STDC_VERSION__ >= 199901L | |
| @@ -116,4 +116,4 @@ | |||
|---|---|---|---|
| 116 | 116 | #endif | |
| 117 | 117 | ||
| 118 | 118 | ||
| 119 | - | #endif /* BACKPORT_H */ | |
| 119 | + | #endif /* CUTILS_BACKPORT_H */ | |
Ainclude/cutils/bitfuncs.h
| @@ -0,0 +1,17 @@ | |||
|---|---|---|---|
| 1 | + | #ifndef CUTILS_BITFUNCS_H | |
| 2 | + | #define CUTILS_BITFUNCS_H | |
| 3 | + | ||
| 4 | + | #include <stdlib.h> | |
| 5 | + | #include <cutils/common.h> | |
| 6 | + | ||
| 7 | + | #define numbits(type) (sizeof(type)*CHAR_BIT) | |
| 8 | + | ||
| 9 | + | #define bitstring(type, value) _bitstring(numbits(type), (type)(value)) | |
| 10 | + | /* FIXME: fix out of range calls (bitstring(unsigned char, 256)) */ | |
| 11 | + | char* _bitstring(size_t size, uintmax_t value); | |
| 12 | + | ||
| 13 | + | #define getbit(type, value, bit) (((value) & (((type)1 << (bit)))) >> (bit)) | |
| 14 | + | #define setbit(type, value, bit) ((value) |= ((type)1 << (bit))) | |
| 15 | + | #define clearbit(type, value, bit) ((value) &= (type)~((type)1 << (bit))) | |
| 16 | + | ||
| 17 | + | #endif /* CUTILS_BITFUNCS_H */ | |
Minclude/cutils/byte_array.h
| @@ -1,5 +1,5 @@ | |||
|---|---|---|---|
| 1 | - | #ifndef BYTE_ARRAY_H | |
| 2 | - | #define BYTE_ARRAY_H | |
| 1 | + | #ifndef CUTILS_BYTE_ARRAY_H | |
| 2 | + | #define CUTILS_BYTE_ARRAY_H | |
| 3 | 3 | #include <stdlib.h> | |
| 4 | 4 | #include <string.h> | |
| 5 | 5 | #include <errno.h> | |
| @@ -9,7 +9,7 @@ | |||
|---|---|---|---|
| 9 | 9 | ||
| 10 | 10 | typedef struct Bytearray | |
| 11 | 11 | { | |
| 12 | - | char* items; | |
| 12 | + | byte* items; | |
| 13 | 13 | size_t capacity; | |
| 14 | 14 | size_t length; | |
| 15 | 15 | size_t element_size; | |
| @@ -34,5 +34,5 @@ HEDLEY_NON_NULL(3) | |||
|---|---|---|---|
| 34 | 34 | size_t* bytearray_find(const Bytearray* haystack, const void* needle, int (*cmp)(const void*, const void*)); | |
| 35 | 35 | ||
| 36 | 36 | ||
| 37 | - | #endif /* BYTE_ARRAY_H */ | |
| 37 | + | #endif /* CUTILS_BYTE_ARRAY_H */ | |
| 38 | 38 | ||
Minclude/cutils/common.h
| @@ -4,5 +4,6 @@ | |||
|---|---|---|---|
| 4 | 4 | #include <cutils/backport.h> | |
| 5 | 5 | #include <cutils/hedley.h> | |
| 6 | 6 | #include <cutils/extensions.h> | |
| 7 | + | #include <cutils/misc.h> | |
| 7 | 8 | ||
| 8 | 9 | #endif /* CUTILS_COMMON_H */ | |
Minclude/cutils/cutils.h
| @@ -1,5 +1,5 @@ | |||
|---|---|---|---|
| 1 | - | #ifndef CUTILS_H | |
| 2 | - | #define CUTILS_H | |
| 1 | + | #ifndef CUTILS_CUTILS_H | |
| 2 | + | #define CUTILS_CUTILS_H | |
| 3 | 3 | ||
| 4 | 4 | #include <cutils/vector.h> | |
| 5 | 5 | #include <cutils/dyn_string.h> | |
| @@ -8,5 +8,6 @@ | |||
|---|---|---|---|
| 8 | 8 | #include <cutils/linked-list.h> | |
| 9 | 9 | #include <cutils/extensions.h> | |
| 10 | 10 | #include <cutils/backport.h> | |
| 11 | + | #include <cutils/bitfuncs.h> | |
| 11 | 12 | ||
| 12 | - | #endif /* CUTILS_H */ | |
| 13 | + | #endif /* CUTILS_CUTILS_H */ | |
Minclude/cutils/dyn_string.h
| @@ -1,5 +1,5 @@ | |||
|---|---|---|---|
| 1 | - | #ifndef DYN_STRING_H | |
| 2 | - | #define DYN_STRING_H | |
| 1 | + | #ifndef CUTILS_DYN_STRING_H | |
| 2 | + | #define CUTILS_DYN_STRING_H | |
| 3 | 3 | ||
| 4 | 4 | #define STRING_DEFAULT_SIZE 8 | |
| 5 | 5 | ||
| @@ -31,8 +31,8 @@ void string_remove_range(String* string, size_t index, size_t length); | |||
|---|---|---|---|
| 31 | 31 | bool string_adjust_size(String* string, size_t size); | |
| 32 | 32 | /* TODO: rework find functions to take extra argument of result count | |
| 33 | 33 | * and then find that many and return them in a vector/linked list */ | |
| 34 | - | size_t* string_find_char(const String* haystack, const char needle); | |
| 35 | - | size_t* string_find_str(const String* haystack, const String* needle); | |
| 34 | + | bool string_find_char(const String* haystack, const char needle, size_t* pos); | |
| 35 | + | bool string_find_str(const String* haystack, const String* needle, size_t* pos); | |
| 36 | 36 | ||
| 37 | 37 | bool string_concat(String* string, const String* other); | |
| 38 | 38 | ||
| @@ -42,4 +42,4 @@ char* to_cstring(const String* string); | |||
|---|---|---|---|
| 42 | 42 | char* to_cstring_del(String* string); | |
| 43 | 43 | ||
| 44 | 44 | ||
| 45 | - | #endif /* DYN_STRING_H */ | |
| 45 | + | #endif /* CUTILS_DYN_STRING_H */ | |
Minclude/cutils/extensions.h
| @@ -1,5 +1,5 @@ | |||
|---|---|---|---|
| 1 | - | #ifndef EXTENSIONS_H | |
| 2 | - | #define EXTENSIONS_H | |
| 1 | + | #ifndef CUTILS_EXTENSIONS_H | |
| 2 | + | #define CUTILS_EXTENSIONS_H | |
| 3 | 3 | ||
| 4 | 4 | #include <cutils/hedley.h> | |
| 5 | 5 | #include <errno.h> | |
| @@ -7,12 +7,23 @@ | |||
|---|---|---|---|
| 7 | 7 | #include <cutils/backport.h> | |
| 8 | 8 | #include <string.h> | |
| 9 | 9 | #include <ctype.h> | |
| 10 | + | #include <stdarg.h> | |
| 11 | + | #include <stdio.h> | |
| 10 | 12 | ||
| 11 | - | int strcmp_nocase(const char* s1, const char* s2); | |
| 12 | - | int strncmp_nocase(const char* s1, const char* s2, size_t n); | |
| 13 | + | int cutil_strcasecmp(const char* s1, const char* s2); | |
| 14 | + | int cutil_strncasecmp(const char* s1, const char* s2, size_t n); | |
| 15 | + | ||
| 16 | + | size_t cutil_strnlen(const char *s, size_t maxlen); | |
| 17 | + | ||
| 18 | + | char* cutil_strdup(const char *s); | |
| 19 | + | char* cutil_strndup(const char *s, size_t n); | |
| 20 | + | ||
| 21 | + | #if __STDC_VERSION__ >= 199901L | |
| 22 | + | int cutil_asprintf(char** strp, const char* format, ...); | |
| 23 | + | #endif | |
| 13 | 24 | ||
| 14 | 25 | HEDLEY_INLINE | |
| 15 | - | static void* reallocsafe(void *ptr, size_t nmemb, size_t size) | |
| 26 | + | static void* cutil_reallocarray(void *ptr, size_t nmemb, size_t size) | |
| 16 | 27 | { | |
| 17 | 28 | size_t new_size = nmemb * size; | |
| 18 | 29 | ||
| @@ -26,17 +37,17 @@ static void* reallocsafe(void *ptr, size_t nmemb, size_t size) | |||
|---|---|---|---|
| 26 | 37 | } | |
| 27 | 38 | ||
| 28 | 39 | HEDLEY_INLINE | |
| 29 | - | static void* reallocsafe_inc(void *ptr, size_t nmemb, size_t size, size_t add) | |
| 40 | + | static void* cutil_reallocarray_inc(void *ptr, size_t nmemb, size_t size, size_t add) | |
| 30 | 41 | { | |
| 31 | 42 | if(SIZE_MAX - add < size) | |
| 32 | 43 | { | |
| 33 | 44 | errno = ENOMEM; | |
| 34 | 45 | return NULL; | |
| 35 | 46 | } else { | |
| 36 | - | return reallocsafe(ptr, nmemb, size+add); | |
| 47 | + | return cutil_reallocarray(ptr, nmemb, size+add); | |
| 37 | 48 | } | |
| 38 | 49 | } | |
| 39 | 50 | ||
| 40 | 51 | ||
| 41 | 52 | ||
| 42 | - | #endif /* EXTENSIONS_H */ | |
| 53 | + | #endif /* CUTILS_EXTENSIONS_H */ | |
Minclude/cutils/linked-list.h
| @@ -1,5 +1,5 @@ | |||
|---|---|---|---|
| 1 | - | #ifndef LINKED_LIST_H | |
| 2 | - | #define LINKED_LIST_H | |
| 1 | + | #ifndef CUTILS_LINKED_LIST_H | |
| 2 | + | #define CUTILS_LINKED_LIST_H | |
| 3 | 3 | ||
| 4 | 4 | #include <cutils/common.h> | |
| 5 | 5 | #include <stdlib.h> | |
| @@ -84,4 +84,4 @@ static void ll_swap(LinkedList* ll, size_t item1, size_t item2) | |||
|---|---|---|---|
| 84 | 84 | } | |
| 85 | 85 | ||
| 86 | 86 | ||
| 87 | - | #endif /* LINKED_LIST_H */ | |
| 87 | + | #endif /* CUTILS_LINKED_LIST_H */ | |
Minclude/cutils/misc.h
| @@ -1,5 +1,5 @@ | |||
|---|---|---|---|
| 1 | - | #ifndef MISC_H | |
| 2 | - | #define MISC_H | |
| 1 | + | #ifndef CUTILS_MISC_H | |
| 2 | + | #define CUTILS_MISC_H | |
| 3 | 3 | ||
| 4 | 4 | #ifdef __unix__ | |
| 5 | 5 | #ifndef _BSD_SOURCE | |
| @@ -19,6 +19,16 @@ | |||
|---|---|---|---|
| 19 | 19 | #include <cutils/common.h> | |
| 20 | 20 | #include <ctype.h> | |
| 21 | 21 | ||
| 22 | + | typedef unsigned char byte; | |
| 23 | + | ||
| 24 | + | #ifndef CUTILS_NO_SHORTHANDLES | |
| 25 | + | typedef unsigned char uchar; | |
| 26 | + | typedef unsigned short ushort; | |
| 27 | + | typedef unsigned int uint; | |
| 28 | + | typedef unsigned long ulong; | |
| 29 | + | typedef unsigned long long ullong; | |
| 30 | + | #endif /* CUTILS_NO_SHORTHANDLES */ | |
| 31 | + | ||
| 22 | 32 | #if __STDC_VERSION__ >= 199901L | |
| 23 | 33 | #ifdef UINT32_MAX | |
| 24 | 34 | uint32_t ntoh32(uint32_t const net); | |
| @@ -26,7 +36,7 @@ | |||
|---|---|---|---|
| 26 | 36 | HEDLEY_INLINE | |
| 27 | 37 | static void memqswap_stack(void* item1, void* item2, size_t length) | |
| 28 | 38 | { | |
| 29 | - | unsigned char tmp[length]; | |
| 39 | + | byte tmp[length]; | |
| 30 | 40 | memcpy(tmp, item1, length); | |
| 31 | 41 | memcpy(item1, item2, length); | |
| 32 | 42 | memcpy(item2, tmp, length); | |
| @@ -36,8 +46,8 @@ | |||
|---|---|---|---|
| 36 | 46 | HEDLEY_INLINE | |
| 37 | 47 | static void memswap(void* item1, void* item2, size_t length) | |
| 38 | 48 | { | |
| 39 | - | unsigned char *item1_tmp, *item2_tmp; | |
| 40 | - | unsigned char tmp; | |
| 49 | + | byte *item1_tmp, *item2_tmp; | |
| 50 | + | byte tmp; | |
| 41 | 51 | ||
| 42 | 52 | item1_tmp = item1; | |
| 43 | 53 | item2_tmp = item2; | |
| @@ -70,4 +80,4 @@ void sleep_ms(unsigned int milliseconds); | |||
|---|---|---|---|
| 70 | 80 | uintmax_t timespec_ms(const struct timespec* ts); | |
| 71 | 81 | #endif | |
| 72 | 82 | ||
| 73 | - | #endif /* MISC_H */ | |
| 83 | + | #endif /* CUTILS_MISC_H */ | |
Minclude/cutils/vector.h
| @@ -1,5 +1,5 @@ | |||
|---|---|---|---|
| 1 | - | #ifndef VECTOR_H | |
| 2 | - | #define VECTOR_H | |
| 1 | + | #ifndef CUTILS_VECTOR_H | |
| 2 | + | #define CUTILS_VECTOR_H | |
| 3 | 3 | ||
| 4 | 4 | #define VECTOR_DEFAULT_SIZE 4 | |
| 5 | 5 | ||
| @@ -34,4 +34,4 @@ HEDLEY_NON_NULL(3) | |||
|---|---|---|---|
| 34 | 34 | size_t* vector_find(const Vector* haystack, const void* needle, int (*cmp)(const void*, const void*)); | |
| 35 | 35 | ||
| 36 | 36 | ||
| 37 | - | #endif /* VECTOR_H */ | |
| 37 | + | #endif /* CUTILS_VECTOR_H */ | |
Asrc/bitfuncs.c
| @@ -0,0 +1,22 @@ | |||
|---|---|---|---|
| 1 | + | #include <cutils/bitfuncs.h> | |
| 2 | + | ||
| 3 | + | char* _bitstring(size_t size, uintmax_t value) | |
| 4 | + | { | |
| 5 | + | char* ret = NULL; | |
| 6 | + | size_t i, pos; | |
| 7 | + | ||
| 8 | + | bool leading_zero = true; | |
| 9 | + | for(i = 1, pos = 0; i <= size; i++) | |
| 10 | + | { | |
| 11 | + | byte tmp = (byte)((value & ((uintmax_t)1 << (size-i))) >> (size-i)); | |
| 12 | + | if(tmp && leading_zero) | |
| 13 | + | { | |
| 14 | + | leading_zero = false; | |
| 15 | + | ret = malloc(size-i); | |
| 16 | + | ret[size-i-1] = '\0'; | |
| 17 | + | } | |
| 18 | + | if(!leading_zero) | |
| 19 | + | ret[pos++] = tmp ? '1' : '0'; | |
| 20 | + | } | |
| 21 | + | return ret; | |
| 22 | + | } | |
Msrc/byte_array.c
| @@ -95,8 +95,8 @@ bool bytearray_adjust_size(Bytearray* bytearray, size_t size) | |||
|---|---|---|---|
| 95 | 95 | { | |
| 96 | 96 | while(bytearray->capacity < size) | |
| 97 | 97 | { | |
| 98 | - | char* tmp = bytearray->items; | |
| 99 | - | bytearray->items = reallocsafe_inc(bytearray->items, sizeof(*bytearray->items), bytearray->capacity, bytearray->capacity); | |
| 98 | + | byte* tmp = bytearray->items; | |
| 99 | + | bytearray->items = cutil_reallocarray_inc(bytearray->items, bytearray->capacity, bytearray->element_size, bytearray->capacity); | |
| 100 | 100 | if(!bytearray->items) | |
| 101 | 101 | { | |
| 102 | 102 | bytearray->items = tmp; | |
| @@ -111,8 +111,8 @@ bool bytearray_shrink(Bytearray* bytearray) | |||
|---|---|---|---|
| 111 | 111 | { | |
| 112 | 112 | while(bytearray->capacity > bytearray->length*2) | |
| 113 | 113 | { | |
| 114 | - | char* tmp = bytearray->items; | |
| 115 | - | bytearray->items = realloc(bytearray->items, (bytearray->capacity*bytearray->element_size)/2); | |
| 114 | + | byte* tmp = bytearray->items; | |
| 115 | + | bytearray->items = cutil_reallocarray(bytearray->items, bytearray->capacity/2, bytearray->element_size); | |
| 116 | 116 | if(!bytearray->items) | |
| 117 | 117 | { | |
| 118 | 118 | bytearray->items = tmp; | |
Msrc/dyn_string.c
| @@ -75,41 +75,39 @@ bool string_concat(String* string, const String* other) | |||
|---|---|---|---|
| 75 | 75 | return true; | |
| 76 | 76 | } | |
| 77 | 77 | ||
| 78 | - | size_t* string_find_char(const String* haystack, const char needle) | |
| 78 | + | bool string_find_char(const String* haystack, const char needle, size_t* pos) | |
| 79 | 79 | { | |
| 80 | - | size_t i, *ret; | |
| 80 | + | size_t i; | |
| 81 | 81 | ||
| 82 | 82 | for(i = 0; i < haystack->length; i++) | |
| 83 | 83 | { | |
| 84 | 84 | if(haystack->chars[i] == needle) | |
| 85 | 85 | { | |
| 86 | - | ret = malloc(sizeof(*ret)); | |
| 87 | - | *ret = i; | |
| 88 | - | return ret; | |
| 86 | + | *pos = i; | |
| 87 | + | return true; | |
| 89 | 88 | } | |
| 90 | 89 | } | |
| 91 | - | return NULL; | |
| 90 | + | return false; | |
| 92 | 91 | } | |
| 93 | 92 | ||
| 94 | - | size_t* string_find_str(const String* haystack, const String* needle) | |
| 93 | + | bool string_find_str(const String* haystack, const String* needle, size_t* pos) | |
| 95 | 94 | { | |
| 96 | - | size_t *ret, i, j, tmp; | |
| 95 | + | size_t i, j, tmp; | |
| 97 | 96 | ||
| 98 | 97 | for(i = 0; i < haystack->length; i++) | |
| 99 | 98 | { | |
| 100 | - | for(j = 0, tmp = i; j < needle->length; j++, i++) | |
| 99 | + | for(j = 0, tmp = i; j < needle->length && i < haystack->length; j++, i++) | |
| 101 | 100 | { | |
| 102 | 101 | if(haystack->chars[i] != needle->chars[j]) | |
| 103 | 102 | { | |
| 104 | 103 | break; | |
| 105 | 104 | } else if(j == needle->length-1){ | |
| 106 | - | ret = malloc(sizeof(*ret)); | |
| 107 | - | *ret = tmp; | |
| 108 | - | return ret; | |
| 105 | + | *pos = tmp; | |
| 106 | + | return true; | |
| 109 | 107 | } | |
| 110 | 108 | } | |
| 111 | 109 | } | |
| 112 | - | return NULL; | |
| 110 | + | return false; | |
| 113 | 111 | } | |
| 114 | 112 | ||
| 115 | 113 | bool string_adjust_size(String* string, size_t size) | |
| @@ -117,7 +115,7 @@ bool string_adjust_size(String* string, size_t size) | |||
|---|---|---|---|
| 117 | 115 | while(string->capacity < size) | |
| 118 | 116 | { | |
| 119 | 117 | char* tmp = string->chars; | |
| 120 | - | string->chars = reallocsafe_inc(string->chars, sizeof(*string->chars), string->capacity, string->capacity); | |
| 118 | + | string->chars = cutil_reallocarray_inc(string->chars, sizeof(*string->chars), string->capacity, string->capacity); | |
| 121 | 119 | if(!string->chars) | |
| 122 | 120 | { | |
| 123 | 121 | string->chars = tmp; | |
Msrc/extensions.c
| @@ -1,6 +1,6 @@ | |||
|---|---|---|---|
| 1 | 1 | #include <cutils/extensions.h> | |
| 2 | 2 | ||
| 3 | - | int strcmp_nocase(const char* s1, const char* s2) | |
| 3 | + | int cutil_strcasecmp(const char* s1, const char* s2) | |
| 4 | 4 | { | |
| 5 | 5 | const unsigned char* p1 = (const unsigned char*)s1; | |
| 6 | 6 | const unsigned char* p2 = (const unsigned char*)s2; | |
| @@ -18,7 +18,7 @@ int strcmp_nocase(const char* s1, const char* s2) | |||
|---|---|---|---|
| 18 | 18 | return c1 - c2; | |
| 19 | 19 | } | |
| 20 | 20 | ||
| 21 | - | int strncmp_nocase(const char* s1, const char* s2, size_t n) | |
| 21 | + | int cutil_strncasecmp(const char* s1, const char* s2, size_t n) | |
| 22 | 22 | { | |
| 23 | 23 | const unsigned char* p1 = (const unsigned char*)s1; | |
| 24 | 24 | const unsigned char* p2 = (const unsigned char*)s2; | |
| @@ -38,3 +38,60 @@ int strncmp_nocase(const char* s1, const char* s2, size_t n) | |||
|---|---|---|---|
| 38 | 38 | ||
| 39 | 39 | return c1 - c2; | |
| 40 | 40 | } | |
| 41 | + | ||
| 42 | + | size_t cutil_strnlen(const char *s, size_t maxlen) | |
| 43 | + | { | |
| 44 | + | size_t len; | |
| 45 | + | ||
| 46 | + | for(len = 0; len < maxlen; len++, s++) | |
| 47 | + | { | |
| 48 | + | if (!*s) | |
| 49 | + | break; | |
| 50 | + | } | |
| 51 | + | return (len); | |
| 52 | + | } | |
| 53 | + | ||
| 54 | + | char* cutil_strdup(const char *s) | |
| 55 | + | { | |
| 56 | + | char* ret; | |
| 57 | + | size_t len = strlen(s); | |
| 58 | + | ||
| 59 | + | ret = malloc(len+1); | |
| 60 | + | memcpy(ret, s, len+1); | |
| 61 | + | ||
| 62 | + | return ret; | |
| 63 | + | } | |
| 64 | + | ||
| 65 | + | char* cutil_strndup(const char *s, size_t n) | |
| 66 | + | { | |
| 67 | + | char* ret; | |
| 68 | + | size_t len = cutil_strnlen(s, n); | |
| 69 | + | ||
| 70 | + | len = len <= n ? len : n; | |
| 71 | + | ||
| 72 | + | ret = malloc(len+1); | |
| 73 | + | memcpy(ret, s, len+1); | |
| 74 | + | ||
| 75 | + | return ret; | |
| 76 | + | } | |
| 77 | + | ||
| 78 | + | #if __STDC_VERSION__ >= 199901L | |
| 79 | + | int cutil_asprintf(char** strp, const char* format, ...) | |
| 80 | + | { | |
| 81 | + | va_list ap; | |
| 82 | + | size_t needed; | |
| 83 | + | int ret; | |
| 84 | + | ||
| 85 | + | va_start(ap, format); | |
| 86 | + | needed = (size_t)vsnprintf(NULL, 0, format, ap); | |
| 87 | + | va_end(ap); | |
| 88 | + | ||
| 89 | + | *strp = malloc(needed); | |
| 90 | + | ||
| 91 | + | va_start(ap, format); | |
| 92 | + | ret = snprintf(*strp, needed, format, ap); | |
| 93 | + | va_end(ap); | |
| 94 | + | ||
| 95 | + | return ret; | |
| 96 | + | } | |
| 97 | + | #endif | |
Msrc/meson.build
| @@ -1,4 +1,4 @@ | |||
|---|---|---|---|
| 1 | - | src = ['vector.c', 'dyn_string.c', 'misc.c', 'byte_array.c', 'linked-list.c', 'extensions.c'] | |
| 1 | + | src = ['vector.c', 'dyn_string.c', 'misc.c', 'byte_array.c', 'linked-list.c', 'extensions.c', 'bitfuncs.c'] | |
| 2 | 2 | srctest = src + ['test.c'] | |
| 3 | 3 | ||
| 4 | 4 | cutils_lib = library('cutils', src, include_directories : inc, install: true) | |
Msrc/test.c
| @@ -70,7 +70,7 @@ static void test_string(void) | |||
|---|---|---|---|
| 70 | 70 | String* string; | |
| 71 | 71 | String* string2; | |
| 72 | 72 | char* cstring; | |
| 73 | - | size_t* tmp; | |
| 73 | + | size_t tmp; | |
| 74 | 74 | ||
| 75 | 75 | string = new_string(); | |
| 76 | 76 | ||
| @@ -107,9 +107,8 @@ static void test_string(void) | |||
|---|---|---|---|
| 107 | 107 | ||
| 108 | 108 | string = from_cstring("abcd"); | |
| 109 | 109 | string2 = from_cstring("cd"); | |
| 110 | - | tmp = string_find_str(string, string2); | |
| 111 | - | assert(*tmp == 2); | |
| 112 | - | free(tmp); | |
| 110 | + | string_find_str(string, string2, &tmp); | |
| 111 | + | assert(tmp == 2); | |
| 113 | 112 | ||
| 114 | 113 | delete_string(string2); | |
| 115 | 114 | delete_string(string); | |
| @@ -127,7 +126,7 @@ static void test_bytearray(void) | |||
|---|---|---|---|
| 127 | 126 | bytearray_push(bt, "d"); | |
| 128 | 127 | bytearray_push(bt, "\0"); | |
| 129 | 128 | ||
| 130 | - | assert(strcmp(bt->items, "abcd") == 0); | |
| 129 | + | assert(strcmp((char*)bt->items, "abcd") == 0); | |
| 131 | 130 | ||
| 132 | 131 | tmpchar = bytearray_pop(bt, NULL); | |
| 133 | 132 | assert(*tmpchar == '\0'); | |
| @@ -248,6 +247,24 @@ static void test_ll(void) | |||
|---|---|---|---|
| 248 | 247 | } | |
| 249 | 248 | } | |
| 250 | 249 | ||
| 250 | + | static void test_bitfuncs(void) | |
| 251 | + | { | |
| 252 | + | byte tmp = 0; | |
| 253 | + | ||
| 254 | + | setbit(byte, tmp, 0); | |
| 255 | + | assert(tmp == 1); | |
| 256 | + | setbit(byte, tmp, 1); | |
| 257 | + | assert(tmp == 3); | |
| 258 | + | setbit(byte, tmp, 2); | |
| 259 | + | assert(tmp == 7); | |
| 260 | + | ||
| 261 | + | clearbit(byte, tmp, 2); | |
| 262 | + | assert(tmp == 3); | |
| 263 | + | ||
| 264 | + | assert(getbit(byte, tmp, 0) == 1); | |
| 265 | + | assert(getbit(byte, tmp, 5) == 0); | |
| 266 | + | } | |
| 267 | + | ||
| 251 | 268 | int main(int argc, char** argv) | |
| 252 | 269 | { | |
| 253 | 270 | /* memswap tests */ | |
| @@ -276,13 +293,14 @@ int main(int argc, char** argv) | |||
|---|---|---|---|
| 276 | 293 | test_timespec(); | |
| 277 | 294 | #endif | |
| 278 | 295 | test_ll(); | |
| 296 | + | test_bitfuncs(); | |
| 279 | 297 | ||
| 280 | 298 | /* misc tests */ | |
| 281 | 299 | sleep_ms(1000); | |
| 282 | - | assert(strcmp_nocase("ASD", "aSd") == 0); | |
| 283 | - | assert(strcmp_nocase("BSD", "asd") > 0); | |
| 284 | - | assert(strcmp_nocase("ASD", "bsd") < 0); | |
| 285 | - | assert(strncmp_nocase("ASDn", "asd", 3) == 0); | |
| 286 | - | assert(strncmp_nocase("ASDn", "asd", 4) >0); | |
| 300 | + | assert(cutil_strcasecmp("ASD", "aSd") == 0); | |
| 301 | + | assert(cutil_strcasecmp("BSD", "asd") > 0); | |
| 302 | + | assert(cutil_strcasecmp("ASD", "bsd") < 0); | |
| 303 | + | assert(cutil_strncasecmp("ASDn", "asd", 3) == 0); | |
| 304 | + | assert(cutil_strncasecmp("ASDn", "asd", 4) >0); | |
| 287 | 305 | return 0; | |
| 288 | 306 | } | |
Msrc/vector.c
| @@ -90,7 +90,7 @@ bool vector_adjust_size(Vector* vector, size_t size) | |||
|---|---|---|---|
| 90 | 90 | while(vector->capacity < size) | |
| 91 | 91 | { | |
| 92 | 92 | void** tmp = vector->items; | |
| 93 | - | vector->items = reallocsafe_inc(vector->items, sizeof(*vector->items), vector->capacity, vector->capacity); | |
| 93 | + | vector->items = cutil_reallocarray_inc(vector->items, vector->capacity, sizeof(*vector->items), vector->capacity); | |
| 94 | 94 | if(!vector->items) | |
| 95 | 95 | { | |
| 96 | 96 | vector->items = tmp; | |
| @@ -106,7 +106,7 @@ bool vector_shrink(Vector* vector) | |||
|---|---|---|---|
| 106 | 106 | while(vector->capacity > (vector->length*2)) | |
| 107 | 107 | { | |
| 108 | 108 | void** tmp = vector->items; | |
| 109 | - | vector->items = realloc(vector->items, vector->capacity/2*sizeof(*vector->items)); | |
| 109 | + | vector->items = cutil_reallocarray(vector->items, vector->capacity/2, sizeof(*vector->items)); | |
| 110 | 110 | if(!vector->items) | |
| 111 | 111 | { | |
| 112 | 112 | vector->items = tmp; | |