xref: /PHP-7.1/Zend/zend_smart_str.h (revision 03f3b847)
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_smart_str_public.h"
24 
25 #define smart_str_appends_ex(dest, src, what) \
26 	smart_str_appendl_ex((dest), (src), strlen(src), (what))
27 #define smart_str_appends(dest, src) \
28 	smart_str_appendl((dest), (src), strlen(src))
29 #define smart_str_appendc(dest, c) \
30 	smart_str_appendc_ex((dest), (c), 0)
31 #define smart_str_appendl(dest, src, len) \
32 	smart_str_appendl_ex((dest), (src), (len), 0)
33 #define smart_str_append(dest, src) \
34 	smart_str_append_ex((dest), (src), 0)
35 #define smart_str_append_smart_str(dest, src) \
36 	smart_str_append_smart_str_ex((dest), (src), 0)
37 #define smart_str_sets(dest, src) \
38 	smart_str_setl((dest), (src), strlen(src));
39 #define smart_str_append_long(dest, val) \
40 	smart_str_append_long_ex((dest), (val), 0)
41 #define smart_str_append_unsigned(dest, val) \
42 	smart_str_append_unsigned_ex((dest), (val), 0)
43 
44 BEGIN_EXTERN_C()
45 
46 ZEND_API void ZEND_FASTCALL smart_str_erealloc(smart_str *str, size_t len);
47 ZEND_API void ZEND_FASTCALL smart_str_realloc(smart_str *str, size_t len);
48 ZEND_API void ZEND_FASTCALL smart_str_append_escaped(smart_str *str, const char *s, size_t l);
49 
END_EXTERN_C()50 END_EXTERN_C()
51 
52 static zend_always_inline size_t smart_str_alloc(smart_str *str, size_t len, zend_bool persistent) {
53 	if (UNEXPECTED(!str->s)) {
54 		goto do_smart_str_realloc;
55 	} else {
56 		len += ZSTR_LEN(str->s);
57 		if (UNEXPECTED(len >= str->a)) {
58 do_smart_str_realloc:
59 			if (persistent) {
60 				smart_str_realloc(str, len);
61 			} else {
62 				smart_str_erealloc(str, len);
63 			}
64 		}
65 	}
66 	return len;
67 }
68 
smart_str_free(smart_str * str)69 static zend_always_inline void smart_str_free(smart_str *str) {
70 	if (str->s) {
71 		zend_string_release(str->s);
72 		str->s = NULL;
73 	}
74 	str->a = 0;
75 }
76 
smart_str_0(smart_str * str)77 static zend_always_inline void smart_str_0(smart_str *str) {
78 	if (str->s) {
79 		ZSTR_VAL(str->s)[ZSTR_LEN(str->s)] = '\0';
80 	}
81 }
82 
smart_str_appendc_ex(smart_str * dest,char ch,zend_bool persistent)83 static zend_always_inline void smart_str_appendc_ex(smart_str *dest, char ch, zend_bool persistent) {
84 	size_t new_len = smart_str_alloc(dest, 1, persistent);
85 	ZSTR_VAL(dest->s)[new_len - 1] = ch;
86 	ZSTR_LEN(dest->s) = new_len;
87 }
88 
smart_str_appendl_ex(smart_str * dest,const char * str,size_t len,zend_bool persistent)89 static zend_always_inline void smart_str_appendl_ex(smart_str *dest, const char *str, size_t len, zend_bool persistent) {
90 	size_t new_len = smart_str_alloc(dest, len, persistent);
91 	memcpy(ZSTR_VAL(dest->s) + ZSTR_LEN(dest->s), str, len);
92 	ZSTR_LEN(dest->s) = new_len;
93 }
94 
smart_str_append_ex(smart_str * dest,const zend_string * src,zend_bool persistent)95 static zend_always_inline void smart_str_append_ex(smart_str *dest, const zend_string *src, zend_bool persistent) {
96 	smart_str_appendl_ex(dest, ZSTR_VAL(src), ZSTR_LEN(src), persistent);
97 }
98 
smart_str_append_smart_str_ex(smart_str * dest,const smart_str * src,zend_bool persistent)99 static zend_always_inline void smart_str_append_smart_str_ex(smart_str *dest, const smart_str *src, zend_bool persistent) {
100 	if (src->s && ZSTR_LEN(src->s)) {
101 		smart_str_append_ex(dest, src->s, persistent);
102 	}
103 }
104 
smart_str_append_long_ex(smart_str * dest,zend_long num,zend_bool persistent)105 static zend_always_inline void smart_str_append_long_ex(smart_str *dest, zend_long num, zend_bool persistent) {
106 	char buf[32];
107 	char *result = zend_print_long_to_buf(buf + sizeof(buf) - 1, num);
108 	smart_str_appendl_ex(dest, result, buf + sizeof(buf) - 1 - result, persistent);
109 }
110 
smart_str_append_unsigned_ex(smart_str * dest,zend_ulong num,zend_bool persistent)111 static zend_always_inline void smart_str_append_unsigned_ex(smart_str *dest, zend_ulong num, zend_bool persistent) {
112 	char buf[32];
113 	char *result = zend_print_ulong_to_buf(buf + sizeof(buf) - 1, num);
114 	smart_str_appendl_ex(dest, result, buf + sizeof(buf) - 1 - result, persistent);
115 }
116 
smart_str_setl(smart_str * dest,const char * src,size_t len)117 static zend_always_inline void smart_str_setl(smart_str *dest, const char *src, size_t len) {
118 	smart_str_free(dest);
119 	smart_str_appendl(dest, src, len);
120 }
121 
122 #endif
123