xref: /unit/src/nxt_string.h (revision 1975:6a47cab8f271)
1 
2 /*
3  * Copyright (C) Igor Sysoev
4  * Copyright (C) NGINX, Inc.
5  */
6 
7 #ifndef _NXT_STRING_H_INCLUDED_
8 #define _NXT_STRING_H_INCLUDED_
9 
10 
11 #define                                                                       \
12 nxt_lowcase(c)                                                                \
13     (u_char) ((c >= 'A' && c <= 'Z') ? c | 0x20 : c)
14 
15 #define                                                                       \
16 nxt_upcase(c)                                                                 \
17     (u_char) ((c >= 'a' && c <= 'z') ? c & ~0x20 : c)
18 
19 #define                                                                       \
20 nxt_isdigit(c)                                                                \
21     ((u_char) ((c) - '0') <= 9)
22 
23 #define                                                                       \
24 nxt_strtod(s, endptr)                                                         \
25     strtod((char *) s, (char **) endptr)
26 
27 
28 #define                                                                       \
29 nxt_strlen(s)                                                                 \
30     strlen((char *) s)
31 
32 
33 #define                                                                       \
34 nxt_strdup(s)                                                                 \
35     strdup((char *) s)
36 
37 
38 #define                                                                       \
39 nxt_strchr(buf, delim)                                                        \
40     (u_char *) strchr((char *) buf, delim)
41 
42 
43 #define                                                                       \
44 nxt_memzero(buf, length)                                                      \
45     (void) memset(buf, 0, length)
46 
47 
48 #define                                                                       \
49 nxt_memset(buf, c, length)                                                    \
50     (void) memset(buf, c, length)
51 
52 
53 #define                                                                       \
54 nxt_memcpy(dst, src, length)                                                  \
55     (void) memcpy(dst, src, length)
56 
57 
58 NXT_EXPORT void nxt_memcpy_lowcase(u_char *dst, const u_char *src,
59     size_t length);
60 NXT_EXPORT void nxt_memcpy_upcase(u_char *dst, const u_char *src,
61     size_t length);
62 
63 
64 /*
65  * nxt_cpymem() is an inline function but not a macro to
66  * eliminate possible double evaluation of length "length".
67  */
68 nxt_inline void *
nxt_cpymem(void * dst,const void * src,size_t length)69 nxt_cpymem(void *dst, const void *src, size_t length)
70 {
71     return ((u_char *) memcpy(dst, src, length)) + length;
72 }
73 
74 
75 #define                                                                       \
76 nxt_memmove(dst, src, length)                                                 \
77     (void) memmove(dst, src, length)
78 
79 
80 #define                                                                       \
81 nxt_memcmp(s1, s2, length)                                                    \
82     memcmp((char *) s1, (char *) s2, length)
83 
84 
85 #define                                                                       \
86 nxt_memchr(s, c, length)                                                      \
87     memchr((char *) s, c, length)
88 
89 
90 #define                                                                       \
91 nxt_strcmp(s1, s2)                                                            \
92     strcmp((char *) s1, (char *) s2)
93 
94 
95 #define                                                                       \
96 nxt_strncmp(s1, s2, length)                                                   \
97     strncmp((char *) s1, (char *) s2, length)
98 
99 
100 NXT_EXPORT u_char *nxt_cpystr(u_char *dst, const u_char *src);
101 NXT_EXPORT u_char *nxt_cpystrn(u_char *dst, const u_char *src, size_t length);
102 NXT_EXPORT nxt_int_t nxt_strcasecmp(const u_char *s1, const u_char *s2);
103 NXT_EXPORT nxt_int_t nxt_strncasecmp(const u_char *s1, const u_char *s2,
104     size_t length);
105 NXT_EXPORT nxt_int_t nxt_memcasecmp(const void *p1, const void *p2,
106     size_t length);
107 
108 NXT_EXPORT u_char *nxt_memstrn(const u_char *s, const u_char *end,
109     const char *ss, size_t length);
110 NXT_EXPORT u_char *nxt_memcasestrn(const u_char *s, const u_char *end,
111     const char *ss, size_t length);
112 NXT_EXPORT u_char *nxt_rmemstrn(const u_char *s, const u_char *end,
113     const char *ss, size_t length);
114 NXT_EXPORT size_t nxt_str_strip(u_char *start, u_char *end);
115 
116 
117 typedef struct {
118     size_t                    length;
119     u_char                    *start;
120 } nxt_str_t;
121 
122 
123 #define nxt_string(str)       { nxt_length(str), (u_char *) str }
124 #define nxt_string_zero(str)  { sizeof(str), (u_char *) str }
125 #define nxt_null_string       { 0, NULL }
126 
127 
128 #define                                                                       \
129 nxt_str_set(str, text)                                                        \
130     do {                                                                      \
131         (str)->length = nxt_length(text);                                     \
132         (str)->start = (u_char *) text;                                       \
133     } while (0)
134 
135 
136 #define                                                                       \
137 nxt_str_null(str)                                                             \
138     do {                                                                      \
139         (str)->length = 0;                                                    \
140         (str)->start = NULL;                                                  \
141     } while (0)
142 
143 
144 NXT_EXPORT nxt_str_t *nxt_str_alloc(nxt_mp_t *mp, size_t length);
145 NXT_EXPORT nxt_str_t *nxt_str_dup(nxt_mp_t *mp, nxt_str_t *dst,
146     const nxt_str_t *src);
147 NXT_EXPORT char *nxt_str_cstrz(nxt_mp_t *mp, const nxt_str_t *src);
148 
149 
150 #define                                                                       \
151 nxt_strstr_eq(s1, s2)                                                         \
152     (((s1)->length == (s2)->length)                                           \
153       && (nxt_memcmp((s1)->start, (s2)->start, (s1)->length) == 0))
154 
155 
156 #define                                                                       \
157 nxt_strcasestr_eq(s1, s2)                                                     \
158     (((s1)->length == (s2)->length)                                           \
159       && (nxt_memcasecmp((s1)->start, (s2)->start, (s1)->length) == 0))
160 
161 
162 #define                                                                       \
163 nxt_str_eq(s, p, _length)                                                     \
164     (((s)->length == _length) && (nxt_memcmp((s)->start, p, _length) == 0))
165 
166 
167 #define                                                                       \
168 nxt_str_start(s, p, _length)                                                  \
169     (((s)->length >= _length) && (nxt_memcmp((s)->start, p, _length) == 0))
170 
171 
172 #define                                                                       \
173 nxt_strchr_eq(s, c)                                                           \
174     (((s)->length == 1) && ((s)->start[0] == c))
175 
176 
177 #define                                                                       \
178 nxt_strchr_start(s, c)                                                        \
179     (((s)->length != 0) && ((s)->start[0] == c))
180 
181 
182 NXT_EXPORT nxt_int_t nxt_strverscmp(const u_char *s1, const u_char *s2);
183 NXT_EXPORT nxt_bool_t nxt_strvers_match(u_char *version, u_char *prefix,
184     size_t length);
185 
186 NXT_EXPORT u_char *nxt_decode_uri(u_char *dst, u_char *src, size_t length);
187 NXT_EXPORT u_char *nxt_decode_uri_plus(u_char *dst, u_char *src, size_t length);
188 NXT_EXPORT uintptr_t nxt_encode_uri(u_char *dst, u_char *src, size_t length);
189 NXT_EXPORT uintptr_t nxt_encode_complex_uri(u_char *dst, u_char *src,
190     size_t length);
191 NXT_EXPORT nxt_bool_t nxt_is_complex_uri_encoded(u_char *s, size_t length);
192 
193 NXT_EXPORT ssize_t nxt_base64_decode(u_char *dst, u_char *src, size_t length);
194 
195 extern const uint8_t  nxt_hex2int[256];
196 
197 
198 #endif /* _NXT_STRING_H_INCLUDED_ */
199