php-internal-docs 8.4.8
Unofficial docs for php/php-src
Loading...
Searching...
No Matches
zend_smart_str.c
Go to the documentation of this file.
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: Dmitry Stogov <dmitry@php.net> |
14 +----------------------------------------------------------------------+
15 */
16
17#include <zend.h>
18#include "zend_smart_str.h"
19#include "zend_smart_string.h"
20#include "zend_enum.h"
21
22#define SMART_STR_OVERHEAD (ZEND_MM_OVERHEAD + _ZSTR_HEADER_SIZE + 1)
23#define SMART_STR_START_SIZE 256
24#define SMART_STR_START_LEN (SMART_STR_START_SIZE - SMART_STR_OVERHEAD)
25#define SMART_STR_PAGE 4096
26
27#define SMART_STR_NEW_LEN(len) \
28 (ZEND_MM_ALIGNED_SIZE_EX(len + SMART_STR_OVERHEAD, SMART_STR_PAGE) - SMART_STR_OVERHEAD)
29
31{
32 if (UNEXPECTED(!str->s)) {
33 str->a = len <= SMART_STR_START_LEN
36 str->s = zend_string_alloc(str->a, 0);
37 ZSTR_LEN(str->s) = 0;
38 } else {
39 str->a = SMART_STR_NEW_LEN(len);
40 str->s = (zend_string *) erealloc2(str->s, str->a + _ZSTR_HEADER_SIZE + 1, _ZSTR_HEADER_SIZE + ZSTR_LEN(str->s));
41 }
42}
43
45{
46 if (UNEXPECTED(!str->s)) {
47 str->a = len <= SMART_STR_START_LEN
50 str->s = zend_string_alloc(str->a, 1);
51 ZSTR_LEN(str->s) = 0;
52 } else {
53 str->a = SMART_STR_NEW_LEN(len);
54 str->s = (zend_string *) perealloc(str->s, str->a + _ZSTR_HEADER_SIZE + 1, 1);
55 }
56}
57
58/* Windows uses VK_ESCAPE instead of \e */
59#ifndef VK_ESCAPE
60#define VK_ESCAPE '\e'
61#endif
62
63static size_t zend_compute_escaped_string_len(const char *s, size_t l) {
64 size_t i, len = l;
65 for (i = 0; i < l; ++i) {
66 char c = s[i];
67 if (c == '\n' || c == '\r' || c == '\t' ||
68 c == '\f' || c == '\v' || c == '\\' || c == VK_ESCAPE) {
69 len += 1;
70 } else if (c < 32 || c > 126) {
71 len += 3;
72 }
73 }
74 return len;
75}
76
77ZEND_API void ZEND_FASTCALL smart_str_append_escaped(smart_str *str, const char *s, size_t l) {
78 char *res;
79 size_t i, len = zend_compute_escaped_string_len(s, l);
80
81 smart_str_alloc(str, len, 0);
82 res = &ZSTR_VAL(str->s)[ZSTR_LEN(str->s)];
83 ZSTR_LEN(str->s) += len;
84
85 for (i = 0; i < l; ++i) {
86 unsigned char c = s[i];
87 if (c < 32 || c == '\\' || c > 126) {
88 *res++ = '\\';
89 switch (c) {
90 case '\n': *res++ = 'n'; break;
91 case '\r': *res++ = 'r'; break;
92 case '\t': *res++ = 't'; break;
93 case '\f': *res++ = 'f'; break;
94 case '\v': *res++ = 'v'; break;
95 case '\\': *res++ = '\\'; break;
96 case VK_ESCAPE: *res++ = 'e'; break;
97 default:
98 *res++ = 'x';
99 if ((c >> 4) < 10) {
100 *res++ = (c >> 4) + '0';
101 } else {
102 *res++ = (c >> 4) + 'A' - 10;
103 }
104 if ((c & 0xf) < 10) {
105 *res++ = (c & 0xf) + '0';
106 } else {
107 *res++ = (c & 0xf) + 'A' - 10;
108 }
109 }
110 } else {
111 *res++ = c;
112 }
113 }
114}
115
117 smart_str *str, double num, int precision, bool zero_fraction) {
119 /* Model snprintf precision behavior. */
120 zend_gcvt(num, precision ? precision : 1, '.', 'E', buf);
121 smart_str_appends(str, buf);
122 if (zero_fraction && zend_finite(num) && !strchr(buf, '.')) {
123 smart_str_appendl(str, ".0", 2);
124 }
125}
126
127ZEND_API void smart_str_append_printf(smart_str *dest, const char *format, ...) {
128 va_list arg;
129 va_start(arg, format);
130 zend_printf_to_smart_str(dest, format, arg);
131 va_end(arg);
132}
133
134#define SMART_STRING_OVERHEAD (ZEND_MM_OVERHEAD + 1)
135#define SMART_STRING_START_SIZE 256
136#define SMART_STRING_START_LEN (SMART_STRING_START_SIZE - SMART_STRING_OVERHEAD)
137#define SMART_STRING_PAGE 4096
138
140{
141 if (!str->c) {
142 str->len = 0;
145 } else {
147 }
148 str->c = pemalloc(str->a + 1, 1);
149 } else {
150 if (UNEXPECTED((size_t) len > SIZE_MAX - str->len)) {
151 zend_error_noreturn(E_ERROR, "String size overflow");
152 }
153 len += str->len;
155 str->c = perealloc(str->c, str->a + 1, 1);
156 }
157}
158
160{
161 if (!str->c) {
162 str->len = 0;
165 str->c = emalloc(SMART_STRING_START_LEN + 1);
166 } else {
169 str->c = emalloc_large(str->a + 1);
170 } else {
171 /* allocate a huge chunk */
172 str->c = emalloc(str->a + 1);
173 }
174 }
175 } else {
176 if (UNEXPECTED((size_t) len > SIZE_MAX - str->len)) {
177 zend_error_noreturn(E_ERROR, "String size overflow");
178 }
179 len += str->len;
181 str->c = erealloc2(str->c, str->a + 1, str->len);
182 }
183}
184
186{
188
189 if (ZSTR_LEN(value) > length) {
190 smart_str_appendl(str, "...", sizeof("...")-1);
191 }
192}
193
194ZEND_API void ZEND_FASTCALL smart_str_append_scalar(smart_str *dest, const zval *value, size_t truncate) {
196
197 switch (Z_TYPE_P(value)) {
198 case IS_UNDEF:
199 case IS_NULL:
200 smart_str_appendl(dest, "NULL", sizeof("NULL")-1);
201 break;
202
203 case IS_TRUE:
204 case IS_FALSE:
205 smart_str_appends(dest, Z_TYPE_P(value) == IS_TRUE ? "true" : "false");
206 break;
207
208 case IS_DOUBLE:
209 smart_str_append_double(dest, Z_DVAL_P(value), (int) EG(precision), true);
210 break;
211
212 case IS_LONG:
213 smart_str_append_long(dest, Z_LVAL_P(value));
214 break;
215
216 case IS_STRING:
217 smart_str_appendc(dest, '\'');
219 smart_str_appendc(dest, '\'');
220 break;
221
223 }
224}
225
227{
228 if (Z_TYPE_P(value) <= IS_STRING) {
229 smart_str_append_scalar(dest, value, truncate);
230 } else if (Z_TYPE_P(value) == IS_OBJECT && (Z_OBJCE_P(value)->ce_flags & ZEND_ACC_ENUM)) {
231 smart_str_append(dest, Z_OBJCE_P(value)->name);
232 smart_str_appends(dest, "::");
233 smart_str_append(dest, Z_STR_P(zend_enum_fetch_case_name(Z_OBJ_P(value))));
234 } else {
235 return FAILURE;
236 }
237 return SUCCESS;
238}
size_t len
Definition apprentice.c:174
strchr(string $haystack, string $needle, bool $before_needle=false)
char s[4]
Definition cdf.c:77
zend_string * res
Definition ffi.c:4692
zval * arg
Definition ffi.c:3975
zend_ffi_ctype_name_buf buf
Definition ffi.c:4685
#define SIZE_MAX
Definition funcs.c:51
#define SUCCESS
Definition hash_sha3.c:261
zend_string * s
ZEND_API ZEND_COLD ZEND_NORETURN void zend_error_noreturn(int type, const char *format,...)
Definition zend.c:1703
void(* zend_printf_to_smart_str)(smart_str *buf, const char *format, va_list ap)
Definition zend.c:92
#define ZEND_MM_ALIGNED_SIZE_EX(size, alignment)
Definition zend_alloc.h:37
#define perealloc(ptr, size, persistent)
Definition zend_alloc.h:201
#define erealloc2(ptr, size, copy_size)
Definition zend_alloc.h:160
#define pemalloc(size, persistent)
Definition zend_alloc.h:189
#define emalloc_large(size)
Definition zend_alloc.h:152
#define emalloc(size)
Definition zend_alloc.h:151
#define ZEND_MM_CHUNK_SIZE
struct _zval_struct zval
#define ZEND_ACC_ENUM
#define ZEND_API
#define E_ERROR
Definition zend_errors.h:23
#define EG(v)
struct _zend_string zend_string
#define zend_finite(a)
#define MIN(a, b)
#define EXPECTED(condition)
#define ZEND_FASTCALL
#define ZEND_ASSERT(c)
#define EMPTY_SWITCH_DEFAULT_CASE()
#define UNEXPECTED(condition)
#define SMART_STR_START_LEN
ZEND_API void ZEND_FASTCALL smart_str_realloc(smart_str *str, size_t len)
ZEND_API void ZEND_FASTCALL smart_str_append_escaped_truncated(smart_str *str, const zend_string *value, size_t length)
ZEND_API void ZEND_FASTCALL smart_str_erealloc(smart_str *str, size_t len)
ZEND_API void ZEND_FASTCALL smart_str_append_double(smart_str *str, double num, int precision, bool zero_fraction)
ZEND_API void ZEND_FASTCALL smart_str_append_escaped(smart_str *str, const char *s, size_t l)
#define SMART_STR_NEW_LEN(len)
ZEND_API zend_result ZEND_FASTCALL smart_str_append_zval(smart_str *dest, const zval *value, size_t truncate)
ZEND_API void smart_str_append_printf(smart_str *dest, const char *format,...)
#define SMART_STRING_PAGE
ZEND_API void ZEND_FASTCALL smart_str_append_scalar(smart_str *dest, const zval *value, size_t truncate)
ZEND_API void ZEND_FASTCALL _smart_string_alloc_persistent(smart_string *str, size_t len)
#define VK_ESCAPE
#define SMART_STRING_START_LEN
#define SMART_STRING_OVERHEAD
ZEND_API void ZEND_FASTCALL _smart_string_alloc(smart_string *str, size_t len)
#define ZSTR_VAL(zstr)
Definition zend_string.h:68
#define ZSTR_LEN(zstr)
Definition zend_string.h:69
#define _ZSTR_HEADER_SIZE
ZEND_API char * zend_gcvt(double value, int ndigit, char dec_point, char exponent, char *buf)
#define ZEND_DOUBLE_MAX_LENGTH
Definition zend_strtod.h:48
#define Z_TYPE_P(zval_p)
Definition zend_types.h:660
#define IS_TRUE
Definition zend_types.h:603
#define IS_FALSE
Definition zend_types.h:602
#define IS_UNDEF
Definition zend_types.h:600
#define IS_STRING
Definition zend_types.h:606
#define Z_OBJ_P(zval_p)
Definition zend_types.h:990
#define IS_DOUBLE
Definition zend_types.h:605
#define Z_STR_P(zval_p)
Definition zend_types.h:972
#define IS_NULL
Definition zend_types.h:601
#define Z_OBJCE_P(zval_p)
@ FAILURE
Definition zend_types.h:61
#define IS_OBJECT
Definition zend_types.h:608
#define IS_LONG
Definition zend_types.h:604
ZEND_RESULT_CODE zend_result
Definition zend_types.h:64
#define Z_DVAL_P(zval_p)
Definition zend_types.h:969
#define Z_LVAL_P(zval_p)
Definition zend_types.h:966
zend_string * name
value