xref: /unit/src/nxt_string.h (revision 0:a63ceefd6ab0)
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 
20 #define NXT_CR             (u_char) 13
21 #define NXT_LF             (u_char) 10
22 #define NXT_CRLF           "\x0d\x0a"
23 #define NXT_CRLF_SIZE      (sizeof(NXT_CRLF) - 1)
24 
25 
26 #define NXT_LINEFEED_SIZE  1
27 
28 #define                                                                       \
29 nxt_linefeed(p)                                                               \
30     *p++ = NXT_LF
31 
32 
33 #define                                                                       \
34 nxt_strlen(s)                                                                 \
35     strlen((char *) s)
36 
37 
38 #define                                                                       \
39 nxt_memzero(buf, len)                                                         \
40     (void) memset(buf, 0, len)
41 
42 
43 #define                                                                       \
44 nxt_memset(buf, c, len)                                                       \
45     (void) memset(buf, c, len)
46 
47 
48 #define                                                                       \
49 nxt_memcpy(dst, src, len)                                                     \
50     (void) memcpy(dst, src, len)
51 
52 
53 NXT_EXPORT void nxt_memcpy_lowcase(u_char *dst, const u_char *src, size_t len);
54 
55 
56 /*
57  * nxt_cpymem() is an inline function but not macro to
58  * eliminate possible double evaluation of length "len".
59  */
60 nxt_inline void *
61 nxt_cpymem(void *dst, const void *src, size_t len)
62 {
63     return ((u_char *) memcpy(dst, src, len)) + len;
64 }
65 
66 
67 #define                                                                       \
68 nxt_memmove(dst, src, len)                                                    \
69     (void) memmove(dst, src, len)
70 
71 
72 #define                                                                       \
73 nxt_memcmp(s1, s2, len)                                                       \
74     memcmp((char *) s1, (char *) s2, len)
75 
76 
77 #define                                                                       \
78 nxt_memchr(s, c, len)                                                         \
79     memchr((char *) s, c, len)
80 
81 
82 #define                                                                       \
83 nxt_strcmp(s1, s2)                                                            \
84     strcmp((char *) s1, (char *) s2)
85 
86 
87 #define                                                                       \
88 nxt_strncmp(s1, s2, len)                                                      \
89     strncmp((char *) s1, (char *) s2, len)
90 
91 
92 NXT_EXPORT u_char *nxt_cpystrn(u_char *dst, const u_char *src, size_t len);
93 NXT_EXPORT nxt_int_t nxt_strcasecmp(const u_char *s1, const u_char *s2);
94 NXT_EXPORT nxt_int_t nxt_strncasecmp(const u_char *s1, const u_char *s2,
95     size_t len);
96 NXT_EXPORT nxt_int_t nxt_memcasecmp(const u_char *s1, const u_char *s2,
97     size_t len);
98 
99 NXT_EXPORT u_char *nxt_memstrn(const u_char *s, const u_char *end,
100     const char *ss, size_t len);
101 NXT_EXPORT u_char *nxt_memcasestrn(const u_char *s, const u_char *end,
102     const char *ss, size_t len);
103 NXT_EXPORT u_char *nxt_rmemstrn(const u_char *s, const u_char *end,
104     const char *ss, size_t len);
105 NXT_EXPORT size_t nxt_str_strip(u_char *start, u_char *end);
106 
107 
108 typedef struct {
109     size_t                    len;
110     u_char                    *data;
111 } nxt_str_t;
112 
113 
114 #define nxt_string(str)       { sizeof(str) - 1, (u_char *) str }
115 #define nxt_string_zero(str)  { sizeof(str), (u_char *) str }
116 #define nxt_null_string       { 0, NULL }
117 
118 
119 #define                                                                       \
120 nxt_str_set(str, text)                                                        \
121     do {                                                                      \
122         (str)->len = sizeof(text) - 1;                                        \
123         (str)->data = (u_char *) text;                                        \
124     } while (0)
125 
126 
127 #define                                                                       \
128 nxt_str_null(str)                                                             \
129     do {                                                                      \
130         (str)->len = 0;                                                       \
131         (str)->data = NULL;                                                   \
132     } while (0)
133 
134 
135 NXT_EXPORT nxt_str_t *nxt_str_alloc(nxt_mem_pool_t *mp, size_t len);
136 NXT_EXPORT nxt_str_t *nxt_str_dup(nxt_mem_pool_t *mp, nxt_str_t *dst,
137     const nxt_str_t *src);
138 NXT_EXPORT char *nxt_str_copy(nxt_mem_pool_t *mp, const nxt_str_t *src);
139 
140 
141 #define                                                                       \
142 nxt_strstr_eq(s1, s2)                                                         \
143     (((s1)->len == (s2)->len)                                                 \
144       && (nxt_memcmp((s1)->data, (s2)->data, (s1)->len) == 0))
145 
146 
147 #define                                                                       \
148 nxt_strcasestr_eq(s1, s2)                                                     \
149     (((s1)->len == (s2)->len)                                                 \
150       && (nxt_memcasecmp((s1)->data, (s2)->data, (s1)->len) == 0))
151 
152 
153 #define                                                                       \
154 nxt_str_eq(s, p, _len)                                                        \
155     (((s)->len == _len) && (nxt_memcmp((s)->data, p, _len) == 0))
156 
157 
158 #define                                                                       \
159 nxt_str_start(s, p, _len)                                                     \
160     (((s)->len > _len) && (nxt_memcmp((s)->data, p, _len) == 0))
161 
162 
163 #define                                                                       \
164 nxt_strchr_eq(s, c)                                                           \
165     (((s)->len == 1) && ((s)->data[0] == c))
166 
167 
168 #define                                                                       \
169 nxt_strchr_start(s, c)                                                        \
170     (((s)->len != 0) && ((s)->data[0] == c))
171 
172 
173 #endif /* _NXT_STRING_H_INCLUDED_ */
174