1 : /*
2 : +----------------------------------------------------------------------+
3 : | PHP Version 6 |
4 : +----------------------------------------------------------------------+
5 : | Copyright (c) 1997-2009 The PHP Group |
6 : +----------------------------------------------------------------------+
7 : | This source file is subject to version 3.01 of the PHP license, |
8 : | that is bundled with this package in the file LICENSE, and is |
9 : | available through the world-wide-web at the following url: |
10 : | http://www.php.net/license/3_01.txt |
11 : | If you did not receive a copy of the PHP license and are unable to |
12 : | obtain it through the world-wide-web, please send a note to |
13 : | license@php.net so we can mail you a copy immediately. |
14 : +----------------------------------------------------------------------+
15 : | Author: Sascha Schumann <sascha@schumann.cx> |
16 : +----------------------------------------------------------------------+
17 : */
18 :
19 : /* $Id: php_smart_str.h 276986 2009-03-10 23:40:06Z helly $ */
20 :
21 : #ifndef PHP_SMART_STR_H
22 : #define PHP_SMART_STR_H
23 :
24 : #include "php_smart_str_public.h"
25 :
26 : #include <stdlib.h>
27 : #ifndef SMART_STR_USE_REALLOC
28 : #include <zend.h>
29 : #endif
30 :
31 : /* It's safe to write to "one behind the length" as we allocate two extra bytes
32 : * to allow for Unicode trailers */
33 : #define smart_str_0(x) do { \
34 : if ((x)->c) { \
35 : (x)->c[(x)->len] = '\0'; \
36 : (x)->c[(x)->len + 1] = '\0'; \
37 : } \
38 : } while (0)
39 :
40 : #ifndef SMART_STR_PREALLOC
41 : #define SMART_STR_PREALLOC 128
42 : #endif
43 :
44 : #ifndef SMART_STR_START_SIZE
45 : #define SMART_STR_START_SIZE 78
46 : #endif
47 :
48 : #ifdef SMART_STR_USE_REALLOC
49 : #define SMART_STR_REALLOC(a,b,c) realloc((a),(b))
50 : #else
51 : #define SMART_STR_REALLOC(a,b,c) perealloc((a),(b),(c))
52 : #endif
53 :
54 : #define SMART_STR_DO_REALLOC(d, what) \
55 : (d)->c = SMART_STR_REALLOC((d)->c, (d)->a + 2, (what))
56 :
57 : #define smart_str_alloc4(d, n, what, newlen) do { \
58 : if (!(d)->c) { \
59 : (d)->len = 0; \
60 : newlen = (n); \
61 : (d)->a = newlen < SMART_STR_START_SIZE \
62 : ? SMART_STR_START_SIZE \
63 : : newlen + SMART_STR_PREALLOC; \
64 : SMART_STR_DO_REALLOC(d, what); \
65 : } else { \
66 : newlen = (d)->len + (n); \
67 : if (newlen >= (d)->a) { \
68 : (d)->a = newlen + SMART_STR_PREALLOC; \
69 : SMART_STR_DO_REALLOC(d, what); \
70 : } \
71 : } \
72 : } while (0)
73 :
74 : #define smart_str_alloc(d, n, what) \
75 : smart_str_alloc4((d), (n), (what), newlen)
76 :
77 : /* wrapper */
78 :
79 : #define smart_str_appends_ex(dest, src, what) \
80 : smart_str_appendl_ex((dest), (src), strlen(src), (what))
81 : #define smart_str_appends(dest, src) \
82 : smart_str_appendl((dest), (src), strlen(src))
83 :
84 : /* normal character appending */
85 : #define smart_str_appendc(dest, c) \
86 : smart_str_appendc_ex((dest), (c), 0)
87 :
88 : /* appending of a single UTF-16 code unit (2 byte)*/
89 : #define smart_str_append2c(dest, c) do { \
90 : smart_str_appendc_ex((dest), (c&0xFF), 0); \
91 : smart_str_appendc_ex((dest), (c&0xFF00 ? c>>8 : '\0'), 0); \
92 : } while (0)
93 :
94 : #define smart_str_free(s) \
95 : smart_str_free_ex((s), 0)
96 : #define smart_str_appendl(dest, src, len) \
97 : smart_str_appendl_ex((dest), (src), (len), 0)
98 : #define smart_str_append(dest, src) \
99 : smart_str_append_ex((dest), (src), 0)
100 : #define smart_str_append_long(dest, val) \
101 : smart_str_append_long_ex((dest), (val), 0)
102 : #define smart_str_append_off_t(dest, val) \
103 : smart_str_append_off_t_ex((dest), (val), 0)
104 : #define smart_str_append_unsigned(dest, val) \
105 : smart_str_append_unsigned_ex((dest), (val), 0)
106 :
107 : #define smart_str_appendc_ex(dest, ch, what) do { \
108 : register size_t __nl; \
109 : smart_str_alloc4((dest), 1, (what), __nl); \
110 : (dest)->len = __nl; \
111 : ((unsigned char *) (dest)->c)[(dest)->len - 1] = (ch); \
112 : } while (0)
113 :
114 : #define smart_str_free_ex(s, what) do { \
115 : smart_str *__s = (smart_str *) (s); \
116 : if (__s->c) { \
117 : pefree(__s->c, what); \
118 : __s->c = NULL; \
119 : } \
120 : __s->a = __s->len = 0; \
121 : } while (0)
122 :
123 : #define smart_str_appendl_ex(dest, src, nlen, what) do { \
124 : register size_t __nl; \
125 : smart_str *__dest = (smart_str *) (dest); \
126 : \
127 : smart_str_alloc4(__dest, (nlen), (what), __nl); \
128 : memcpy(__dest->c + __dest->len, (src), (nlen)); \
129 : __dest->len = __nl; \
130 : } while (0)
131 :
132 : /* input: buf points to the END of the buffer */
133 : #define smart_str_print_unsigned4(buf, num, vartype, result) do { \
134 : char *__p = (buf); \
135 : vartype __num = (num); \
136 : *__p = '\0'; \
137 : do { \
138 : *--__p = (char) (__num % 10) + '0'; \
139 : __num /= 10; \
140 : } while (__num > 0); \
141 : result = __p; \
142 : } while (0)
143 :
144 : /* buf points to the END of the buffer */
145 : #define smart_str_print_long4(buf, num, vartype, result) do { \
146 : if (num < 0) { \
147 : /* this might cause problems when dealing with LONG_MIN \
148 : and machines which don't support long long. Works \
149 : flawlessly on 32bit x86 */ \
150 : smart_str_print_unsigned4((buf), -(num), vartype, (result)); \
151 : *--(result) = '-'; \
152 : } else { \
153 : smart_str_print_unsigned4((buf), (num), vartype, (result)); \
154 : } \
155 : } while (0)
156 :
157 : /*
158 : * these could be replaced using a braced-group inside an expression
159 : * for GCC compatible compilers, e.g.
160 : *
161 : * #define f(..) ({char *r;..;__r;})
162 : */
163 :
164 29011 : static inline char *smart_str_print_long(char *buf, long num) {
165 : char *r;
166 29011 : smart_str_print_long4(buf, num, unsigned long, r);
167 29011 : return r;
168 : }
169 :
170 0 : static inline char *smart_str_print_unsigned(char *buf, long num) {
171 : char *r;
172 0 : smart_str_print_unsigned4(buf, num, unsigned long, r);
173 0 : return r;
174 : }
175 :
176 : #define smart_str_append_generic_ex(dest, num, type, vartype, func) do { \
177 : char __b[32]; \
178 : char *__t; \
179 : smart_str_print##func##4 (__b + sizeof(__b) - 1, (num), vartype, __t); \
180 : smart_str_appendl_ex((dest), __t, __b + sizeof(__b) - 1 - __t, (type)); \
181 : } while (0)
182 :
183 : #define smart_str_append_unsigned_ex(dest, num, type) \
184 : smart_str_append_generic_ex((dest), (num), (type), unsigned long, _unsigned)
185 :
186 : #define smart_str_append_long_ex(dest, num, type) \
187 : smart_str_append_generic_ex((dest), (num), (type), unsigned long, _long)
188 :
189 : #define smart_str_append_off_t_ex(dest, num, type) \
190 : smart_str_append_generic_ex((dest), (num), (type), off_t, _long)
191 :
192 : #define smart_str_append_ex(dest, src, what) \
193 : smart_str_appendl_ex((dest), ((smart_str *)(src))->c, \
194 : ((smart_str *)(src))->len, (what));
195 :
196 :
197 : #define smart_str_setl(dest, src, nlen) do { \
198 : (dest)->len = (nlen); \
199 : (dest)->a = (nlen) + 1; \
200 : (dest)->c = (char *) (src); \
201 : } while (0)
202 :
203 : #define smart_str_sets(dest, src) \
204 : smart_str_setl((dest), (src), strlen(src));
205 :
206 : #endif
|