Mercurial > dovecot > core-2.2
annotate src/lib/strfuncs.c @ 22664:fea53c2725c0
director: Fix director_max_parallel_moves/kicks type
Should be uint, not time.
author | Timo Sirainen <timo.sirainen@dovecot.fi> |
---|---|
date | Thu, 09 Nov 2017 12:24:16 +0200 |
parents | da955f909fdd |
children | cb108f786fb4 |
rev | line source |
---|---|
21390
2e2563132d5f
Updated copyright notices to include the year 2017.
Stephan Bosch <stephan.bosch@dovecot.fi>
parents:
21323
diff
changeset
|
1 /* Copyright (c) 2002-2017 Dovecot authors, see the included COPYING file */ |
0 | 2 |
805
5ac361acb316
Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents:
678
diff
changeset
|
3 /* @UNSAFE: whole file */ |
5ac361acb316
Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents:
678
diff
changeset
|
4 |
0 | 5 #include "lib.h" |
5681
09415e6a0892
Require C99 compatible vsnprintf(). Removed printf_string_upper_bound() and
Timo Sirainen <tss@iki.fi>
parents:
5679
diff
changeset
|
6 #include "printf-format-fix.h" |
0 | 7 #include "strfuncs.h" |
19791
5c4e9921b65f
lib: Added [pt]_array_const_string_join()
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
19790
diff
changeset
|
8 #include "array.h" |
0 | 9 |
10 #include <stdio.h> | |
11 #include <limits.h> | |
12 #include <ctype.h> | |
13 | |
14 #define STRCONCAT_BUFSIZE 512 | |
15 | |
14629
c93ca5e46a8a
Marked functions parameters that are allowed to be NULL. Some APIs were also changed.
Timo Sirainen <tss@iki.fi>
parents:
14470
diff
changeset
|
16 const unsigned char uchar_nul = '\0'; |
c93ca5e46a8a
Marked functions parameters that are allowed to be NULL. Some APIs were also changed.
Timo Sirainen <tss@iki.fi>
parents:
14470
diff
changeset
|
17 |
21951
3ef988b95d96
lib: Add mem_equals_timing_safe()
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21390
diff
changeset
|
18 volatile int timing_safety_unoptimization; |
3ef988b95d96
lib: Add mem_equals_timing_safe()
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21390
diff
changeset
|
19 |
807
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
20 int i_snprintf(char *dest, size_t max_chars, const char *format, ...) |
0 | 21 { |
5681
09415e6a0892
Require C99 compatible vsnprintf(). Removed printf_string_upper_bound() and
Timo Sirainen <tss@iki.fi>
parents:
5679
diff
changeset
|
22 va_list args; |
0 | 23 int ret; |
24 | |
483
990dae663bc3
Assert i_snprintf() string lengths to INT_MAX instead of SSIZE_T_MAX since
Timo Sirainen <tss@iki.fi>
parents:
482
diff
changeset
|
25 i_assert(max_chars < INT_MAX); |
0 | 26 |
27 va_start(args, format); | |
5681
09415e6a0892
Require C99 compatible vsnprintf(). Removed printf_string_upper_bound() and
Timo Sirainen <tss@iki.fi>
parents:
5679
diff
changeset
|
28 ret = vsnprintf(dest, max_chars, printf_format_fix_unsafe(format), |
09415e6a0892
Require C99 compatible vsnprintf(). Removed printf_string_upper_bound() and
Timo Sirainen <tss@iki.fi>
parents:
5679
diff
changeset
|
29 args); |
859
c4754c5770f3
We weren't using va_list properly, especially gcc/PowerPC didn't like it.
Timo Sirainen <tss@iki.fi>
parents:
847
diff
changeset
|
30 va_end(args); |
c4754c5770f3
We weren't using va_list properly, especially gcc/PowerPC didn't like it.
Timo Sirainen <tss@iki.fi>
parents:
847
diff
changeset
|
31 |
5681
09415e6a0892
Require C99 compatible vsnprintf(). Removed printf_string_upper_bound() and
Timo Sirainen <tss@iki.fi>
parents:
5679
diff
changeset
|
32 i_assert(ret >= 0); |
09415e6a0892
Require C99 compatible vsnprintf(). Removed printf_string_upper_bound() and
Timo Sirainen <tss@iki.fi>
parents:
5679
diff
changeset
|
33 return (unsigned int)ret < max_chars ? 0 : -1; |
0 | 34 } |
35 | |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
898
diff
changeset
|
36 char *p_strdup(pool_t pool, const char *str) |
0 | 37 { |
839
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
38 void *mem; |
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
39 size_t len; |
0 | 40 |
64
83ae914a583a
added t_strdup_noconst() which can be used instead of (char *) t_strdup().
Timo Sirainen <tss@iki.fi>
parents:
63
diff
changeset
|
41 if (str == NULL) |
83ae914a583a
added t_strdup_noconst() which can be used instead of (char *) t_strdup().
Timo Sirainen <tss@iki.fi>
parents:
63
diff
changeset
|
42 return NULL; |
83ae914a583a
added t_strdup_noconst() which can be used instead of (char *) t_strdup().
Timo Sirainen <tss@iki.fi>
parents:
63
diff
changeset
|
43 |
11972
7f550a7bd9d7
p_strdup(): Small optimization.
Timo Sirainen <tss@iki.fi>
parents:
11087
diff
changeset
|
44 len = strlen(str) + 1; |
839
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
45 mem = p_malloc(pool, len); |
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
46 memcpy(mem, str, len); |
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
47 return mem; |
0 | 48 } |
49 | |
18715 | 50 void *p_memdup(pool_t pool, const void *data, size_t size) |
51 { | |
52 void *mem; | |
53 | |
54 mem = p_malloc(pool, size); | |
55 memcpy(mem, data, size); | |
56 return mem; | |
57 } | |
58 | |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
898
diff
changeset
|
59 char *p_strdup_empty(pool_t pool, const char *str) |
0 | 60 { |
839
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
61 if (str == NULL || *str == '\0') |
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
62 return NULL; |
0 | 63 |
839
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
64 return p_strdup(pool, str); |
0 | 65 } |
66 | |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
898
diff
changeset
|
67 char *p_strdup_until(pool_t pool, const void *start, const void *end) |
5
1b34ec11fff8
Message data is parsed in blocks (no longer entirely mmap()ed). Several
Timo Sirainen <tss@iki.fi>
parents:
0
diff
changeset
|
68 { |
183
4a7ab9e94f25
size_t fixes for lib/. Changed OFF_T_FORMAT to PRIuOFF_T which is more
Timo Sirainen <tss@iki.fi>
parents:
179
diff
changeset
|
69 size_t size; |
5
1b34ec11fff8
Message data is parsed in blocks (no longer entirely mmap()ed). Several
Timo Sirainen <tss@iki.fi>
parents:
0
diff
changeset
|
70 char *mem; |
1b34ec11fff8
Message data is parsed in blocks (no longer entirely mmap()ed). Several
Timo Sirainen <tss@iki.fi>
parents:
0
diff
changeset
|
71 |
898
0d5be52d7131
Use unsigned char* when accessing non-NUL terminating strings. Compiler
Timo Sirainen <tss@iki.fi>
parents:
896
diff
changeset
|
72 i_assert((const char *) start <= (const char *) end); |
5
1b34ec11fff8
Message data is parsed in blocks (no longer entirely mmap()ed). Several
Timo Sirainen <tss@iki.fi>
parents:
0
diff
changeset
|
73 |
898
0d5be52d7131
Use unsigned char* when accessing non-NUL terminating strings. Compiler
Timo Sirainen <tss@iki.fi>
parents:
896
diff
changeset
|
74 size = (size_t) ((const char *) end - (const char *) start); |
8 | 75 |
76 mem = p_malloc(pool, size + 1); | |
5
1b34ec11fff8
Message data is parsed in blocks (no longer entirely mmap()ed). Several
Timo Sirainen <tss@iki.fi>
parents:
0
diff
changeset
|
77 memcpy(mem, start, size); |
1b34ec11fff8
Message data is parsed in blocks (no longer entirely mmap()ed). Several
Timo Sirainen <tss@iki.fi>
parents:
0
diff
changeset
|
78 return mem; |
1b34ec11fff8
Message data is parsed in blocks (no longer entirely mmap()ed). Several
Timo Sirainen <tss@iki.fi>
parents:
0
diff
changeset
|
79 } |
1b34ec11fff8
Message data is parsed in blocks (no longer entirely mmap()ed). Several
Timo Sirainen <tss@iki.fi>
parents:
0
diff
changeset
|
80 |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
898
diff
changeset
|
81 char *p_strndup(pool_t pool, const void *str, size_t max_chars) |
0 | 82 { |
83 char *mem; | |
183
4a7ab9e94f25
size_t fixes for lib/. Changed OFF_T_FORMAT to PRIuOFF_T which is more
Timo Sirainen <tss@iki.fi>
parents:
179
diff
changeset
|
84 size_t len; |
0 | 85 |
839
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
86 i_assert(max_chars != (size_t)-1); |
0 | 87 |
88 if (str == NULL) | |
89 return NULL; | |
90 | |
91 len = 0; | |
1838
6e834ae1da0e
p_strndup() read str[max_chars] byte - could have possibly crashed in some
Timo Sirainen <tss@iki.fi>
parents:
1823
diff
changeset
|
92 while (len < max_chars && ((const char *) str)[len] != '\0') |
0 | 93 len++; |
94 | |
1785 | 95 mem = p_malloc(pool, len+1); |
0 | 96 memcpy(mem, str, len); |
97 mem[len] = '\0'; | |
98 return mem; | |
99 } | |
100 | |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
898
diff
changeset
|
101 char *p_strdup_printf(pool_t pool, const char *format, ...) |
0 | 102 { |
103 va_list args; | |
104 char *ret; | |
105 | |
106 va_start(args, format); | |
107 ret = p_strdup_vprintf(pool, format, args); | |
108 va_end(args); | |
109 | |
110 return ret; | |
111 } | |
112 | |
5681
09415e6a0892
Require C99 compatible vsnprintf(). Removed printf_string_upper_bound() and
Timo Sirainen <tss@iki.fi>
parents:
5679
diff
changeset
|
113 char *t_noalloc_strdup_vprintf(const char *format, va_list args, |
09415e6a0892
Require C99 compatible vsnprintf(). Removed printf_string_upper_bound() and
Timo Sirainen <tss@iki.fi>
parents:
5679
diff
changeset
|
114 unsigned int *size_r) |
0 | 115 { |
5681
09415e6a0892
Require C99 compatible vsnprintf(). Removed printf_string_upper_bound() and
Timo Sirainen <tss@iki.fi>
parents:
5679
diff
changeset
|
116 #define SNPRINTF_INITIAL_EXTRA_SIZE 256 |
859
c4754c5770f3
We weren't using va_list properly, especially gcc/PowerPC didn't like it.
Timo Sirainen <tss@iki.fi>
parents:
847
diff
changeset
|
117 va_list args2; |
5681
09415e6a0892
Require C99 compatible vsnprintf(). Removed printf_string_upper_bound() and
Timo Sirainen <tss@iki.fi>
parents:
5679
diff
changeset
|
118 char *tmp; |
21322
5ab8dc1a4a6f
global: Change string position/length from unsigned int to size_t
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21321
diff
changeset
|
119 size_t init_size; |
5681
09415e6a0892
Require C99 compatible vsnprintf(). Removed printf_string_upper_bound() and
Timo Sirainen <tss@iki.fi>
parents:
5679
diff
changeset
|
120 int ret; |
17185
9b50caaa4467
DEBUG: Make sure errno isn't changed by vsnprintf()/malloc()/free() implementations.
Timo Sirainen <tss@iki.fi>
parents:
17130
diff
changeset
|
121 #ifdef DEBUG |
9b50caaa4467
DEBUG: Make sure errno isn't changed by vsnprintf()/malloc()/free() implementations.
Timo Sirainen <tss@iki.fi>
parents:
17130
diff
changeset
|
122 int old_errno = errno; |
9b50caaa4467
DEBUG: Make sure errno isn't changed by vsnprintf()/malloc()/free() implementations.
Timo Sirainen <tss@iki.fi>
parents:
17130
diff
changeset
|
123 #endif |
233
3f92df43cfa7
t_push()/t_pop() around various *sprintf() functions, so "%m" format won't
Timo Sirainen <tss@iki.fi>
parents:
183
diff
changeset
|
124 |
859
c4754c5770f3
We weren't using va_list properly, especially gcc/PowerPC didn't like it.
Timo Sirainen <tss@iki.fi>
parents:
847
diff
changeset
|
125 VA_COPY(args2, args); |
c4754c5770f3
We weren't using va_list properly, especially gcc/PowerPC didn't like it.
Timo Sirainen <tss@iki.fi>
parents:
847
diff
changeset
|
126 |
5681
09415e6a0892
Require C99 compatible vsnprintf(). Removed printf_string_upper_bound() and
Timo Sirainen <tss@iki.fi>
parents:
5679
diff
changeset
|
127 /* the format string is modified only if %m exists in it. it happens |
09415e6a0892
Require C99 compatible vsnprintf(). Removed printf_string_upper_bound() and
Timo Sirainen <tss@iki.fi>
parents:
5679
diff
changeset
|
128 only in error conditions, so don't try to t_push() here since it'll |
09415e6a0892
Require C99 compatible vsnprintf(). Removed printf_string_upper_bound() and
Timo Sirainen <tss@iki.fi>
parents:
5679
diff
changeset
|
129 just slow down the normal code path. */ |
09415e6a0892
Require C99 compatible vsnprintf(). Removed printf_string_upper_bound() and
Timo Sirainen <tss@iki.fi>
parents:
5679
diff
changeset
|
130 format = printf_format_fix_get_len(format, &init_size); |
09415e6a0892
Require C99 compatible vsnprintf(). Removed printf_string_upper_bound() and
Timo Sirainen <tss@iki.fi>
parents:
5679
diff
changeset
|
131 init_size += SNPRINTF_INITIAL_EXTRA_SIZE; |
09415e6a0892
Require C99 compatible vsnprintf(). Removed printf_string_upper_bound() and
Timo Sirainen <tss@iki.fi>
parents:
5679
diff
changeset
|
132 |
09415e6a0892
Require C99 compatible vsnprintf(). Removed printf_string_upper_bound() and
Timo Sirainen <tss@iki.fi>
parents:
5679
diff
changeset
|
133 tmp = t_buffer_get(init_size); |
09415e6a0892
Require C99 compatible vsnprintf(). Removed printf_string_upper_bound() and
Timo Sirainen <tss@iki.fi>
parents:
5679
diff
changeset
|
134 ret = vsnprintf(tmp, init_size, format, args); |
09415e6a0892
Require C99 compatible vsnprintf(). Removed printf_string_upper_bound() and
Timo Sirainen <tss@iki.fi>
parents:
5679
diff
changeset
|
135 i_assert(ret >= 0); |
837
ec6dd72cb8e3
Use vsnprintf() always when possible, even if we went through the
Timo Sirainen <tss@iki.fi>
parents:
807
diff
changeset
|
136 |
5681
09415e6a0892
Require C99 compatible vsnprintf(). Removed printf_string_upper_bound() and
Timo Sirainen <tss@iki.fi>
parents:
5679
diff
changeset
|
137 *size_r = ret + 1; |
09415e6a0892
Require C99 compatible vsnprintf(). Removed printf_string_upper_bound() and
Timo Sirainen <tss@iki.fi>
parents:
5679
diff
changeset
|
138 if ((unsigned int)ret >= init_size) { |
09415e6a0892
Require C99 compatible vsnprintf(). Removed printf_string_upper_bound() and
Timo Sirainen <tss@iki.fi>
parents:
5679
diff
changeset
|
139 /* didn't fit with the first guess. now we know the size, |
09415e6a0892
Require C99 compatible vsnprintf(). Removed printf_string_upper_bound() and
Timo Sirainen <tss@iki.fi>
parents:
5679
diff
changeset
|
140 so try again. */ |
09415e6a0892
Require C99 compatible vsnprintf(). Removed printf_string_upper_bound() and
Timo Sirainen <tss@iki.fi>
parents:
5679
diff
changeset
|
141 tmp = t_buffer_get(*size_r); |
09415e6a0892
Require C99 compatible vsnprintf(). Removed printf_string_upper_bound() and
Timo Sirainen <tss@iki.fi>
parents:
5679
diff
changeset
|
142 ret = vsnprintf(tmp, *size_r, format, args2); |
09415e6a0892
Require C99 compatible vsnprintf(). Removed printf_string_upper_bound() and
Timo Sirainen <tss@iki.fi>
parents:
5679
diff
changeset
|
143 i_assert((unsigned int)ret == *size_r-1); |
09415e6a0892
Require C99 compatible vsnprintf(). Removed printf_string_upper_bound() and
Timo Sirainen <tss@iki.fi>
parents:
5679
diff
changeset
|
144 } |
17185
9b50caaa4467
DEBUG: Make sure errno isn't changed by vsnprintf()/malloc()/free() implementations.
Timo Sirainen <tss@iki.fi>
parents:
17130
diff
changeset
|
145 #ifdef DEBUG |
9b50caaa4467
DEBUG: Make sure errno isn't changed by vsnprintf()/malloc()/free() implementations.
Timo Sirainen <tss@iki.fi>
parents:
17130
diff
changeset
|
146 /* we rely on errno not changing. it shouldn't. */ |
9b50caaa4467
DEBUG: Make sure errno isn't changed by vsnprintf()/malloc()/free() implementations.
Timo Sirainen <tss@iki.fi>
parents:
17130
diff
changeset
|
147 i_assert(errno == old_errno); |
9b50caaa4467
DEBUG: Make sure errno isn't changed by vsnprintf()/malloc()/free() implementations.
Timo Sirainen <tss@iki.fi>
parents:
17130
diff
changeset
|
148 #endif |
17396
03870eea2857
lib: pair VA_COPY with va_end
Phil Carmody <phil@dovecot.fi>
parents:
17355
diff
changeset
|
149 va_end(args2); |
5681
09415e6a0892
Require C99 compatible vsnprintf(). Removed printf_string_upper_bound() and
Timo Sirainen <tss@iki.fi>
parents:
5679
diff
changeset
|
150 return tmp; |
09415e6a0892
Require C99 compatible vsnprintf(). Removed printf_string_upper_bound() and
Timo Sirainen <tss@iki.fi>
parents:
5679
diff
changeset
|
151 } |
09415e6a0892
Require C99 compatible vsnprintf(). Removed printf_string_upper_bound() and
Timo Sirainen <tss@iki.fi>
parents:
5679
diff
changeset
|
152 |
09415e6a0892
Require C99 compatible vsnprintf(). Removed printf_string_upper_bound() and
Timo Sirainen <tss@iki.fi>
parents:
5679
diff
changeset
|
153 char *p_strdup_vprintf(pool_t pool, const char *format, va_list args) |
09415e6a0892
Require C99 compatible vsnprintf(). Removed printf_string_upper_bound() and
Timo Sirainen <tss@iki.fi>
parents:
5679
diff
changeset
|
154 { |
09415e6a0892
Require C99 compatible vsnprintf(). Removed printf_string_upper_bound() and
Timo Sirainen <tss@iki.fi>
parents:
5679
diff
changeset
|
155 char *tmp, *buf; |
09415e6a0892
Require C99 compatible vsnprintf(). Removed printf_string_upper_bound() and
Timo Sirainen <tss@iki.fi>
parents:
5679
diff
changeset
|
156 unsigned int size; |
09415e6a0892
Require C99 compatible vsnprintf(). Removed printf_string_upper_bound() and
Timo Sirainen <tss@iki.fi>
parents:
5679
diff
changeset
|
157 |
09415e6a0892
Require C99 compatible vsnprintf(). Removed printf_string_upper_bound() and
Timo Sirainen <tss@iki.fi>
parents:
5679
diff
changeset
|
158 tmp = t_noalloc_strdup_vprintf(format, args, &size); |
09415e6a0892
Require C99 compatible vsnprintf(). Removed printf_string_upper_bound() and
Timo Sirainen <tss@iki.fi>
parents:
5679
diff
changeset
|
159 if (pool->datastack_pool) { |
09415e6a0892
Require C99 compatible vsnprintf(). Removed printf_string_upper_bound() and
Timo Sirainen <tss@iki.fi>
parents:
5679
diff
changeset
|
160 t_buffer_alloc(size); |
09415e6a0892
Require C99 compatible vsnprintf(). Removed printf_string_upper_bound() and
Timo Sirainen <tss@iki.fi>
parents:
5679
diff
changeset
|
161 return tmp; |
09415e6a0892
Require C99 compatible vsnprintf(). Removed printf_string_upper_bound() and
Timo Sirainen <tss@iki.fi>
parents:
5679
diff
changeset
|
162 } else { |
09415e6a0892
Require C99 compatible vsnprintf(). Removed printf_string_upper_bound() and
Timo Sirainen <tss@iki.fi>
parents:
5679
diff
changeset
|
163 buf = p_malloc(pool, size); |
09415e6a0892
Require C99 compatible vsnprintf(). Removed printf_string_upper_bound() and
Timo Sirainen <tss@iki.fi>
parents:
5679
diff
changeset
|
164 memcpy(buf, tmp, size - 1); |
09415e6a0892
Require C99 compatible vsnprintf(). Removed printf_string_upper_bound() and
Timo Sirainen <tss@iki.fi>
parents:
5679
diff
changeset
|
165 return buf; |
09415e6a0892
Require C99 compatible vsnprintf(). Removed printf_string_upper_bound() and
Timo Sirainen <tss@iki.fi>
parents:
5679
diff
changeset
|
166 } |
0 | 167 } |
168 | |
6418
46d9ee79f292
Removed _ prefix from all public APIs.
Timo Sirainen <tss@iki.fi>
parents:
5681
diff
changeset
|
169 char *vstrconcat(const char *str1, va_list args, size_t *ret_len) |
0 | 170 { |
171 const char *str; | |
1820 | 172 char *temp; |
173 size_t bufsize, i, len; | |
0 | 174 |
175 if (str1 == NULL) | |
176 return NULL; | |
177 | |
842 | 178 str = str1; |
179 bufsize = STRCONCAT_BUFSIZE; | |
180 temp = t_buffer_get(bufsize); | |
0 | 181 |
1820 | 182 i = 0; |
842 | 183 do { |
1820 | 184 len = strlen(str); |
0 | 185 |
1820 | 186 if (i + len >= bufsize) { |
842 | 187 /* need more memory */ |
1820 | 188 bufsize = nearest_power(i + len + 1); |
0 | 189 temp = t_buffer_reget(temp, bufsize); |
190 } | |
1820 | 191 |
1821 | 192 memcpy(temp + i, str, len); i += len; |
1820 | 193 |
194 /* next string */ | |
195 str = va_arg(args, const char *); | |
842 | 196 } while (str != NULL); |
0 | 197 |
1820 | 198 i_assert(i < bufsize); |
847
7f41a4b33975
t_strsplit() returns now const char **, which removes a few nasty casts.
Timo Sirainen <tss@iki.fi>
parents:
842
diff
changeset
|
199 |
1820 | 200 temp[i++] = '\0'; |
201 *ret_len = i; | |
0 | 202 return temp; |
203 } | |
204 | |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
898
diff
changeset
|
205 char *p_strconcat(pool_t pool, const char *str1, ...) |
0 | 206 { |
207 va_list args; | |
3385
fe4a297379fc
If p_malloc() used data stack (with DEBUG enabled it might have written a
Timo Sirainen <tss@iki.fi>
parents:
3269
diff
changeset
|
208 char *temp, *ret; |
183
4a7ab9e94f25
size_t fixes for lib/. Changed OFF_T_FORMAT to PRIuOFF_T which is more
Timo Sirainen <tss@iki.fi>
parents:
179
diff
changeset
|
209 size_t len; |
0 | 210 |
211 va_start(args, str1); | |
212 | |
6940
414c9d631a81
Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents:
6429
diff
changeset
|
213 if (pool->datastack_pool) { |
414c9d631a81
Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents:
6429
diff
changeset
|
214 ret = vstrconcat(str1, args, &len); |
414c9d631a81
Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents:
6429
diff
changeset
|
215 if (ret != NULL) |
414c9d631a81
Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents:
6429
diff
changeset
|
216 t_buffer_alloc(len); |
414c9d631a81
Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents:
6429
diff
changeset
|
217 } else { |
21321
d80f9583a9ec
lib: Optimization - p_strconcat() doesn't need to allocate from data stack
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
20887
diff
changeset
|
218 temp = vstrconcat(str1, args, &len); |
d80f9583a9ec
lib: Optimization - p_strconcat() doesn't need to allocate from data stack
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
20887
diff
changeset
|
219 ret = p_malloc(pool, len); |
d80f9583a9ec
lib: Optimization - p_strconcat() doesn't need to allocate from data stack
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
20887
diff
changeset
|
220 memcpy(ret, temp, len); |
0 | 221 } |
222 | |
223 va_end(args); | |
224 return ret; | |
225 } | |
226 | |
839
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
227 const char *t_strdup(const char *str) |
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
228 { |
1782
2f3d906d99d8
data_stack_pool split into two: unsafe_data_stack_pool which works like
Timo Sirainen <tss@iki.fi>
parents:
1763
diff
changeset
|
229 return p_strdup(unsafe_data_stack_pool, str); |
839
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
230 } |
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
231 |
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
232 char *t_strdup_noconst(const char *str) |
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
233 { |
1782
2f3d906d99d8
data_stack_pool split into two: unsafe_data_stack_pool which works like
Timo Sirainen <tss@iki.fi>
parents:
1763
diff
changeset
|
234 return p_strdup(unsafe_data_stack_pool, str); |
839
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
235 } |
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
236 |
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
237 const char *t_strdup_empty(const char *str) |
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
238 { |
1782
2f3d906d99d8
data_stack_pool split into two: unsafe_data_stack_pool which works like
Timo Sirainen <tss@iki.fi>
parents:
1763
diff
changeset
|
239 return p_strdup_empty(unsafe_data_stack_pool, str); |
839
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
240 } |
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
241 |
898
0d5be52d7131
Use unsigned char* when accessing non-NUL terminating strings. Compiler
Timo Sirainen <tss@iki.fi>
parents:
896
diff
changeset
|
242 const char *t_strdup_until(const void *start, const void *end) |
839
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
243 { |
1782
2f3d906d99d8
data_stack_pool split into two: unsafe_data_stack_pool which works like
Timo Sirainen <tss@iki.fi>
parents:
1763
diff
changeset
|
244 return p_strdup_until(unsafe_data_stack_pool, start, end); |
839
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
245 } |
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
246 |
898
0d5be52d7131
Use unsigned char* when accessing non-NUL terminating strings. Compiler
Timo Sirainen <tss@iki.fi>
parents:
896
diff
changeset
|
247 const char *t_strndup(const void *str, size_t max_chars) |
839
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
248 { |
1782
2f3d906d99d8
data_stack_pool split into two: unsafe_data_stack_pool which works like
Timo Sirainen <tss@iki.fi>
parents:
1763
diff
changeset
|
249 return p_strndup(unsafe_data_stack_pool, str, max_chars); |
839
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
250 } |
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
251 |
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
252 const char *t_strdup_printf(const char *format, ...) |
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
253 { |
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
254 va_list args; |
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
255 const char *ret; |
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
256 |
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
257 va_start(args, format); |
1782
2f3d906d99d8
data_stack_pool split into two: unsafe_data_stack_pool which works like
Timo Sirainen <tss@iki.fi>
parents:
1763
diff
changeset
|
258 ret = p_strdup_vprintf(unsafe_data_stack_pool, format, args); |
839
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
259 va_end(args); |
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
260 |
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
261 return ret; |
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
262 } |
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
263 |
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
264 const char *t_strdup_vprintf(const char *format, va_list args) |
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
265 { |
1782
2f3d906d99d8
data_stack_pool split into two: unsafe_data_stack_pool which works like
Timo Sirainen <tss@iki.fi>
parents:
1763
diff
changeset
|
266 return p_strdup_vprintf(unsafe_data_stack_pool, format, args); |
839
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
267 } |
34cb1d196d2b
String function cleanups. Allocating 0 bytes of memory is treated as error
Timo Sirainen <tss@iki.fi>
parents:
837
diff
changeset
|
268 |
0 | 269 const char *t_strconcat(const char *str1, ...) |
270 { | |
271 va_list args; | |
272 const char *ret; | |
183
4a7ab9e94f25
size_t fixes for lib/. Changed OFF_T_FORMAT to PRIuOFF_T which is more
Timo Sirainen <tss@iki.fi>
parents:
179
diff
changeset
|
273 size_t len; |
0 | 274 |
275 va_start(args, str1); | |
276 | |
6418
46d9ee79f292
Removed _ prefix from all public APIs.
Timo Sirainen <tss@iki.fi>
parents:
5681
diff
changeset
|
277 ret = vstrconcat(str1, args, &len); |
0 | 278 if (ret != NULL) |
279 t_buffer_alloc(len); | |
280 | |
281 va_end(args); | |
282 return ret; | |
283 } | |
284 | |
285 const char *t_strcut(const char *str, char cutchar) | |
286 { | |
287 const char *p; | |
288 | |
289 for (p = str; *p != '\0'; p++) { | |
290 if (*p == cutchar) | |
5
1b34ec11fff8
Message data is parsed in blocks (no longer entirely mmap()ed). Several
Timo Sirainen <tss@iki.fi>
parents:
0
diff
changeset
|
291 return t_strdup_until(str, p); |
0 | 292 } |
293 | |
294 return str; | |
295 } | |
296 | |
18468
90298bc1a481
lib: Added t_str_replace() to replace one character with another one
Timo Sirainen <tss@iki.fi>
parents:
18137
diff
changeset
|
297 const char *t_str_replace(const char *str, char from, char to) |
90298bc1a481
lib: Added t_str_replace() to replace one character with another one
Timo Sirainen <tss@iki.fi>
parents:
18137
diff
changeset
|
298 { |
90298bc1a481
lib: Added t_str_replace() to replace one character with another one
Timo Sirainen <tss@iki.fi>
parents:
18137
diff
changeset
|
299 char *out; |
21322
5ab8dc1a4a6f
global: Change string position/length from unsigned int to size_t
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21321
diff
changeset
|
300 size_t i, len; |
18468
90298bc1a481
lib: Added t_str_replace() to replace one character with another one
Timo Sirainen <tss@iki.fi>
parents:
18137
diff
changeset
|
301 |
90298bc1a481
lib: Added t_str_replace() to replace one character with another one
Timo Sirainen <tss@iki.fi>
parents:
18137
diff
changeset
|
302 if (strchr(str, from) == NULL) |
90298bc1a481
lib: Added t_str_replace() to replace one character with another one
Timo Sirainen <tss@iki.fi>
parents:
18137
diff
changeset
|
303 return str; |
90298bc1a481
lib: Added t_str_replace() to replace one character with another one
Timo Sirainen <tss@iki.fi>
parents:
18137
diff
changeset
|
304 |
90298bc1a481
lib: Added t_str_replace() to replace one character with another one
Timo Sirainen <tss@iki.fi>
parents:
18137
diff
changeset
|
305 len = strlen(str); |
90298bc1a481
lib: Added t_str_replace() to replace one character with another one
Timo Sirainen <tss@iki.fi>
parents:
18137
diff
changeset
|
306 out = t_malloc(len + 1); |
90298bc1a481
lib: Added t_str_replace() to replace one character with another one
Timo Sirainen <tss@iki.fi>
parents:
18137
diff
changeset
|
307 for (i = 0; i < len; i++) { |
90298bc1a481
lib: Added t_str_replace() to replace one character with another one
Timo Sirainen <tss@iki.fi>
parents:
18137
diff
changeset
|
308 if (str[i] == from) |
90298bc1a481
lib: Added t_str_replace() to replace one character with another one
Timo Sirainen <tss@iki.fi>
parents:
18137
diff
changeset
|
309 out[i] = to; |
90298bc1a481
lib: Added t_str_replace() to replace one character with another one
Timo Sirainen <tss@iki.fi>
parents:
18137
diff
changeset
|
310 else |
90298bc1a481
lib: Added t_str_replace() to replace one character with another one
Timo Sirainen <tss@iki.fi>
parents:
18137
diff
changeset
|
311 out[i] = str[i]; |
90298bc1a481
lib: Added t_str_replace() to replace one character with another one
Timo Sirainen <tss@iki.fi>
parents:
18137
diff
changeset
|
312 } |
90298bc1a481
lib: Added t_str_replace() to replace one character with another one
Timo Sirainen <tss@iki.fi>
parents:
18137
diff
changeset
|
313 out[i] = '\0'; |
90298bc1a481
lib: Added t_str_replace() to replace one character with another one
Timo Sirainen <tss@iki.fi>
parents:
18137
diff
changeset
|
314 return out; |
90298bc1a481
lib: Added t_str_replace() to replace one character with another one
Timo Sirainen <tss@iki.fi>
parents:
18137
diff
changeset
|
315 } |
90298bc1a481
lib: Added t_str_replace() to replace one character with another one
Timo Sirainen <tss@iki.fi>
parents:
18137
diff
changeset
|
316 |
6422
18173a52f721
Renamed strocpy() to i_strocpy().
Timo Sirainen <tss@iki.fi>
parents:
6420
diff
changeset
|
317 int i_strocpy(char *dest, const char *src, size_t dstsize) |
807
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
318 { |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
319 if (dstsize == 0) |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
320 return -1; |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
321 |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
322 while (*src != '\0' && dstsize > 1) { |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
323 *dest++ = *src++; |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
324 dstsize--; |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
325 } |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
326 |
11039 | 327 *dest = '\0'; |
807
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
328 return *src == '\0' ? 0 : -1; |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
329 } |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
330 |
0 | 331 char *str_ucase(char *str) |
332 { | |
333 char *p; | |
334 | |
335 for (p = str; *p != '\0'; p++) | |
336 *p = i_toupper(*p); | |
337 return str; | |
338 } | |
339 | |
340 char *str_lcase(char *str) | |
341 { | |
342 char *p; | |
343 | |
344 for (p = str; *p != '\0'; p++) | |
345 *p = i_tolower(*p); | |
346 return str; | |
347 } | |
348 | |
2057
5e0167577399
Fixed var_expand() to take a table of variables rather than a few predefined
Timo Sirainen <tss@iki.fi>
parents:
1838
diff
changeset
|
349 const char *t_str_lcase(const char *str) |
5e0167577399
Fixed var_expand() to take a table of variables rather than a few predefined
Timo Sirainen <tss@iki.fi>
parents:
1838
diff
changeset
|
350 { |
14391
ba1fc76e3a2c
Changes to make static analyzer happier.
Timo Sirainen <tss@iki.fi>
parents:
14133
diff
changeset
|
351 i_assert(str != NULL); |
ba1fc76e3a2c
Changes to make static analyzer happier.
Timo Sirainen <tss@iki.fi>
parents:
14133
diff
changeset
|
352 |
2057
5e0167577399
Fixed var_expand() to take a table of variables rather than a few predefined
Timo Sirainen <tss@iki.fi>
parents:
1838
diff
changeset
|
353 return str_lcase(t_strdup_noconst(str)); |
5e0167577399
Fixed var_expand() to take a table of variables rather than a few predefined
Timo Sirainen <tss@iki.fi>
parents:
1838
diff
changeset
|
354 } |
5e0167577399
Fixed var_expand() to take a table of variables rather than a few predefined
Timo Sirainen <tss@iki.fi>
parents:
1838
diff
changeset
|
355 |
5e0167577399
Fixed var_expand() to take a table of variables rather than a few predefined
Timo Sirainen <tss@iki.fi>
parents:
1838
diff
changeset
|
356 const char *t_str_ucase(const char *str) |
5e0167577399
Fixed var_expand() to take a table of variables rather than a few predefined
Timo Sirainen <tss@iki.fi>
parents:
1838
diff
changeset
|
357 { |
14391
ba1fc76e3a2c
Changes to make static analyzer happier.
Timo Sirainen <tss@iki.fi>
parents:
14133
diff
changeset
|
358 i_assert(str != NULL); |
ba1fc76e3a2c
Changes to make static analyzer happier.
Timo Sirainen <tss@iki.fi>
parents:
14133
diff
changeset
|
359 |
2057
5e0167577399
Fixed var_expand() to take a table of variables rather than a few predefined
Timo Sirainen <tss@iki.fi>
parents:
1838
diff
changeset
|
360 return str_ucase(t_strdup_noconst(str)); |
5e0167577399
Fixed var_expand() to take a table of variables rather than a few predefined
Timo Sirainen <tss@iki.fi>
parents:
1838
diff
changeset
|
361 } |
5e0167577399
Fixed var_expand() to take a table of variables rather than a few predefined
Timo Sirainen <tss@iki.fi>
parents:
1838
diff
changeset
|
362 |
19459
6cb9889cc960
lib: Removed t_str_trim() for now due to collision in Pigeonhole v0.4.9
Timo Sirainen <tss@iki.fi>
parents:
19433
diff
changeset
|
363 #if 0 /* FIXME: wait for v2.3 due to a collision with pigeonhole */ |
19433
922de4bc4777
lib: Created t_str_trim() functions to trim characters from beginning and end of string.
Stephan Bosch <stephan@rename-it.nl>
parents:
18715
diff
changeset
|
364 const char *t_str_trim(const char *str, const char *chars) |
922de4bc4777
lib: Created t_str_trim() functions to trim characters from beginning and end of string.
Stephan Bosch <stephan@rename-it.nl>
parents:
18715
diff
changeset
|
365 { |
922de4bc4777
lib: Created t_str_trim() functions to trim characters from beginning and end of string.
Stephan Bosch <stephan@rename-it.nl>
parents:
18715
diff
changeset
|
366 const char *p, *pend, *begin; |
922de4bc4777
lib: Created t_str_trim() functions to trim characters from beginning and end of string.
Stephan Bosch <stephan@rename-it.nl>
parents:
18715
diff
changeset
|
367 |
922de4bc4777
lib: Created t_str_trim() functions to trim characters from beginning and end of string.
Stephan Bosch <stephan@rename-it.nl>
parents:
18715
diff
changeset
|
368 pend = str + strlen(str); |
922de4bc4777
lib: Created t_str_trim() functions to trim characters from beginning and end of string.
Stephan Bosch <stephan@rename-it.nl>
parents:
18715
diff
changeset
|
369 if (pend == str) |
922de4bc4777
lib: Created t_str_trim() functions to trim characters from beginning and end of string.
Stephan Bosch <stephan@rename-it.nl>
parents:
18715
diff
changeset
|
370 return ""; |
922de4bc4777
lib: Created t_str_trim() functions to trim characters from beginning and end of string.
Stephan Bosch <stephan@rename-it.nl>
parents:
18715
diff
changeset
|
371 |
922de4bc4777
lib: Created t_str_trim() functions to trim characters from beginning and end of string.
Stephan Bosch <stephan@rename-it.nl>
parents:
18715
diff
changeset
|
372 p = str; |
922de4bc4777
lib: Created t_str_trim() functions to trim characters from beginning and end of string.
Stephan Bosch <stephan@rename-it.nl>
parents:
18715
diff
changeset
|
373 while (p < pend && strchr(chars, *p) != NULL) |
922de4bc4777
lib: Created t_str_trim() functions to trim characters from beginning and end of string.
Stephan Bosch <stephan@rename-it.nl>
parents:
18715
diff
changeset
|
374 p++; |
922de4bc4777
lib: Created t_str_trim() functions to trim characters from beginning and end of string.
Stephan Bosch <stephan@rename-it.nl>
parents:
18715
diff
changeset
|
375 begin = p; |
922de4bc4777
lib: Created t_str_trim() functions to trim characters from beginning and end of string.
Stephan Bosch <stephan@rename-it.nl>
parents:
18715
diff
changeset
|
376 |
922de4bc4777
lib: Created t_str_trim() functions to trim characters from beginning and end of string.
Stephan Bosch <stephan@rename-it.nl>
parents:
18715
diff
changeset
|
377 p = pend - 1; |
922de4bc4777
lib: Created t_str_trim() functions to trim characters from beginning and end of string.
Stephan Bosch <stephan@rename-it.nl>
parents:
18715
diff
changeset
|
378 while (p > begin && strchr(chars, *p) != NULL) |
922de4bc4777
lib: Created t_str_trim() functions to trim characters from beginning and end of string.
Stephan Bosch <stephan@rename-it.nl>
parents:
18715
diff
changeset
|
379 p--; |
922de4bc4777
lib: Created t_str_trim() functions to trim characters from beginning and end of string.
Stephan Bosch <stephan@rename-it.nl>
parents:
18715
diff
changeset
|
380 |
922de4bc4777
lib: Created t_str_trim() functions to trim characters from beginning and end of string.
Stephan Bosch <stephan@rename-it.nl>
parents:
18715
diff
changeset
|
381 if (p <= begin) |
922de4bc4777
lib: Created t_str_trim() functions to trim characters from beginning and end of string.
Stephan Bosch <stephan@rename-it.nl>
parents:
18715
diff
changeset
|
382 return ""; |
922de4bc4777
lib: Created t_str_trim() functions to trim characters from beginning and end of string.
Stephan Bosch <stephan@rename-it.nl>
parents:
18715
diff
changeset
|
383 return t_strdup_until(begin, p+1); |
922de4bc4777
lib: Created t_str_trim() functions to trim characters from beginning and end of string.
Stephan Bosch <stephan@rename-it.nl>
parents:
18715
diff
changeset
|
384 } |
19459
6cb9889cc960
lib: Removed t_str_trim() for now due to collision in Pigeonhole v0.4.9
Timo Sirainen <tss@iki.fi>
parents:
19433
diff
changeset
|
385 #endif |
19433
922de4bc4777
lib: Created t_str_trim() functions to trim characters from beginning and end of string.
Stephan Bosch <stephan@rename-it.nl>
parents:
18715
diff
changeset
|
386 |
922de4bc4777
lib: Created t_str_trim() functions to trim characters from beginning and end of string.
Stephan Bosch <stephan@rename-it.nl>
parents:
18715
diff
changeset
|
387 const char *str_ltrim(const char *str, const char *chars) |
922de4bc4777
lib: Created t_str_trim() functions to trim characters from beginning and end of string.
Stephan Bosch <stephan@rename-it.nl>
parents:
18715
diff
changeset
|
388 { |
922de4bc4777
lib: Created t_str_trim() functions to trim characters from beginning and end of string.
Stephan Bosch <stephan@rename-it.nl>
parents:
18715
diff
changeset
|
389 const char *p; |
922de4bc4777
lib: Created t_str_trim() functions to trim characters from beginning and end of string.
Stephan Bosch <stephan@rename-it.nl>
parents:
18715
diff
changeset
|
390 |
922de4bc4777
lib: Created t_str_trim() functions to trim characters from beginning and end of string.
Stephan Bosch <stephan@rename-it.nl>
parents:
18715
diff
changeset
|
391 if (*str == '\0') |
922de4bc4777
lib: Created t_str_trim() functions to trim characters from beginning and end of string.
Stephan Bosch <stephan@rename-it.nl>
parents:
18715
diff
changeset
|
392 return ""; |
922de4bc4777
lib: Created t_str_trim() functions to trim characters from beginning and end of string.
Stephan Bosch <stephan@rename-it.nl>
parents:
18715
diff
changeset
|
393 |
922de4bc4777
lib: Created t_str_trim() functions to trim characters from beginning and end of string.
Stephan Bosch <stephan@rename-it.nl>
parents:
18715
diff
changeset
|
394 p = str; |
922de4bc4777
lib: Created t_str_trim() functions to trim characters from beginning and end of string.
Stephan Bosch <stephan@rename-it.nl>
parents:
18715
diff
changeset
|
395 while (*p != '\0' && strchr(chars, *p) != NULL) |
922de4bc4777
lib: Created t_str_trim() functions to trim characters from beginning and end of string.
Stephan Bosch <stephan@rename-it.nl>
parents:
18715
diff
changeset
|
396 p++; |
922de4bc4777
lib: Created t_str_trim() functions to trim characters from beginning and end of string.
Stephan Bosch <stephan@rename-it.nl>
parents:
18715
diff
changeset
|
397 |
922de4bc4777
lib: Created t_str_trim() functions to trim characters from beginning and end of string.
Stephan Bosch <stephan@rename-it.nl>
parents:
18715
diff
changeset
|
398 return p; |
922de4bc4777
lib: Created t_str_trim() functions to trim characters from beginning and end of string.
Stephan Bosch <stephan@rename-it.nl>
parents:
18715
diff
changeset
|
399 } |
922de4bc4777
lib: Created t_str_trim() functions to trim characters from beginning and end of string.
Stephan Bosch <stephan@rename-it.nl>
parents:
18715
diff
changeset
|
400 |
922de4bc4777
lib: Created t_str_trim() functions to trim characters from beginning and end of string.
Stephan Bosch <stephan@rename-it.nl>
parents:
18715
diff
changeset
|
401 const char *t_str_ltrim(const char *str, const char *chars) |
922de4bc4777
lib: Created t_str_trim() functions to trim characters from beginning and end of string.
Stephan Bosch <stephan@rename-it.nl>
parents:
18715
diff
changeset
|
402 { |
922de4bc4777
lib: Created t_str_trim() functions to trim characters from beginning and end of string.
Stephan Bosch <stephan@rename-it.nl>
parents:
18715
diff
changeset
|
403 return t_strdup(str_ltrim(str, chars)); |
922de4bc4777
lib: Created t_str_trim() functions to trim characters from beginning and end of string.
Stephan Bosch <stephan@rename-it.nl>
parents:
18715
diff
changeset
|
404 } |
922de4bc4777
lib: Created t_str_trim() functions to trim characters from beginning and end of string.
Stephan Bosch <stephan@rename-it.nl>
parents:
18715
diff
changeset
|
405 |
922de4bc4777
lib: Created t_str_trim() functions to trim characters from beginning and end of string.
Stephan Bosch <stephan@rename-it.nl>
parents:
18715
diff
changeset
|
406 const char *t_str_rtrim(const char *str, const char *chars) |
922de4bc4777
lib: Created t_str_trim() functions to trim characters from beginning and end of string.
Stephan Bosch <stephan@rename-it.nl>
parents:
18715
diff
changeset
|
407 { |
922de4bc4777
lib: Created t_str_trim() functions to trim characters from beginning and end of string.
Stephan Bosch <stephan@rename-it.nl>
parents:
18715
diff
changeset
|
408 const char *p, *pend; |
922de4bc4777
lib: Created t_str_trim() functions to trim characters from beginning and end of string.
Stephan Bosch <stephan@rename-it.nl>
parents:
18715
diff
changeset
|
409 |
922de4bc4777
lib: Created t_str_trim() functions to trim characters from beginning and end of string.
Stephan Bosch <stephan@rename-it.nl>
parents:
18715
diff
changeset
|
410 pend = str + strlen(str); |
922de4bc4777
lib: Created t_str_trim() functions to trim characters from beginning and end of string.
Stephan Bosch <stephan@rename-it.nl>
parents:
18715
diff
changeset
|
411 if (pend == str) |
922de4bc4777
lib: Created t_str_trim() functions to trim characters from beginning and end of string.
Stephan Bosch <stephan@rename-it.nl>
parents:
18715
diff
changeset
|
412 return ""; |
922de4bc4777
lib: Created t_str_trim() functions to trim characters from beginning and end of string.
Stephan Bosch <stephan@rename-it.nl>
parents:
18715
diff
changeset
|
413 |
922de4bc4777
lib: Created t_str_trim() functions to trim characters from beginning and end of string.
Stephan Bosch <stephan@rename-it.nl>
parents:
18715
diff
changeset
|
414 p = pend - 1; |
922de4bc4777
lib: Created t_str_trim() functions to trim characters from beginning and end of string.
Stephan Bosch <stephan@rename-it.nl>
parents:
18715
diff
changeset
|
415 while (p > str && strchr(chars, *p) != NULL) |
922de4bc4777
lib: Created t_str_trim() functions to trim characters from beginning and end of string.
Stephan Bosch <stephan@rename-it.nl>
parents:
18715
diff
changeset
|
416 p--; |
922de4bc4777
lib: Created t_str_trim() functions to trim characters from beginning and end of string.
Stephan Bosch <stephan@rename-it.nl>
parents:
18715
diff
changeset
|
417 if (p <= str) |
922de4bc4777
lib: Created t_str_trim() functions to trim characters from beginning and end of string.
Stephan Bosch <stephan@rename-it.nl>
parents:
18715
diff
changeset
|
418 return ""; |
922de4bc4777
lib: Created t_str_trim() functions to trim characters from beginning and end of string.
Stephan Bosch <stephan@rename-it.nl>
parents:
18715
diff
changeset
|
419 return t_strdup_until(str, p+1); |
922de4bc4777
lib: Created t_str_trim() functions to trim characters from beginning and end of string.
Stephan Bosch <stephan@rename-it.nl>
parents:
18715
diff
changeset
|
420 } |
922de4bc4777
lib: Created t_str_trim() functions to trim characters from beginning and end of string.
Stephan Bosch <stephan@rename-it.nl>
parents:
18715
diff
changeset
|
421 |
988
8028c4dcf38f
mail-storage.h interface changes, affects pretty much everything.
Timo Sirainen <tss@iki.fi>
parents:
903
diff
changeset
|
422 int null_strcmp(const char *s1, const char *s2) |
8028c4dcf38f
mail-storage.h interface changes, affects pretty much everything.
Timo Sirainen <tss@iki.fi>
parents:
903
diff
changeset
|
423 { |
8028c4dcf38f
mail-storage.h interface changes, affects pretty much everything.
Timo Sirainen <tss@iki.fi>
parents:
903
diff
changeset
|
424 if (s1 == NULL) |
8028c4dcf38f
mail-storage.h interface changes, affects pretty much everything.
Timo Sirainen <tss@iki.fi>
parents:
903
diff
changeset
|
425 return s2 == NULL ? 0 : -1; |
8028c4dcf38f
mail-storage.h interface changes, affects pretty much everything.
Timo Sirainen <tss@iki.fi>
parents:
903
diff
changeset
|
426 if (s2 == NULL) |
8028c4dcf38f
mail-storage.h interface changes, affects pretty much everything.
Timo Sirainen <tss@iki.fi>
parents:
903
diff
changeset
|
427 return 1; |
8028c4dcf38f
mail-storage.h interface changes, affects pretty much everything.
Timo Sirainen <tss@iki.fi>
parents:
903
diff
changeset
|
428 |
993
cba414845f1b
null_strcmp() was broken -> SORT was broken.
Timo Sirainen <tss@iki.fi>
parents:
988
diff
changeset
|
429 return strcmp(s1, s2); |
988
8028c4dcf38f
mail-storage.h interface changes, affects pretty much everything.
Timo Sirainen <tss@iki.fi>
parents:
903
diff
changeset
|
430 } |
8028c4dcf38f
mail-storage.h interface changes, affects pretty much everything.
Timo Sirainen <tss@iki.fi>
parents:
903
diff
changeset
|
431 |
20887
ead4bfb88da1
lib: Add null_strcasecmp
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
19791
diff
changeset
|
432 int null_strcasecmp(const char *s1, const char *s2) |
ead4bfb88da1
lib: Add null_strcasecmp
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
19791
diff
changeset
|
433 { |
ead4bfb88da1
lib: Add null_strcasecmp
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
19791
diff
changeset
|
434 if (s1 == NULL) |
ead4bfb88da1
lib: Add null_strcasecmp
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
19791
diff
changeset
|
435 return s2 == NULL ? 0 : -1; |
ead4bfb88da1
lib: Add null_strcasecmp
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
19791
diff
changeset
|
436 if (s2 == NULL) |
ead4bfb88da1
lib: Add null_strcasecmp
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
19791
diff
changeset
|
437 return 1; |
ead4bfb88da1
lib: Add null_strcasecmp
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
19791
diff
changeset
|
438 |
ead4bfb88da1
lib: Add null_strcasecmp
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
19791
diff
changeset
|
439 return strcasecmp(s1, s2); |
ead4bfb88da1
lib: Add null_strcasecmp
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
19791
diff
changeset
|
440 } |
ead4bfb88da1
lib: Add null_strcasecmp
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
19791
diff
changeset
|
441 |
6423
13b0a3ba5f7b
Added i_ prefix to strcmp_p(), strcasecmp_p() and memcasecmp().
Timo Sirainen <tss@iki.fi>
parents:
6422
diff
changeset
|
442 int i_memcasecmp(const void *p1, const void *p2, size_t size) |
898
0d5be52d7131
Use unsigned char* when accessing non-NUL terminating strings. Compiler
Timo Sirainen <tss@iki.fi>
parents:
896
diff
changeset
|
443 { |
0d5be52d7131
Use unsigned char* when accessing non-NUL terminating strings. Compiler
Timo Sirainen <tss@iki.fi>
parents:
896
diff
changeset
|
444 const unsigned char *s1 = p1; |
0d5be52d7131
Use unsigned char* when accessing non-NUL terminating strings. Compiler
Timo Sirainen <tss@iki.fi>
parents:
896
diff
changeset
|
445 const unsigned char *s2 = p2; |
0d5be52d7131
Use unsigned char* when accessing non-NUL terminating strings. Compiler
Timo Sirainen <tss@iki.fi>
parents:
896
diff
changeset
|
446 int ret; |
0d5be52d7131
Use unsigned char* when accessing non-NUL terminating strings. Compiler
Timo Sirainen <tss@iki.fi>
parents:
896
diff
changeset
|
447 |
0d5be52d7131
Use unsigned char* when accessing non-NUL terminating strings. Compiler
Timo Sirainen <tss@iki.fi>
parents:
896
diff
changeset
|
448 while (size > 0) { |
0d5be52d7131
Use unsigned char* when accessing non-NUL terminating strings. Compiler
Timo Sirainen <tss@iki.fi>
parents:
896
diff
changeset
|
449 ret = i_toupper(*s1) - i_toupper(*s2); |
0d5be52d7131
Use unsigned char* when accessing non-NUL terminating strings. Compiler
Timo Sirainen <tss@iki.fi>
parents:
896
diff
changeset
|
450 if (ret != 0) |
0d5be52d7131
Use unsigned char* when accessing non-NUL terminating strings. Compiler
Timo Sirainen <tss@iki.fi>
parents:
896
diff
changeset
|
451 return ret; |
0d5be52d7131
Use unsigned char* when accessing non-NUL terminating strings. Compiler
Timo Sirainen <tss@iki.fi>
parents:
896
diff
changeset
|
452 |
0d5be52d7131
Use unsigned char* when accessing non-NUL terminating strings. Compiler
Timo Sirainen <tss@iki.fi>
parents:
896
diff
changeset
|
453 s1++; s2++; size--; |
0d5be52d7131
Use unsigned char* when accessing non-NUL terminating strings. Compiler
Timo Sirainen <tss@iki.fi>
parents:
896
diff
changeset
|
454 } |
0d5be52d7131
Use unsigned char* when accessing non-NUL terminating strings. Compiler
Timo Sirainen <tss@iki.fi>
parents:
896
diff
changeset
|
455 |
0d5be52d7131
Use unsigned char* when accessing non-NUL terminating strings. Compiler
Timo Sirainen <tss@iki.fi>
parents:
896
diff
changeset
|
456 return 0; |
0d5be52d7131
Use unsigned char* when accessing non-NUL terminating strings. Compiler
Timo Sirainen <tss@iki.fi>
parents:
896
diff
changeset
|
457 } |
0d5be52d7131
Use unsigned char* when accessing non-NUL terminating strings. Compiler
Timo Sirainen <tss@iki.fi>
parents:
896
diff
changeset
|
458 |
14921
d3db2ba15d00
Removed CONTEXT_TYPE_SAFETY macro and reimplemented its functionality better.
Timo Sirainen <tss@iki.fi>
parents:
14629
diff
changeset
|
459 int bsearch_strcmp(const char *key, const char *const *member) |
4075
bd7aafc736eb
Added strcmp_p and bsearch_strcmp.
Timo Sirainen <timo.sirainen@movial.fi>
parents:
3863
diff
changeset
|
460 { |
bd7aafc736eb
Added strcmp_p and bsearch_strcmp.
Timo Sirainen <timo.sirainen@movial.fi>
parents:
3863
diff
changeset
|
461 return strcmp(key, *member); |
bd7aafc736eb
Added strcmp_p and bsearch_strcmp.
Timo Sirainen <timo.sirainen@movial.fi>
parents:
3863
diff
changeset
|
462 } |
bd7aafc736eb
Added strcmp_p and bsearch_strcmp.
Timo Sirainen <timo.sirainen@movial.fi>
parents:
3863
diff
changeset
|
463 |
17355
de99b926a9ad
lib: cosmetic - rename parameters so that .h and .c files match
Phil Carmody <phil@dovecot.fi>
parents:
17185
diff
changeset
|
464 int i_strcmp_p(const char *const *p1, const char *const *p2) |
4075
bd7aafc736eb
Added strcmp_p and bsearch_strcmp.
Timo Sirainen <timo.sirainen@movial.fi>
parents:
3863
diff
changeset
|
465 { |
17355
de99b926a9ad
lib: cosmetic - rename parameters so that .h and .c files match
Phil Carmody <phil@dovecot.fi>
parents:
17185
diff
changeset
|
466 return strcmp(*p1, *p2); |
4075
bd7aafc736eb
Added strcmp_p and bsearch_strcmp.
Timo Sirainen <timo.sirainen@movial.fi>
parents:
3863
diff
changeset
|
467 } |
bd7aafc736eb
Added strcmp_p and bsearch_strcmp.
Timo Sirainen <timo.sirainen@movial.fi>
parents:
3863
diff
changeset
|
468 |
14921
d3db2ba15d00
Removed CONTEXT_TYPE_SAFETY macro and reimplemented its functionality better.
Timo Sirainen <tss@iki.fi>
parents:
14629
diff
changeset
|
469 int bsearch_strcasecmp(const char *key, const char *const *member) |
2195 | 470 { |
2326
0a8c9bfc05a1
bsearch_strcasecmp() actually called strcmp..
Timo Sirainen <tss@iki.fi>
parents:
2195
diff
changeset
|
471 return strcasecmp(key, *member); |
2195 | 472 } |
473 | |
17355
de99b926a9ad
lib: cosmetic - rename parameters so that .h and .c files match
Phil Carmody <phil@dovecot.fi>
parents:
17185
diff
changeset
|
474 int i_strcasecmp_p(const char *const *p1, const char *const *p2) |
2421
d141e1bfdd63
We never do blocking reads/writes to network anymore. Changed imap and pop3
Timo Sirainen <tss@iki.fi>
parents:
2326
diff
changeset
|
475 { |
17355
de99b926a9ad
lib: cosmetic - rename parameters so that .h and .c files match
Phil Carmody <phil@dovecot.fi>
parents:
17185
diff
changeset
|
476 return strcasecmp(*p1, *p2); |
2421
d141e1bfdd63
We never do blocking reads/writes to network anymore. Changed imap and pop3
Timo Sirainen <tss@iki.fi>
parents:
2326
diff
changeset
|
477 } |
d141e1bfdd63
We never do blocking reads/writes to network anymore. Changed imap and pop3
Timo Sirainen <tss@iki.fi>
parents:
2326
diff
changeset
|
478 |
21951
3ef988b95d96
lib: Add mem_equals_timing_safe()
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21390
diff
changeset
|
479 bool mem_equals_timing_safe(const void *p1, const void *p2, size_t size) |
3ef988b95d96
lib: Add mem_equals_timing_safe()
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21390
diff
changeset
|
480 { |
3ef988b95d96
lib: Add mem_equals_timing_safe()
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21390
diff
changeset
|
481 const unsigned char *s1 = p1, *s2 = p2; |
3ef988b95d96
lib: Add mem_equals_timing_safe()
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21390
diff
changeset
|
482 size_t i; |
3ef988b95d96
lib: Add mem_equals_timing_safe()
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21390
diff
changeset
|
483 int ret = 0; |
3ef988b95d96
lib: Add mem_equals_timing_safe()
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21390
diff
changeset
|
484 |
3ef988b95d96
lib: Add mem_equals_timing_safe()
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21390
diff
changeset
|
485 for (i = 0; i < size; i++) |
3ef988b95d96
lib: Add mem_equals_timing_safe()
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21390
diff
changeset
|
486 ret |= s1[i] ^ s2[i]; |
3ef988b95d96
lib: Add mem_equals_timing_safe()
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21390
diff
changeset
|
487 |
3ef988b95d96
lib: Add mem_equals_timing_safe()
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21390
diff
changeset
|
488 /* make sure the compiler optimizer doesn't try to break out of the |
3ef988b95d96
lib: Add mem_equals_timing_safe()
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21390
diff
changeset
|
489 above loop early. */ |
3ef988b95d96
lib: Add mem_equals_timing_safe()
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21390
diff
changeset
|
490 timing_safety_unoptimization = ret; |
3ef988b95d96
lib: Add mem_equals_timing_safe()
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21390
diff
changeset
|
491 return ret == 0; |
3ef988b95d96
lib: Add mem_equals_timing_safe()
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21390
diff
changeset
|
492 } |
3ef988b95d96
lib: Add mem_equals_timing_safe()
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21390
diff
changeset
|
493 |
3615
0a885029543b
Added p_strsplit(), p_strsplit_spaces() and strarray_remove()
Timo Sirainen <tss@iki.fi>
parents:
3520
diff
changeset
|
494 static char ** |
22637
da955f909fdd
lib: *_strsplit() - implement more efficient version for a single separator char
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
22030
diff
changeset
|
495 split_str_slow(pool_t pool, const char *data, const char *separators, bool spaces) |
0 | 496 { |
3615
0a885029543b
Added p_strsplit(), p_strsplit_spaces() and strarray_remove()
Timo Sirainen <tss@iki.fi>
parents:
3520
diff
changeset
|
497 char **array; |
0 | 498 char *str; |
3615
0a885029543b
Added p_strsplit(), p_strsplit_spaces() and strarray_remove()
Timo Sirainen <tss@iki.fi>
parents:
3520
diff
changeset
|
499 unsigned int count, alloc_count, new_alloc_count; |
0 | 500 |
3778
91689264d812
*_strsplit_spaces() was skipping only space separators, while it should have
Timo Sirainen <tss@iki.fi>
parents:
3619
diff
changeset
|
501 if (spaces) { |
91689264d812
*_strsplit_spaces() was skipping only space separators, while it should have
Timo Sirainen <tss@iki.fi>
parents:
3619
diff
changeset
|
502 /* skip leading separators */ |
3803
1e3ed6636bfe
Fix for last change in *_strsplit_spaces().
Timo Sirainen <tss@iki.fi>
parents:
3778
diff
changeset
|
503 while (*data != '\0' && strchr(separators, *data) != NULL) |
3778
91689264d812
*_strsplit_spaces() was skipping only space separators, while it should have
Timo Sirainen <tss@iki.fi>
parents:
3619
diff
changeset
|
504 data++; |
91689264d812
*_strsplit_spaces() was skipping only space separators, while it should have
Timo Sirainen <tss@iki.fi>
parents:
3619
diff
changeset
|
505 } |
1823
cf9fa86e2a91
If input is empty, return only list with NULL.
Timo Sirainen <tss@iki.fi>
parents:
1821
diff
changeset
|
506 if (*data == '\0') |
3615
0a885029543b
Added p_strsplit(), p_strsplit_spaces() and strarray_remove()
Timo Sirainen <tss@iki.fi>
parents:
3520
diff
changeset
|
507 return p_new(pool, char *, 1); |
0 | 508 |
3615
0a885029543b
Added p_strsplit(), p_strsplit_spaces() and strarray_remove()
Timo Sirainen <tss@iki.fi>
parents:
3520
diff
changeset
|
509 str = p_strdup(pool, data); |
0 | 510 |
3615
0a885029543b
Added p_strsplit(), p_strsplit_spaces() and strarray_remove()
Timo Sirainen <tss@iki.fi>
parents:
3520
diff
changeset
|
511 alloc_count = 32; |
0a885029543b
Added p_strsplit(), p_strsplit_spaces() and strarray_remove()
Timo Sirainen <tss@iki.fi>
parents:
3520
diff
changeset
|
512 array = p_new(pool, char *, alloc_count); |
0 | 513 |
3615
0a885029543b
Added p_strsplit(), p_strsplit_spaces() and strarray_remove()
Timo Sirainen <tss@iki.fi>
parents:
3520
diff
changeset
|
514 array[0] = str; count = 1; |
0 | 515 while (*str != '\0') { |
516 if (strchr(separators, *str) != NULL) { | |
517 /* separator found */ | |
3615
0a885029543b
Added p_strsplit(), p_strsplit_spaces() and strarray_remove()
Timo Sirainen <tss@iki.fi>
parents:
3520
diff
changeset
|
518 if (count+1 >= alloc_count) { |
0a885029543b
Added p_strsplit(), p_strsplit_spaces() and strarray_remove()
Timo Sirainen <tss@iki.fi>
parents:
3520
diff
changeset
|
519 new_alloc_count = nearest_power(alloc_count+1); |
0a885029543b
Added p_strsplit(), p_strsplit_spaces() and strarray_remove()
Timo Sirainen <tss@iki.fi>
parents:
3520
diff
changeset
|
520 array = p_realloc(pool, array, |
0a885029543b
Added p_strsplit(), p_strsplit_spaces() and strarray_remove()
Timo Sirainen <tss@iki.fi>
parents:
3520
diff
changeset
|
521 sizeof(char *) * alloc_count, |
0a885029543b
Added p_strsplit(), p_strsplit_spaces() and strarray_remove()
Timo Sirainen <tss@iki.fi>
parents:
3520
diff
changeset
|
522 sizeof(char *) * |
0a885029543b
Added p_strsplit(), p_strsplit_spaces() and strarray_remove()
Timo Sirainen <tss@iki.fi>
parents:
3520
diff
changeset
|
523 new_alloc_count); |
0a885029543b
Added p_strsplit(), p_strsplit_spaces() and strarray_remove()
Timo Sirainen <tss@iki.fi>
parents:
3520
diff
changeset
|
524 alloc_count = new_alloc_count; |
0 | 525 } |
526 | |
3778
91689264d812
*_strsplit_spaces() was skipping only space separators, while it should have
Timo Sirainen <tss@iki.fi>
parents:
3619
diff
changeset
|
527 *str = '\0'; |
91689264d812
*_strsplit_spaces() was skipping only space separators, while it should have
Timo Sirainen <tss@iki.fi>
parents:
3619
diff
changeset
|
528 if (spaces) { |
3803
1e3ed6636bfe
Fix for last change in *_strsplit_spaces().
Timo Sirainen <tss@iki.fi>
parents:
3778
diff
changeset
|
529 while (str[1] != '\0' && |
1e3ed6636bfe
Fix for last change in *_strsplit_spaces().
Timo Sirainen <tss@iki.fi>
parents:
3778
diff
changeset
|
530 strchr(separators, str[1]) != NULL) |
3778
91689264d812
*_strsplit_spaces() was skipping only space separators, while it should have
Timo Sirainen <tss@iki.fi>
parents:
3619
diff
changeset
|
531 str++; |
3619
86ddbe18538c
*_strsplit_spaces(): Ignore trailing spaces.
Timo Sirainen <tss@iki.fi>
parents:
3615
diff
changeset
|
532 |
3778
91689264d812
*_strsplit_spaces() was skipping only space separators, while it should have
Timo Sirainen <tss@iki.fi>
parents:
3619
diff
changeset
|
533 /* ignore trailing separators */ |
3619
86ddbe18538c
*_strsplit_spaces(): Ignore trailing spaces.
Timo Sirainen <tss@iki.fi>
parents:
3615
diff
changeset
|
534 if (str[1] == '\0') |
86ddbe18538c
*_strsplit_spaces(): Ignore trailing spaces.
Timo Sirainen <tss@iki.fi>
parents:
3615
diff
changeset
|
535 break; |
1820 | 536 } |
537 | |
3615
0a885029543b
Added p_strsplit(), p_strsplit_spaces() and strarray_remove()
Timo Sirainen <tss@iki.fi>
parents:
3520
diff
changeset
|
538 array[count++] = str+1; |
0 | 539 } |
540 | |
541 str++; | |
542 } | |
847
7f41a4b33975
t_strsplit() returns now const char **, which removes a few nasty casts.
Timo Sirainen <tss@iki.fi>
parents:
842
diff
changeset
|
543 |
3615
0a885029543b
Added p_strsplit(), p_strsplit_spaces() and strarray_remove()
Timo Sirainen <tss@iki.fi>
parents:
3520
diff
changeset
|
544 i_assert(count < alloc_count); |
0a885029543b
Added p_strsplit(), p_strsplit_spaces() and strarray_remove()
Timo Sirainen <tss@iki.fi>
parents:
3520
diff
changeset
|
545 array[count] = NULL; |
0 | 546 |
847
7f41a4b33975
t_strsplit() returns now const char **, which removes a few nasty casts.
Timo Sirainen <tss@iki.fi>
parents:
842
diff
changeset
|
547 return array; |
0 | 548 } |
549 | |
22637
da955f909fdd
lib: *_strsplit() - implement more efficient version for a single separator char
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
22030
diff
changeset
|
550 static char ** |
da955f909fdd
lib: *_strsplit() - implement more efficient version for a single separator char
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
22030
diff
changeset
|
551 split_str_fast(pool_t pool, const char *data, char sep) |
da955f909fdd
lib: *_strsplit() - implement more efficient version for a single separator char
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
22030
diff
changeset
|
552 { |
da955f909fdd
lib: *_strsplit() - implement more efficient version for a single separator char
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
22030
diff
changeset
|
553 char **array, *str; |
da955f909fdd
lib: *_strsplit() - implement more efficient version for a single separator char
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
22030
diff
changeset
|
554 unsigned int count, alloc_count, new_alloc_count; |
da955f909fdd
lib: *_strsplit() - implement more efficient version for a single separator char
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
22030
diff
changeset
|
555 |
da955f909fdd
lib: *_strsplit() - implement more efficient version for a single separator char
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
22030
diff
changeset
|
556 if (*data == '\0') |
da955f909fdd
lib: *_strsplit() - implement more efficient version for a single separator char
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
22030
diff
changeset
|
557 return p_new(pool, char *, 1); |
da955f909fdd
lib: *_strsplit() - implement more efficient version for a single separator char
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
22030
diff
changeset
|
558 |
da955f909fdd
lib: *_strsplit() - implement more efficient version for a single separator char
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
22030
diff
changeset
|
559 str = p_strdup(pool, data); |
da955f909fdd
lib: *_strsplit() - implement more efficient version for a single separator char
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
22030
diff
changeset
|
560 |
da955f909fdd
lib: *_strsplit() - implement more efficient version for a single separator char
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
22030
diff
changeset
|
561 alloc_count = 32; |
da955f909fdd
lib: *_strsplit() - implement more efficient version for a single separator char
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
22030
diff
changeset
|
562 array = p_new(pool, char *, alloc_count); |
da955f909fdd
lib: *_strsplit() - implement more efficient version for a single separator char
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
22030
diff
changeset
|
563 |
da955f909fdd
lib: *_strsplit() - implement more efficient version for a single separator char
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
22030
diff
changeset
|
564 array[0] = str; count = 1; |
da955f909fdd
lib: *_strsplit() - implement more efficient version for a single separator char
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
22030
diff
changeset
|
565 while ((str = strchr(str, sep)) != NULL) { |
da955f909fdd
lib: *_strsplit() - implement more efficient version for a single separator char
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
22030
diff
changeset
|
566 /* separator found */ |
da955f909fdd
lib: *_strsplit() - implement more efficient version for a single separator char
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
22030
diff
changeset
|
567 if (count+1 >= alloc_count) { |
da955f909fdd
lib: *_strsplit() - implement more efficient version for a single separator char
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
22030
diff
changeset
|
568 new_alloc_count = nearest_power(alloc_count+1); |
da955f909fdd
lib: *_strsplit() - implement more efficient version for a single separator char
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
22030
diff
changeset
|
569 array = p_realloc(pool, array, |
da955f909fdd
lib: *_strsplit() - implement more efficient version for a single separator char
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
22030
diff
changeset
|
570 sizeof(char *) * alloc_count, |
da955f909fdd
lib: *_strsplit() - implement more efficient version for a single separator char
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
22030
diff
changeset
|
571 sizeof(char *) * |
da955f909fdd
lib: *_strsplit() - implement more efficient version for a single separator char
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
22030
diff
changeset
|
572 new_alloc_count); |
da955f909fdd
lib: *_strsplit() - implement more efficient version for a single separator char
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
22030
diff
changeset
|
573 alloc_count = new_alloc_count; |
da955f909fdd
lib: *_strsplit() - implement more efficient version for a single separator char
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
22030
diff
changeset
|
574 } |
da955f909fdd
lib: *_strsplit() - implement more efficient version for a single separator char
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
22030
diff
changeset
|
575 *str++ = '\0'; |
da955f909fdd
lib: *_strsplit() - implement more efficient version for a single separator char
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
22030
diff
changeset
|
576 array[count++] = str; |
da955f909fdd
lib: *_strsplit() - implement more efficient version for a single separator char
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
22030
diff
changeset
|
577 } |
da955f909fdd
lib: *_strsplit() - implement more efficient version for a single separator char
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
22030
diff
changeset
|
578 i_assert(count < alloc_count); |
da955f909fdd
lib: *_strsplit() - implement more efficient version for a single separator char
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
22030
diff
changeset
|
579 i_assert(array[count] == NULL); |
da955f909fdd
lib: *_strsplit() - implement more efficient version for a single separator char
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
22030
diff
changeset
|
580 |
da955f909fdd
lib: *_strsplit() - implement more efficient version for a single separator char
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
22030
diff
changeset
|
581 return array; |
da955f909fdd
lib: *_strsplit() - implement more efficient version for a single separator char
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
22030
diff
changeset
|
582 } |
da955f909fdd
lib: *_strsplit() - implement more efficient version for a single separator char
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
22030
diff
changeset
|
583 |
da955f909fdd
lib: *_strsplit() - implement more efficient version for a single separator char
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
22030
diff
changeset
|
584 static char ** |
da955f909fdd
lib: *_strsplit() - implement more efficient version for a single separator char
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
22030
diff
changeset
|
585 split_str(pool_t pool, const char *data, const char *separators, bool spaces) |
da955f909fdd
lib: *_strsplit() - implement more efficient version for a single separator char
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
22030
diff
changeset
|
586 { |
da955f909fdd
lib: *_strsplit() - implement more efficient version for a single separator char
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
22030
diff
changeset
|
587 i_assert(*separators != '\0'); |
da955f909fdd
lib: *_strsplit() - implement more efficient version for a single separator char
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
22030
diff
changeset
|
588 |
da955f909fdd
lib: *_strsplit() - implement more efficient version for a single separator char
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
22030
diff
changeset
|
589 if (separators[1] == '\0' && !spaces) |
da955f909fdd
lib: *_strsplit() - implement more efficient version for a single separator char
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
22030
diff
changeset
|
590 return split_str_fast(pool, data, separators[0]); |
da955f909fdd
lib: *_strsplit() - implement more efficient version for a single separator char
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
22030
diff
changeset
|
591 else |
da955f909fdd
lib: *_strsplit() - implement more efficient version for a single separator char
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
22030
diff
changeset
|
592 return split_str_slow(pool, data, separators, spaces); |
da955f909fdd
lib: *_strsplit() - implement more efficient version for a single separator char
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
22030
diff
changeset
|
593 } |
da955f909fdd
lib: *_strsplit() - implement more efficient version for a single separator char
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
22030
diff
changeset
|
594 |
1820 | 595 const char **t_strsplit(const char *data, const char *separators) |
596 { | |
6420
a8b515e1a26f
Removed _ prefixes from function names.
Timo Sirainen <tss@iki.fi>
parents:
6418
diff
changeset
|
597 return (const char **)split_str(unsafe_data_stack_pool, data, |
3615
0a885029543b
Added p_strsplit(), p_strsplit_spaces() and strarray_remove()
Timo Sirainen <tss@iki.fi>
parents:
3520
diff
changeset
|
598 separators, FALSE); |
1820 | 599 } |
600 | |
601 const char **t_strsplit_spaces(const char *data, const char *separators) | |
602 { | |
6420
a8b515e1a26f
Removed _ prefixes from function names.
Timo Sirainen <tss@iki.fi>
parents:
6418
diff
changeset
|
603 return (const char **)split_str(unsafe_data_stack_pool, data, |
3615
0a885029543b
Added p_strsplit(), p_strsplit_spaces() and strarray_remove()
Timo Sirainen <tss@iki.fi>
parents:
3520
diff
changeset
|
604 separators, TRUE); |
0a885029543b
Added p_strsplit(), p_strsplit_spaces() and strarray_remove()
Timo Sirainen <tss@iki.fi>
parents:
3520
diff
changeset
|
605 } |
0a885029543b
Added p_strsplit(), p_strsplit_spaces() and strarray_remove()
Timo Sirainen <tss@iki.fi>
parents:
3520
diff
changeset
|
606 |
0a885029543b
Added p_strsplit(), p_strsplit_spaces() and strarray_remove()
Timo Sirainen <tss@iki.fi>
parents:
3520
diff
changeset
|
607 char **p_strsplit(pool_t pool, const char *data, const char *separators) |
0a885029543b
Added p_strsplit(), p_strsplit_spaces() and strarray_remove()
Timo Sirainen <tss@iki.fi>
parents:
3520
diff
changeset
|
608 { |
6420
a8b515e1a26f
Removed _ prefixes from function names.
Timo Sirainen <tss@iki.fi>
parents:
6418
diff
changeset
|
609 return split_str(pool, data, separators, FALSE); |
3615
0a885029543b
Added p_strsplit(), p_strsplit_spaces() and strarray_remove()
Timo Sirainen <tss@iki.fi>
parents:
3520
diff
changeset
|
610 } |
0a885029543b
Added p_strsplit(), p_strsplit_spaces() and strarray_remove()
Timo Sirainen <tss@iki.fi>
parents:
3520
diff
changeset
|
611 |
0a885029543b
Added p_strsplit(), p_strsplit_spaces() and strarray_remove()
Timo Sirainen <tss@iki.fi>
parents:
3520
diff
changeset
|
612 char **p_strsplit_spaces(pool_t pool, const char *data, |
0a885029543b
Added p_strsplit(), p_strsplit_spaces() and strarray_remove()
Timo Sirainen <tss@iki.fi>
parents:
3520
diff
changeset
|
613 const char *separators) |
0a885029543b
Added p_strsplit(), p_strsplit_spaces() and strarray_remove()
Timo Sirainen <tss@iki.fi>
parents:
3520
diff
changeset
|
614 { |
6420
a8b515e1a26f
Removed _ prefixes from function names.
Timo Sirainen <tss@iki.fi>
parents:
6418
diff
changeset
|
615 return split_str(pool, data, separators, TRUE); |
1820 | 616 } |
617 | |
14470 | 618 const char **t_strsplit_tab(const char *data) |
619 { | |
620 const char **array; | |
621 char *dest; | |
622 unsigned int i, array_idx, array_size, dest_size; | |
623 | |
624 if (*data == '\0') | |
625 return t_new(const char *, 1); | |
626 | |
627 array_size = 1; | |
628 dest_size = 128; | |
629 dest = t_buffer_get(dest_size+1); | |
630 for (i = 0; data[i] != '\0'; i++) { | |
631 if (i >= dest_size) { | |
632 dest_size = nearest_power(dest_size+1); | |
633 dest = t_buffer_reget(dest, dest_size+1); | |
634 } | |
635 if (data[i] != '\t') | |
636 dest[i] = data[i]; | |
637 else { | |
638 dest[i] = '\0'; | |
639 array_size++; | |
640 } | |
641 } | |
642 i_assert(i <= dest_size); | |
643 dest[i] = '\0'; | |
644 t_buffer_alloc(i+1); | |
645 dest_size = i; | |
646 | |
647 array = t_new(const char *, array_size + 1); | |
648 array[0] = dest; array_idx = 1; | |
649 | |
650 for (i = 0; i < dest_size; i++) { | |
651 if (dest[i] == '\0') | |
652 array[array_idx++] = dest+i+1; | |
653 } | |
654 i_assert(array_idx == array_size); | |
655 array[array_idx] = NULL; | |
656 return array; | |
657 } | |
658 | |
4332
2428e6821eb4
Added p_strsplit_free()
Timo Sirainen <timo.sirainen@movial.fi>
parents:
4075
diff
changeset
|
659 void p_strsplit_free(pool_t pool, char **arr) |
2428e6821eb4
Added p_strsplit_free()
Timo Sirainen <timo.sirainen@movial.fi>
parents:
4075
diff
changeset
|
660 { |
2428e6821eb4
Added p_strsplit_free()
Timo Sirainen <timo.sirainen@movial.fi>
parents:
4075
diff
changeset
|
661 p_free(pool, arr[0]); |
2428e6821eb4
Added p_strsplit_free()
Timo Sirainen <timo.sirainen@movial.fi>
parents:
4075
diff
changeset
|
662 p_free(pool, arr); |
2428e6821eb4
Added p_strsplit_free()
Timo Sirainen <timo.sirainen@movial.fi>
parents:
4075
diff
changeset
|
663 } |
2428e6821eb4
Added p_strsplit_free()
Timo Sirainen <timo.sirainen@movial.fi>
parents:
4075
diff
changeset
|
664 |
6424
687efa138e77
Renamed strarray_length/remote() to str_array_length/remove()
Timo Sirainen <tss@iki.fi>
parents:
6423
diff
changeset
|
665 unsigned int str_array_length(const char *const *arr) |
3014 | 666 { |
667 unsigned int count; | |
668 | |
669 if (arr == NULL) | |
670 return 0; | |
671 | |
672 for (count = 0; *arr != NULL; arr++) | |
673 count++; | |
674 | |
675 return count; | |
676 } | |
677 | |
19790
8d3736bf4a90
lib: Move code from t_strarray_join() into a more generic p_strarray_join_n()
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
19552
diff
changeset
|
678 static char * |
8d3736bf4a90
lib: Move code from t_strarray_join() into a more generic p_strarray_join_n()
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
19552
diff
changeset
|
679 p_strarray_join_n(pool_t pool, const char *const *arr, unsigned int arr_len, |
8d3736bf4a90
lib: Move code from t_strarray_join() into a more generic p_strarray_join_n()
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
19552
diff
changeset
|
680 const char *separator) |
3038 | 681 { |
682 size_t alloc_len, sep_len, len, pos, needed_space; | |
19790
8d3736bf4a90
lib: Move code from t_strarray_join() into a more generic p_strarray_join_n()
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
19552
diff
changeset
|
683 unsigned int i; |
3038 | 684 char *str; |
685 | |
3061
42c1ca1f7a45
Renamed strarray_join() to t_strarray_join() and made it not to crash..
Timo Sirainen <tss@iki.fi>
parents:
3038
diff
changeset
|
686 sep_len = strlen(separator); |
3038 | 687 alloc_len = 64; |
19790
8d3736bf4a90
lib: Move code from t_strarray_join() into a more generic p_strarray_join_n()
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
19552
diff
changeset
|
688 str = t_buffer_get(alloc_len); |
8d3736bf4a90
lib: Move code from t_strarray_join() into a more generic p_strarray_join_n()
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
19552
diff
changeset
|
689 pos = 0; |
3038 | 690 |
19790
8d3736bf4a90
lib: Move code from t_strarray_join() into a more generic p_strarray_join_n()
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
19552
diff
changeset
|
691 for (i = 0; i < arr_len; i++) { |
8d3736bf4a90
lib: Move code from t_strarray_join() into a more generic p_strarray_join_n()
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
19552
diff
changeset
|
692 len = strlen(arr[i]); |
3038 | 693 needed_space = pos + len + sep_len + 1; |
3804
ae629e747baf
t_strarray_join() was broken with input longer than 63 bytes (good thing we
Timo Sirainen <tss@iki.fi>
parents:
3803
diff
changeset
|
694 if (needed_space > alloc_len) { |
3038 | 695 alloc_len = nearest_power(needed_space); |
696 str = t_buffer_reget(str, alloc_len); | |
697 } | |
698 | |
699 if (pos != 0) { | |
700 memcpy(str + pos, separator, sep_len); | |
701 pos += sep_len; | |
702 } | |
703 | |
19790
8d3736bf4a90
lib: Move code from t_strarray_join() into a more generic p_strarray_join_n()
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
19552
diff
changeset
|
704 memcpy(str + pos, arr[i], len); |
3038 | 705 pos += len; |
706 } | |
707 str[pos] = '\0'; | |
19790
8d3736bf4a90
lib: Move code from t_strarray_join() into a more generic p_strarray_join_n()
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
19552
diff
changeset
|
708 if (!pool->datastack_pool) |
8d3736bf4a90
lib: Move code from t_strarray_join() into a more generic p_strarray_join_n()
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
19552
diff
changeset
|
709 return p_memdup(pool, str, pos + 1); |
3269
23d49122f015
t_strarray_join() wasn't actually marking the memory allocated that it was
Timo Sirainen <tss@iki.fi>
parents:
3061
diff
changeset
|
710 t_buffer_alloc(pos + 1); |
3038 | 711 return str; |
712 } | |
713 | |
19790
8d3736bf4a90
lib: Move code from t_strarray_join() into a more generic p_strarray_join_n()
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
19552
diff
changeset
|
714 const char *t_strarray_join(const char *const *arr, const char *separator) |
8d3736bf4a90
lib: Move code from t_strarray_join() into a more generic p_strarray_join_n()
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
19552
diff
changeset
|
715 { |
8d3736bf4a90
lib: Move code from t_strarray_join() into a more generic p_strarray_join_n()
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
19552
diff
changeset
|
716 return p_strarray_join_n(unsafe_data_stack_pool, arr, |
8d3736bf4a90
lib: Move code from t_strarray_join() into a more generic p_strarray_join_n()
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
19552
diff
changeset
|
717 str_array_length(arr), separator); |
8d3736bf4a90
lib: Move code from t_strarray_join() into a more generic p_strarray_join_n()
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
19552
diff
changeset
|
718 } |
8d3736bf4a90
lib: Move code from t_strarray_join() into a more generic p_strarray_join_n()
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
19552
diff
changeset
|
719 |
6424
687efa138e77
Renamed strarray_length/remote() to str_array_length/remove()
Timo Sirainen <tss@iki.fi>
parents:
6423
diff
changeset
|
720 bool str_array_remove(const char **arr, const char *value) |
3615
0a885029543b
Added p_strsplit(), p_strsplit_spaces() and strarray_remove()
Timo Sirainen <tss@iki.fi>
parents:
3520
diff
changeset
|
721 { |
0a885029543b
Added p_strsplit(), p_strsplit_spaces() and strarray_remove()
Timo Sirainen <tss@iki.fi>
parents:
3520
diff
changeset
|
722 const char **dest; |
0a885029543b
Added p_strsplit(), p_strsplit_spaces() and strarray_remove()
Timo Sirainen <tss@iki.fi>
parents:
3520
diff
changeset
|
723 |
0a885029543b
Added p_strsplit(), p_strsplit_spaces() and strarray_remove()
Timo Sirainen <tss@iki.fi>
parents:
3520
diff
changeset
|
724 for (; *arr != NULL; arr++) { |
0a885029543b
Added p_strsplit(), p_strsplit_spaces() and strarray_remove()
Timo Sirainen <tss@iki.fi>
parents:
3520
diff
changeset
|
725 if (strcmp(*arr, value) == 0) { |
0a885029543b
Added p_strsplit(), p_strsplit_spaces() and strarray_remove()
Timo Sirainen <tss@iki.fi>
parents:
3520
diff
changeset
|
726 /* found it. now move the rest. */ |
0a885029543b
Added p_strsplit(), p_strsplit_spaces() and strarray_remove()
Timo Sirainen <tss@iki.fi>
parents:
3520
diff
changeset
|
727 for (dest = arr, arr++; *arr != NULL; arr++, dest++) |
0a885029543b
Added p_strsplit(), p_strsplit_spaces() and strarray_remove()
Timo Sirainen <tss@iki.fi>
parents:
3520
diff
changeset
|
728 *dest = *arr; |
0a885029543b
Added p_strsplit(), p_strsplit_spaces() and strarray_remove()
Timo Sirainen <tss@iki.fi>
parents:
3520
diff
changeset
|
729 *dest = NULL; |
0a885029543b
Added p_strsplit(), p_strsplit_spaces() and strarray_remove()
Timo Sirainen <tss@iki.fi>
parents:
3520
diff
changeset
|
730 return TRUE; |
0a885029543b
Added p_strsplit(), p_strsplit_spaces() and strarray_remove()
Timo Sirainen <tss@iki.fi>
parents:
3520
diff
changeset
|
731 } |
0a885029543b
Added p_strsplit(), p_strsplit_spaces() and strarray_remove()
Timo Sirainen <tss@iki.fi>
parents:
3520
diff
changeset
|
732 } |
0a885029543b
Added p_strsplit(), p_strsplit_spaces() and strarray_remove()
Timo Sirainen <tss@iki.fi>
parents:
3520
diff
changeset
|
733 return FALSE; |
0a885029543b
Added p_strsplit(), p_strsplit_spaces() and strarray_remove()
Timo Sirainen <tss@iki.fi>
parents:
3520
diff
changeset
|
734 } |
0a885029543b
Added p_strsplit(), p_strsplit_spaces() and strarray_remove()
Timo Sirainen <tss@iki.fi>
parents:
3520
diff
changeset
|
735 |
10412
a9eda7672869
lib-storage: Plugin hook API changed to support per-user plugins within a process.
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
736 bool str_array_find(const char *const *arr, const char *value) |
a9eda7672869
lib-storage: Plugin hook API changed to support per-user plugins within a process.
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
737 { |
a9eda7672869
lib-storage: Plugin hook API changed to support per-user plugins within a process.
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
738 for (; *arr != NULL; arr++) { |
a9eda7672869
lib-storage: Plugin hook API changed to support per-user plugins within a process.
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
739 if (strcmp(*arr, value) == 0) |
a9eda7672869
lib-storage: Plugin hook API changed to support per-user plugins within a process.
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
740 return TRUE; |
a9eda7672869
lib-storage: Plugin hook API changed to support per-user plugins within a process.
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
741 } |
a9eda7672869
lib-storage: Plugin hook API changed to support per-user plugins within a process.
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
742 return FALSE; |
a9eda7672869
lib-storage: Plugin hook API changed to support per-user plugins within a process.
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
743 } |
a9eda7672869
lib-storage: Plugin hook API changed to support per-user plugins within a process.
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
744 |
7915 | 745 bool str_array_icase_find(const char *const *arr, const char *value) |
746 { | |
747 for (; *arr != NULL; arr++) { | |
748 if (strcasecmp(*arr, value) == 0) | |
749 return TRUE; | |
750 } | |
751 return FALSE; | |
752 } | |
753 | |
10457 | 754 const char **p_strarray_dup(pool_t pool, const char *const *arr) |
755 { | |
756 unsigned int i; | |
757 const char **ret; | |
758 char *p; | |
759 size_t len, size = sizeof(const char *); | |
760 | |
21323
d223fad9767f
global: Make sure *_malloc() calculations won't cause integer overflows.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21322
diff
changeset
|
761 /* @UNSAFE: integer overflow checks are missing */ |
10457 | 762 for (i = 0; arr[i] != NULL; i++) |
763 size += sizeof(const char *) + strlen(arr[i]) + 1; | |
764 | |
765 ret = p_malloc(pool, size); | |
766 p = PTR_OFFSET(ret, sizeof(const char *) * (i + 1)); | |
767 for (i = 0; arr[i] != NULL; i++) { | |
768 len = strlen(arr[i]) + 1; | |
769 memcpy(p, arr[i], len); | |
770 ret[i] = p; | |
771 p += len; | |
772 } | |
773 i_assert(PTR_OFFSET(ret, size) == (void *)p); | |
774 return ret; | |
775 } | |
776 | |
807
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
777 const char *dec2str(uintmax_t number) |
0 | 778 { |
22030
ba2b3089a728
lib: Use t_malloc instead of t_malloc_no0
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
22029
diff
changeset
|
779 return dec2str_buf(t_malloc(MAX_INT_STRLEN), number); |
22029
5bb79b92618a
lib: Add dec2str_buf()
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21951
diff
changeset
|
780 } |
5bb79b92618a
lib: Add dec2str_buf()
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21951
diff
changeset
|
781 |
5bb79b92618a
lib: Add dec2str_buf()
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21951
diff
changeset
|
782 char *dec2str_buf(char buffer[STATIC_ARRAY MAX_INT_STRLEN], uintmax_t number) |
5bb79b92618a
lib: Add dec2str_buf()
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21951
diff
changeset
|
783 { |
807
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
784 int pos; |
0 | 785 |
807
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
786 pos = MAX_INT_STRLEN; |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
787 buffer[--pos] = '\0'; |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
788 do { |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
789 buffer[--pos] = (number % 10) + '0'; |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
790 number /= 10; |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
791 } while (number != 0 && pos >= 0); |
847
7f41a4b33975
t_strsplit() returns now const char **, which removes a few nasty casts.
Timo Sirainen <tss@iki.fi>
parents:
842
diff
changeset
|
792 |
807
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
793 i_assert(pos >= 0); |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
794 return buffer + pos; |
179
0ffecd4e7e1a
added dec2str() function and largest_t type, removed itoa() and ltoa()
Timo Sirainen <tss@iki.fi>
parents:
68
diff
changeset
|
795 } |
19791
5c4e9921b65f
lib: Added [pt]_array_const_string_join()
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
19790
diff
changeset
|
796 |
5c4e9921b65f
lib: Added [pt]_array_const_string_join()
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
19790
diff
changeset
|
797 char *p_array_const_string_join(pool_t pool, const ARRAY_TYPE(const_string) *arr, |
5c4e9921b65f
lib: Added [pt]_array_const_string_join()
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
19790
diff
changeset
|
798 const char *separator) |
5c4e9921b65f
lib: Added [pt]_array_const_string_join()
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
19790
diff
changeset
|
799 { |
5c4e9921b65f
lib: Added [pt]_array_const_string_join()
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
19790
diff
changeset
|
800 if (array_count(arr) == 0) |
5c4e9921b65f
lib: Added [pt]_array_const_string_join()
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
19790
diff
changeset
|
801 return ""; |
5c4e9921b65f
lib: Added [pt]_array_const_string_join()
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
19790
diff
changeset
|
802 return p_strarray_join_n(pool, array_idx(arr, 0), array_count(arr), separator); |
5c4e9921b65f
lib: Added [pt]_array_const_string_join()
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
19790
diff
changeset
|
803 } |