| 1 | #ifndef _XH_STRING_H_ | ||
| 2 | #define _XH_STRING_H_ | ||
| 3 | |||
| 4 | #include "xh_config.h" | ||
| 5 | #include "xh_core.h" | ||
| 6 | |||
| 7 | #ifdef XH_LITTLE_ENDIAN | ||
| 8 | #define xh_str_equal2(p, c0, c1) \ | ||
| 9 | ((((uint32_t *) (p))[0] & 0xffff) == ((c1 << 8) | c0)) | ||
| 10 | |||
| 11 | #define xh_str_equal3(p, c0, c1, c2) \ | ||
| 12 | ((((uint32_t *) (p))[0] & 0xffffff) == ((c2 << 16) | (c1 << 8) | c0)) | ||
| 13 | |||
| 14 | #define xh_str_equal4(p, c0, c1, c2, c3) \ | ||
| 15 | (*(uint32_t *) (p) == ((c3 << 24) | (c2 << 16) | (c1 << 8) | c0)) | ||
| 16 | #else | ||
| 17 | #define xh_str_equal2(p, c0, c1) \ | ||
| 18 | ((((uint32_t *) (p))[0] & 0xffff0000) == ((c0 << 24) | (c1 << 16))) | ||
| 19 | |||
| 20 | #define xh_str_equal3(p, c0, c1, c2) \ | ||
| 21 | ((((uint32_t *) (p))[0] & 0xffffff00) == ((c0 << 24) | (c1 << 16) | (c2 << 8))) | ||
| 22 | |||
| 23 | #define xh_str_equal4(p, c0, c1, c2, c3) \ | ||
| 24 | (*(uint32_t *) (p) == ((c0 << 24) | (c1 << 16) | (c2 << 8) | c3)) | ||
| 25 | #endif | ||
| 26 | |||
| 27 | #define xh_str_equal5(p, c0, c1, c2, c3, c4) \ | ||
| 28 | (xh_str_equal4(p, c0, c1, c2, c3) && (p)[4] == c4) | ||
| 29 | |||
| 30 | #define xh_str_equal6(p, c0, c1, c2, c3, c4, c5) \ | ||
| 31 | (xh_str_equal4(p, c0, c1, c2, c3) && xh_str_equal2(&(p)[4], c4, c5)) | ||
| 32 | |||
| 33 | #define xh_str_equal7(p, c0, c1, c2, c3, c4, c5, c6) \ | ||
| 34 | (xh_str_equal4(p, c0, c1, c2, c3) && xh_str_equal3(&(p)[4], c4, c5, c6)) | ||
| 35 | |||
| 36 | #define xh_str_equal8(p, c0, c1, c2, c3, c4, c5, c6, c7) \ | ||
| 37 | (xh_str_equal4(p, c0, c1, c2, c3) && xh_str_equal4(&(p)[4], c4, c5, c6, c7)) | ||
| 38 | |||
| 39 | #define xh_str_equal9(p, c0, c1, c2, c3, c4, c5, c6, c7, c8) \ | ||
| 40 | (xh_str_equal8(p, c0, c1, c2, c3, c4, c5, c6, c7) && (p)[8] == c8) | ||
| 41 | |||
| 42 | #define xh_str_equal10(p, c0, c1, c2, c3, c4, c5, c6, c7, c8, c9) \ | ||
| 43 | (xh_str_equal8(p, c0, c1, c2, c3, c4, c5, c6, c7) && xh_str_equal2(&p[8], c8, c9)) | ||
| 44 | |||
| 45 | #define xh_str_equal11(p, c0, c1, c2, c3, c4, c5, c6, c7, c8, c9, c10) \ | ||
| 46 | (xh_str_equal8(p, c0, c1, c2, c3, c4, c5, c6, c7) && xh_str_equal3(&p[8], c8, c9, c10)) | ||
| 47 | |||
| 48 | #define xh_str_equal12(p, c0, c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11)\ | ||
| 49 | (xh_str_equal8(p, c0, c1, c2, c3, c4, c5, c6, c7) && xh_str_equal4(&(p)[8], c8, c9, c10, c11)) | ||
| 50 | |||
| 51 | #define xh_str_equal13(p, c0, c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11, c12)\ | ||
| 52 | (xh_str_equal8(p, c0, c1, c2, c3, c4, c5, c6, c7) && xh_str_equal5(&(p)[8], c8, c9, c10, c11, c12)) | ||
| 53 | |||
| 54 | #define xh_str_equal14(p, c0, c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11, c12, c13)\ | ||
| 55 | (xh_str_equal8(p, c0, c1, c2, c3, c4, c5, c6, c7) && xh_str_equal6(&(p)[8], c8, c9, c10, c11, c12, c13)) | ||
| 56 | |||
| 57 | |||
| 58 | #define xh_strcmp(s1, s2) strcmp((const char *) (s1), (const char *) (s2)) | ||
| 59 | #define xh_strcasecmp(s1, s2) strcasecmp((const char *) (s1), (const char *) (s2)) | ||
| 60 | #define xh_strncmp(s1, s2, n) strncmp((const char *) (s1), (const char *) (s2), (n)) | ||
| 61 | #define xh_strlen(s) strlen((const char *) (s)) | ||
| 62 | #define xh_strcpy(d, s) strcpy((char *) (d), (const char *) (s)) | ||
| 63 | #define xh_strncpy(d, s, n) strncpy((char *) (d), (const char *) (s), (n)) | ||
| 64 | |||
| 65 | #ifdef XH_SPARC_ARCH | ||
| 66 | #define xh_str_auto_equal2(p, c0, c1, s) (xh_strncmp(p, s, 2) == 0) | ||
| 67 | #define xh_str_auto_equal3(p, c0, c1, c2, s) (xh_strncmp(p, s, 3) == 0) | ||
| 68 | #define xh_str_auto_equal4(p, c0, c1, c2, c3, s) (xh_strncmp(p, s, 4) == 0) | ||
| 69 | #define xh_str_auto_equal5(p, c0, c1, c2, c3, c4, s) (xh_strncmp(p, s, 5) == 0) | ||
| 70 | #define xh_str_auto_equal6(p, c0, c1, c2, c3, c4, c5, s) (xh_strncmp(p, s, 6) == 0) | ||
| 71 | #define xh_str_auto_equal7(p, c0, c1, c2, c3, c4, c5, c6, s) (xh_strncmp(p, s, 7) == 0) | ||
| 72 | #define xh_str_auto_equal8(p, c0, c1, c2, c3, c4, c5, c6, c7, s) (xh_strncmp(p, s, 8) == 0 | ||
| 73 | #define xh_str_auto_equal9(p, c0, c1, c2, c3, c4, c5, c6, c7, c8, s) (xh_strncmp(p, s, 9) == 0) | ||
| 74 | #define xh_str_auto_equal10(p, c0, c1, c2, c3, c4, c5, c6, c7, c8, c9, s) (xh_strncmp(p, s, 10) == 0) | ||
| 75 | #define xh_str_auto_equal11(p, c0, c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, s) (xh_strncmp(p, s, 11) == 0) | ||
| 76 | #define xh_str_auto_equal12(p, c0, c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11, s) (xh_strncmp(p, s, 12) == 0) | ||
| 77 | #define xh_str_auto_equal13(p, c0, c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11, c12, s) (xh_strncmp(p, s, 13) == 0) | ||
| 78 | #define xh_str_auto_equal14(p, c0, c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11, c12, c13, s) \ | ||
| 79 | (xh_strncmp(p, s, 14) == 0) | ||
| 80 | #else | ||
| 81 | #define xh_str_auto_equal2(p, c0, c1, s) xh_str_equal2(p, c0, c1) | ||
| 82 | #define xh_str_auto_equal3(p, c0, c1, c2, s) xh_str_equal3(p, c0, c1, c2) | ||
| 83 | #define xh_str_auto_equal4(p, c0, c1, c2, c3, s) xh_str_equal4(p, c0, c1, c2, c3) | ||
| 84 | #define xh_str_auto_equal5(p, c0, c1, c2, c3, c4, s) xh_str_equal5(p, c0, c1, c2, c3, c4) | ||
| 85 | #define xh_str_auto_equal6(p, c0, c1, c2, c3, c4, c5, s) xh_str_equal6(p, c0, c1, c2, c3, c4, c5) | ||
| 86 | #define xh_str_auto_equal7(p, c0, c1, c2, c3, c4, c5, c6, s) xh_str_equal7(p, c0, c1, c2, c3, c4, c5, c6) | ||
| 87 | #define xh_str_auto_equal8(p, c0, c1, c2, c3, c4, c5, c6, c7, s) \ | ||
| 88 | xh_str_equal8(p, c0, c1, c2, c3, c4, c5, c6, c7) | ||
| 89 | #define xh_str_auto_equal9(p, c0, c1, c2, c3, c4, c5, c6, c7, c8, s) \ | ||
| 90 | xh_str_equal9(p, c0, c1, c2, c3, c4, c5, c6, c7, c8) | ||
| 91 | #define xh_str_auto_equal10(p, c0, c1, c2, c3, c4, c5, c6, c7, c8, c9, s) \ | ||
| 92 | xh_str_equal10(p, c0, c1, c2, c3, c4, c5, c6, c7, c8, c9) | ||
| 93 | #define xh_str_auto_equal11(p, c0, c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, s) \ | ||
| 94 | xh_str_equal11(p, c0, c1, c2, c3, c4, c5, c6, c7, c8, c9, c10) | ||
| 95 | #define xh_str_auto_equal12(p, c0, c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11, s) \ | ||
| 96 | xh_str_equal12(p, c0, c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11) | ||
| 97 | #define xh_str_auto_equal13(p, c0, c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11, c12, s) \ | ||
| 98 | xh_str_equal13(p, c0, c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11, c12) | ||
| 99 | #define xh_str_auto_equal14(p, c0, c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11, c12, c13, s) \ | ||
| 100 | xh_str_equal14(p, c0, c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11, c12, c13) | ||
| 101 | #endif | ||
| 102 | |||
| 103 | XH_INLINE xh_char_t * | ||
| 104 | xh_str_trim(xh_char_t *s, size_t *len) | ||
| 105 | { | ||
| 106 | xh_char_t *end, ch; | ||
| 107 | |||
| 108 | end = s + *len; | ||
| 109 | |||
| 110 | while ((ch = *s++) == ' ' || ch == '\t' || ch == '\n' || ch == '\r'); | ||
| 111 | if (ch == '\0') { | ||
| 112 | *len = 0; | ||
| 113 | return s - 1; | ||
| 114 | } | ||
| 115 | |||
| 116 | s--; | ||
| 117 | |||
| 118 | while (--end != s && ((ch = *end) == ' ' || ch == '\t' || ch == '\n' || ch == '\r')); | ||
| 119 | |||
| 120 | *len = end - s + 1; | ||
| 121 | |||
| 122 | return s; | ||
| 123 | } | ||
| 124 | |||
| 125 | XH_INLINE xh_char_t * | ||
| 126 | 18430 | xh_str_copy(xh_char_t *dest, const xh_char_t *src, size_t n) | |
| 127 | { | ||
| 128 | 18430 | dest[--n] = '\0'; | |
| 129 | 18430 | return XH_CHAR_CAST strncpy((char *) dest, (const char *) src, n); | |
| 130 | } | ||
| 131 | |||
| 132 | XH_INLINE xh_char_t * | ||
| 133 | xh_str_range_copy(xh_char_t *dest, const xh_char_t *src, size_t l, size_t n) | ||
| 134 | { | ||
| 135 | if (l < n) n = l + 1; | ||
| 136 | dest[--n] = '\0'; | ||
| 137 | return XH_CHAR_CAST strncpy((char *) dest, (const char *) src, n); | ||
| 138 | } | ||
| 139 | |||
| 140 | XH_INLINE void | ||
| 141 | xh_memmove(xh_char_t *dest, const xh_char_t *src, size_t n) | ||
| 142 | { | ||
| 143 | while (n--) *dest++ = *src++; | ||
| 144 | } | ||
| 145 | |||
| 146 | XH_INLINE xh_bool_t | ||
| 147 | xh_str_is_xml(xh_char_t *s) | ||
| 148 | { | ||
| 149 | xh_char_t ch; | ||
| 150 | |||
| 151 | while ((ch = *s++) == ' ' || ch =='\t' || ch == '\n' || ch == '\r'); | ||
| 152 | if (ch == '<') return TRUE; | ||
| 153 | |||
| 154 | return FALSE; | ||
| 155 | } | ||
| 156 | |||
| 157 | #endif /* _XH_STRING_H_ */ |