liba 0.1.15
An algorithm library based on C/C++
Loading...
Searching...
No Matches
str.h
Go to the documentation of this file.
1
6#ifndef LIBA_STR_H
7#define LIBA_STR_H
8
9#include "a.h"
10#include <stdarg.h>
11#include <string.h>
12
19// clang-format off
20#define A_STR_INIT {A_NULL, 0, 0}
21// clang-format on
22
26typedef struct a_str
27{
28 char *ptr_;
32
38A_INTERN char *a_str_ptr(a_str const *ctx) { return ctx->ptr_; }
39
45A_INTERN a_size a_str_len(a_str const *ctx) { return ctx->num_; }
46
52A_INTERN a_size a_str_mem(a_str const *ctx) { return ctx->mem_; }
53
61A_INTERN char a_str_at_(a_str const *ctx, a_size idx) { return ctx->ptr_[idx]; }
62
70A_INTERN char a_str_at(a_str const *ctx, a_size idx)
71{
72 return idx < ctx->num_ ? ctx->ptr_[idx] : 0;
73}
74
82A_INTERN char a_str_idx(a_str const *ctx, a_diff idx)
83{
84 a_size const num = idx >= 0 ? a_size_c(idx) : a_size_c(idx) + ctx->num_;
85 return num < ctx->num_ ? ctx->ptr_[num] : 0;
86}
87
94A_INTERN void a_str_setlen_(a_str *ctx, a_size num) { ctx->num_ = num; }
95
104A_INTERN int a_str_setlen(a_str *ctx, a_size num)
105{
106 return num < ctx->mem_ ? ((void)(ctx->num_ = num), A_SUCCESS) : A_FAILURE;
107}
108
113A_INTERN void a_str_drop(a_str *ctx) { ctx->num_ = 0; }
114
115#if defined(__cplusplus)
116extern "C" {
117#endif /* __cplusplus */
118
122A_EXTERN a_str *a_str_new(void);
123
128A_EXTERN void a_str_die(a_str *ctx);
129
134A_EXTERN void a_str_ctor(a_str *ctx);
135
140A_EXTERN void a_str_dtor(a_str *ctx);
141
150A_EXTERN int a_str_copy(a_str *ctx, a_str const *obj);
151
157A_EXTERN void a_str_move(a_str *ctx, a_str *obj);
158
165A_EXTERN char *a_str_exit(a_str *ctx);
166
175A_EXTERN int a_str_alloc(a_str *ctx, a_size mem);
176A_EXTERN int a_str_alloc_(a_str *ctx, a_size mem);
177
189A_EXTERN int a_str_cmp_(void const *p0, a_size n0, void const *p1, a_size n1);
190
200A_EXTERN int a_str_cmp(a_str const *lhs, a_str const *rhs);
201
212A_EXTERN int a_str_cmpn(a_str const *ctx, void const *pdata, a_size nbyte);
213
223A_EXTERN int a_str_cmps(a_str const *ctx, void const *str);
224
231A_EXTERN int a_str_getc(a_str *ctx);
232A_EXTERN int a_str_getc_(a_str *ctx);
233
241A_EXTERN int a_str_putc(a_str *ctx, int c);
242A_EXTERN int a_str_putc_(a_str *ctx, int c);
243
251A_EXTERN a_size a_str_getn(a_str *ctx, void *pdata, a_size nbyte);
252A_EXTERN a_size a_str_getn_(a_str *ctx, void *pdata, a_size nbyte);
253
263A_EXTERN int a_str_setn(a_str *ctx, void const *pdata, a_size nbyte);
264A_EXTERN int a_str_setn_(a_str *ctx, void const *pdata, a_size nbyte);
265
275A_EXTERN int a_str_putn(a_str *ctx, void const *pdata, a_size nbyte);
276A_EXTERN int a_str_putn_(a_str *ctx, void const *pdata, a_size nbyte);
277
286A_EXTERN int a_str_sets(a_str *ctx, void const *str);
287A_EXTERN int a_str_sets_(a_str *ctx, void const *str);
288
297A_EXTERN int a_str_puts(a_str *ctx, void const *str);
298A_EXTERN int a_str_puts_(a_str *ctx, void const *str);
299
307A_EXTERN A_FORMAT(__printf__, 2, 0) int a_str_setv(a_str *ctx, char const *fmt, va_list va);
308
316A_EXTERN A_FORMAT(__printf__, 2, 0) int a_str_putv(a_str *ctx, char const *fmt, va_list va);
317
324A_EXTERN A_FORMAT(__printf__, 2, 3) int a_str_setf(a_str *ctx, char const *fmt, ...);
325
332A_EXTERN A_FORMAT(__printf__, 2, 3) int a_str_putf(a_str *ctx, char const *fmt, ...);
333
342A_EXTERN int a_str_set(a_str *ctx, a_str const *obj);
343A_EXTERN int a_str_set_(a_str *ctx, a_str const *obj);
344
353A_EXTERN int a_str_put(a_str *ctx, a_str const *obj);
354A_EXTERN int a_str_put_(a_str *ctx, a_str const *obj);
355
361A_EXTERN a_size a_str_utflen(a_str const *ctx);
362
363#if defined(__cplusplus)
364} /* extern "C" */
365#endif /* __cplusplus */
366
369#endif /* a/str.h */
algorithm library
int a_str_alloc(a_str *ctx, size_t mem)
allocate memory for a pointer to string structure
char * a_str_ptr(a_str const *ctx)
string for a pointer to string structure
Definition str.h:38
int a_str_put(a_str *ctx, a_str const *obj)
put the string structure obj to the string structure ctx
size_t a_str_len(a_str const *ctx)
length for a pointer to string structure
Definition str.h:45
int a_str_cmp_(void const *p0, size_t n0, void const *p1, size_t n1)
compare the memory block 0 with the memory block 1
size_t a_str_mem(a_str const *ctx)
memory for a pointer to string structure
Definition str.h:52
char a_str_at_(a_str const *ctx, size_t idx)
access specified character for a pointer to string structure
Definition str.h:61
int a_str_cmpn(a_str const *ctx, void const *pdata, size_t nbyte)
compare the string ctx with the memory block
a_str * a_str_new(void)
allocate a pointer to string structure from memory
size_t a_str_getn(a_str *ctx, void *pdata, size_t nbyte)
get memory block to a pointer to string structure
void a_str_move(a_str *ctx, a_str *obj)
initialize a pointer to string structure by moving
int a_str_getc(a_str *ctx)
get character for a pointer to string structure
int a_str_setlen(a_str *ctx, size_t num)
set length for a pointer to string structure
Definition str.h:104
size_t a_str_utflen(a_str const *ctx)
length for a pointer to string structure using UTF-8
void a_str_setlen_(a_str *ctx, size_t num)
set length for a pointer to string structure
Definition str.h:94
void a_str_drop(a_str *ctx)
drop all characters for a pointer to string structure
Definition str.h:113
void a_str_ctor(a_str *ctx)
constructor for string structure
char a_str_at(a_str const *ctx, size_t idx)
access specified character for a pointer to string structure
Definition str.h:70
int a_str_setv(a_str *ctx, char const *fmt, va_list va)
format string to a pointer to string structure via va_list
int a_str_puts(a_str *ctx, void const *str)
put C string to a pointer to string structure
int a_str_putc(a_str *ctx, int c)
put character to a pointer to string structure
int a_str_setn(a_str *ctx, void const *pdata, size_t nbyte)
set memory block to a pointer to string structure
char * a_str_exit(a_str *ctx)
terminate a pointer to string structure
int a_str_set(a_str *ctx, a_str const *obj)
set the string structure obj to the string structure ctx
int a_str_putn(a_str *ctx, void const *pdata, size_t nbyte)
put memory block to a pointer to string structure
void a_str_die(a_str *ctx)
deallocate a pointer to string structure
int a_str_putf(a_str *ctx, char const *fmt,...)
format string append to a pointer to string structure
struct a_str a_str
instance structure for basic string
int a_str_copy(a_str *ctx, a_str const *obj)
initialize a pointer to string structure by copying
int a_str_cmps(a_str const *ctx, void const *str)
compare the string ctx with the C string str
int a_str_setf(a_str *ctx, char const *fmt,...)
format string to a pointer to string structure
int a_str_sets(a_str *ctx, void const *str)
set C string to a pointer to string structure
int a_str_cmp(a_str const *lhs, a_str const *rhs)
compare the string lhs with the string rhs
char a_str_idx(a_str const *ctx, ptrdiff_t idx)
access specified character for a pointer to string structure
Definition str.h:82
void a_str_dtor(a_str *ctx)
destructor for string structure
int a_str_putv(a_str *ctx, char const *fmt, va_list va)
format string append to a pointer to string structure via va_list
#define a_size
Definition a.h:610
#define a_diff
Definition a.h:598
#define a_size_c(x)
Definition a.h:607
@ A_SUCCESS
Definition a.h:983
@ A_FAILURE
Definition a.h:984
instance structure for basic string
Definition str.h:27
size_t num_
Definition str.h:29
size_t mem_
Definition str.h:30
char * ptr_
Definition str.h:28