7 PHPSTR_API phpstr
*phpstr_init_ex(phpstr
*buf
, size_t chunk_size
, int flags
)
10 buf
= pemalloc(sizeof(phpstr
), flags
& PHPSTR_INIT_PERSISTENT
);
14 buf
->size
= (chunk_size
) ? chunk_size
: PHPSTR_DEFAULT_SIZE
;
15 buf
->pmem
= (flags
& PHPSTR_INIT_PERSISTENT
) ? 1 : 0;
16 buf
->data
= (flags
& PHPSTR_INIT_PREALLOC
) ? pemalloc(buf
->size
, buf
->pmem
) : NULL
;
17 buf
->free
= (flags
& PHPSTR_INIT_PREALLOC
) ? buf
->size
: 0;
24 PHPSTR_API phpstr
*phpstr_from_string_ex(phpstr
*buf
, const char *string
, size_t length
)
26 if ((buf
= phpstr_init(buf
))) {
27 if (PHPSTR_NOMEM
== phpstr_append(buf
, string
, length
)) {
28 pefree(buf
, buf
->pmem
);
35 PHPSTR_API
size_t phpstr_resize_ex(phpstr
*buf
, size_t len
, size_t override_size
, int allow_error
)
39 fprintf(stderr
, "RESIZE: size=%lu, used=%lu, free=%lu\n", buf
->size
, buf
->used
, buf
->free
);
41 if (buf
->free
< len
) {
42 size_t size
= override_size
? override_size
: buf
->size
;
44 while ((size
+ buf
->free
) < len
) {
49 ptr
= perealloc_recoverable(buf
->data
, buf
->used
+ buf
->free
+ size
, buf
->pmem
);
51 ptr
= perealloc(buf
->data
, buf
->used
+ buf
->free
+ size
, buf
->pmem
);
66 PHPSTR_API
size_t phpstr_shrink(phpstr
*buf
)
68 /* avoid another realloc on fixation */
70 char *ptr
= perealloc(buf
->data
, buf
->used
+ 1, buf
->pmem
);
82 PHPSTR_API
size_t phpstr_append(phpstr
*buf
, const char *append
, size_t append_len
)
84 if (PHPSTR_NOMEM
== phpstr_resize(buf
, append_len
)) {
87 memcpy(buf
->data
+ buf
->used
, append
, append_len
);
88 buf
->used
+= append_len
;
89 buf
->free
-= append_len
;
93 PHPSTR_API
size_t phpstr_appendf(phpstr
*buf
, const char *format
, ...)
97 size_t append_len
, alloc
;
99 va_start(argv
, format
);
100 append_len
= vspprintf(&append
, 0, format
, argv
);
103 alloc
= phpstr_append(buf
, append
, append_len
);
106 if (PHPSTR_NOMEM
== alloc
) {
112 PHPSTR_API
size_t phpstr_insert(phpstr
*buf
, const char *insert
, size_t insert_len
, size_t offset
)
114 if (PHPSTR_NOMEM
== phpstr_resize(buf
, insert_len
)) {
117 memmove(buf
->data
+ offset
+ insert_len
, buf
->data
+ offset
, insert_len
);
118 memcpy(buf
->data
+ offset
, insert
, insert_len
);
119 buf
->used
+= insert_len
;
120 buf
->free
-= insert_len
;
124 PHPSTR_API
size_t phpstr_insertf(phpstr
*buf
, size_t offset
, const char *format
, ...)
128 size_t insert_len
, alloc
;
130 va_start(argv
, format
);
131 insert_len
= vspprintf(&insert
, 0, format
, argv
);
134 alloc
= phpstr_insert(buf
, insert
, insert_len
, offset
);
137 if (PHPSTR_NOMEM
== alloc
) {
143 PHPSTR_API
size_t phpstr_prepend(phpstr
*buf
, const char *prepend
, size_t prepend_len
)
145 if (PHPSTR_NOMEM
== phpstr_resize(buf
, prepend_len
)) {
148 memmove(buf
->data
+ prepend_len
, buf
->data
, buf
->used
);
149 memcpy(buf
->data
, prepend
, prepend_len
);
150 buf
->used
+= prepend_len
;
151 buf
->free
-= prepend_len
;
155 PHPSTR_API
size_t phpstr_prependf(phpstr
*buf
, const char *format
, ...)
159 size_t prepend_len
, alloc
;
161 va_start(argv
, format
);
162 prepend_len
= vspprintf(&prepend
, 0, format
, argv
);
165 alloc
= phpstr_prepend(buf
, prepend
, prepend_len
);
168 if (PHPSTR_NOMEM
== alloc
) {
174 PHPSTR_API
char *phpstr_data(const phpstr
*buf
, char **into
, size_t *len
)
176 char *copy
= ecalloc(1, buf
->used
+ 1);
177 memcpy(copy
, buf
->data
, buf
->used
);
187 PHPSTR_API phpstr
*phpstr_dup(const phpstr
*buf
)
189 phpstr
*dup
= phpstr_clone(buf
);
190 if (PHPSTR_NOMEM
== phpstr_append(dup
, buf
->data
, buf
->used
)) {
196 PHPSTR_API
size_t phpstr_cut(phpstr
*buf
, size_t offset
, size_t length
)
198 if (offset
>= buf
->used
) {
201 if (offset
+ length
> buf
->used
) {
202 length
= buf
->used
- offset
;
204 memmove(buf
->data
+ offset
, buf
->data
+ offset
+ length
, buf
->used
- length
);
210 PHPSTR_API phpstr
*phpstr_sub(const phpstr
*buf
, size_t offset
, size_t length
)
212 if (offset
>= buf
->used
) {
215 size_t need
= 1 + ((length
+ offset
) > buf
->used
? (buf
->used
- offset
) : (length
- offset
));
216 phpstr
*sub
= phpstr_init_ex(NULL
, need
, PHPSTR_INIT_PREALLOC
| (buf
->pmem
? PHPSTR_INIT_PERSISTENT
:0));
218 if (PHPSTR_NOMEM
== phpstr_append(sub
, buf
->data
+ offset
, need
)) {
221 sub
->size
= buf
->size
;
228 PHPSTR_API phpstr
*phpstr_right(const phpstr
*buf
, size_t length
)
230 if (length
< buf
->used
) {
231 return phpstr_sub(buf
, buf
->used
- length
, length
);
233 return phpstr_sub(buf
, 0, buf
->used
);
238 PHPSTR_API phpstr
*phpstr_merge_va(phpstr
*buf
, unsigned argc
, va_list argv
)
241 buf
= phpstr_init(buf
);
245 phpstr_free_t f
= va_arg(argv
, phpstr_free_t
);
246 phpstr
*current
= va_arg(argv
, phpstr
*);
247 phpstr_append(buf
, current
->data
, current
->used
);
248 FREE_PHPSTR(f
, current
);
255 PHPSTR_API phpstr
*phpstr_merge_ex(phpstr
*buf
, unsigned argc
, ...)
260 va_start(argv
, argc
);
261 ret
= phpstr_merge_va(buf
, argc
, argv
);
266 PHPSTR_API phpstr
*phpstr_merge(unsigned argc
, ...)
271 va_start(argv
, argc
);
272 ret
= phpstr_merge_va(NULL
, argc
, argv
);
277 PHPSTR_API phpstr
*phpstr_fix(phpstr
*buf
)
279 if (PHPSTR_NOMEM
== phpstr_resize_ex(buf
, 1, 1, 0)) {
282 buf
->data
[buf
->used
] = '\0';
286 PHPSTR_API
int phpstr_cmp(phpstr
*left
, phpstr
*right
)
288 if (left
->used
> right
->used
) {
290 } else if (right
->used
> left
->used
) {
293 return memcmp(left
->data
, right
->data
, left
->used
);
297 PHPSTR_API
void phpstr_reset(phpstr
*buf
)
299 buf
->free
+= buf
->used
;
303 PHPSTR_API
void phpstr_dtor(phpstr
*buf
)
306 pefree(buf
->data
, buf
->pmem
);
313 PHPSTR_API
void phpstr_free(phpstr
**buf
)
317 pefree(*buf
, (*buf
)->pmem
);
322 PHPSTR_API
size_t phpstr_chunk_buffer(phpstr
**s
, const char *data
, size_t data_len
, char **chunk
, size_t chunk_size
)
329 *s
= phpstr_init_ex(NULL
, chunk_size
<< 1, chunk_size
? PHPSTR_INIT_PREALLOC
: 0);
334 phpstr_append(storage
, data
, data_len
);
338 phpstr_data(storage
, chunk
, &chunk_size
);
343 if (storage
->used
>= (chunk_size
= storage
->size
>> 1)) {
344 *chunk
= estrndup(storage
->data
, chunk_size
);
345 phpstr_cut(storage
, 0, chunk_size
);
352 PHPSTR_API
void phpstr_chunked_output(phpstr
**s
, const char *data
, size_t data_len
, size_t chunk_len
, phpstr_passthru_func passthru
, void *opaque TSRMLS_DC
)
357 while ((got
= phpstr_chunk_buffer(s
, data
, data_len
, &chunk
, chunk_len
))) {
358 passthru(opaque
, chunk
, got TSRMLS_CC
);
360 /* we already got the last chunk,
361 and freed all resources */
366 STR_SET(chunk
, NULL
);
376 * vim600: sw=4 ts=4 fdm=marker