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