#include <zend.h>
#endif
+/* It's safe to write to "one behind the length" as we allocate two extra bytes
+ * to allow for Unicode trailers */
#define smart_str_0(x) do { \
if ((x)->c) { \
(x)->c[(x)->len] = '\0'; \
+ (x)->c[(x)->len + 1] = '\0'; \
} \
} while (0)
#endif
#define SMART_STR_DO_REALLOC(d, what) \
- (d)->c = SMART_STR_REALLOC((d)->c, (d)->a + 1, (what))
+ (d)->c = SMART_STR_REALLOC((d)->c, (d)->a + 2, (what))
#define smart_str_alloc4(d, n, what, newlen) do { \
if (!(d)->c) { \
#define smart_str_appends(dest, src) \
smart_str_appendl((dest), (src), strlen(src))
-/* normall character appending */
+/* normal character appending */
#define smart_str_appendc(dest, c) \
smart_str_appendc_ex((dest), (c), 0)
/* appending of a single UTF-16 code unit (2 byte)*/
-#define smart_str_append2c(dest, c) while (0) { \
+#define smart_str_append2c(dest, c) do { \
smart_str_appendc_ex((dest), (c&0xFF), 0); \
smart_str_appendc_ex((dest), (c&0xFF00 ? c>>8 : '\0'), 0); \
-}
+} while (0)
#define smart_str_free(s) \
smart_str_free_ex((s), 0)
} \
} while (0)
-#define INS_STRING(unicode, s_uni, xbuf, s, slen) \
+#define INS_STRING(unicode, s_unicode, xbuf, s, s_len) \
do { \
if (unicode) { \
- size_t newlen, p, sz = 2*(slen); \
+ size_t newlen, p, sz = 2*(s_len); \
smart_str_alloc(xbuf, (sz), 0); \
- if (s_uni) { \
+ if (s_unicode) { \
memcpy(xbuf->c + xbuf->len, s, (sz)); \
} else { \
- p = (slen); \
+ p = (s_len); \
while(p--) { \
- smart_str_append2c(xbuf, *s++); \
+ smart_str_append2c(xbuf, *s); \
+ *s++; \
} \
} \
- xbuf->len += (sz); \
} else { \
- smart_str_appendl(xbuf, s, slen); \
+ smart_str_appendl(xbuf, s, s_len); \
} \
} while (0)
} else { \
smart_str_alloc(xbuf, sz, 0); \
memset(xbuf->c + xbuf->len, ch, sz); \
+ xbuf->len += sz; \
} \
- xbuf->len += sz; \
} \
} while (0)
if (max_len && xbuf.len > max_len) {
xbuf.len = max_len;
}
- smart_str_appendc(&xbuf, '\0'); /* we need \0\0 as termination */
smart_str_0(&xbuf);
*pbuf = xbuf.c;