2 #if !defined _CRT_SECURE_NO_WARNINGS
3 #define _CRT_SECURE_NO_WARNINGS /* NOLINT */
4 #endif /* _CRT_SECURE_NO_WARNINGS */
10 a_str
*const ctx
= (a_str
*)a_alloc(A_NULL
, sizeof(a_str
));
11 if (ctx
) { a_str_ctor(ctx
); }
15 void a_str_die(a_str
*ctx
)
24 void a_str_ctor(a_str
*ctx
)
31 void a_str_dtor(a_str
*ctx
)
35 a_alloc(ctx
->ptr_
, 0);
42 int a_str_copy(a_str
*ctx
, a_str
const *obj
)
46 ok
= a_str_alloc_(ctx
, obj
->num_
+ 1);
49 if (obj
->num_
) { a_copy(ctx
->ptr_
, obj
->ptr_
, obj
->num_
); }
50 ctx
->ptr_
[obj
->num_
] = 0;
51 ctx
->num_
= obj
->num_
;
56 void a_str_move(a_str
*ctx
, a_str
*obj
)
58 a_copy(ctx
, obj
, sizeof(*obj
));
59 a_zero(obj
, sizeof(*obj
));
62 char *a_str_exit(a_str
*ctx
)
64 char *const str
= ctx
->ptr_
;
67 ctx
->ptr_
[ctx
->num_
] = 0;
75 int a_str_alloc_(a_str
*ctx
, a_size mem
)
78 mem
= a_size_up(sizeof(void *), mem
);
79 ptr
= (char *)a_alloc(ctx
->ptr_
, mem
);
89 int a_str_alloc(a_str
*ctx
, a_size mem
)
93 return a_str_alloc_(ctx
, mem
);
98 int a_str_cmp_(void const *p0
, a_size n0
, void const *p1
, a_size n1
)
103 ok
= memcmp(p0
, p1
, A_MIN(n0
, n1
));
104 if (ok
) { return ok
; }
106 return (n0
> n1
) - (n0
< n1
);
109 int a_str_cmp(a_str
const *ctx
, a_str
const *str
)
111 return a_str_cmp_(ctx
->ptr_
, ctx
->num_
, str
->ptr_
, str
->num_
);
114 int a_str_cmpn(a_str
const *ctx
, void const *pdata
, a_size nbyte
)
116 return a_str_cmp_(ctx
->ptr_
, ctx
->num_
, pdata
, nbyte
);
119 int a_str_cmps(a_str
const *ctx
, void const *str
)
121 return a_str_cmp_(ctx
->ptr_
, ctx
->num_
, str
, strlen((char const *)str
));
124 int a_str_getc_(a_str
*ctx
)
129 c
= (int)ctx
->ptr_
[--ctx
->num_
];
134 int a_str_getc(a_str
*ctx
)
139 c
= (int)ctx
->ptr_
[--ctx
->num_
];
140 ctx
->ptr_
[ctx
->num_
] = 0;
145 int a_str_putc_(a_str
*ctx
, int c
)
147 if (a_str_alloc(ctx
, ctx
->num_
+ 1) == A_SUCCESS
)
149 ctx
->ptr_
[ctx
->num_
++] = (char)c
;
155 int a_str_putc(a_str
*ctx
, int c
)
157 if (a_str_alloc(ctx
, ctx
->num_
+ 2) == A_SUCCESS
)
159 ctx
->ptr_
[ctx
->num_
++] = (char)c
;
160 ctx
->ptr_
[ctx
->num_
] = 0;
166 a_size
a_str_getn_(a_str
*ctx
, void *pdata
, a_size nbyte
)
168 if (nbyte
> ctx
->num_
) { nbyte
= ctx
->num_
; }
172 if (pdata
) { a_copy(pdata
, ctx
->ptr_
+ ctx
->num_
, nbyte
); }
177 a_size
a_str_getn(a_str
*ctx
, void *pdata
, a_size nbyte
)
179 if (nbyte
> ctx
->num_
) { nbyte
= ctx
->num_
; }
183 if (pdata
) { a_copy(pdata
, ctx
->ptr_
+ ctx
->num_
, nbyte
); }
184 ctx
->ptr_
[ctx
->num_
] = 0;
189 int a_str_setn_(a_str
*ctx
, void const *pdata
, a_size nbyte
)
191 int ok
= a_str_alloc(ctx
, nbyte
);
192 if (ok
== A_SUCCESS
&& nbyte
)
194 a_copy(ctx
->ptr_
, pdata
, nbyte
);
200 int a_str_putn_(a_str
*ctx
, void const *pdata
, a_size nbyte
)
202 int ok
= a_str_alloc(ctx
, ctx
->num_
+ nbyte
);
203 if (ok
== A_SUCCESS
&& nbyte
)
205 a_copy(ctx
->ptr_
+ ctx
->num_
, pdata
, nbyte
);
211 int a_str_setn(a_str
*ctx
, void const *pdata
, a_size nbyte
)
213 int ok
= a_str_alloc(ctx
, nbyte
+ 1);
216 if (nbyte
) { a_copy(ctx
->ptr_
, pdata
, nbyte
); }
217 ctx
->ptr_
[nbyte
] = 0;
223 int a_str_putn(a_str
*ctx
, void const *pdata
, a_size nbyte
)
225 int ok
= a_str_alloc(ctx
, ctx
->num_
+ nbyte
+ 1);
230 a_copy(ctx
->ptr_
+ ctx
->num_
, pdata
, nbyte
);
233 ctx
->ptr_
[ctx
->num_
] = 0;
238 int a_str_sets_(a_str
*ctx
, void const *str
)
240 return a_str_setn_(ctx
, str
, strlen((char const *)str
));
242 int a_str_sets(a_str
*ctx
, void const *str
)
244 return a_str_setn(ctx
, str
, strlen((char const *)str
));
247 int a_str_puts_(a_str
*ctx
, void const *str
)
249 return a_str_putn_(ctx
, str
, strlen((char const *)str
));
251 int a_str_puts(a_str
*ctx
, void const *str
)
253 return a_str_putn(ctx
, str
, strlen((char const *)str
));
256 int a_str_set_(a_str
*ctx
, a_str
const *obj
)
258 return a_str_setn_(ctx
, obj
->ptr_
, obj
->num_
);
260 int a_str_set(a_str
*ctx
, a_str
const *obj
)
262 return a_str_setn(ctx
, obj
->ptr_
, obj
->num_
);
265 int a_str_put_(a_str
*ctx
, a_str
const *obj
)
267 return a_str_putn_(ctx
, obj
->ptr_
, obj
->num_
);
269 int a_str_put(a_str
*ctx
, a_str
const *obj
)
271 return a_str_putn(ctx
, obj
->ptr_
, obj
->num_
);
276 int a_str_setv(a_str
*ctx
, char const *fmt
, va_list va
)
281 res
= vsnprintf(ctx
->ptr_
, ctx
->mem_
, fmt
, ap
);
283 if ((a_size
)res
+ 1 > ctx
->mem_
)
285 if (A_UNLIKELY(a_str_alloc_(ctx
, (a_size
)res
+ 1))) { return 0; }
287 res
= vsnprintf(ctx
->ptr_
, ctx
->mem_
, fmt
, ap
);
290 if (res
>= 0) { ctx
->num_
= (a_size
)res
; }
294 int a_str_putv(a_str
*ctx
, char const *fmt
, va_list va
)
299 char *ptr
= ctx
->ptr_
? ctx
->ptr_
+ ctx
->num_
: ctx
->ptr_
;
301 mem
= ctx
->mem_
- ctx
->num_
;
302 res
= vsnprintf(ptr
, mem
, fmt
, ap
);
303 mem
= ctx
->num_
+ (a_size
)(res
+ 1);
307 if (A_UNLIKELY(a_str_alloc_(ctx
, mem
))) { return 0; }
309 ptr
= ctx
->ptr_
+ ctx
->num_
;
310 mem
= ctx
->mem_
- ctx
->num_
;
311 res
= vsnprintf(ptr
, mem
, fmt
, ap
);
314 if (res
> 0) { ctx
->num_
+= (a_size
)res
; }
318 int a_str_setf(a_str
*ctx
, char const *fmt
, ...)
323 res
= a_str_setv(ctx
, fmt
, va
);
328 int a_str_putf(a_str
*ctx
, char const *fmt
, ...)
333 res
= a_str_putv(ctx
, fmt
, va
);
340 a_size
a_str_utflen(a_str
const *ctx
)
345 char const *head
= ctx
->ptr_
;
346 char const *const tail
= head
+ ctx
->num_
;
347 unsigned int offset
= a_utf_decode(head
, A_NULL
);
348 for (; offset
; offset
= a_utf_decode(head
, A_NULL
))
352 if (head
>= tail
) { break; }