xref: /PHP-8.1/Zend/zend_smart_str.h (revision 05ef6334)
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 #define smart_str_appends_ex(dest, src, what) \
25 	smart_str_appendl_ex((dest), (src), strlen(src), (what))
26 #define smart_str_appends(dest, src) \
27 	smart_str_appendl((dest), (src), strlen(src))
28 #define smart_str_extend(dest, len) \
29 	smart_str_extend_ex((dest), (len), 0)
30 #define smart_str_appendc(dest, c) \
31 	smart_str_appendc_ex((dest), (c), 0)
32 #define smart_str_appendl(dest, src, len) \
33 	smart_str_appendl_ex((dest), (src), (len), 0)
34 #define smart_str_append(dest, src) \
35 	smart_str_append_ex((dest), (src), 0)
36 #define smart_str_append_smart_str(dest, src) \
37 	smart_str_append_smart_str_ex((dest), (src), 0)
38 #define smart_str_sets(dest, src) \
39 	smart_str_setl((dest), (src), strlen(src));
40 #define smart_str_append_long(dest, val) \
41 	smart_str_append_long_ex((dest), (val), 0)
42 #define smart_str_append_unsigned(dest, val) \
43 	smart_str_append_unsigned_ex((dest), (val), 0)
44 #define smart_str_free(dest) \
45 	smart_str_free_ex((dest), 0)
46 
47 BEGIN_EXTERN_C()
48 
49 ZEND_API void ZEND_FASTCALL smart_str_erealloc(smart_str *str, size_t len);
50 ZEND_API void ZEND_FASTCALL smart_str_realloc(smart_str *str, size_t len);
51 ZEND_API void ZEND_FASTCALL smart_str_append_escaped(smart_str *str, const char *s, size_t l);
52 /* If zero_fraction is true, then a ".0" will be added to numbers that would not otherwise
53  * have a fractional part and look like integers. */
54 ZEND_API void ZEND_FASTCALL smart_str_append_double(
55 		smart_str *str, double num, int precision, bool zero_fraction);
56 ZEND_API void smart_str_append_printf(smart_str *dest, const char *format, ...)
57 	ZEND_ATTRIBUTE_FORMAT(printf, 2, 3);
58 ZEND_API void ZEND_FASTCALL smart_str_append_escaped_truncated(smart_str *str, zend_string *value, size_t length);
59 ZEND_API void ZEND_FASTCALL smart_str_append_scalar(smart_str *str, zval *value, size_t truncate);
END_EXTERN_C()60 END_EXTERN_C()
61 
62 static zend_always_inline size_t smart_str_alloc(smart_str *str, size_t len, bool persistent) {
63 	if (UNEXPECTED(!str->s)) {
64 		goto do_smart_str_realloc;
65 	} else {
66 		len += ZSTR_LEN(str->s);
67 		if (UNEXPECTED(len >= str->a)) {
68 do_smart_str_realloc:
69 			if (persistent) {
70 				smart_str_realloc(str, len);
71 			} else {
72 				smart_str_erealloc(str, len);
73 			}
74 		}
75 	}
76 	return len;
77 }
78 
smart_str_extend_ex(smart_str * dest,size_t len,bool persistent)79 static zend_always_inline char* smart_str_extend_ex(smart_str *dest, size_t len, bool persistent) {
80 	size_t new_len = smart_str_alloc(dest, len, persistent);
81 	char *ret = ZSTR_VAL(dest->s) + ZSTR_LEN(dest->s);
82 	ZSTR_LEN(dest->s) = new_len;
83 	return ret;
84 }
85 
smart_str_free_ex(smart_str * str,bool persistent)86 static zend_always_inline void smart_str_free_ex(smart_str *str, bool persistent) {
87 	if (str->s) {
88 		zend_string_release_ex(str->s, persistent);
89 		str->s = NULL;
90 	}
91 	str->a = 0;
92 }
93 
smart_str_0(smart_str * str)94 static zend_always_inline void smart_str_0(smart_str *str) {
95 	if (str->s) {
96 		ZSTR_VAL(str->s)[ZSTR_LEN(str->s)] = '\0';
97 	}
98 }
99 
smart_str_get_len(smart_str * str)100 static zend_always_inline size_t smart_str_get_len(smart_str *str) {
101 	return str->s ? ZSTR_LEN(str->s) : 0;
102 }
103 
smart_str_extract(smart_str * str)104 static zend_always_inline zend_string *smart_str_extract(smart_str *str) {
105 	if (str->s) {
106 		zend_string *res;
107 		smart_str_0(str);
108 		res = str->s;
109 		str->s = NULL;
110 		return res;
111 	} else {
112 		return ZSTR_EMPTY_ALLOC();
113 	}
114 }
115 
smart_str_appendc_ex(smart_str * dest,char ch,bool persistent)116 static zend_always_inline void smart_str_appendc_ex(smart_str *dest, char ch, bool persistent) {
117 	size_t new_len = smart_str_alloc(dest, 1, persistent);
118 	ZSTR_VAL(dest->s)[new_len - 1] = ch;
119 	ZSTR_LEN(dest->s) = new_len;
120 }
121 
smart_str_appendl_ex(smart_str * dest,const char * str,size_t len,bool persistent)122 static zend_always_inline void smart_str_appendl_ex(smart_str *dest, const char *str, size_t len, bool persistent) {
123 	size_t new_len = smart_str_alloc(dest, len, persistent);
124 	memcpy(ZSTR_VAL(dest->s) + ZSTR_LEN(dest->s), str, len);
125 	ZSTR_LEN(dest->s) = new_len;
126 }
127 
smart_str_append_ex(smart_str * dest,const zend_string * src,bool persistent)128 static zend_always_inline void smart_str_append_ex(smart_str *dest, const zend_string *src, bool persistent) {
129 	smart_str_appendl_ex(dest, ZSTR_VAL(src), ZSTR_LEN(src), persistent);
130 }
131 
smart_str_append_smart_str_ex(smart_str * dest,const smart_str * src,bool persistent)132 static zend_always_inline void smart_str_append_smart_str_ex(smart_str *dest, const smart_str *src, bool persistent) {
133 	if (src->s && ZSTR_LEN(src->s)) {
134 		smart_str_append_ex(dest, src->s, persistent);
135 	}
136 }
137 
smart_str_append_long_ex(smart_str * dest,zend_long num,bool persistent)138 static zend_always_inline void smart_str_append_long_ex(smart_str *dest, zend_long num, bool persistent) {
139 	char buf[32];
140 	char *result = zend_print_long_to_buf(buf + sizeof(buf) - 1, num);
141 	smart_str_appendl_ex(dest, result, buf + sizeof(buf) - 1 - result, persistent);
142 }
143 
smart_str_append_unsigned_ex(smart_str * dest,zend_ulong num,bool persistent)144 static zend_always_inline void smart_str_append_unsigned_ex(smart_str *dest, zend_ulong num, bool persistent) {
145 	char buf[32];
146 	char *result = zend_print_ulong_to_buf(buf + sizeof(buf) - 1, num);
147 	smart_str_appendl_ex(dest, result, buf + sizeof(buf) - 1 - result, persistent);
148 }
149 
smart_str_setl(smart_str * dest,const char * src,size_t len)150 static zend_always_inline void smart_str_setl(smart_str *dest, const char *src, size_t len) {
151 	smart_str_free(dest);
152 	smart_str_appendl(dest, src, len);
153 }
154 
155 #endif
156