|
| 1 | +/* |
| 2 | + +----------------------------------------------------------------------+ |
| 3 | + | PHP Version 5 | |
| 4 | + +----------------------------------------------------------------------+ |
| 5 | + | Copyright (c) 1997-2014 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 | + | [email protected] so we can mail you a copy immediately. | |
| 14 | + +----------------------------------------------------------------------+ |
| 15 | + | Author: Sascha Schumann <[email protected]> | |
| 16 | + | Xinchen Hui <[email protected]> | |
| 17 | + +----------------------------------------------------------------------+ |
| 18 | + */ |
| 19 | + |
| 20 | +/* $Id$ */ |
| 21 | + |
| 22 | +#ifndef PHP_SMART_STRING_H |
| 23 | +#define PHP_SMART_STRING_H |
| 24 | + |
| 25 | +#include "php_smart_string_public.h" |
| 26 | + |
| 27 | +#include <stdlib.h> |
| 28 | +#ifndef SMART_STR_USE_REALLOC |
| 29 | +#include <zend.h> |
| 30 | +#endif |
| 31 | + |
| 32 | +#define smart_string_0(x) do { \ |
| 33 | + if ((x)->c) { \ |
| 34 | + (x)->c[(x)->len] = '\0'; \ |
| 35 | + } \ |
| 36 | +} while (0) |
| 37 | + |
| 38 | +#ifndef SMART_STRING_PREALLOC |
| 39 | +#define SMART_STRING_PREALLOC 128 |
| 40 | +#endif |
| 41 | + |
| 42 | +#ifndef SMART_STRING_START_SIZE |
| 43 | +#define SMART_STRING_START_SIZE 78 |
| 44 | +#endif |
| 45 | + |
| 46 | +#ifdef SMART_STRING_USE_REALLOC |
| 47 | +#define SMART_STRING_REALLOC(a,b,c) realloc((a),(b)) |
| 48 | +#else |
| 49 | +#define SMART_STRING_REALLOC(a,b,c) perealloc((a),(b),(c)) |
| 50 | +#endif |
| 51 | + |
| 52 | +#define SMART_STRING_DO_REALLOC(d, what) \ |
| 53 | + (d)->c = SMART_STRING_REALLOC((d)->c, (d)->a + 1, (what)) |
| 54 | + |
| 55 | +#define smart_string_alloc4(d, n, what, newlen) do { \ |
| 56 | + if (!(d)->c) { \ |
| 57 | + (d)->len = 0; \ |
| 58 | + newlen = (n); \ |
| 59 | + (d)->a = newlen < SMART_STRING_START_SIZE \ |
| 60 | + ? SMART_STRING_START_SIZE \ |
| 61 | + : newlen + SMART_STRING_PREALLOC; \ |
| 62 | + SMART_STRING_DO_REALLOC(d, what); \ |
| 63 | + } else { \ |
| 64 | + newlen = (d)->len + (n); \ |
| 65 | + if (newlen >= (d)->a) { \ |
| 66 | + (d)->a = newlen + SMART_STRING_PREALLOC; \ |
| 67 | + SMART_STRING_DO_REALLOC(d, what); \ |
| 68 | + } \ |
| 69 | + } \ |
| 70 | +} while (0) |
| 71 | + |
| 72 | +#define smart_string_alloc(d, n, what) \ |
| 73 | + smart_string_alloc4((d), (n), (what), newlen) |
| 74 | + |
| 75 | +/* wrapper */ |
| 76 | + |
| 77 | +#define smart_string_appends_ex(dest, src, what) \ |
| 78 | + smart_string_appendl_ex((dest), (src), strlen(src), (what)) |
| 79 | +#define smart_string_appends(dest, src) \ |
| 80 | + smart_string_appendl((dest), (src), strlen(src)) |
| 81 | + |
| 82 | +#define smart_string_appendc(dest, c) \ |
| 83 | + smart_string_appendc_ex((dest), (c), 0) |
| 84 | +#define smart_string_free(s) \ |
| 85 | + smart_string_free_ex((s), 0) |
| 86 | +#define smart_string_appendl(dest, src, len) \ |
| 87 | + smart_string_appendl_ex((dest), (src), (len), 0) |
| 88 | +#define smart_string_append(dest, src) \ |
| 89 | + smart_string_append_ex((dest), (src), 0) |
| 90 | +#define smart_string_append_long(dest, val) \ |
| 91 | + smart_string_append_long_ex((dest), (val), 0) |
| 92 | +#define smart_string_append_off_t(dest, val) \ |
| 93 | + smart_string_append_off_t_ex((dest), (val), 0) |
| 94 | +#define smart_string_append_unsigned(dest, val) \ |
| 95 | + smart_string_append_unsigned_ex((dest), (val), 0) |
| 96 | + |
| 97 | +#define smart_string_appendc_ex(dest, ch, what) do { \ |
| 98 | + register size_t __nl; \ |
| 99 | + smart_string_alloc4((dest), 1, (what), __nl); \ |
| 100 | + (dest)->len = __nl; \ |
| 101 | + ((unsigned char *) (dest)->c)[(dest)->len - 1] = (ch); \ |
| 102 | +} while (0) |
| 103 | + |
| 104 | +#define smart_string_free_ex(s, what) do { \ |
| 105 | + smart_string *__s = (smart_string *) (s); \ |
| 106 | + if (__s->c) { \ |
| 107 | + pefree(__s->c, what); \ |
| 108 | + __s->c = NULL; \ |
| 109 | + } \ |
| 110 | + __s->a = __s->len = 0; \ |
| 111 | +} while (0) |
| 112 | + |
| 113 | +#define smart_string_appendl_ex(dest, src, nlen, what) do { \ |
| 114 | + register size_t __nl; \ |
| 115 | + smart_string *__dest = (smart_string *) (dest); \ |
| 116 | + \ |
| 117 | + smart_string_alloc4(__dest, (nlen), (what), __nl); \ |
| 118 | + memcpy(__dest->c + __dest->len, (src), (nlen)); \ |
| 119 | + __dest->len = __nl; \ |
| 120 | +} while (0) |
| 121 | + |
| 122 | +/* input: buf points to the END of the buffer */ |
| 123 | +#define smart_string_print_unsigned4(buf, num, vartype, result) do { \ |
| 124 | + char *__p = (buf); \ |
| 125 | + vartype __num = (num); \ |
| 126 | + *__p = '\0'; \ |
| 127 | + do { \ |
| 128 | + *--__p = (char) (__num % 10) + '0'; \ |
| 129 | + __num /= 10; \ |
| 130 | + } while (__num > 0); \ |
| 131 | + result = __p; \ |
| 132 | +} while (0) |
| 133 | + |
| 134 | +/* buf points to the END of the buffer */ |
| 135 | +#define smart_string_print_long4(buf, num, vartype, result) do { \ |
| 136 | + if (num < 0) { \ |
| 137 | + /* this might cause problems when dealing with LONG_MIN \ |
| 138 | + and machines which don't support long long. Works \ |
| 139 | + flawlessly on 32bit x86 */ \ |
| 140 | + smart_string_print_unsigned4((buf), -(num), vartype, (result)); \ |
| 141 | + *--(result) = '-'; \ |
| 142 | + } else { \ |
| 143 | + smart_string_print_unsigned4((buf), (num), vartype, (result)); \ |
| 144 | + } \ |
| 145 | +} while (0) |
| 146 | + |
| 147 | +/* |
| 148 | + * these could be replaced using a braced-group inside an expression |
| 149 | + * for GCC compatible compilers, e.g. |
| 150 | + * |
| 151 | + * #define f(..) ({char *r;..;__r;}) |
| 152 | + */ |
| 153 | + |
| 154 | +static inline char *smart_string_print_long(char *buf, long num) { |
| 155 | + char *r; |
| 156 | + smart_string_print_long4(buf, num, unsigned long, r); |
| 157 | + return r; |
| 158 | +} |
| 159 | + |
| 160 | +static inline char *smart_string_print_unsigned(char *buf, long num) { |
| 161 | + char *r; |
| 162 | + smart_string_print_unsigned4(buf, num, unsigned long, r); |
| 163 | + return r; |
| 164 | +} |
| 165 | + |
| 166 | +#define smart_string_append_generic_ex(dest, num, type, vartype, func) do { \ |
| 167 | + char __b[32]; \ |
| 168 | + char *__t; \ |
| 169 | + smart_string_print##func##4 (__b + sizeof(__b) - 1, (num), vartype, __t); \ |
| 170 | + smart_string_appendl_ex((dest), __t, __b + sizeof(__b) - 1 - __t, (type)); \ |
| 171 | +} while (0) |
| 172 | + |
| 173 | +#define smart_string_append_unsigned_ex(dest, num, type) \ |
| 174 | + smart_string_append_generic_ex((dest), (num), (type), unsigned long, _unsigned) |
| 175 | + |
| 176 | +#define smart_string_append_long_ex(dest, num, type) \ |
| 177 | + smart_string_append_generic_ex((dest), (num), (type), unsigned long, _long) |
| 178 | + |
| 179 | +#define smart_string_append_off_t_ex(dest, num, type) \ |
| 180 | + smart_string_append_generic_ex((dest), (num), (type), off_t, _long) |
| 181 | + |
| 182 | +#define smart_string_append_ex(dest, src, what) \ |
| 183 | + smart_string_appendl_ex((dest), ((smart_string *)(src))->c, \ |
| 184 | + ((smart_string *)(src))->len, (what)); |
| 185 | + |
| 186 | + |
| 187 | +#define smart_string_setl(dest, src, nlen) do { \ |
| 188 | + (dest)->len = (nlen); \ |
| 189 | + (dest)->a = (nlen) + 1; \ |
| 190 | + (dest)->c = (char *) (src); \ |
| 191 | +} while (0) |
| 192 | + |
| 193 | +#define smart_string_sets(dest, src) \ |
| 194 | + smart_string_setl((dest), (src), strlen(src)); |
| 195 | + |
| 196 | +#endif |
0 commit comments