added case insensitive strcmp and extra memswap implementation + tests

AuthorKonata <konata@posteo.jp>
Date
Commitf9407df0201ad4394d75a2cee05d7b9097a434d0
Parente8864d4
3 files changed, 74 insertions(+), 9 deletions(-)
Minclude/cutils/misc.h
@@ -17,6 +17,7 @@
1717 #include <string.h>
1818 #include <stdlib.h>
1919 #include <cutils/common.h>
20+#include <ctype.h>
2021
2122 #if __STDC_VERSION__ >= 199901L
2223 #include <stdint.h>
@@ -24,6 +25,15 @@
2425 #ifdef UINT32_MAX
2526 uint32_t ntoh32(uint32_t const net);
2627 #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+ }
2737 #else
2838 typedef unsigned long max_uint_t;
2939 #endif
@@ -31,8 +41,8 @@
3141 HEDLEY_INLINE
3242 static void memswap(void* item1, void* item2, size_t length)
3343 {
34- char *item1_tmp, *item2_tmp;
35- char tmp;
44+ unsigned char *item1_tmp, *item2_tmp;
45+ unsigned char tmp;
3646
3747 item1_tmp = item1;
3848 item2_tmp = item2;
@@ -46,16 +56,11 @@ static void memswap(void* item1, void* item2, size_t length)
4656 }
4757
4858 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)
5060 {
51- void* tmp = malloc(length);
52- if(!tmp)
53- return false;
5461 memcpy(tmp, item1, length);
5562 memcpy(item1, item2, length);
5663 memcpy(item2, tmp, length);
57- free(tmp);
58- return true;
5964 }
6065
6166 #if __STDC_VERSION__ >= 201112L
@@ -63,6 +68,8 @@ static bool memqswap(void* item1, void* item2, size_t length)
6368 #endif
6469
6570 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);
6673
6774 #if __STDC_VERSION__ >= 201112L
6875 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)
1010 #endif
1111 }
1212
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+
1352 #if __STDC_VERSION__ >= 199901L
1453 #ifdef UINT32_MAX
1554 uint32_t ntoh32(uint32_t const net)
Msrc/test.c
@@ -246,15 +246,28 @@ static void test_ll(void)
246246
247247 delete_ll(ll, NULL);
248248 }
249-
250249 }
251250
252251 int main(int argc, char** argv)
253252 {
253+ /* memswap tests */
254254 int a = 5;
255255 int b = 2;
256+ void* tmp = malloc(sizeof(int));
257+
256258 memswap(&a, &b, sizeof(int));
257259 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 */
258271
259272 test_vector();
260273 test_string();
@@ -264,6 +277,12 @@ int main(int argc, char** argv)
264277 #endif
265278 test_ll();
266279
280+ /* misc tests */
267281 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);
268287 return 0;
269288 }