Skip to content

Commit 38f72ce

Browse files
committed
Sync smart_string implementation with smart_str
Switch to using inline functions instead of macros, etc.
1 parent abb91ee commit 38f72ce

File tree

2 files changed

+85
-96
lines changed

2 files changed

+85
-96
lines changed

Zend/zend_smart_string.h

Lines changed: 85 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,13 @@
2222
#ifndef PHP_SMART_STRING_H
2323
#define PHP_SMART_STRING_H
2424

25-
#include "zend_smart_string.h"
25+
#include "zend_smart_string_public.h"
2626

2727
#include <stdlib.h>
2828
#ifndef SMART_STR_USE_REALLOC
2929
#include <zend.h>
3030
#endif
3131

32-
#define smart_string_0(x) do { \
33-
if ((x)->c) { \
34-
(x)->c[(x)->len] = '\0'; \
35-
} \
36-
} while (0)
37-
3832
#ifndef SMART_STRING_PREALLOC
3933
#define SMART_STRING_PREALLOC 128
4034
#endif
@@ -52,98 +46,94 @@
5246
#define SMART_STRING_DO_REALLOC(d, what) \
5347
(d)->c = SMART_STRING_REALLOC((d)->c, (d)->a + 1, (what))
5448

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-
if(UNEXPECTED((size_t)n > SIZE_MAX - (d)->len)) { \
65-
zend_error(E_ERROR, "String size overflow"); \
66-
} \
67-
newlen = (d)->len + (n); \
68-
if (newlen >= (d)->a) { \
69-
(d)->a = newlen + SMART_STRING_PREALLOC; \
70-
SMART_STRING_DO_REALLOC(d, what); \
71-
} \
72-
} \
73-
} while (0)
74-
75-
#define smart_string_alloc(d, n, what) \
76-
smart_string_alloc4((d), (n), (what), newlen)
77-
7849
/* wrapper */
7950

80-
#define smart_string_appends_ex(dest, src, what) \
81-
smart_string_appendl_ex((dest), (src), strlen(src), (what))
82-
#define smart_string_appends(dest, src) \
83-
smart_string_appendl((dest), (src), strlen(src))
51+
#define smart_string_appends_ex(str, src, what) \
52+
smart_string_appendl_ex((str), (src), strlen(src), (what))
53+
#define smart_string_appends(str, src) \
54+
smart_string_appendl((str), (src), strlen(src))
55+
#define smart_string_append_ex(str, src, what) \
56+
smart_string_appendl_ex((str), ((smart_string *)(src))->c, \
57+
((smart_string *)(src))->len, (what));
58+
#define smart_string_sets(str, src) \
59+
smart_string_setl((str), (src), strlen(src));
8460

