util.c (1119B)
1 #include <string.h> 2 #include <stddef.h> 3 #include "memory.h" 4 5 char * 6 next_utf8(char *str) { 7 while ((*str & 0xC0) == 0x80) 8 str++; 9 return str; 10 } 11 12 size_t 13 next_utf8_len(char *str, size_t len) { 14 size_t cur = 0; 15 while (cur < len && (str[cur] & 0xC0) == 0x80) 16 cur++; 17 return cur; 18 } 19 20 /* FIXME: change these to macros somehow */ 21 size_t 22 add_sz(size_t a, size_t b) { 23 if (a > SIZE_MAX - b) 24 err_overflow(); 25 return a + b; 26 } 27 28 size_t 29 add_sz3(size_t a, size_t b, size_t c) { 30 if (b > SIZE_MAX - c || a > SIZE_MAX - (b + c)) 31 err_overflow(); 32 return a + b + c; 33 } 34 35 void 36 swap_sz(size_t *a, size_t *b) { 37 size_t tmp = *a; 38 *a = *b; 39 *b = tmp; 40 } 41 42 void 43 sort_range(size_t *l1, size_t *b1, size_t *l2, size_t *b2) { 44 if (*l1 == *l2 && *b1 > *b2) { 45 swap_sz(b1, b2); 46 } else if (*l1 > *l2) { 47 swap_sz(l1, l2); 48 swap_sz(b1, b2); 49 } 50 } 51 52 int 53 str_array_equal(const char *terminated, const char *array, size_t len) { 54 if (!strncmp(terminated, array, len)) { 55 /* this is kind of inefficient, but there's no way to know 56 otherwise if strncmp just stopped comparing after a '\0' */ 57 return strlen(terminated) == len; 58 } 59 return 0; 60 }