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_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
45 BEGIN_EXTERN_C()
46
47 ZEND_API void ZEND_FASTCALL smart_str_erealloc(smart_str *str, size_t len);
48 ZEND_API void ZEND_FASTCALL smart_str_realloc(smart_str *str, size_t len);
49 ZEND_API void ZEND_FASTCALL smart_str_append_escaped(smart_str *str, const char *s, size_t l);
50 ZEND_API void ZEND_FASTCALL smart_str_append_printf(smart_str *dest, const char *format, ...)
51 ZEND_ATTRIBUTE_FORMAT(printf, 2, 3);
52
END_EXTERN_C()53 END_EXTERN_C()
54
55 static zend_always_inline size_t smart_str_alloc(smart_str *str, size_t len, zend_bool persistent) {
56 if (UNEXPECTED(!str->s)) {
57 goto do_smart_str_realloc;
58 } else {
59 len += ZSTR_LEN(str->s);
60 if (UNEXPECTED(len >= str->a)) {
61 do_smart_str_realloc:
62 if (persistent) {
63 smart_str_realloc(str, len);
64 } else {
65 smart_str_erealloc(str, len);
66 }
67 }
68 }
69 return len;
70 }
71
smart_str_free(smart_str * str)72 static zend_always_inline void smart_str_free(smart_str *str) {
73 if (str->s) {
74 zend_string_release(str->s);
75 str->s = NULL;
76 }
77 str->a = 0;
78 }
79
smart_str_0(smart_str * str)80 static zend_always_inline void smart_str_0(smart_str *str) {
81 if (str->s) {
82 ZSTR_VAL(str->s)[ZSTR_LEN(str->s)] = '\0';
83 }
84 }
85
smart_str_get_len(smart_str * str)86 static zend_always_inline size_t smart_str_get_len(smart_str *str) {
87 return str->s ? ZSTR_LEN(str->s) : 0;
88 }
89
smart_str_extract(smart_str * str)90 static zend_always_inline zend_string *smart_str_extract(smart_str *str) {
91 if (str->s) {
92 zend_string *res;
93 smart_str_0(str);
94 res = str->s;
95 str->s = NULL;
96 return res;
97 } else {
98 return ZSTR_EMPTY_ALLOC();
99 }
100 }
101
smart_str_appendc_ex(smart_str * dest,char ch,zend_bool persistent)102 static zend_always_inline void smart_str_appendc_ex(smart_str *dest, char ch, zend_bool persistent) {
103 size_t new_len = smart_str_alloc(dest, 1, persistent);
104 ZSTR_VAL(dest->s)[new_len - 1] = ch;
105 ZSTR_LEN(dest->s) = new_len;
106 }
107
smart_str_appendl_ex(smart_str * dest,const char * str,size_t len,zend_bool persistent)108 static zend_always_inline void smart_str_appendl_ex(smart_str *dest, const char *str, size_t len, zend_bool persistent) {
109 size_t new_len = smart_str_alloc(dest, len, persistent);
110 memcpy(ZSTR_VAL(dest->s) + ZSTR_LEN(dest->s), str, len);
111 ZSTR_LEN(dest->s) = new_len;
112 }
113
smart_str_append_ex(smart_str * dest,const zend_string * src,zend_bool persistent)114 static zend_always_inline void smart_str_append_ex(smart_str *dest, const zend_string *src, zend_bool persistent) {
115 smart_str_appendl_ex(dest, ZSTR_VAL(src), ZSTR_LEN(src), persistent);
116 }
117
smart_str_append_smart_str_ex(smart_str * dest,const smart_str * src,zend_bool persistent)118 static zend_always_inline void smart_str_append_smart_str_ex(smart_str *dest, const smart_str *src, zend_bool persistent) {
119 if (src->s && ZSTR_LEN(src->s)) {
120 smart_str_append_ex(dest, src->s, persistent);
121 }
122 }
123
smart_str_append_long_ex(smart_str * dest,zend_long num,zend_bool persistent)124 static zend_always_inline void smart_str_append_long_ex(smart_str *dest, zend_long num, zend_bool persistent) {
125 char buf[32];
126 char *result = zend_print_long_to_buf(buf + sizeof(buf) - 1, num);
127 smart_str_appendl_ex(dest, result, buf + sizeof(buf) - 1 - result, persistent);
128 }
129
smart_str_append_unsigned_ex(smart_str * dest,zend_ulong num,zend_bool persistent)130 static zend_always_inline void smart_str_append_unsigned_ex(smart_str *dest, zend_ulong num, zend_bool persistent) {
131 char buf[32];
132 char *result = zend_print_ulong_to_buf(buf + sizeof(buf) - 1, num);
133 smart_str_appendl_ex(dest, result, buf + sizeof(buf) - 1 - result, persistent);
134 }
135
smart_str_setl(smart_str * dest,const char * src,size_t len)136 static zend_always_inline void smart_str_setl(smart_str *dest, const char *src, size_t len) {
137 smart_str_free(dest);
138 smart_str_appendl(dest, src, len);
139 }
140
141 #endif
142
143 /*
144 * Local variables:
145 * tab-width: 4
146 * c-basic-offset: 4
147 * indent-tabs-mode: t
148 * End:
149 * vim600: sw=4 ts=4 fdm=marker
150 * vim<600: sw=4 ts=4
151 */
152