85-
#define smart_string_appendc(dest, c) \
86-
smart_string_appendc_ex((dest), (c), 0)
61+
#define smart_string_appendc(str, c) \
62+
smart_string_appendc_ex((str), (c), 0)
8763
#define smart_string_free(s) \
8864
smart_string_free_ex((s), 0)
89-
#define smart_string_appendl(dest, src, len) \
90-
smart_string_appendl_ex((dest), (src), (len), 0)
91-
#define smart_string_append(dest, src) \
92-
smart_string_append_ex((dest), (src), 0)
93-
#define smart_string_append_long(dest, val) \
94-
smart_string_append_long_ex((dest), (val), 0)
95-
#define smart_string_append_unsigned(dest, val) \
96-
smart_string_append_unsigned_ex((dest), (val), 0)
97-
98-
#define smart_string_appendc_ex(dest, ch, what) do { \
99-
size_t __nl; \
100-
smart_string_alloc4((dest), 1, (what), __nl); \
101-
(dest)->len = __nl; \
102-
((unsigned char *) (dest)->c)[(dest)->len - 1] = (ch); \
103-
} while (0)
104-
105-
#define smart_string_free_ex(s, what) do { \
106-
smart_string *__s = (smart_string *) (s); \
107-
if (__s->c) { \
108-
pefree(__s->c, what); \
109-
__s->c = NULL; \
110-
} \
111-
__s->a = __s->len = 0; \
112-
} while (0)
113-
114-
#define smart_string_appendl_ex(dest, src, nlen, what) do { \
115-
size_t __nl; \
116-
smart_string *__dest = (smart_string *) (dest); \
117-
\
118-
smart_string_alloc4(__dest, (nlen), (what), __nl); \
119-
memcpy(__dest->c + __dest->len, (src), (nlen)); \
120-
__dest->len = __nl; \
121-
} while (0)
122-
123-
#define smart_string_append_generic_ex(dest, num, type, vartype, func) do { \
124-
char __b[32]; \
125-
char *__t = zend_print##func##_to_buf(__b + sizeof(__b) - 1, (num)); \
126-
smart_string_appendl_ex((dest), __t, __b + sizeof(__b) - 1 - __t, (type)); \
127-
} while (0)
128-
129-
#define smart_string_append_unsigned_ex(dest, num, type) \
130-
smart_string_append_generic_ex((dest), (num), (type), zend_ulong, _ulong)
131-
132-
#define smart_string_append_long_ex(dest, num, type) \
133-
smart_string_append_generic_ex((dest), (num), (type), zend_ulong, _long)
134-
135-
#define smart_string_append_ex(dest, src, what) \
136-
smart_string_appendl_ex((dest), ((smart_string *)(src))->c, \
137-
((smart_string *)(src))->len, (what));
138-
139-
140-
#define smart_string_setl(dest, src, nlen) do { \
141-
(dest)->len = (nlen); \
142-
(dest)->a = (nlen) + 1; \
143-
(dest)->c = (char *) (src); \
144-
} while (0)
145-
146-
#define smart_string_sets(dest, src) \
147-
smart_string_setl((dest), (src), strlen(src));
65+
#define smart_string_appendl(str, src, len) \
66+
smart_string_appendl_ex((str), (src), (len), 0)
67+
#define smart_string_append(str, src) \
68+
smart_string_append_ex((str), (src), 0)
69+
#define smart_string_append_long(str, val) \
70+
smart_string_append_long_ex((str), (val), 0)
71+
#define smart_string_append_unsigned(str, val) \
72+
smart_string_append_unsigned_ex((str), (val), 0)
73+
74+
static zend_always_inline size_t smart_string_alloc(smart_string *str, size_t len, zend_bool persistent) {
75+
if (!str->c) {
76+
str->len = 0;
77+
str->a = len < SMART_STRING_START_SIZE
78+
? SMART_STRING_START_SIZE
79+
: len + SMART_STRING_PREALLOC;
80+
SMART_STRING_DO_REALLOC(str, persistent);
81+
return len;
82+
} else {
83+
if (UNEXPECTED((size_t) len > SIZE_MAX - str->len)) {
84+
zend_error(E_ERROR, "String size overflow");
85+
}
86+
len += str->len;
87+
if (UNEXPECTED(len >= str->a)) {
88+
str->a = len + SMART_STRING_PREALLOC;
89+
SMART_STRING_DO_REALLOC(str, persistent);
90+
}
91+
}
92+
return len;
93+
}
94+
95+
static zend_always_inline void smart_string_free_ex(smart_string *str, zend_bool persistent) {
96+
if (str->c) {
97+
pefree(str->c, persistent);
98+
str->c = NULL;
99+
}
100+
str->a = str->len = 0;
101+
}
102+
103+
static zend_always_inline void smart_string_0(smart_string *str) {
104+
if (str->c) {
105+
str->c[str->len] = '\0';
106+
}
107+
}
108+
109+
static zend_always_inline void smart_string_appendc_ex(smart_string *dest, char ch, zend_bool persistent) {
110+
dest->len = smart_string_alloc(dest, 1, persistent);
111+
dest->c[dest->len - 1] = ch;
112+
}
113+
114+
static zend_always_inline void smart_string_appendl_ex(smart_string *dest, const char *str, size_t len, zend_bool persistent) {
115+
size_t new_len = smart_string_alloc(dest, len, persistent);
116+
memcpy(dest->c + dest->len, str, len);
117+
dest->len = new_len;
118+
119+
}
120+
121+
static zend_always_inline void smart_string_append_long_ex(smart_string *dest, zend_long num, zend_bool persistent) {
122+
char buf[32];
123+
char *result = zend_print_long_to_buf(buf + sizeof(buf) - 1, num);
124+
smart_string_appendl_ex(dest, result, buf + sizeof(buf) - 1 - result, persistent);
125+
}
126+
127+
static zend_always_inline void smart_string_append_unsigned_ex(smart_string *dest, zend_ulong num, zend_bool persistent) {
128+
char buf[32];
129+
char *result = zend_print_ulong_to_buf(buf + sizeof(buf) - 1, num);
130+
smart_string_appendl_ex(dest, result, buf + sizeof(buf) - 1 - result, persistent);
131+
}
132+
133+
static zend_always_inline void smart_string_setl(smart_string *dest, char *src, size_t len) {
134+
dest->len = len;
135+
dest->a = len + 1;
136+
dest->c = src;
137+
}
148138

149139
#endif

main/spprintf.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,6 @@
138138
} while (0);
139139

140140
#define PAD_CHAR(xbuf, ch, count, is_char) do { \
141-
size_t newlen; \
142141
if ((is_char)) { \
143142
smart_string_alloc(((smart_string *)(xbuf)), (count), 0); \
144143
memset(((smart_string *)(xbuf))->c + ((smart_string *)(xbuf))->len, (ch), (count)); \

0 commit comments

Comments
 (0)