1 /*
2 +----------------------------------------------------------------------+
3 | PHP Version 7 |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 1997-2018 The PHP Group |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
15 | Author: Sascha Schumann <sascha@schumann.cx> |
16 +----------------------------------------------------------------------+
17 */
18
19 #ifndef ZEND_SMART_STR_H
20 #define ZEND_SMART_STR_H
21
22 #include <zend.h>
23 #include "zend_globals.h"
24 #include "zend_smart_str_public.h"
25
26 #define smart_str_appends_ex(dest, src, what) \
27 smart_str_appendl_ex((dest), (src), strlen(src), (what))
28 #define smart_str_appends(dest, src) \
29 smart_str_appendl((dest), (src), strlen(src))
30 #define smart_str_extend(dest, len) \
31 smart_str_extend_ex((dest), (len), 0)
32 #define smart_str_appendc(dest, c) \
33 smart_str_appendc_ex((dest), (c), 0)
34 #define smart_str_appendl(dest, src, len) \
35 smart_str_appendl_ex((dest), (src), (len), 0)
36 #define smart_str_append(dest, src) \
37 smart_str_append_ex((dest), (src), 0)
38 #define smart_str_append_smart_str(dest, src) \
39 smart_str_append_smart_str_ex((dest), (src), 0)
40 #define smart_str_sets(dest, src) \
41 smart_str_setl((dest), (src), strlen(src));
42 #define smart_str_append_long(dest, val) \
43 smart_str_append_long_ex((dest), (val), 0)
44 #define smart_str_append_unsigned(dest, val) \
45 smart_str_append_unsigned_ex((dest), (val), 0)
46 #define smart_str_free(dest) \
47 smart_str_free_ex((dest), 0)
48
49 BEGIN_EXTERN_C()
50
51 ZEND_API void ZEND_FASTCALL smart_str_erealloc(smart_str *str, size_t len);
52 ZEND_API void ZEND_FASTCALL smart_str_realloc(smart_str *str, size_t len);
53 ZEND_API void ZEND_FASTCALL smart_str_append_escaped(smart_str *str, const char *s, size_t l);
54 ZEND_API void smart_str_append_printf(smart_str *dest, const char *format, ...)
55 ZEND_ATTRIBUTE_FORMAT(printf, 2, 3);
56
END_EXTERN_C()57 END_EXTERN_C()
58
59 static zend_always_inline size_t smart_str_alloc(smart_str *str, size_t len, zend_bool persistent) {
60 if (UNEXPECTED(!str->s)) {
61 goto do_smart_str_realloc;
62 } else {
63 len += ZSTR_LEN(str->s);
64 if (UNEXPECTED(len >= str->a)) {
65 do_smart_str_realloc:
66 if (persistent) {
67 smart_str_realloc(str, len);
68 } else {
69 smart_str_erealloc(str, len);
70 }
71 }
72 }
73 return len;
74 }
75
smart_str_extend_ex(smart_str * dest,size_t len,zend_bool persistent)76 static zend_always_inline char* smart_str_extend_ex(smart_str *dest, size_t len, zend_bool persistent) {
77 size_t new_len = smart_str_alloc(dest, len, persistent);
78 char *ret = ZSTR_VAL(dest->s) + ZSTR_LEN(dest->s);
79 ZSTR_LEN(dest->s) = new_len;
80 return ret;
81 }
82
smart_str_free_ex(smart_str * str,zend_bool persistent)83 static zend_always_inline void smart_str_free_ex(smart_str *str, zend_bool persistent) {
84 if (str->s) {
85 zend_string_release_ex(str->s, persistent);
86 str->s = NULL;
87 }
88 str->a = 0;
89 }
90
smart_str_0(smart_str * str)91 static zend_always_inline void smart_str_0(smart_str *str) {
92 if (str->s) {
93 ZSTR_VAL(str->s)[ZSTR_LEN(str->s)] = '\0';
94 }
95 }
96
smart_str_get_len(smart_str * str)97 static zend_always_inline size_t smart_str_get_len(smart_str *str) {
98 return str->s ? ZSTR_LEN(str->s) : 0;
99 }
100
smart_str_extract(smart_str * str)101 static zend_always_inline zend_string *smart_str_extract(smart_str *str) {
102 if (str->s) {
103 zend_string *res;
104 smart_str_0(str);
105 res = str->s;
106 str->s = NULL;
107 return res;
108 } else {
109 return ZSTR_EMPTY_ALLOC();
110 }
111 }
112
smart_str_appendc_ex(smart_str * dest,char ch,zend_bool persistent)113 static zend_always_inline void smart_str_appendc_ex(smart_str *dest, char ch, zend_bool persistent) {
114 size_t new_len = smart_str_alloc(dest, 1, persistent);
115 ZSTR_VAL(dest->s)[new_len - 1] = ch;
116 ZSTR_LEN(dest->s) = new_len;
117 }
118
smart_str_appendl_ex(smart_str * dest,const char * str,size_t len,zend_bool persistent)119 static zend_always_inline void smart_str_appendl_ex(smart_str *dest, const char *str, size_t len, zend_bool persistent) {
120 size_t new_len = smart_str_alloc(dest, len, persistent);
121 memcpy(ZSTR_VAL(dest->s) + ZSTR_LEN(dest->s), str, len);
122 ZSTR_LEN(dest->s) = new_len;
123 }
124
smart_str_append_ex(smart_str * dest,const zend_string * src,zend_bool persistent)125 static zend_always_inline void smart_str_append_ex(smart_str *dest, const zend_string *src, zend_bool persistent) {
126 smart_str_appendl_ex(dest, ZSTR_VAL(src), ZSTR_LEN(src), persistent);
127 }
128
smart_str_append_smart_str_ex(smart_str * dest,const smart_str * src,zend_bool persistent)129 static zend_always_inline void smart_str_append_smart_str_ex(smart_str *dest, const smart_str *src, zend_bool persistent) {
130 if (src->s && ZSTR_LEN(src->s)) {
131 smart_str_append_ex(dest, src->s, persistent);
132 }
133 }
134
smart_str_append_long_ex(smart_str * dest,zend_long num,zend_bool persistent)135 static zend_always_inline void smart_str_append_long_ex(smart_str *dest, zend_long num, zend_bool persistent) {
136 char buf[32];
137 char *result = zend_print_long_to_buf(buf + sizeof(buf) - 1, num);
138 smart_str_appendl_ex(dest, result, buf + sizeof(buf) - 1 - result, persistent);
139 }
140
smart_str_append_unsigned_ex(smart_str * dest,zend_ulong num,zend_bool persistent)141 static zend_always_inline void smart_str_append_unsigned_ex(smart_str *dest, zend_ulong num, zend_bool persistent) {
142 char buf[32];
143 char *result = zend_print_ulong_to_buf(buf + sizeof(buf) - 1, num);
144 smart_str_appendl_ex(dest, result, buf + sizeof(buf) - 1 - result, persistent);
145 }
146
smart_str_setl(smart_str * dest,const char * src,size_t len)147 static zend_always_inline void smart_str_setl(smart_str *dest, const char *src, size_t len) {
148 smart_str_free(dest);
149 smart_str_appendl(dest, src, len);
150 }
151
152 #endif
153
154 /*
155 * Local variables:
156 * tab-width: 4
157 * c-basic-offset: 4
158 * indent-tabs-mode: t
159 * End:
160 * vim600: sw=4 ts=4 fdm=marker
161 * vim<600: sw=4 ts=4
162 */
163