nxt_string.c (0:a63ceefd6ab0) nxt_string.c (10:a8e68ed06863)
1
2/*
3 * Copyright (C) Igor Sysoev
4 * Copyright (C) NGINX, Inc.
5 */
6
7#include <nxt_main.h>
8
9
10nxt_str_t *
1
2/*
3 * Copyright (C) Igor Sysoev
4 * Copyright (C) NGINX, Inc.
5 */
6
7#include <nxt_main.h>
8
9
10nxt_str_t *
11nxt_str_alloc(nxt_mem_pool_t *mp, size_t len)
11nxt_str_alloc(nxt_mem_pool_t *mp, size_t length)
12{
13 nxt_str_t *s;
14
12{
13 nxt_str_t *s;
14
15 /* The string data is allocated aligned to be close to nxt_str_t. */
16 s = nxt_mem_alloc(mp, sizeof(nxt_str_t) + len);
15 /* The string start is allocated aligned to be close to nxt_str_t. */
16 s = nxt_mem_alloc(mp, sizeof(nxt_str_t) + length);
17
18 if (nxt_fast_path(s != NULL)) {
17
18 if (nxt_fast_path(s != NULL)) {
19 s->len = len;
20 s->data = (u_char *) s + sizeof(nxt_str_t);
19 s->length = length;
20 s->start = (u_char *) s + sizeof(nxt_str_t);
21 }
22
23 return s;
24}
25
26
27/*
28 * nxt_str_dup() creates a new string with a copy of a source string.
29 * If length of the source string is zero, then the new string anyway
30 * gets a pointer somewhere in mem_pool.
31 */
32
33nxt_str_t *
34nxt_str_dup(nxt_mem_pool_t *mp, nxt_str_t *dst, const nxt_str_t *src)
35{
36 u_char *p;
37
38 if (dst == NULL) {
21 }
22
23 return s;
24}
25
26
27/*
28 * nxt_str_dup() creates a new string with a copy of a source string.
29 * If length of the source string is zero, then the new string anyway
30 * gets a pointer somewhere in mem_pool.
31 */
32
33nxt_str_t *
34nxt_str_dup(nxt_mem_pool_t *mp, nxt_str_t *dst, const nxt_str_t *src)
35{
36 u_char *p;
37
38 if (dst == NULL) {
39 /* The string data is allocated aligned to be close to nxt_str_t. */
40 dst = nxt_mem_alloc(mp, sizeof(nxt_str_t) + src->len);
39 /* The string start is allocated aligned to be close to nxt_str_t. */
40 dst = nxt_mem_alloc(mp, sizeof(nxt_str_t) + src->length);
41 if (nxt_slow_path(dst == NULL)) {
42 return NULL;
43 }
44
45 p = (u_char *) dst;
46 p += sizeof(nxt_str_t);
41 if (nxt_slow_path(dst == NULL)) {
42 return NULL;
43 }
44
45 p = (u_char *) dst;
46 p += sizeof(nxt_str_t);
47 dst->data = p;
47 dst->start = p;
48
49 } else {
48
49 } else {
50 dst->data = nxt_mem_nalloc(mp, src->len);
51 if (nxt_slow_path(dst->data == NULL)) {
50 dst->start = nxt_mem_nalloc(mp, src->length);
51 if (nxt_slow_path(dst->start == NULL)) {
52 return NULL;
53 }
54 }
55
52 return NULL;
53 }
54 }
55
56 nxt_memcpy(dst->data, src->data, src->len);
57 dst->len = src->len;
56 nxt_memcpy(dst->start, src->start, src->length);
57 dst->length = src->length;
58
59 return dst;
60}
61
62
63/*
64 * nxt_str_copy() creates a C style zero-terminated copy of a source
65 * nxt_str_t. The function is intended to create strings suitable
66 * for libc and kernel interfaces so result is pointer to char instead
67 * of u_char to minimize casts. The copy is aligned to 2 bytes thus
68 * the lowest bit may be used as marker.
69 */
70
71char *
72nxt_str_copy(nxt_mem_pool_t *mp, const nxt_str_t *src)
73{
74 char *p, *dst;
75
58
59 return dst;
60}
61
62
63/*
64 * nxt_str_copy() creates a C style zero-terminated copy of a source
65 * nxt_str_t. The function is intended to create strings suitable
66 * for libc and kernel interfaces so result is pointer to char instead
67 * of u_char to minimize casts. The copy is aligned to 2 bytes thus
68 * the lowest bit may be used as marker.
69 */
70
71char *
72nxt_str_copy(nxt_mem_pool_t *mp, const nxt_str_t *src)
73{
74 char *p, *dst;
75
76 dst = nxt_mem_align(mp, 2, src->len + 1);
76 dst = nxt_mem_align(mp, 2, src->length + 1);
77
78 if (nxt_fast_path(dst != NULL)) {
77
78 if (nxt_fast_path(dst != NULL)) {
79 p = nxt_cpymem(dst, src->data, src->len);
79 p = nxt_cpymem(dst, src->start, src->length);
80 *p = '\0';
81 }
82
83 return dst;
84}
85
86
87void
80 *p = '\0';
81 }
82
83 return dst;
84}
85
86
87void
88nxt_memcpy_lowcase(u_char *dst, const u_char *src, size_t len)
88nxt_memcpy_lowcase(u_char *dst, const u_char *src, size_t length)
89{
90 u_char c;
91
89{
90 u_char c;
91
92 while (len != 0) {
92 while (length != 0) {
93 c = *src++;
94 *dst++ = nxt_lowcase(c);
93 c = *src++;
94 *dst++ = nxt_lowcase(c);
95 len--;
95 length--;
96 }
97}
98
99
100u_char *
96 }
97}
98
99
100u_char *
101nxt_cpystrn(u_char *dst, const u_char *src, size_t len)
101nxt_cpystrn(u_char *dst, const u_char *src, size_t length)
102{
102{
103 if (len == 0) {
103 if (length == 0) {
104 return dst;
105 }
106
104 return dst;
105 }
106
107 while (--len != 0) {
107 while (--length != 0) {
108 *dst = *src;
109
110 if (*dst == '\0') {
111 return dst;
112 }
113
114 dst++;
115 src++;

--- 27 unchanged lines hidden (view full) ---

143 if (c1 == 0) {
144 return 0;
145 }
146 }
147}
148
149
150nxt_int_t
108 *dst = *src;
109
110 if (*dst == '\0') {
111 return dst;
112 }
113
114 dst++;
115 src++;

--- 27 unchanged lines hidden (view full) ---

143 if (c1 == 0) {
144 return 0;
145 }
146 }
147}
148
149
150nxt_int_t
151nxt_strncasecmp(const u_char *s1, const u_char *s2, size_t len)
151nxt_strncasecmp(const u_char *s1, const u_char *s2, size_t length)
152{
153 u_char c1, c2;
154 nxt_int_t n;
155
152{
153 u_char c1, c2;
154 nxt_int_t n;
155
156 while (len-- != 0) {
156 while (length-- != 0) {
157 c1 = *s1++;
158 c2 = *s2++;
159
160 c1 = nxt_lowcase(c1);
161 c2 = nxt_lowcase(c2);
162
163 n = c1 - c2;
164

--- 6 unchanged lines hidden (view full) ---

171 }
172 }
173
174 return 0;
175}
176
177
178nxt_int_t
157 c1 = *s1++;
158 c2 = *s2++;
159
160 c1 = nxt_lowcase(c1);
161 c2 = nxt_lowcase(c2);
162
163 n = c1 - c2;
164

--- 6 unchanged lines hidden (view full) ---

171 }
172 }
173
174 return 0;
175}
176
177
178nxt_int_t
179nxt_memcasecmp(const u_char *s1, const u_char *s2, size_t len)
179nxt_memcasecmp(const u_char *s1, const u_char *s2, size_t length)
180{
181 u_char c1, c2;
182 nxt_int_t n;
183
180{
181 u_char c1, c2;
182 nxt_int_t n;
183
184 while (len-- != 0) {
184 while (length-- != 0) {
185 c1 = *s1++;
186 c2 = *s2++;
187
188 c1 = nxt_lowcase(c1);
189 c2 = nxt_lowcase(c2);
190
191 n = c1 - c2;
192
193 if (n != 0) {
194 return n;
195 }
196 }
197
198 return 0;
199}
200
201
202/*
203 * nxt_memstrn() is intended for search of static substring "ss"
185 c1 = *s1++;
186 c2 = *s2++;
187
188 c1 = nxt_lowcase(c1);
189 c2 = nxt_lowcase(c2);
190
191 n = c1 - c2;
192
193 if (n != 0) {
194 return n;
195 }
196 }
197
198 return 0;
199}
200
201
202/*
203 * nxt_memstrn() is intended for search of static substring "ss"
204 * with known length "len" in string "s" limited by parameter "end".
204 * with known length "length" in string "s" limited by parameter "end".
205 * Zeros are ignored in both strings.
206 */
207
208u_char *
205 * Zeros are ignored in both strings.
206 */
207
208u_char *
209nxt_memstrn(const u_char *s, const u_char *end, const char *ss, size_t len)
209nxt_memstrn(const u_char *s, const u_char *end, const char *ss, size_t length)
210{
211 u_char c1, c2, *s2;
212
213 s2 = (u_char *) ss;
214 c2 = *s2++;
210{
211 u_char c1, c2, *s2;
212
213 s2 = (u_char *) ss;
214 c2 = *s2++;
215 len--;
215 length--;
216
217 while (s < end) {
218 c1 = *s++;
219
220 if (c1 == c2) {
221
216
217 while (s < end) {
218 c1 = *s++;
219
220 if (c1 == c2) {
221
222 if (s + len > end) {
222 if (s + length > end) {
223 return NULL;
224 }
225
223 return NULL;
224 }
225
226 if (nxt_memcmp(s, s2, len) == 0) {
226 if (nxt_memcmp(s, s2, length) == 0) {
227 return (u_char *) s - 1;
228 }
229 }
230 }
231
232 return NULL;
233}
234
235
236/*
237 * nxt_strcasestrn() is intended for caseless search of static substring
227 return (u_char *) s - 1;
228 }
229 }
230 }
231
232 return NULL;
233}
234
235
236/*
237 * nxt_strcasestrn() is intended for caseless search of static substring
238 * "ss" with known length "len" in string "s" limited by parameter "end".
238 * "ss" with known length "length" in string "s" limited by parameter "end".
239 * Zeros are ignored in both strings.
240 */
241
242u_char *
239 * Zeros are ignored in both strings.
240 */
241
242u_char *
243nxt_memcasestrn(const u_char *s, const u_char *end, const char *ss, size_t len)
243nxt_memcasestrn(const u_char *s, const u_char *end, const char *ss,
244 size_t length)
244{
245 u_char c1, c2, *s2;
246
247 s2 = (u_char *) ss;
248 c2 = *s2++;
249 c2 = nxt_lowcase(c2);
245{
246 u_char c1, c2, *s2;
247
248 s2 = (u_char *) ss;
249 c2 = *s2++;
250 c2 = nxt_lowcase(c2);
250 len--;
251 length--;
251
252 while (s < end) {
253 c1 = *s++;
254 c1 = nxt_lowcase(c1);
255
256 if (c1 == c2) {
257
252
253 while (s < end) {
254 c1 = *s++;
255 c1 = nxt_lowcase(c1);
256
257 if (c1 == c2) {
258
258 if (s + len > end) {
259 if (s + length > end) {
259 return NULL;
260 }
261
260 return NULL;
261 }
262
262 if (nxt_memcasecmp(s, s2, len) == 0) {
263 if (nxt_memcasecmp(s, s2, length) == 0) {
263 return (u_char *) s - 1;
264 }
265 }
266 }
267
268 return NULL;
269}
270
271
272/*
273 * nxt_rstrstrn() is intended to search for static substring "ss"
264 return (u_char *) s - 1;
265 }
266 }
267 }
268
269 return NULL;
270}
271
272
273/*
274 * nxt_rstrstrn() is intended to search for static substring "ss"
274 * with known length "len" in string "s" limited by parameter "end"
275 * with known length "length" in string "s" limited by parameter "end"
275 * in reverse order. Zeros are ignored in both strings.
276 */
277
278u_char *
276 * in reverse order. Zeros are ignored in both strings.
277 */
278
279u_char *
279nxt_rmemstrn(const u_char *s, const u_char *end, const char *ss, size_t len)
280nxt_rmemstrn(const u_char *s, const u_char *end, const char *ss, size_t length)
280{
281 u_char c1, c2;
282 const u_char *s1, *s2;
283
281{
282 u_char c1, c2;
283 const u_char *s1, *s2;
284
284 s1 = end - len;
285 s1 = end - length;
285 s2 = (u_char *) ss;
286 c2 = *s2++;
286 s2 = (u_char *) ss;
287 c2 = *s2++;
287 len--;
288 length--;
288
289 while (s < s1) {
290 c1 = *s1;
291
292 if (c1 == c2) {
289
290 while (s < s1) {
291 c1 = *s1;
292
293 if (c1 == c2) {
293 if (nxt_memcmp(s1 + 1, s2, len) == 0) {
294 if (nxt_memcmp(s1 + 1, s2, length) == 0) {
294 return (u_char *) s1;
295 }
296 }
297
298 s1--;
299 }
300
301 return NULL;

--- 16 unchanged lines hidden ---
295 return (u_char *) s1;
296 }
297 }
298
299 s1--;
300 }
301
302 return NULL;

--- 16 unchanged lines hidden ---