xref: /PHP-7.0/Zend/zend_smart_str.h (revision 478f119a)
1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 7                                                        |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 1997-2017 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 
END_EXTERN_C()49 END_EXTERN_C()
50 
51 static zend_always_inline size_t smart_str_alloc(smart_str *str, size_t len, zend_bool persistent) {
52 	if (UNEXPECTED(!str->s)) {
53 		goto do_smart_str_realloc;
54 	} else {
55 		len += ZSTR_LEN(str->s);
56 		if (UNEXPECTED(len >= str->a)) {
57 do_smart_str_realloc:
58 			if (persistent) {
59 				smart_str_realloc(str, len);
60 			} else {
61 				smart_str_erealloc(str, len);
62 			}
63 		}
64 	}
65 	return len;
66 }
67 
smart_str_free(smart_str * str)68 static zend_always_inline void smart_str_free(smart_str *str) {
69 	if (str->s) {
70 		zend_string_release(str->s);
71 		str->s = NULL;
72 	}
73 	str->a = 0;
74 }
75 
smart_str_0(smart_str * str)76 static zend_always_inline void smart_str_0(smart_str *str) {
77 	if (str->s) {
78 		ZSTR_VAL(str->s)[ZSTR_LEN(str->s)] = '\0';
79 	}
80 }
81 
smart_str_appendc_ex(smart_str * dest,char ch,zend_bool persistent)82 static zend_always_inline void smart_str_appendc_ex(smart_str *dest, char ch, zend_bool persistent) {
83 	size_t new_len = smart_str_alloc(dest, 1, persistent);
84 	ZSTR_VAL(dest->s)[new_len - 1] = ch;
85 	ZSTR_LEN(dest->s) = new_len;
86 }
87 
smart_str_appendl_ex(smart_str * dest,const char * str,size_t len,zend_bool persistent)88 static zend_always_inline void smart_str_appendl_ex(smart_str *dest, const char *str, size_t len, zend_bool persistent) {
89 	size_t new_len = smart_str_alloc(dest, len, persistent);
90 	memcpy(ZSTR_VAL(dest->s) + ZSTR_LEN(dest->s), str, len);
91 	ZSTR_LEN(dest->s) = new_len;
92 }
93 
smart_str_append_ex(smart_str * dest,const zend_string * src,zend_bool persistent)94 static zend_always_inline void smart_str_append_ex(smart_str *dest, const zend_string *src, zend_bool persistent) {
95 	smart_str_appendl_ex(dest, ZSTR_VAL(src), ZSTR_LEN(src), persistent);
96 }
97 
smart_str_append_smart_str_ex(smart_str * dest,const smart_str * src,zend_bool persistent)98 static zend_always_inline void smart_str_append_smart_str_ex(smart_str *dest, const smart_str *src, zend_bool persistent) {
99 	if (src->s && ZSTR_LEN(src->s)) {
100 		smart_str_append_ex(dest, src->s, persistent);
101 	}
102 }
103 
smart_str_append_long_ex(smart_str * dest,zend_long num,zend_bool persistent)104 static zend_always_inline void smart_str_append_long_ex(smart_str *dest, zend_long num, zend_bool persistent) {
105 	char buf[32];
106 	char *result = zend_print_long_to_buf(buf + sizeof(buf) - 1, num);
107 	smart_str_appendl_ex(dest, result, buf + sizeof(buf) - 1 - result, persistent);
108 }
109 
smart_str_append_unsigned_ex(smart_str * dest,zend_ulong num,zend_bool persistent)110 static zend_always_inline void smart_str_append_unsigned_ex(smart_str *dest, zend_ulong num, zend_bool persistent) {
111 	char buf[32];
112 	char *result = zend_print_ulong_to_buf(buf + sizeof(buf) - 1, num);
113 	smart_str_appendl_ex(dest, result, buf + sizeof(buf) - 1 - result, persistent);
114 }
115 
smart_str_setl(smart_str * dest,const char * src,size_t len)116 static zend_always_inline void smart_str_setl(smart_str *dest, const char *src, size_t len) {
117 	smart_str_free(dest);
118 	smart_str_appendl(dest, src, len);
119 }
120 
121 #endif
122 
123