7 #ifndef PHPSTR_DEFAULT_SIZE
8 #define PHPSTR_DEFAULT_SIZE 256
11 PHPSTR_API phpstr
*phpstr_init_ex(phpstr
*buf
, size_t chunk_size
, zend_bool pre_alloc
)
14 buf
= emalloc(sizeof(phpstr
));
17 buf
->size
= chunk_size
> 0 ? chunk_size
: PHPSTR_DEFAULT_SIZE
;
18 buf
->data
= pre_alloc
? emalloc(buf
->size
) : NULL
;
19 buf
->free
= pre_alloc
? buf
->size
: 0;
25 PHPSTR_API phpstr
*phpstr_from_string_ex(phpstr
*buf
, const char *string
, size_t length
)
27 buf
= phpstr_init(buf
);
28 phpstr_append(buf
, string
, length
);
32 PHPSTR_API
void phpstr_resize_ex(phpstr
*buf
, size_t len
, size_t override_size
)
34 if (buf
->free
< len
) {
35 size_t size
= override_size
? override_size
: buf
->size
;
36 while ((size
+ buf
->free
) < len
) {
40 buf
->data
= erealloc(buf
->data
, buf
->used
+ buf
->free
+ size
);
42 buf
->data
= emalloc(size
);
48 PHPSTR_API
void phpstr_append(phpstr
*buf
, const char *append
, size_t append_len
)
50 phpstr_resize(buf
, append_len
);
51 memcpy(buf
->data
+ buf
->used
, append
, append_len
);
52 buf
->used
+= append_len
;
53 buf
->free
-= append_len
;
56 PHPSTR_API
size_t phpstr_appendf(phpstr
*buf
, const char *format
, ...)
62 va_start(argv
, format
);
63 append_len
= vspprintf(&append
, 0, format
, argv
);
66 phpstr_append(buf
, append
, append_len
);
72 PHPSTR_API
void phpstr_insert(phpstr
*buf
, const char *insert
, size_t insert_len
, size_t offset
)
74 phpstr_resize(buf
, insert_len
);
75 memmove(buf
->data
+ offset
+ insert_len
, buf
->data
+ offset
, insert_len
);
76 memcpy(buf
->data
+ offset
, insert
, insert_len
);
77 buf
->used
+= insert_len
;
78 buf
->free
-= insert_len
;
81 PHPSTR_API
size_t phpstr_insertf(phpstr
*buf
, size_t offset
, const char *format
, ...)
87 va_start(argv
, format
);
88 insert_len
= vspprintf(&insert
, 0, format
, argv
);
91 phpstr_insert(buf
, insert
, insert_len
, offset
);
97 PHPSTR_API
void phpstr_prepend(phpstr
*buf
, const char *prepend
, size_t prepend_len
)
99 phpstr_resize(buf
, prepend_len
);
100 memmove(buf
->data
+ prepend_len
, buf
->data
, buf
->used
);
101 memcpy(buf
->data
, prepend
, prepend_len
);
102 buf
->used
+= prepend_len
;
103 buf
->free
-= prepend_len
;
106 PHPSTR_API
size_t phpstr_prependf(phpstr
*buf
, const char *format
, ...)
112 va_start(argv
, format
);
113 prepend_len
= vspprintf(&prepend
, 0, format
, argv
);
116 phpstr_prepend(buf
, prepend
, prepend_len
);
122 PHPSTR_API
char *phpstr_data(const phpstr
*buf
, char **into
, size_t *len
)
124 char *copy
= ecalloc(1, buf
->used
+ 1);
125 memcpy(copy
, buf
->data
, buf
->used
);
135 PHPSTR_API phpstr
*phpstr_dup(const phpstr
*buf
)
137 phpstr
*dup
= phpstr_clone(buf
);
138 phpstr_append(dup
, buf
->data
, buf
->used
);
142 PHPSTR_API
size_t phpstr_cut(phpstr
*buf
, size_t offset
, size_t length
)
144 if (offset
>= buf
->used
) {
147 if (offset
+ length
> buf
->used
) {
148 length
= buf
->used
- offset
;
150 memmove(buf
->data
+ offset
, buf
->data
+ offset
+ length
, buf
->used
- length
);
156 PHPSTR_API phpstr
*phpstr_sub(const phpstr
*buf
, size_t offset
, size_t length
)
158 if (offset
>= buf
->used
) {
161 size_t need
= (length
+ offset
) > buf
->used
? (buf
->used
- offset
) : (length
- offset
);
162 phpstr
*sub
= phpstr_init_ex(NULL
, need
, 1);
163 phpstr_append(sub
, buf
->data
+ offset
, need
);
164 sub
->size
= buf
->size
;
169 PHPSTR_API phpstr
*phpstr_right(const phpstr
*buf
, size_t length
)
171 if (length
< buf
->used
) {
172 return phpstr_sub(buf
, buf
->used
- length
, length
);
174 return phpstr_sub(buf
, 0, buf
->used
);
179 PHPSTR_API phpstr
*phpstr_merge_va(phpstr
*buf
, unsigned argc
, va_list argv
)
182 buf
= phpstr_init(buf
);
185 phpstr_free_t f
= va_arg(argv
, phpstr_free_t
);
186 phpstr
*current
= va_arg(argv
, phpstr
*);
187 phpstr_append(buf
, current
->data
, current
->used
);
188 FREE_PHPSTR(f
, current
);
194 PHPSTR_API phpstr
*phpstr_merge_ex(phpstr
*buf
, unsigned argc
, ...)
199 va_start(argv
, argc
);
200 ret
= phpstr_merge_va(buf
, argc
, argv
);
205 PHPSTR_API phpstr
*phpstr_merge(unsigned argc
, ...)
210 va_start(argv
, argc
);
211 ret
= phpstr_merge_va(NULL
, argc
, argv
);
216 PHPSTR_API
void phpstr_fix(phpstr
*buf
)
218 phpstr_resize_ex(buf
, 1, 1);
219 buf
->data
[buf
->used
] = '\0';
222 PHPSTR_API
int phpstr_cmp(phpstr
*left
, phpstr
*right
)
224 if (left
->used
> right
->used
) {
226 } else if (right
->used
> left
->used
) {
229 return memcmp(left
->data
, right
->data
, left
->used
);
233 PHPSTR_API
void phpstr_dtor(phpstr
*buf
)
243 PHPSTR_API
void phpstr_free(phpstr
*buf
)
256 * vim600: sw=4 ts=4 fdm=marker