70673a92ed7c4fd4c8b5f737b75442b98a4d73c9
7 PHPSTR_API phpstr
*phpstr_init_ex(phpstr
*buf
, size_t chunk_size
, zend_bool pre_alloc
)
10 buf
= emalloc(sizeof(phpstr
));
13 buf
->size
= chunk_size
> 0 ? chunk_size
: PHPSTR_DEFAULT_SIZE
;
14 buf
->data
= pre_alloc
? emalloc(buf
->size
) : NULL
;
15 buf
->free
= pre_alloc
? buf
->size
: 0;
21 PHPSTR_API phpstr
*phpstr_from_string_ex(phpstr
*buf
, const char *string
, size_t length
)
23 buf
= phpstr_init(buf
);
24 phpstr_append(buf
, string
, length
);
28 PHPSTR_API
void phpstr_resize_ex(phpstr
*buf
, size_t len
, size_t override_size
)
30 if (buf
->free
< len
) {
31 size_t size
= override_size
? override_size
: buf
->size
;
32 while ((size
+ buf
->free
) < len
) {
36 buf
->data
= erealloc(buf
->data
, buf
->used
+ buf
->free
+ size
);
38 buf
->data
= emalloc(size
);
44 PHPSTR_API
size_t phpstr_append(phpstr
*buf
, const char *append
, size_t append_len
)
46 phpstr_resize(buf
, append_len
);
47 memcpy(buf
->data
+ buf
->used
, append
, append_len
);
48 buf
->used
+= append_len
;
49 buf
->free
-= append_len
;
53 PHPSTR_API
size_t phpstr_appendf(phpstr
*buf
, const char *format
, ...)
59 va_start(argv
, format
);
60 append_len
= vspprintf(&append
, 0, format
, argv
);
63 phpstr_append(buf
, append
, append_len
);
69 PHPSTR_API
size_t phpstr_insert(phpstr
*buf
, const char *insert
, size_t insert_len
, size_t offset
)
71 phpstr_resize(buf
, insert_len
);
72 memmove(buf
->data
+ offset
+ insert_len
, buf
->data
+ offset
, insert_len
);
73 memcpy(buf
->data
+ offset
, insert
, insert_len
);
74 buf
->used
+= insert_len
;
75 buf
->free
-= insert_len
;
79 PHPSTR_API
size_t phpstr_insertf(phpstr
*buf
, size_t offset
, const char *format
, ...)
85 va_start(argv
, format
);
86 insert_len
= vspprintf(&insert
, 0, format
, argv
);
89 phpstr_insert(buf
, insert
, insert_len
, offset
);
95 PHPSTR_API
size_t phpstr_prepend(phpstr
*buf
, const char *prepend
, size_t prepend_len
)
97 phpstr_resize(buf
, prepend_len
);
98 memmove(buf
->data
+ prepend_len
, buf
->data
, buf
->used
);
99 memcpy(buf
->data
, prepend
, prepend_len
);
100 buf
->used
+= prepend_len
;
101 buf
->free
-= prepend_len
;
105 PHPSTR_API
size_t phpstr_prependf(phpstr
*buf
, const char *format
, ...)
111 va_start(argv
, format
);
112 prepend_len
= vspprintf(&prepend
, 0, format
, argv
);
115 phpstr_prepend(buf
, prepend
, prepend_len
);
121 PHPSTR_API
char *phpstr_data(const phpstr
*buf
, char **into
, size_t *len
)
123 char *copy
= ecalloc(1, buf
->used
+ 1);
124 memcpy(copy
, buf
->data
, buf
->used
);
134 PHPSTR_API phpstr
*phpstr_dup(const phpstr
*buf
)
136 phpstr
*dup
= phpstr_clone(buf
);
137 phpstr_append(dup
, buf
->data
, buf
->used
);
141 PHPSTR_API
size_t phpstr_cut(phpstr
*buf
, size_t offset
, size_t length
)
143 if (offset
>= buf
->used
) {
146 if (offset
+ length
> buf
->used
) {
147 length
= buf
->used
- offset
;
149 memmove(buf
->data
+ offset
, buf
->data
+ offset
+ length
, buf
->used
- length
);
155 PHPSTR_API phpstr
*phpstr_sub(const phpstr
*buf
, size_t offset
, size_t length
)
157 if (offset
>= buf
->used
) {
160 size_t need
= (length
+ offset
) > buf
->used
? (buf
->used
- offset
) : (length
- offset
);
161 phpstr
*sub
= phpstr_init_ex(NULL
, need
, 1);
162 phpstr_append(sub
, buf
->data
+ offset
, need
);
163 sub
->size
= buf
->size
;
168 PHPSTR_API phpstr
*phpstr_right(const phpstr
*buf
, size_t length
)
170 if (length
< buf
->used
) {
171 return phpstr_sub(buf
, buf
->used
- length
, length
);
173 return phpstr_sub(buf
, 0, buf
->used
);
178 PHPSTR_API phpstr
*phpstr_merge_va(phpstr
*buf
, unsigned argc
, va_list argv
)
181 buf
= phpstr_init(buf
);
184 phpstr_free_t f
= va_arg(argv
, phpstr_free_t
);
185 phpstr
*current
= va_arg(argv
, phpstr
*);
186 phpstr_append(buf
, current
->data
, current
->used
);
187 FREE_PHPSTR(f
, current
);
193 PHPSTR_API phpstr
*phpstr_merge_ex(phpstr
*buf
, unsigned argc
, ...)
198 va_start(argv
, argc
);
199 ret
= phpstr_merge_va(buf
, argc
, argv
);
204 PHPSTR_API phpstr
*phpstr_merge(unsigned argc
, ...)
209 va_start(argv
, argc
);
210 ret
= phpstr_merge_va(NULL
, argc
, argv
);
215 PHPSTR_API
void phpstr_fix(phpstr
*buf
)
217 phpstr_resize_ex(buf
, 1, 1);
218 buf
->data
[buf
->used
] = '\0';
221 PHPSTR_API
int phpstr_cmp(phpstr
*left
, phpstr
*right
)
223 if (left
->used
> right
->used
) {
225 } else if (right
->used
> left
->used
) {
228 return memcmp(left
->data
, right
->data
, left
->used
);
232 PHPSTR_API
void phpstr_dtor(phpstr
*buf
)
234 STR_SET(buf
->data
, NULL
);
239 PHPSTR_API
void phpstr_free(phpstr
**buf
)
253 * vim600: sw=4 ts=4 fdm=marker