xref: /PHP-8.3/Zend/zend_smart_string.h (revision c8955c07)
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    | https://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    |         Xinchen Hui <laruence@php.net>                               |
15    +----------------------------------------------------------------------+
16  */
17 
18 #ifndef PHP_SMART_STRING_H
19 #define PHP_SMART_STRING_H
20 
21 #include "zend_smart_string_public.h"
22 
23 #include <stdlib.h>
24 #include <zend.h>
25 
26 /* wrapper */
27 
28 #define smart_string_appends_ex(str, src, what) \
29 	smart_string_appendl_ex((str), (src), strlen(src), (what))
30 #define smart_string_appends(str, src) \
31 	smart_string_appendl((str), (src), strlen(src))
32 #define smart_string_append_ex(str, src, what) \
33 	smart_string_appendl_ex((str), ((smart_string *)(src))->c, \
34 		((smart_string *)(src))->len, (what));
35 #define smart_string_sets(str, src) \
36 	smart_string_setl((str), (src), strlen(src));
37 
38 #define smart_string_appendc(str, c) \
39 	smart_string_appendc_ex((str), (c), 0)
40 #define smart_string_free(s) \
41 	smart_string_free_ex((s), 0)
42 #define smart_string_appendl(str, src, len) \
43 	smart_string_appendl_ex((str), (src), (len), 0)
44 #define smart_string_append(str, src) \
45 	smart_string_append_ex((str), (src), 0)
46 #define smart_string_append_long(str, val) \
47 	smart_string_append_long_ex((str), (val), 0)
48 #define smart_string_append_unsigned(str, val) \
49 	smart_string_append_unsigned_ex((str), (val), 0)
50 
51 ZEND_API void ZEND_FASTCALL _smart_string_alloc_persistent(smart_string *str, size_t len);
52 ZEND_API void ZEND_FASTCALL _smart_string_alloc(smart_string *str, size_t len);
53 
smart_string_alloc(smart_string * str,size_t len,bool persistent)54 static zend_always_inline size_t smart_string_alloc(smart_string *str, size_t len, bool persistent) {
55 	if (UNEXPECTED(!str->c) || UNEXPECTED(len >= str->a - str->len)) {
56 		if (persistent) {
57 			_smart_string_alloc_persistent(str, len);
58 		} else {
59 			_smart_string_alloc(str, len);
60 		}
61 	}
62 	return str->len + len;
63 }
64 
smart_string_free_ex(smart_string * str,bool persistent)65 static zend_always_inline void smart_string_free_ex(smart_string *str, bool persistent) {
66 	if (str->c) {
67 		pefree(str->c, persistent);
68 		str->c = NULL;
69 	}
70 	str->a = str->len = 0;
71 }
72 
smart_string_0(smart_string * str)73 static zend_always_inline void smart_string_0(smart_string *str) {
74 	if (str->c) {
75 		str->c[str->len] = '\0';
76 	}
77 }
78 
smart_string_appendc_ex(smart_string * dest,char ch,bool persistent)79 static zend_always_inline void smart_string_appendc_ex(smart_string *dest, char ch, bool persistent) {
80 	dest->len = smart_string_alloc(dest, 1, persistent);
81 	dest->c[dest->len - 1] = ch;
82 }
83 
smart_string_appendl_ex(smart_string * dest,const char * str,size_t len,bool persistent)84 static zend_always_inline void smart_string_appendl_ex(smart_string *dest, const char *str, size_t len, bool persistent) {
85 	size_t new_len = smart_string_alloc(dest, len, persistent);
86 	memcpy(dest->c + dest->len, str, len);
87 	dest->len = new_len;
88 
89 }
90 
smart_string_append_long_ex(smart_string * dest,zend_long num,bool persistent)91 static zend_always_inline void smart_string_append_long_ex(smart_string *dest, zend_long num, bool persistent) {
92 	char buf[32];
93 	char *result = zend_print_long_to_buf(buf + sizeof(buf) - 1, num);
94 	smart_string_appendl_ex(dest, result, buf + sizeof(buf) - 1 - result, persistent);
95 }
96 
smart_string_append_unsigned_ex(smart_string * dest,zend_ulong num,bool persistent)97 static zend_always_inline void smart_string_append_unsigned_ex(smart_string *dest, zend_ulong num, bool persistent) {
98 	char buf[32];
99 	char *result = zend_print_ulong_to_buf(buf + sizeof(buf) - 1, num);
100 	smart_string_appendl_ex(dest, result, buf + sizeof(buf) - 1 - result, persistent);
101 }
102 
smart_string_setl(smart_string * dest,char * src,size_t len)103 static zend_always_inline void smart_string_setl(smart_string *dest, char *src, size_t len) {
104 	dest->len = len;
105 	dest->a = len + 1;
106 	dest->c = src;
107 }
108 
smart_string_reset(smart_string * str)109 static zend_always_inline void smart_string_reset(smart_string *str) {
110 	str->len = 0;
111 }
112 
113 #endif
114