php-internal-docs 8.4.8
Unofficial docs for php/php-src
Loading...
Searching...
No Matches
zend_smart_string.h
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: 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
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
53
54static 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) {
58 } else {
60 }
61 }
62 return str->len + len;
63}
64
65static 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
73static zend_always_inline void smart_string_0(smart_string *str) {
74 if (str->c) {
75 str->c[str->len] = '\0';
76 }
77}
78
79static 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
84static 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
91static 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
97static 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
103static 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
109static zend_always_inline void smart_string_reset(smart_string *str) {
110 str->len = 0;
111}
112
113#endif
size_t len
Definition apprentice.c:174
zend_long ch
Definition ffi.c:4580
memcpy(ptr1, ptr2, size)
ffi persistent
Definition ffi.c:3633
zend_ffi_ctype_name_buf buf
Definition ffi.c:4685
#define NULL
Definition gdcache.h:45
#define pefree(ptr, persistent)
Definition zend_alloc.h:191
#define ZEND_API
int32_t zend_long
Definition zend_long.h:42
uint32_t zend_ulong
Definition zend_long.h:43
#define zend_always_inline
#define ZEND_FASTCALL
#define UNEXPECTED(condition)
ZEND_API void ZEND_FASTCALL _smart_string_alloc_persistent(smart_string *str, size_t len)
ZEND_API void ZEND_FASTCALL _smart_string_alloc(smart_string *str, size_t len)
bool result