blob: c57aa8977a45726c1aa88639d28b89dc56cd43d9 [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
Willy Tarreau2ba67272017-09-21 15:24:10 +020048/* makes a constant ist from a constant string, for use in array declarations */
49#define IST(str) { .ptr = str "", .len = (sizeof str "") - 1 }
50
Willy Tarreaue11f7272017-05-30 17:49:36 +020051/* makes an ist from a regular zero terminated string. Null has length 0.
52 * Constants are detected and replaced with constant initializers. Other values
53 * are measured by hand without strlen() as it's much cheaper and inlinable on
54 * small strings. The construct is complex because we must never call
55 * __builtin_strlen() with an expression otherwise it involves a real
56 * measurement.
57 */
58#if __GNUC__ >= 4
59// gcc >= 4 detects constant propagation of str through __x and resolves the
60// length of constant strings easily.
61#define ist(str) ({ \
62 char *__x = (void *)(str); \
63 (struct ist){ \
64 .ptr = __x, \
65 .len = __builtin_constant_p(str) ? \
66 ((void *)str == (void *)0) ? 0 : \
67 __builtin_strlen(__x) : \
68 ({ \
69 size_t __l = 0; \
70 if (__x) for (__l--; __x[++__l]; ) ; \
71 __l; \
72 }) \
73 }; \
74})
75#else
76// gcc < 4 can't do this, and the side effect is a warning each time a NULL is
77// passed to ist() due to the check on __builtin_strlen(). It doesn't have the
78// ability to know that this code is never called.
79#define ist(str) ({ \
80 char *__x = (void *)(str); \
81 (struct ist){ \
82 .ptr = __x, \
83 .len = __builtin_constant_p(str) ? \
84 ((void *)str == (void *)0) ? 0 : \
85 __builtin_strlen(str) : \
86 ({ \
87 size_t __l = 0; \
88 if (__x) for (__l--; __x[++__l]; ) ; \
89 __l; \
90 }) \
91 }; \
92})
93#endif
94
95/* makes an ist struct from a string and a length */
96static inline struct ist ist2(const void *ptr, size_t len)
97{
98 return (struct ist){ .ptr = (char *)ptr, .len = len };
99}
100
101/* returns the length of the string */
102static inline size_t istlen(const struct ist ist)
103{
104 return ist.len;
105}
106
107/* skips to next character in the string, always stops at the end */
108static inline struct ist istnext(const struct ist ist)
109{
110 struct ist ret = ist;
111
112 if (ret.len) {
113 ret.len--;
114 ret.ptr++;
115 }
116 return ret;
117}
118
119/* copies the contents from string <ist> to buffer <buf> and adds a trailing
120 * zero. The caller must ensure <buf> is large enough.
121 */
122static inline struct ist istpad(void *buf, const struct ist ist)
123{
124 struct ist ret = { .ptr = buf, .len = ist.len };
125
126 for (ret.len = 0; ret.len < ist.len; ret.len++)
127 ret.ptr[ret.len] = ist.ptr[ret.len];
128
129 ret.ptr[ret.len] = 0;
130 return ret;
131}
132
133/* trims string <ist> to no more than <size> characters. The string is
134 * returned.
135 */
136static inline struct ist isttrim(const struct ist ist, size_t size)
137{
138 struct ist ret = ist;
139
140 if (ret.len > size)
141 ret.len = size;
142 return ret;
143}
144
145/* trims string <ist> to no more than <size>-1 characters and ensures that a
146 * zero is placed after <ist.len> (possibly reduced by one) and before <size>,
147 * unless <size> is already zero. The string is returned. This is mostly aimed
148 * at building printable strings that need to be zero-terminated.
149 */
150static inline struct ist istzero(const struct ist ist, size_t size)
151{
152 struct ist ret = ist;
153
154 if (!size)
155 ret.len = 0;
156 else {
157 if (ret.len > size - 1)
158 ret.len = size - 1;
159 ret.ptr[ret.len] = 0;
160 }
161 return ret;
162}
163
164/* returns the ordinal difference between two strings :
165 * < 0 if ist1 < ist2
166 * = 0 if ist1 == ist2
167 * > 0 if ist1 > ist2
168 */
169static inline int istdiff(const struct ist ist1, const struct ist ist2)
170{
171 struct ist l = ist1;
172 struct ist r = ist2;
173
174 do {
175 if (!l.len--)
176 return -r.len;
177 if (!r.len--)
178 return 1;
179 } while (*l.ptr++ == *r.ptr++);
180
181 return *(unsigned char *)(l.ptr - 1) - *(unsigned char *)(r.ptr - 1);
182}
183
184/* returns non-zero if <ist1> starts like <ist2> (empty strings do match) */
185static inline int istmatch(const struct ist ist1, const struct ist ist2)
186{
187 struct ist l = ist1;
188 struct ist r = ist2;
189
190 if (l.len < r.len)
191 return 0;
192
193 while (r.len--) {
194 if (*l.ptr++ != *r.ptr++)
195 return 0;
196 }
197 return 1;
198}
199
200/* returns non-zero if <ist1> starts like <ist2> on the first <count>
201 * characters (empty strings do match).
202 */
203static inline int istnmatch(const struct ist ist1, const struct ist ist2, size_t count)
204{
205 struct ist l = ist1;
206 struct ist r = ist2;
207
208 if (l.len > count)
209 l.len = count;
210 if (r.len > count)
211 r.len = count;
212 return istmatch(l, r);
213}
214
215/* returns non-zero if <ist1> equals <ist2> (empty strings are equal) */
216static inline int isteq(const struct ist ist1, const struct ist ist2)
217{
218 struct ist l = ist1;
219 struct ist r = ist2;
220
221 if (l.len != r.len)
222 return 0;
223
224 while (l.len--) {
225 if (*l.ptr++ != *r.ptr++)
226 return 0;
227 }
228 return 1;
229}
230
231/* returns non-zero if <ist1> equals <ist2> on the first <count> characters
232 * (empty strings are equal).
233 */
234static inline int istneq(const struct ist ist1, const struct ist ist2, size_t count)
235{
236 struct ist l = ist1;
237 struct ist r = ist2;
238
239 if (l.len > count)
240 l.len = count;
241 if (r.len > count)
242 r.len = count;
243 return isteq(l, r);
244}
245
246/* copies <src> over <dst> for a maximum of <count> bytes. Returns the number
247 * of characters copied (src.len), or -1 if it does not fit. In all cases, the
248 * contents are copied prior to reporting an error, so that the destination
249 * at least contains a valid but truncated string.
250 */
251static inline ssize_t istcpy(struct ist *dst, const struct ist src, size_t count)
252{
253 dst->len = 0;
254
255 if (count > src.len)
256 count = src.len;
257
258 while (dst->len < count) {
259 dst->ptr[dst->len] = src.ptr[dst->len];
260 dst->len++;
261 }
262
263 if (dst->len == src.len)
264 return src.len;
265
266 return -1;
267}
268
269/* copies <src> over <dst> for a maximum of <count> bytes. Returns the number
270 * of characters copied, or -1 if it does not fit. A (possibly truncated) valid
271 * copy of <src> is always left into <dst>, and a trailing \0 is appended as
272 * long as <count> is not null, even if that results in reducing the string by
273 * one character.
274 */
275static inline ssize_t istscpy(struct ist *dst, const struct ist src, size_t count)
276{
277 dst->len = 0;
278
279 if (!count)
280 goto fail;
281
282 if (count > src.len)
283 count = src.len + 1;
284
285 while (dst->len < count - 1) {
286 dst->ptr[dst->len] = src.ptr[dst->len];
287 dst->len++;
288 }
289
290 dst->ptr[dst->len] = 0;
291 if (dst->len == src.len)
292 return src.len;
293 fail:
294 return -1;
295}
296
297/* appends <src> after <dst> for a maximum of <count> total bytes in <dst> after
298 * the copy. <dst> is assumed to be <count> or less before the call. The new
299 * string's length is returned, or -1 if a truncation happened. In all cases,
300 * the contents are copied prior to reporting an error, so that the destination
301 * at least contains a valid but truncated string.
302 */
303static inline ssize_t istcat(struct ist *dst, const struct ist src, size_t count)
304{
305 const char *s = src.ptr;
306
307 while (dst->len < count && s != src.ptr + src.len)
308 dst->ptr[dst->len++] = *s++;
309
310 if (s == src.ptr + src.len)
311 return dst->len;
312
313 return -1;
314}
315
316/* appends <src> after <dst> for a maximum of <count> total bytes in <dst> after
317 * the copy. <dst> is assumed to be <count> or less before the call. The new
318 * string's length is returned, or -1 if a truncation happened. In all cases,
319 * the contents are copied prior to reporting an error, so that the destination
320 * at least contains a valid but truncated string.
321 */
322static inline ssize_t istscat(struct ist *dst, const struct ist src, size_t count)
323{
324 const char *s = src.ptr;
325
326 if (!count)
327 goto fail;
328
329 while (dst->len < count - 1 && s != src.ptr + src.len) {
330 dst->ptr[dst->len++] = *s++;
331 }
332
333 dst->ptr[dst->len] = 0;
334 if (s == src.ptr + src.len)
335 return dst->len;
336 fail:
337 return -1;
338}
339
340/* looks for first occurrence of character <chr> in string <ist>. Returns the
341 * pointer if found, or NULL if not found.
342 */
343static inline char *istchr(const struct ist ist, char chr)
344{
345 char *s = ist.ptr;
346
347 do {
348 if (s >= ist.ptr + ist.len)
349 return NULL;
350 } while (*s++ != chr);
351 return s - 1;
352}
353
354/* looks for first occurrence of character <chr> in string <ist> and returns
355 * the tail of the string starting with this character, or (ist.end,0) if not
356 * found.
357 */
358static inline struct ist istfind(const struct ist ist, char chr)
359{
360 struct ist ret = ist;
361
362 while (ret.len--) {
363 if (*ret.ptr++ == chr)
364 return ist2(ret.ptr - 1, ret.len + 1);
365 }
366 return ist2(ret.ptr, 0);
367}
368
369/* looks for first occurrence of character different from <chr> in string <ist>
370 * and returns the tail of the string starting at this character, or (ist_end,0)
371 * if not found.
372 */
373static inline struct ist istskip(const struct ist ist, char chr)
374{
375 struct ist ret = ist;
376
377 while (ret.len--) {
378 if (*ret.ptr++ != chr)
379 return ist2(ret.ptr - 1, ret.len + 1);
380 }
381 return ist2(ret.ptr, 0);
382}
383
384/* looks for first occurrence of string <pat> in string <ist> and returns the
385 * tail of the string starting at this position, or (NULL,0) if not found. The
386 * empty pattern is found everywhere.
387 */
388static inline struct ist istist(const struct ist ist, const struct ist pat)
389{
390 struct ist ret = ist;
391 size_t pos;
392
393 if (!pat.len)
394 return ret;
395
396 while (1) {
397 loop:
398 ret = istfind(ret, *pat.ptr);
399 if (ret.len < pat.len)
400 break;
401
402 /* ret.len >= 1, pat.len >= 1 and *ret.ptr == *pat.ptr */
403
404 ret = istnext(ret);
405 for (pos = 0; pos < pat.len - 1; ) {
406 ++pos;
407 if (ret.ptr[pos - 1] != pat.ptr[pos])
408 goto loop;
409 }
410 return ist2(ret.ptr - 1, ret.len + 1);
411 }
412 return ist2(NULL, 0);
413}
414
415#endif