added case insensitive strcmp and extra memswap implementation + tests
Minclude/cutils/misc.h
| @@ -17,6 +17,7 @@ | |||
|---|---|---|---|
| 17 | 17 | #include <string.h> | |
| 18 | 18 | #include <stdlib.h> | |
| 19 | 19 | #include <cutils/common.h> | |
| 20 | + | #include <ctype.h> | |
| 20 | 21 | ||
| 21 | 22 | #if __STDC_VERSION__ >= 199901L | |
| 22 | 23 | #include <stdint.h> | |
| @@ -24,6 +25,15 @@ | |||
|---|---|---|---|
| 24 | 25 | #ifdef UINT32_MAX | |
| 25 | 26 | uint32_t ntoh32(uint32_t const net); | |
| 26 | 27 | #endif | |
| 28 | + | HEDLEY_INLINE | |
| 29 | + | static void memqswap_stack(void* item1, void* item2, size_t length) | |
| 30 | + | { | |
| 31 | + | unsigned char tmp[length]; | |
| 32 | + | memcpy(tmp, item1, length); | |
| 33 | + | memcpy(item1, item2, length); | |
| 34 | + | memcpy(item2, tmp, length); | |
| 35 | + | free(tmp); | |
| 36 | + | } | |
| 27 | 37 | #else | |
| 28 | 38 | typedef unsigned long max_uint_t; | |
| 29 | 39 | #endif | |
| @@ -31,8 +41,8 @@ | |||
|---|---|---|---|
| 31 | 41 | HEDLEY_INLINE | |
| 32 | 42 | static void memswap(void* item1, void* item2, size_t length) | |
| 33 | 43 | { | |
| 34 | - | char *item1_tmp, *item2_tmp; | |
| 35 | - | char tmp; | |
| 44 | + | unsigned char *item1_tmp, *item2_tmp; | |
| 45 | + | unsigned char tmp; | |
| 36 | 46 | ||
| 37 | 47 | item1_tmp = item1; | |
| 38 | 48 | item2_tmp = item2; | |
| @@ -46,16 +56,11 @@ static void memswap(void* item1, void* item2, size_t length) | |||
|---|---|---|---|
| 46 | 56 | } | |
| 47 | 57 | ||
| 48 | 58 | HEDLEY_INLINE | |
| 49 | - | static bool memqswap(void* item1, void* item2, size_t length) | |
| 59 | + | static void memqswap(void* item1, void* item2, void* tmp, size_t length) | |
| 50 | 60 | { | |
| 51 | - | void* tmp = malloc(length); | |
| 52 | - | if(!tmp) | |
| 53 | - | return false; | |
| 54 | 61 | memcpy(tmp, item1, length); | |
| 55 | 62 | memcpy(item1, item2, length); | |
| 56 | 63 | memcpy(item2, tmp, length); | |
| 57 | - | free(tmp); | |
| 58 | - | return true; | |
| 59 | 64 | } | |
| 60 | 65 | ||
| 61 | 66 | #if __STDC_VERSION__ >= 201112L | |
| @@ -63,6 +68,8 @@ static bool memqswap(void* item1, void* item2, size_t length) | |||
|---|---|---|---|
| 63 | 68 | #endif | |
| 64 | 69 | ||
| 65 | 70 | void sleep_ms(unsigned int milliseconds); | |
| 71 | + | int strcmp_nocase(const char* s1, const char* s2); | |
| 72 | + | int strncmp_nocase(const char* s1, const char* s2, size_t n); | |
| 66 | 73 | ||
| 67 | 74 | #if __STDC_VERSION__ >= 201112L | |
| 68 | 75 | struct timespec timespec_diff(const struct timespec* old_ts, const struct timespec* new_ts); | |
Msrc/misc.c
| @@ -10,6 +10,45 @@ void sleep_ms(unsigned int milliseconds) | |||
|---|---|---|---|
| 10 | 10 | #endif | |
| 11 | 11 | } | |
| 12 | 12 | ||
| 13 | + | int strcmp_nocase(const char* s1, const char* s2) | |
| 14 | + | { | |
| 15 | + | const unsigned char* p1 = (const unsigned char*)s1; | |
| 16 | + | const unsigned char* p2 = (const unsigned char*)s2; | |
| 17 | + | unsigned char c1, c2; | |
| 18 | + | ||
| 19 | + | do | |
| 20 | + | { | |
| 21 | + | c1 = (unsigned char)tolower(*p1++); | |
| 22 | + | c2 = (unsigned char)tolower(*p2++); | |
| 23 | + | if (c1 == '\0') | |
| 24 | + | return c1 - c2; | |
| 25 | + | } | |
| 26 | + | while (c1 == c2); | |
| 27 | + | ||
| 28 | + | return c1 - c2; | |
| 29 | + | } | |
| 30 | + | ||
| 31 | + | int strncmp_nocase(const char* s1, const char* s2, size_t n) | |
| 32 | + | { | |
| 33 | + | const unsigned char* p1 = (const unsigned char*)s1; | |
| 34 | + | const unsigned char* p2 = (const unsigned char*)s2; | |
| 35 | + | unsigned char c1, c2; | |
| 36 | + | ||
| 37 | + | if(n == 0) | |
| 38 | + | return 0; | |
| 39 | + | ||
| 40 | + | do | |
| 41 | + | { | |
| 42 | + | c1 = (unsigned char)tolower(*p1++); | |
| 43 | + | c2 = (unsigned char)tolower(*p2++); | |
| 44 | + | if (c1 == '\0') | |
| 45 | + | return c1 - c2; | |
| 46 | + | } | |
| 47 | + | while (c1 == c2 && --n); | |
| 48 | + | ||
| 49 | + | return c1 - c2; | |
| 50 | + | } | |
| 51 | + | ||
| 13 | 52 | #if __STDC_VERSION__ >= 199901L | |
| 14 | 53 | #ifdef UINT32_MAX | |
| 15 | 54 | uint32_t ntoh32(uint32_t const net) | |
Msrc/test.c
| @@ -246,15 +246,28 @@ static void test_ll(void) | |||
|---|---|---|---|
| 246 | 246 | ||
| 247 | 247 | delete_ll(ll, NULL); | |
| 248 | 248 | } | |
| 249 | - | ||
| 250 | 249 | } | |
| 251 | 250 | ||
| 252 | 251 | int main(int argc, char** argv) | |
| 253 | 252 | { | |
| 253 | + | /* memswap tests */ | |
| 254 | 254 | int a = 5; | |
| 255 | 255 | int b = 2; | |
| 256 | + | void* tmp = malloc(sizeof(int)); | |
| 257 | + | ||
| 256 | 258 | memswap(&a, &b, sizeof(int)); | |
| 257 | 259 | assert(a == 2 && b == 5); | |
| 260 | + | #if __STDC_VERSION__ >= 199901L | |
| 261 | + | memqswap_stack(&a, &b, sizeof(int)); | |
| 262 | + | assert(a == 5 && b == 2); | |
| 263 | + | memqswap(&a, &b, tmp, sizeof(int)); | |
| 264 | + | assert(a == 2 && b == 5); | |
| 265 | + | #else | |
| 266 | + | memqswap(&a, &b, tmp, sizeof(int)); | |
| 267 | + | assert(a == 5 && b == 2); | |
| 268 | + | #endif | |
| 269 | + | free(tmp); | |
| 270 | + | /* memswap tests */ | |
| 258 | 271 | ||
| 259 | 272 | test_vector(); | |
| 260 | 273 | test_string(); | |
| @@ -264,6 +277,12 @@ int main(int argc, char** argv) | |||
|---|---|---|---|
| 264 | 277 | #endif | |
| 265 | 278 | test_ll(); | |
| 266 | 279 | ||
| 280 | + | /* misc tests */ | |
| 267 | 281 | 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); | |
| 268 | 287 | return 0; | |
| 269 | 288 | } | |