blob: c9260022b9e271c825a29c94a832d6dc90443260 [file] [log] [blame]
Willy Tarreaue11f7272017-05-30 17:49:36 +02001/*
2 * include/common/ist.h
3 * Very simple indirect string manipulation functions.
4 *
5 * Copyright (C) 2014-2017 Willy Tarreau - w@1wt.eu
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining
8 * a copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sublicense, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be
16 * included in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
20 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25 * OTHER DEALINGS IN THE SOFTWARE.
26 */
27
28#ifndef _COMMON_IST_H
29#define _COMMON_IST_H
30
31#include <string.h>
32
33#include <common/config.h>
34
35/* This string definition will most often be used to represent a read-only
36 * string returned from a function, based on the starting point and its length
37 * in bytes. No storage is provided, only a pointer and a length. The types
38 * here are important as we only want to have 2 native machine words there so
39 * that on modern architectures the compiler is capable of efficiently
40 * returning a register pair without having to allocate stack room from the
41 * caller. This is done with -freg-struct which is often enabled by default.
42 */
43struct ist {
44 char *ptr;
45 size_t len;
46};
47
48/* makes an ist from a regular zero terminated string. Null has length 0.
49 * Constants are detected and replaced with constant initializers. Other values
50 * are measured by hand without strlen() as it's much cheaper and inlinable on
51 * small strings. The construct is complex because we must never call
52 * __builtin_strlen() with an expression otherwise it involves a real
53 * measurement.
54 */
55#if __GNUC__ >= 4
56// gcc >= 4 detects constant propagation of str through __x and resolves the
57// length of constant strings easily.
58#define ist(str) ({ \
59 char *__x = (void *)(str); \
60 (struct ist){ \
61 .ptr = __x, \
62 .len = __builtin_constant_p(str) ? \
63 ((void *)str == (void *)0) ? 0 : \
64 __builtin_strlen(__x) : \
65 ({ \
66 size_t __l = 0; \
67 if (__x) for (__l--; __x[++__l]; ) ; \
68 __l; \
69 }) \
70 }; \
71})
72#else
73// gcc < 4 can't do this, and the side effect is a warning each time a NULL is
74// passed to ist() due to the check on __builtin_strlen(). It doesn't have the
75// ability to know that this code is never called.
76#define ist(str) ({ \
77 char *__x = (void *)(str); \
78 (struct ist){ \
79 .ptr = __x, \
80 .len = __builtin_constant_p(str) ? \
81 ((void *)str == (void *)0) ? 0 : \
82 __builtin_strlen(str) : \
83 ({ \
84 size_t __l = 0; \
85 if (__x) for (__l--; __x[++__l]; ) ; \
86 __l; \
87 }) \
88 }; \
89})
90#endif
91
92/* makes an ist struct from a string and a length */
93static inline struct ist ist2(const void *ptr, size_t len)
94{
95 return (struct ist){ .ptr = (char *)ptr, .len = len };
96}
97
98/* returns the length of the string */
99static inline size_t istlen(const struct ist ist)
100{
101 return ist.len;
102}
103
104/* skips to next character in the string, always stops at the end */
105static inline struct ist istnext(const struct ist ist)
106{
107 struct ist ret = ist;
108
109 if (ret.len) {
110 ret.len--;
111 ret.ptr++;
112 }
113 return ret;
114}
115
116/* copies the contents from string <ist> to buffer <buf> and adds a trailing
117 * zero. The caller must ensure <buf> is large enough.
118 */
119static inline struct ist istpad(void *buf, const struct ist ist)
120{
121 struct ist ret = { .ptr = buf, .len = ist.len };
122
123 for (ret.len = 0; ret.len < ist.len; ret.len++)
124 ret.ptr[ret.len] = ist.ptr[ret.len];
125
126 ret.ptr[ret.len] = 0;
127 return ret;
128}
129
130/* trims string <ist> to no more than <size> characters. The string is
131 * returned.
132 */
133static inline struct ist isttrim(const struct ist ist, size_t size)
134{
135 struct ist ret = ist;
136
137 if (ret.len > size)
138 ret.len = size;
139 return ret;
140}
141
142/* trims string <ist> to no more than <size>-1 characters and ensures that a
143 * zero is placed after <ist.len> (possibly reduced by one) and before <size>,
144 * unless <size> is already zero. The string is returned. This is mostly aimed
145 * at building printable strings that need to be zero-terminated.
146 */
147static inline struct ist istzero(const struct ist ist, size_t size)
148{
149 struct ist ret = ist;
150
151 if (!size)
152 ret.len = 0;
153 else {
154 if (ret.len > size - 1)
155 ret.len = size - 1;
156 ret.ptr[ret.len] = 0;
157 }
158 return ret;
159}
160
161/* returns the ordinal difference between two strings :
162 * < 0 if ist1 < ist2
163 * = 0 if ist1 == ist2
164 * > 0 if ist1 > ist2
165 */
166static inline int istdiff(const struct ist ist1, const struct ist ist2)
167{
168 struct ist l = ist1;
169 struct ist r = ist2;
170
171 do {
172 if (!l.len--)
173 return -r.len;
174 if (!r.len--)
175 return 1;
176 } while (*l.ptr++ == *r.ptr++);
177
178 return *(unsigned char *)(l.ptr - 1) - *(unsigned char *)(r.ptr - 1);
179}
180
181/* returns non-zero if <ist1> starts like <ist2> (empty strings do match) */
182static inline int istmatch(const struct ist ist1, const struct ist ist2)
183{
184 struct ist l = ist1;
185 struct ist r = ist2;
186
187 if (l.len < r.len)
188 return 0;
189
190 while (r.len--) {
191 if (*l.ptr++ != *r.ptr++)
192 return 0;
193 }
194 return 1;
195}
196
197/* returns non-zero if <ist1> starts like <ist2> on the first <count>
198 * characters (empty strings do match).
199 */
200static inline int istnmatch(const struct ist ist1, const struct ist ist2, size_t count)
201{
202 struct ist l = ist1;
203 struct ist r = ist2;
204
205 if (l.len > count)
206 l.len = count;
207 if (r.len > count)
208 r.len = count;
209 return istmatch(l, r);
210}
211
212/* returns non-zero if <ist1> equals <ist2> (empty strings are equal) */
213static inline int isteq(const struct ist ist1, const struct ist ist2)
214{
215 struct ist l = ist1;
216 struct ist r = ist2;
217
218 if (l.len != r.len)
219 return 0;
220
221 while (l.len--) {
222 if (*l.ptr++ != *r.ptr++)
223 return 0;
224 }
225 return 1;
226}
227
228/* returns non-zero if <ist1> equals <ist2> on the first <count> characters
229 * (empty strings are equal).
230 */
231static inline int istneq(const struct ist ist1, const struct ist ist2, size_t count)
232{
233 struct ist l = ist1;
234 struct ist r = ist2;
235
236 if (l.len > count)
237 l.len = count;
238 if (r.len > count)
239 r.len = count;
240 return isteq(l, r);
241}
242
243/* copies <src> over <dst> for a maximum of <count> bytes. Returns the number
244 * of characters copied (src.len), or -1 if it does not fit. In all cases, the
245 * contents are copied prior to reporting an error, so that the destination
246 * at least contains a valid but truncated string.
247 */
248static inline ssize_t istcpy(struct ist *dst, const struct ist src, size_t count)
249{
250 dst->len = 0;
251
252 if (count > src.len)
253 count = src.len;
254
255 while (dst->len < count) {
256 dst->ptr[dst->len] = src.ptr[dst->len];
257 dst->len++;
258 }
259
260 if (dst->len == src.len)
261 return src.len;
262
263 return -1;
264}
265
266/* copies <src> over <dst> for a maximum of <count> bytes. Returns the number
267 * of characters copied, or -1 if it does not fit. A (possibly truncated) valid
268 * copy of <src> is always left into <dst>, and a trailing \0 is appended as
269 * long as <count> is not null, even if that results in reducing the string by
270 * one character.
271 */
272static inline ssize_t istscpy(struct ist *dst, const struct ist src, size_t count)
273{
274 dst->len = 0;
275
276 if (!count)
277 goto fail;
278
279 if (count > src.len)
280 count = src.len + 1;
281
282 while (dst->len < count - 1) {
283 dst->ptr[dst->len] = src.ptr[dst->len];
284 dst->len++;
285 }
286
287 dst->ptr[dst->len] = 0;
288 if (dst->len == src.len)
289 return src.len;
290 fail:
291 return -1;
292}
293
294/* appends <src> after <dst> for a maximum of <count> total bytes in <dst> after
295 * the copy. <dst> is assumed to be <count> or less before the call. The new
296 * string's length is returned, or -1 if a truncation happened. In all cases,
297 * the contents are copied prior to reporting an error, so that the destination
298 * at least contains a valid but truncated string.
299 */
300static inline ssize_t istcat(struct ist *dst, const struct ist src, size_t count)
301{
302 const char *s = src.ptr;
303
304 while (dst->len < count && s != src.ptr + src.len)
305 dst->ptr[dst->len++] = *s++;
306
307 if (s == src.ptr + src.len)
308 return dst->len;
309
310 return -1;
311}
312
313/* appends <src> after <dst> for a maximum of <count> total bytes in <dst> after
314 * the copy. <dst> is assumed to be <count> or less before the call. The new
315 * string's length is returned, or -1 if a truncation happened. In all cases,
316 * the contents are copied prior to reporting an error, so that the destination
317 * at least contains a valid but truncated string.
318 */
319static inline ssize_t istscat(struct ist *dst, const struct ist src, size_t count)
320{
321 const char *s = src.ptr;
322
323 if (!count)
324 goto fail;
325
326 while (dst->len < count - 1 && s != src.ptr + src.len) {
327 dst->ptr[dst->len++] = *s++;
328 }
329
330 dst->ptr[dst->len] = 0;
331 if (s == src.ptr + src.len)
332 return dst->len;
333 fail:
334 return -1;
335}
336
337/* looks for first occurrence of character <chr> in string <ist>. Returns the
338 * pointer if found, or NULL if not found.
339 */
340static inline char *istchr(const struct ist ist, char chr)
341{
342 char *s = ist.ptr;
343
344 do {
345 if (s >= ist.ptr + ist.len)
346 return NULL;
347 } while (*s++ != chr);
348 return s - 1;
349}
350
351/* looks for first occurrence of character <chr> in string <ist> and returns
352 * the tail of the string starting with this character, or (ist.end,0) if not
353 * found.
354 */
355static inline struct ist istfind(const struct ist ist, char chr)
356{
357 struct ist ret = ist;
358
359 while (ret.len--) {
360 if (*ret.ptr++ == chr)
361 return ist2(ret.ptr - 1, ret.len + 1);
362 }
363 return ist2(ret.ptr, 0);
364}
365
366/* looks for first occurrence of character different from <chr> in string <ist>
367 * and returns the tail of the string starting at this character, or (ist_end,0)
368 * if not found.
369 */
370static inline struct ist istskip(const struct ist ist, char chr)
371{
372 struct ist ret = ist;
373
374 while (ret.len--) {
375 if (*ret.ptr++ != chr)
376 return ist2(ret.ptr - 1, ret.len + 1);
377 }
378 return ist2(ret.ptr, 0);
379}
380
381/* looks for first occurrence of string <pat> in string <ist> and returns the
382 * tail of the string starting at this position, or (NULL,0) if not found. The
383 * empty pattern is found everywhere.
384 */
385static inline struct ist istist(const struct ist ist, const struct ist pat)
386{
387 struct ist ret = ist;
388 size_t pos;
389
390 if (!pat.len)
391 return ret;
392
393 while (1) {
394 loop:
395 ret = istfind(ret, *pat.ptr);
396 if (ret.len < pat.len)
397 break;
398
399 /* ret.len >= 1, pat.len >= 1 and *ret.ptr == *pat.ptr */
400
401 ret = istnext(ret);
402 for (pos = 0; pos < pat.len - 1; ) {
403 ++pos;
404 if (ret.ptr[pos - 1] != pat.ptr[pos])
405 goto loop;
406 }
407 return ist2(ret.ptr - 1, ret.len + 1);
408 }
409 return ist2(NULL, 0);
410}
411
412#endif