xref: /PHP-8.0/Zend/zend_smart_str.h (revision 5d6e923d)
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    | http://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 ZEND_API void smart_str_append_printf(smart_str *dest, const char *format, ...)
53 	ZEND_ATTRIBUTE_FORMAT(printf, 2, 3);
54 
END_EXTERN_C()55 END_EXTERN_C()
56 
57 static zend_always_inline size_t smart_str_alloc(smart_str *str, size_t len, zend_bool persistent) {
58 	if (UNEXPECTED(!str->s)) {
59 		goto do_smart_str_realloc;
60 	} else {
61 		len += ZSTR_LEN(str->s);
62 		if (UNEXPECTED(len >= str->a)) {
63 do_smart_str_realloc:
64 			if (persistent) {
65 				smart_str_realloc(str, len);
66 			} else {
67 				smart_str_erealloc(str, len);
68 			}
69 		}
70 	}
71 	return len;
72 }
73 
smart_str_extend_ex(smart_str * dest,size_t len,zend_bool persistent)74 static zend_always_inline char* smart_str_extend_ex(smart_str *dest, size_t len, zend_bool persistent) {
75 	size_t new_len = smart_str_alloc(dest, len, persistent);
76 	char *ret = ZSTR_VAL(dest->s) + ZSTR_LEN(dest->s);
77 	ZSTR_LEN(dest->s) = new_len;
78 	return ret;
79 }
80 
smart_str_free_ex(smart_str * str,zend_bool persistent)81 static zend_always_inline void smart_str_free_ex(smart_str *str, zend_bool persistent) {
82 	if (str->s) {
83 		zend_string_release_ex(str->s, persistent);
84 		str->s = NULL;
85 	}
86 	str->a = 0;
87 }
88 
smart_str_0(smart_str * str)89 static zend_always_inline void smart_str_0(smart_str *str) {
90 	if (str->s) {
91 		ZSTR_VAL(str->s)[ZSTR_LEN(str->s)] = '\0';
92 	}
93 }
94 
smart_str_get_len(smart_str * str)95 static zend_always_inline size_t smart_str_get_len(smart_str *str) {
96 	return str->s ? ZSTR_LEN(str->s) : 0;
97 }
98 
smart_str_extract(smart_str * str)99 static zend_always_inline zend_string *smart_str_extract(smart_str *str) {
100 	if (str->s) {
101 		zend_string *res;
102 		smart_str_0(str);
103 		res = str->s;
104 		str->s = NULL;
105 		return res;
106 	} else {
107 		return ZSTR_EMPTY_ALLOC();
108 	}
109 }
110 
smart_str_appendc_ex(smart_str * dest,char ch,zend_bool persistent)111 static zend_always_inline void smart_str_appendc_ex(smart_str *dest, char ch, zend_bool persistent) {
112 	size_t new_len = smart_str_alloc(dest, 1, persistent);
113 	ZSTR_VAL(dest->s)[new_len - 1] = ch;
114 	ZSTR_LEN(dest->s) = new_len;
115 }
116 
smart_str_appendl_ex(smart_str * dest,const char * str,size_t len,zend_bool persistent)117 static zend_always_inline void smart_str_appendl_ex(smart_str *dest, const char *str, size_t len, zend_bool persistent) {
118 	size_t new_len = smart_str_alloc(dest, len, persistent);
119 	memcpy(ZSTR_VAL(dest->s) + ZSTR_LEN(dest->s), str, len);
120 	ZSTR_LEN(dest->s) = new_len;
121 }
122 
smart_str_append_ex(smart_str * dest,const zend_string * src,zend_bool persistent)123 static zend_always_inline void smart_str_append_ex(smart_str *dest, const zend_string *src, zend_bool persistent) {
124 	smart_str_appendl_ex(dest, ZSTR_VAL(src), ZSTR_LEN(src), persistent);
125 }
126 
smart_str_append_smart_str_ex(smart_str * dest,const smart_str * src,zend_bool persistent)127 static zend_always_inline void smart_str_append_smart_str_ex(smart_str *dest, const smart_str *src, zend_bool persistent) {
128 	if (src->s && ZSTR_LEN(src->s)) {
129 		smart_str_append_ex(dest, src->s, persistent);
130 	}
131 }
132 
smart_str_append_long_ex(smart_str * dest,zend_long num,zend_bool persistent)133 static zend_always_inline void smart_str_append_long_ex(smart_str *dest, zend_long num, zend_bool persistent) {
134 	char buf[32];
135 	char *result = zend_print_long_to_buf(buf + sizeof(buf) - 1, num);
136 	smart_str_appendl_ex(dest, result, buf + sizeof(buf) - 1 - result, persistent);
137 }
138 
smart_str_append_unsigned_ex(smart_str * dest,zend_ulong num,zend_bool persistent)139 static zend_always_inline void smart_str_append_unsigned_ex(smart_str *dest, zend_ulong num, zend_bool persistent) {
140 	char buf[32];
141 	char *result = zend_print_ulong_to_buf(buf + sizeof(buf) - 1, num);
142 	smart_str_appendl_ex(dest, result, buf + sizeof(buf) - 1 - result, persistent);
143 }
144 
smart_str_setl(smart_str * dest,const char * src,size_t len)145 static zend_always_inline void smart_str_setl(smart_str *dest, const char *src, size_t len) {
146 	smart_str_free(dest);
147 	smart_str_appendl(dest, src, len);
148 }
149 
150 #endif
151