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