00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #ifndef PHP_SMART_STR_H
00020 #define PHP_SMART_STR_H
00021
00022 #include "php_smart_str_public.h"
00023
00024 #include <stdlib.h>
00025 #ifndef SMART_STR_USE_REALLOC
00026 #include <zend.h>
00027 #endif
00028
00029 #define smart_str_0(x) do { \
00030 if ((x)->c) { \
00031 (x)->c[(x)->len] = '\0'; \
00032 } \
00033 } while (0)
00034
00035 #ifndef SMART_STR_PREALLOC
00036 #define SMART_STR_PREALLOC 128
00037 #endif
00038
00039 #ifndef SMART_STR_START_SIZE
00040 #define SMART_STR_START_SIZE 78
00041 #endif
00042
00043 #ifdef SMART_STR_USE_REALLOC
00044 #define SMART_STR_REALLOC(a,b,c) realloc((a),(b))
00045 #else
00046 #define SMART_STR_REALLOC(a,b,c) perealloc((a),(b),(c))
00047 #endif
00048
00049 #define SMART_STR_DO_REALLOC(d, what) \
00050 (d)->c = SMART_STR_REALLOC((d)->c, (d)->a + 1, (what))
00051
00052 #define smart_str_alloc4(d, n, what, newlen) do { \
00053 if (!(d)->c) { \
00054 (d)->len = 0; \
00055 newlen = (n); \
00056 (d)->a = newlen < SMART_STR_START_SIZE \
00057 ? SMART_STR_START_SIZE \
00058 : newlen + SMART_STR_PREALLOC; \
00059 SMART_STR_DO_REALLOC(d, what); \
00060 } else { \
00061 newlen = (d)->len + (n); \
00062 if (newlen >= (d)->a) { \
00063 (d)->a = newlen + SMART_STR_PREALLOC; \
00064 SMART_STR_DO_REALLOC(d, what); \
00065 } \
00066 } \
00067 } while (0)
00068
00069 #define smart_str_alloc(d, n, what) \
00070 smart_str_alloc4((d), (n), (what), newlen)
00071
00072
00073
00074 #define smart_str_appends_ex(dest, src, what) \
00075 smart_str_appendl_ex((dest), (src), strlen(src), (what))
00076 #define smart_str_appends(dest, src) \
00077 smart_str_appendl((dest), (src), strlen(src))
00078
00079 #define smart_str_appendc(dest, c) \
00080 smart_str_appendc_ex((dest), (c), 0)
00081 #define smart_str_free(s) \
00082 smart_str_free_ex((s), 0)
00083 #define smart_str_appendl(dest, src, len) \
00084 smart_str_appendl_ex((dest), (src), (len), 0)
00085 #define smart_str_append(dest, src) \
00086 smart_str_append_ex((dest), (src), 0)
00087 #define smart_str_append_long(dest, val) \
00088 smart_str_append_long_ex((dest), (val), 0)
00089 #define smart_str_append_off_t(dest, val) \
00090 smart_str_append_off_t_ex((dest), (val), 0)
00091 #define smart_str_append_unsigned(dest, val) \
00092 smart_str_append_unsigned_ex((dest), (val), 0)
00093
00094 #define smart_str_appendc_ex(dest, ch, what) do { \
00095 register size_t __nl; \
00096 smart_str_alloc4((dest), 1, (what), __nl); \
00097 (dest)->len = __nl; \
00098 ((unsigned char *) (dest)->c)[(dest)->len - 1] = (ch); \
00099 } while (0)
00100
00101 #define smart_str_free_ex(s, what) do { \
00102 smart_str *__s = (smart_str *) (s); \
00103 if (__s->c) { \
00104 pefree(__s->c, what); \
00105 __s->c = NULL; \
00106 } \
00107 __s->a = __s->len = 0; \
00108 } while (0)
00109
00110 #define smart_str_appendl_ex(dest, src, nlen, what) do { \
00111 register size_t __nl; \
00112 smart_str *__dest = (smart_str *) (dest); \
00113 \
00114 smart_str_alloc4(__dest, (nlen), (what), __nl); \
00115 memcpy(__dest->c + __dest->len, (src), (nlen)); \
00116 __dest->len = __nl; \
00117 } while (0)
00118
00119
00120 #define smart_str_print_unsigned4(buf, num, vartype, result) do { \
00121 char *__p = (buf); \
00122 vartype __num = (num); \
00123 *__p = '\0'; \
00124 do { \
00125 *--__p = (char) (__num % 10) + '0'; \
00126 __num /= 10; \
00127 } while (__num > 0); \
00128 result = __p; \
00129 } while (0)
00130
00131
00132 #define smart_str_print_long4(buf, num, vartype, result) do { \
00133 if (num < 0) { \
00134
00135
00136 \
00137 smart_str_print_unsigned4((buf), -(num), vartype, (result)); \
00138 *--(result) = '-'; \
00139 } else { \
00140 smart_str_print_unsigned4((buf), (num), vartype, (result)); \
00141 } \
00142 } while (0)
00143
00144
00145
00146
00147
00148
00149
00150
00151 static inline char *smart_str_print_long(char *buf, long num) {
00152 char *r;
00153 smart_str_print_long4(buf, num, unsigned long, r);
00154 return r;
00155 }
00156
00157 static inline char *smart_str_print_unsigned(char *buf, long num) {
00158 char *r;
00159 smart_str_print_unsigned4(buf, num, unsigned long, r);
00160 return r;
00161 }
00162
00163 #define smart_str_append_generic_ex(dest, num, type, vartype, func) do { \
00164 char __b[32]; \
00165 char *__t; \
00166 smart_str_print##func##4 (__b + sizeof(__b) - 1, (num), vartype, __t); \
00167 smart_str_appendl_ex((dest), __t, __b + sizeof(__b) - 1 - __t, (type)); \
00168 } while (0)
00169
00170 #define smart_str_append_unsigned_ex(dest, num, type) \
00171 smart_str_append_generic_ex((dest), (num), (type), unsigned long, _unsigned)
00172
00173 #define smart_str_append_long_ex(dest, num, type) \
00174 smart_str_append_generic_ex((dest), (num), (type), unsigned long, _long)
00175
00176 #define smart_str_append_off_t_ex(dest, num, type) \
00177 smart_str_append_generic_ex((dest), (num), (type), off_t, _long)
00178
00179 #define smart_str_append_ex(dest, src, what) \
00180 smart_str_appendl_ex((dest), ((smart_str *)(src))->c, \
00181 ((smart_str *)(src))->len, (what));
00182
00183
00184 #define smart_str_setl(dest, src, nlen) do { \
00185 (dest)->len = (nlen); \
00186 (dest)->a = (nlen) + 1; \
00187 (dest)->c = (char *) (src); \
00188 } while (0)
00189
00190 #define smart_str_sets(dest, src) \
00191 smart_str_setl((dest), (src), strlen(src));
00192
00193 #endif