xref: /php-src/Zend/zend_smart_str.h (revision 998bce11)
1 /*
2    +----------------------------------------------------------------------+
3    | Copyright (c) The PHP Group                                          |
4    +----------------------------------------------------------------------+
5    | This source file is subject to version 3.01 of the PHP license,      |
6    | that is bundled with this package in the file LICENSE, and is        |
7    | available through the world-wide-web at the following url:           |
8    | https://www.php.net/license/3_01.txt                                 |
9    | If you did not receive a copy of the PHP license and are unable to   |
10    | obtain it through the world-wide-web, please send a note to          |
11    | license@php.net so we can mail you a copy immediately.               |
12    +----------------------------------------------------------------------+
13    | Author: Sascha Schumann <sascha@schumann.cx>                         |
14    +----------------------------------------------------------------------+
15  */
16 
17 #ifndef ZEND_SMART_STR_H
18 #define ZEND_SMART_STR_H
19 
20 #include <zend.h>
21 #include "zend_globals.h"
22 #include "zend_smart_str_public.h"
23 
24 BEGIN_EXTERN_C()
25 
26 ZEND_API void ZEND_FASTCALL smart_str_erealloc(smart_str *str, size_t len);
27 ZEND_API void ZEND_FASTCALL smart_str_realloc(smart_str *str, size_t len);
28 ZEND_API void ZEND_FASTCALL smart_str_append_escaped(smart_str *str, const char *s, size_t l);
29 /* If zero_fraction is true, then a ".0" will be added to numbers that would not otherwise
30  * have a fractional part and look like integers. */
31 ZEND_API void ZEND_FASTCALL smart_str_append_double(
32 		smart_str *str, double num, int precision, bool zero_fraction);
33 ZEND_API void smart_str_append_printf(smart_str *dest, const char *format, ...)
34 	ZEND_ATTRIBUTE_FORMAT(printf, 2, 3);
35 ZEND_API void ZEND_FASTCALL smart_str_append_escaped_truncated(smart_str *str, const zend_string *value, size_t length);
36 ZEND_API void ZEND_FASTCALL smart_str_append_scalar(smart_str *str, const zval *value, size_t truncate);
37 ZEND_API zend_result ZEND_FASTCALL smart_str_append_zval(smart_str *dest, const zval *value, size_t truncate);
END_EXTERN_C()38 END_EXTERN_C()
39 
40 static zend_always_inline size_t smart_str_alloc(smart_str *str, size_t len, bool persistent) {
41 	if (UNEXPECTED(!str->s)) {
42 		goto do_smart_str_realloc;
43 	} else {
44 		len += ZSTR_LEN(str->s);
45 		if (UNEXPECTED(len >= str->a)) {
46 do_smart_str_realloc:
47 			if (persistent) {
48 				smart_str_realloc(str, len);
49 			} else {
50 				smart_str_erealloc(str, len);
51 			}
52 		}
53 	}
54 	return len;
55 }
56 
smart_str_extend_ex(smart_str * dest,size_t len,bool persistent)57 static zend_always_inline char* smart_str_extend_ex(smart_str *dest, size_t len, bool persistent) {
58 	size_t new_len = smart_str_alloc(dest, len, persistent);
59 	char *ret = ZSTR_VAL(dest->s) + ZSTR_LEN(dest->s);
60 	ZSTR_LEN(dest->s) = new_len;
61 	return ret;
62 }
63 
smart_str_extend(smart_str * dest,size_t length)64 static zend_always_inline char* smart_str_extend(smart_str *dest, size_t length)
65 {
66 	return smart_str_extend_ex(dest, length, false);
67 }
68 
smart_str_free_ex(smart_str * str,bool persistent)69 static zend_always_inline void smart_str_free_ex(smart_str *str, bool persistent) {
70 	if (str->s) {
71 		zend_string_release_ex(str->s, persistent);
72 		str->s = NULL;
73 	}
74 	str->a = 0;
75 }
76 
smart_str_free(smart_str * str)77 static zend_always_inline void smart_str_free(smart_str *str)
78 {
79 	smart_str_free_ex(str, false);
80 }
81 
smart_str_0(smart_str * str)82 static zend_always_inline void smart_str_0(smart_str *str) {
83 	if (str->s) {
84 		ZSTR_VAL(str->s)[ZSTR_LEN(str->s)] = '\0';
85 	}
86 }
87 
smart_str_get_len(smart_str * str)88 static zend_always_inline size_t smart_str_get_len(smart_str *str) {
89 	return str->s ? ZSTR_LEN(str->s) : 0;
90 }
91 
smart_str_trim_to_size_ex(smart_str * str,bool persistent)92 static zend_always_inline void smart_str_trim_to_size_ex(smart_str *str, bool persistent)
93 {
94 	if (str->s && str->a > ZSTR_LEN(str->s)) {
95 		str->s = zend_string_realloc(str->s, ZSTR_LEN(str->s), persistent);
96 		str->a = ZSTR_LEN(str->s);
97 	}
98 }
99 
smart_str_trim_to_size(smart_str * dest)100 static zend_always_inline void smart_str_trim_to_size(smart_str *dest)
101 {
102 	smart_str_trim_to_size_ex(dest, false);
103 }
104 
smart_str_extract_ex(smart_str * str,bool persistent)105 static zend_always_inline zend_string *smart_str_extract_ex(smart_str *str, bool persistent) {
106 	if (str->s) {
107 		zend_string *res;
108 		smart_str_0(str);
109 		smart_str_trim_to_size_ex(str, persistent);
110 		res = str->s;
111 		str->s = NULL;
112 		return res;
113 	} else {
114 		return ZSTR_EMPTY_ALLOC();
115 	}
116 }
117 
smart_str_extract(smart_str * dest)118 static zend_always_inline zend_string *smart_str_extract(smart_str *dest)
119 {
120 	return smart_str_extract_ex(dest, false);
121 }
122 
smart_str_appendc_ex(smart_str * dest,char ch,bool persistent)123 static zend_always_inline void smart_str_appendc_ex(smart_str *dest, char ch, bool persistent) {
124 	size_t new_len = smart_str_alloc(dest, 1, persistent);
125 	ZSTR_VAL(dest->s)[new_len - 1] = ch;
126 	ZSTR_LEN(dest->s) = new_len;
127 }
128 
smart_str_appendl_ex(smart_str * dest,const char * str,size_t len,bool persistent)129 static zend_always_inline void smart_str_appendl_ex(smart_str *dest, const char *str, size_t len, bool persistent) {
130 	size_t new_len = smart_str_alloc(dest, len, persistent);
131 	memcpy(ZSTR_VAL(dest->s) + ZSTR_LEN(dest->s), str, len);
132 	ZSTR_LEN(dest->s) = new_len;
133 }
134 
smart_str_append_ex(smart_str * dest,const zend_string * src,bool persistent)135 static zend_always_inline void smart_str_append_ex(smart_str *dest, const zend_string *src, bool persistent) {
136 	smart_str_appendl_ex(dest, ZSTR_VAL(src), ZSTR_LEN(src), persistent);
137 }
138 
smart_str_append_smart_str_ex(smart_str * dest,const smart_str * src,bool persistent)139 static zend_always_inline void smart_str_append_smart_str_ex(smart_str *dest, const smart_str *src, bool persistent) {
140 	if (src->s && ZSTR_LEN(src->s)) {
141 		smart_str_append_ex(dest, src->s, persistent);
142 	}
143 }
144 
smart_str_append_long_ex(smart_str * dest,zend_long num,bool persistent)145 static zend_always_inline void smart_str_append_long_ex(smart_str *dest, zend_long num, bool persistent) {
146 	char buf[32];
147 	char *result = zend_print_long_to_buf(buf + sizeof(buf) - 1, num);
148 	smart_str_appendl_ex(dest, result, buf + sizeof(buf) - 1 - result, persistent);
149 }
150 
smart_str_append_long(smart_str * dest,zend_long num)151 static zend_always_inline void smart_str_append_long(smart_str *dest, zend_long num)
152 {
153 	smart_str_append_long_ex(dest, num, false);
154 }
155 
smart_str_append_unsigned_ex(smart_str * dest,zend_ulong num,bool persistent)156 static zend_always_inline void smart_str_append_unsigned_ex(smart_str *dest, zend_ulong num, bool persistent) {
157 	char buf[32];
158 	char *result = zend_print_ulong_to_buf(buf + sizeof(buf) - 1, num);
159 	smart_str_appendl_ex(dest, result, buf + sizeof(buf) - 1 - result, persistent);
160 }
161 
smart_str_append_unsigned(smart_str * dest,zend_ulong num)162 static zend_always_inline void smart_str_append_unsigned(smart_str *dest, zend_ulong num)
163 {
164 	smart_str_append_unsigned_ex(dest, num, false);
165 }
166 
smart_str_appendl(smart_str * dest,const char * src,size_t length)167 static zend_always_inline void smart_str_appendl(smart_str *dest, const char *src, size_t length)
168 {
169 	smart_str_appendl_ex(dest, src, length, false);
170 }
smart_str_appends_ex(smart_str * dest,const char * src,bool persistent)171 static zend_always_inline void smart_str_appends_ex(smart_str *dest, const char *src, bool persistent)
172 {
173 	smart_str_appendl_ex(dest, src, strlen(src), persistent);
174 }
smart_str_appends(smart_str * dest,const char * src)175 static zend_always_inline void smart_str_appends(smart_str *dest, const char *src)
176 {
177 	smart_str_appendl_ex(dest, src, strlen(src), false);
178 }
smart_str_append(smart_str * dest,const zend_string * src)179 static zend_always_inline void smart_str_append(smart_str *dest, const zend_string *src)
180 {
181 	smart_str_append_ex(dest, src, false);
182 }
smart_str_appendc(smart_str * dest,char ch)183 static zend_always_inline void smart_str_appendc(smart_str *dest, char ch)
184 {
185 	smart_str_appendc_ex(dest, ch, false);
186 }
smart_str_append_smart_str(smart_str * dest,const smart_str * src)187 static zend_always_inline void smart_str_append_smart_str(smart_str *dest, const smart_str *src)
188 {
189 	smart_str_append_smart_str_ex(dest, src, false);
190 }
191 
smart_str_setl(smart_str * dest,const char * src,size_t len)192 static zend_always_inline void smart_str_setl(smart_str *dest, const char *src, size_t len) {
193 	smart_str_free(dest);
194 	smart_str_appendl(dest, src, len);
195 }
196 
smart_str_sets(smart_str * dest,const char * src)197 static zend_always_inline void smart_str_sets(smart_str *dest, const char *src)
198 {
199 	smart_str_setl(dest, src, strlen(src));
200 }
201 #endif
202