1 : /* Copyright (c) 2007 James Antill -- See LICENSE file for terms. */
2 :
3 : #ifndef USTR_CMP_H
4 : #error " You should have already included ustr-cmp.h, or just include ustr.h."
5 : #endif
6 :
7 : USTR_CONF_I_PROTO
8 : int ustr_cmp_buf(const struct Ustr *s1, const void *buf, size_t len2)
9 116 : {
10 116 : size_t len1 = 0;
11 116 : size_t lenm = 0;
12 116 : int ret = 0;
13 116 : int def = 0;
14 :
15 57 : USTR_ASSERT(ustr_assert_valid(s1) && buf);
16 :
17 95 : len1 = ustr_len(s1);
18 116 : if (len1 == len2)
19 70 : return (memcmp(ustr_cstr(s1), buf, len1));
20 :
21 46 : if (len1 > len2)
22 : {
23 24 : lenm = len2;
24 24 : def = 1;
25 : }
26 : else
27 : {
28 22 : lenm = len1;
29 22 : def = -1;
30 : }
31 :
32 52 : if (lenm && (ret = memcmp(ustr_cstr(s1), buf, lenm)))
33 8 : return (ret);
34 :
35 38 : return (def);
36 : }
37 :
38 : USTR_CONF_I_PROTO
39 : int ustr_cmp_subustr(const struct Ustr *s1,
40 : const struct Ustr *s2, size_t pos, size_t len)
41 22 : {
42 10 : USTR_ASSERT(ustr_assert_valid(s1) && ustr_assert_valid(s2));
43 :
44 22 : if (!ustr_assert_valid_subustr(s2, pos, len))
45 2 : return (ustr_cmp_buf(s1, "", 0));
46 :
47 20 : return (ustr_cmp_buf(s1, ustr_cstr(s2) + --pos, len));
48 : }
49 :
50 : USTR_CONF_i_PROTO
51 : int ustr__memcasecmp(const void *passed_s1, const void *passed_s2, size_t len)
52 1894 : {
53 2796 : const unsigned char *s1 = passed_s1;
54 2796 : const unsigned char *s2 = passed_s2;
55 :
56 6838 : while (len)
57 : {
58 4452 : unsigned char c1 = *s1;
59 4452 : unsigned char c2 = *s2;
60 :
61 4452 : if ((c1 >= 0x61) && (c1 <= 0x7a))
62 2064 : c1 ^= 0x20;
63 4452 : if ((c2 >= 0x61) && (c2 <= 0x7a))
64 2584 : c2 ^= 0x20;
65 :
66 4452 : if (c1 != c2)
67 2304 : return (c1 - c2);
68 :
69 2148 : ++s1;
70 2148 : ++s2;
71 2148 : --len;
72 : }
73 :
74 492 : return (0);
75 : }
76 :
77 : USTR_CONF_I_PROTO
78 : int ustr_cmp_case_buf(const struct Ustr *s1, const void *buf, size_t len2)
79 182 : {
80 182 : size_t len1 = 0;
81 182 : size_t lenm = 0;
82 182 : int ret = 0;
83 182 : int def = 0;
84 :
85 90 : USTR_ASSERT(ustr_assert_valid(s1) && buf);
86 :
87 137 : len1 = ustr_len(s1);
88 182 : if (len1 == len2)
89 175 : return (ustr__memcasecmp(ustr_cstr(s1), buf, len1));
90 :
91 42 : if (len1 > len2)
92 : {
93 22 : lenm = len2;
94 22 : def = 1;
95 : }
96 : else
97 : {
98 20 : lenm = len1;
99 20 : def = -1;
100 : }
101 :
102 62 : if (lenm && (ret = ustr__memcasecmp(ustr_cstr(s1), buf, lenm)))
103 16 : return (ret);
104 :
105 26 : return (def);
106 : }
107 :
108 : USTR_CONF_I_PROTO
109 : int ustr_cmp_case_subustr(const struct Ustr *s1,
110 : const struct Ustr *s2, size_t pos, size_t len)
111 6 : {
112 2 : USTR_ASSERT(ustr_assert_valid(s1) && ustr_assert_valid(s2));
113 :
114 6 : if (!ustr_assert_valid_subustr(s2, pos, len))
115 2 : return (ustr_cmp_case_buf(s1, "", 0));
116 :
117 4 : return (ustr_cmp_case_buf(s1, ustr_cstr(s2) + --pos, len));
118 : }
119 :
120 : USTR_CONF_I_PROTO int ustr_cmp_case_prefix_buf_eq(const struct Ustr *s1,
121 : const void *buf, size_t len2)
122 98 : {
123 98 : size_t len1 = 0;
124 :
125 48 : USTR_ASSERT(ustr_assert_valid(s1) && buf);
126 :
127 74 : len1 = ustr_len(s1);
128 98 : if (len1 < len2)
129 24 : return (USTR_FALSE);
130 :
131 92 : return (!ustr__memcasecmp(ustr_cstr(s1), buf, len2));
132 : }
133 :
134 : USTR_CONF_I_PROTO int ustr_cmp_case_suffix_buf_eq(const struct Ustr *s1,
135 : const void *buf, size_t len2)
136 86 : {
137 86 : size_t len1 = 0;
138 :
139 42 : USTR_ASSERT(ustr_assert_valid(s1) && buf);
140 :
141 65 : len1 = ustr_len(s1);
142 86 : if (len1 < len2)
143 8 : return (USTR_FALSE);
144 :
145 97 : return (!ustr__memcasecmp(ustr_cstr(s1) + (len1 - len2), buf, len2));
146 : }
147 :
148 : USTR_CONF_I_PROTO
149 : int ustr_cmp_fast_subustr(const struct Ustr *s1,
150 : const struct Ustr *s2, size_t pos, size_t len)
151 78 : {
152 38 : USTR_ASSERT(ustr_assert_valid(s1) && ustr_assert_valid(s2));
153 :
154 78 : if (!ustr_assert_valid_subustr(s2, pos, len))
155 2 : return (ustr_cmp_fast_buf(s1, "", 0));
156 :
157 76 : return (ustr_cmp_fast_buf(s1, ustr_cstr(s2) + --pos, len));
158 : }
159 :
160 : USTR_CONF_I_PROTO
161 : int ustr_cmp_prefix_subustr_eq(const struct Ustr *s1,
162 : const struct Ustr *s2, size_t pos, size_t len)
163 34 : {
164 16 : USTR_ASSERT(ustr_assert_valid(s1) && ustr_assert_valid(s2));
165 :
166 34 : if (!ustr_assert_valid_subustr(s2, pos, len))
167 2 : return (ustr_cmp_prefix_buf_eq(s1, "", 0));
168 :
169 32 : return (ustr_cmp_prefix_buf_eq(s1, ustr_cstr(s2) + --pos, len));
170 : }
171 :
172 : USTR_CONF_I_PROTO
173 : int ustr_cmp_suffix_subustr_eq(const struct Ustr *s1,
174 : const struct Ustr *s2, size_t pos, size_t len)
175 34 : {
176 16 : USTR_ASSERT(ustr_assert_valid(s1) && ustr_assert_valid(s2));
177 :
178 34 : if (!ustr_assert_valid_subustr(s2, pos, len))
179 2 : return (ustr_cmp_suffix_buf_eq(s1, "", 0));
180 :
181 32 : return (ustr_cmp_suffix_buf_eq(s1, ustr_cstr(s2) + --pos, len));
182 : }
183 :
184 : USTR_CONF_I_PROTO
185 : int ustr_cmp_case_prefix_subustr_eq(const struct Ustr *s1,
186 : const struct Ustr *s2,size_t pos,size_t len)
187 10 : {
188 4 : USTR_ASSERT(ustr_assert_valid(s1) && ustr_assert_valid(s2));
189 :
190 10 : if (!ustr_assert_valid_subustr(s2, pos, len))
191 2 : return (ustr_cmp_case_prefix_buf_eq(s1, "", 0));
192 :
193 8 : return (ustr_cmp_case_prefix_buf_eq(s1, ustr_cstr(s2) + --pos, len));
194 : }
195 :
196 : USTR_CONF_I_PROTO
197 : int ustr_cmp_case_suffix_subustr_eq(const struct Ustr *s1,
198 : const struct Ustr *s2,size_t pos,size_t len)
199 14 : {
200 6 : USTR_ASSERT(ustr_assert_valid(s1) && ustr_assert_valid(s2));
201 :
202 14 : if (!ustr_assert_valid_subustr(s2, pos, len))
203 2 : return (ustr_cmp_case_suffix_buf_eq(s1, "", 0));
204 :
205 12 : return (ustr_cmp_case_suffix_buf_eq(s1, ustr_cstr(s2) + --pos, len));
206 : }
207 :
|