xref: /php-src/Zend/zend_smart_str.h (revision c8955c07)
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);
END_EXTERN_C()37 END_EXTERN_C()
38 
39 static zend_always_inline size_t smart_str_alloc(smart_str *str, size_t len, bool persistent) {
40 	if (UNEXPECTED(!str->s)) {
41 		goto do_smart_str_realloc;
42 	} else {
43 		len += ZSTR_LEN(str->s);
44 		if (UNEXPECTED(len >= str->a)) {
45 do_smart_str_realloc:
46 			if (persistent) {
47 				smart_str_realloc(str, len);
48 			} else {
49 				smart_str_erealloc(str, len);
50 			}
51 		}
52 	}
53 	return len;
54 }
55 
smart_str_extend_ex(smart_str * dest,size_t len,bool persistent)56 static zend_always_inline char* smart_str_extend_ex(smart_str *dest, size_t len, bool persistent) {
57 	size_t new_len = smart_str_alloc(dest, len, persistent);
58 	char *ret = ZSTR_VAL(dest->s) + ZSTR_LEN(dest->s);
59 	ZSTR_LEN(dest->s) = new_len;
60 	return ret;
61 }
62 
smart_str_extend(smart_str * dest,size_t length)63 static zend_always_inline char* smart_str_extend(smart_str *dest, size_t length)
64 {
65 	return smart_str_extend_ex(dest, length, false);
66 }
67 
smart_str_free_ex(smart_str * str,bool persistent)68 static zend_always_inline void smart_str_free_ex(smart_str *str, bool persistent) {
69 	if (str->s) {
70 		zend_string_release_ex(str->s, persistent);
71 		str->s = NULL;
72 	}
73 	str->a = 0;
74 }
75 
smart_str_free(smart_str * str)76 static zend_always_inline void smart_str_free(smart_str *str)
77 {
78 	smart_str_free_ex(str, false);
79 }
80 
smart_str_0(smart_str * str)81 static zend_always_inline void smart_str_0(smart_str *str) {
82 	if (str->s) {
83 		ZSTR_VAL(str->s)[ZSTR_LEN(str->s)] = '\0';
84 	}
85 }
86 
smart_str_get_len(smart_str * str)87 static zend_always_inline size_t smart_str_get_len(smart_str *str) {
88 	return str->s ? ZSTR_LEN(str->s) : 0;
89 }
90 
smart_str_trim_to_size_ex(smart_str * str,bool persistent)91 static zend_always_inline void smart_str_trim_to_size_ex(smart_str *str, bool persistent)
92 {
93 	if (str->s && str->a > ZSTR_LEN(str->s)) {
94 		str->s = zend_string_realloc(str->s, ZSTR_LEN(str->s), persistent);
95 		str->a = ZSTR_LEN(str->s);
96 	}
97 }
98 
smart_str_trim_to_size(smart_str * dest)99 static zend_always_inline void smart_str_trim_to_size(smart_str *dest)
100 {
101 	smart_str_trim_to_size_ex(dest, false);
102 }
103 
smart_str_extract_ex(smart_str * str,bool persistent)104 static zend_always_inline zend_string *smart_str_extract_ex(smart_str *str, bool persistent) {
105 	if (str->s) {
106 		zend_string *res;
107 		smart_str_0(str);
108 		smart_str_trim_to_size_ex(str, persistent);
109 		res = str->s;
110 		str->s = NULL;
111 		return res;
112 	} else {
113 		return ZSTR_EMPTY_ALLOC();
114 	}
115 }
116 
smart_str_extract(smart_str * dest)117 static zend_always_inline zend_string *smart_str_extract(smart_str *dest)
118 {
119 	return smart_str_extract_ex(dest, false);
120 }
121 
smart_str_appendc_ex(smart_str * dest,char ch,bool persistent)122 static zend_always_inline void smart_str_appendc_ex(smart_str *dest, char ch, bool persistent) {
123 	size_t new_len = smart_str_alloc(dest, 1, persistent);
124 	ZSTR_VAL(dest->s)[new_len - 1] = ch;
125 	ZSTR_LEN(dest->s) = new_len;
126 }
127 
smart_str_appendl_ex(smart_str * dest,const char * str,size_t len,bool persistent)128 static zend_always_inline void smart_str_appendl_ex(smart_str *dest, const char *str, size_t len, bool persistent) {
129 	size_t new_len = smart_str_alloc(dest, len, persistent);
130 	memcpy(ZSTR_VAL(dest->s) + ZSTR_LEN(dest->s), str, len);
131 	ZSTR_LEN(dest->s) = new_len;
132 }
133 
smart_str_append_ex(smart_str * dest,const zend_string * src,bool persistent)134 static zend_always_inline void smart_str_append_ex(smart_str *dest, const zend_string *src, bool persistent) {
135 	smart_str_appendl_ex(dest, ZSTR_VAL(src), ZSTR_LEN(src), persistent);
136 }
137 
smart_str_append_smart_str_ex(smart_str * dest,const smart_str * src,bool persistent)138 static zend_always_inline void smart_str_append_smart_str_ex(smart_str *dest, const smart_str *src, bool persistent) {
139 	if (src->s && ZSTR_LEN(src->s)) {
140 		smart_str_append_ex(dest, src->s, persistent);
141 	}
142 }
143 
smart_str_append_long_ex(smart_str * dest,zend_long num,bool persistent)144 static zend_always_inline void smart_str_append_long_ex(smart_str *dest, zend_long num, bool persistent) {
145 	char buf[32];
146 	char *result = zend_print_long_to_buf(buf + sizeof(buf) - 1, num);
147 	smart_str_appendl_ex(dest, result, buf + sizeof(buf) - 1 - result, persistent);
148 }
149 
smart_str_append_long(smart_str * dest,zend_long num)150 static zend_always_inline void smart_str_append_long(smart_str *dest, zend_long num)
151 {
152 	smart_str_append_long_ex(dest, num, false);
153 }
154 
smart_str_append_unsigned_ex(smart_str * dest,zend_ulong num,bool persistent)155 static zend_always_inline void smart_str_append_unsigned_ex(smart_str *dest, zend_ulong num, bool persistent) {
156 	char buf[32];
157 	char *result = zend_print_ulong_to_buf(buf + sizeof(buf) - 1, num);
158 	smart_str_appendl_ex(dest, result, buf + sizeof(buf) - 1 - result, persistent);
159 }
160 
smart_str_append_unsigned(smart_str * dest,zend_ulong num)161 static zend_always_inline void smart_str_append_unsigned(smart_str *dest, zend_ulong num)
162 {
163 	smart_str_append_unsigned_ex(dest, num, false);
164 }
165 
smart_str_appendl(smart_str * dest,const char * src,size_t length)166 static zend_always_inline void smart_str_appendl(smart_str *dest, const char *src, size_t length)
167 {
168 	smart_str_appendl_ex(dest, src, length, false);
169 }
smart_str_appends_ex(smart_str * dest,const char * src,bool persistent)170 static zend_always_inline void smart_str_appends_ex(smart_str *dest, const char *src, bool persistent)
171 {
172 	smart_str_appendl_ex(dest, src, strlen(src), persistent);
173 }
smart_str_appends(smart_str * dest,const char * src)174 static zend_always_inline void smart_str_appends(smart_str *dest, const char *src)
175 {
176 	smart_str_appendl_ex(dest, src, strlen(src), false);
177 }
smart_str_append(smart_str * dest,const zend_string * src)178 static zend_always_inline void smart_str_append(smart_str *dest, const zend_string *src)
179 {
180 	smart_str_append_ex(dest, src, false);
181 }
smart_str_appendc(smart_str * dest,char ch)182 static zend_always_inline void smart_str_appendc(smart_str *dest, char ch)
183 {
184 	smart_str_appendc_ex(dest, ch, false);
185 }
smart_str_append_smart_str(smart_str * dest,const smart_str * src)186 static zend_always_inline void smart_str_append_smart_str(smart_str *dest, const smart_str *src)
187 {
188 	smart_str_append_smart_str_ex(dest, src, false);
189 }
190 
smart_str_setl(smart_str * dest,const char * src,size_t len)191 static zend_always_inline void smart_str_setl(smart_str *dest, const char *src, size_t len) {
192 	smart_str_free(dest);
193 	smart_str_appendl(dest, src, len);
194 }
195 
smart_str_sets(smart_str * dest,const char * src)196 static zend_always_inline void smart_str_sets(smart_str *dest, const char *src)
197 {
198 	smart_str_setl(dest, src, strlen(src));
199 }
200 #endif
201