liba 0.1.15
An algorithm library based on C/C++
 
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Modules Pages
Loading...
Searching...
No Matches
str.h
Go to the documentation of this file.
1
5
6#ifndef LIBA_STR_H
7#define LIBA_STR_H
8
9#include "a.h"
10#include <stdarg.h>
11#include <string.h>
12
18
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->mem_ ? ctx->ptr_ + idx : A_NULL;
73}
74
82A_INTERN char *a_str_of(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->mem_ ? ctx->ptr_ + num : A_NULL;
86}
87
94A_INTERN void a_str_setn_(a_str *ctx, a_size num) { ctx->num_ = num; }
95
103A_INTERN int a_str_setn(a_str *ctx, a_size num)
104{
105 int const rc = (num <= ctx->mem_ ? A_SUCCESS : A_OBOUNDS);
106 if (rc == 0) { ctx->num_ = num; }
107 return rc;
108}
109
110#if defined(__cplusplus)
111extern "C" {
112#endif /* __cplusplus */
113
117A_EXTERN a_str *a_str_new(void);
118
123A_EXTERN void a_str_die(a_str *ctx);
124
129A_EXTERN void a_str_ctor(a_str *ctx);
130
135A_EXTERN void a_str_dtor(a_str *ctx);
136
142A_EXTERN void a_str_swap(a_str *lhs, a_str *rhs);
143
150A_EXTERN char *a_str_exit(a_str *ctx);
151
159A_EXTERN int a_str_setm(a_str *ctx, a_size mem);
160A_EXTERN int a_str_setm_(a_str *ctx, a_size mem);
161
173A_EXTERN int a_str_cmp_(void const *p0, a_size n0, void const *p1, a_size n1);
174
184A_EXTERN int a_str_cmp(a_str const *lhs, a_str const *rhs);
185
196A_EXTERN int a_str_cmpn(a_str const *ctx, void const *pdata, a_size nbyte);
197
207A_EXTERN int a_str_cmps(a_str const *ctx, void const *str);
208
215A_EXTERN int a_str_getc(a_str *ctx);
216A_EXTERN int a_str_getc_(a_str *ctx);
217
225A_EXTERN int a_str_catc(a_str *ctx, int c);
226A_EXTERN int a_str_catc_(a_str *ctx, int c);
227
235A_EXTERN a_size a_str_getn(a_str *ctx, void *pdata, a_size nbyte);
236A_EXTERN a_size a_str_getn_(a_str *ctx, void *pdata, a_size nbyte);
237
246A_EXTERN int a_str_catn(a_str *ctx, void const *pdata, a_size nbyte);
247A_EXTERN int a_str_catn_(a_str *ctx, void const *pdata, a_size nbyte);
248
256A_EXTERN int a_str_cats(a_str *ctx, void const *str);
257A_EXTERN int a_str_cats_(a_str *ctx, void const *str);
258
266A_EXTERN A_FORMAT(__printf__, 2, 0) int a_str_catv(a_str *ctx, char const *fmt, va_list va);
267
274A_EXTERN A_FORMAT(__printf__, 2, 3) int a_str_catf(a_str *ctx, char const *fmt, ...);
275
283A_EXTERN int a_str_cat(a_str *ctx, a_str const *obj);
284A_EXTERN int a_str_cat_(a_str *ctx, a_str const *obj);
285
294A_EXTERN void a_str_rtrim(a_str *ctx, char const *s, a_size n);
295A_EXTERN void a_str_rtrim_(a_str *ctx, char const *s, a_size n);
296
305A_EXTERN void a_str_ltrim(a_str *ctx, char const *s, a_size n);
306A_EXTERN void a_str_ltrim_(a_str *ctx, char const *s, a_size n);
307
316A_EXTERN void a_str_trim(a_str *ctx, char const *s, a_size n);
317A_EXTERN void a_str_trim_(a_str *ctx, char const *s, a_size n);
318
326A_EXTERN a_size a_utf_len(a_str const *ctx, a_size *stop);
327
335A_EXTERN int a_utf_catc(a_str *ctx, a_u32 c);
336
337#if defined(__cplusplus)
338} /* extern "C" */
339#endif /* __cplusplus */
340
342
343#endif /* a/str.h */
algorithm library
int a_str_catf(a_str *ctx, char const *fmt,...)
format string append to a pointer to string structure
char * a_str_ptr(a_str const *ctx)
string for a pointer to string structure
Definition str.h:38
void a_str_ltrim(a_str *ctx, char const *s, a_size n)
trim a string by removing leading characters. If n is non-zero, the function uses the array s to spec...
char * a_str_of(a_str const *ctx, a_diff idx)
access specified character for a pointer to string structure
Definition str.h:82
a_str * a_str_new(void)
allocate a pointer to string structure from memory
void a_str_swap(a_str *lhs, a_str *rhs)
swap the contents of two pointers to string structure
void a_str_setn_(a_str *ctx, a_size num)
set length for a pointer to string structure
Definition str.h:94
int a_utf_catc(a_str *ctx, a_u32 c)
concatenate a unicode character to a pointer to string structure
int a_str_catv(a_str *ctx, char const *fmt, va_list va)
format string append to a pointer to string structure via va_list
int a_str_getc(a_str *ctx)
get a character for a pointer to string structure
a_size a_str_getn(a_str *ctx, void *pdata, a_size nbyte)
get memory block to a pointer to string structure
int a_str_catc(a_str *ctx, int c)
concatenate a character to a pointer to string structure
int a_str_setm(a_str *ctx, a_size mem)
allocate memory for a pointer to string structure
int a_str_cmpn(a_str const *ctx, void const *pdata, a_size nbyte)
compare the string ctx with the memory block
int a_str_catn(a_str *ctx, void const *pdata, a_size nbyte)
concatenate memory block to a pointer to string structure
void a_str_ctor(a_str *ctx)
constructor for string structure
void a_str_trim(a_str *ctx, char const *s, a_size n)
trim a string by removing both leading and trailing characters. If n is non-zero, the function uses t...
char * a_str_exit(a_str *ctx)
terminate a pointer to string structure
int a_str_cat(a_str *ctx, a_str const *obj)
concatenate the string structure obj to the string structure ctx
a_size a_utf_len(a_str const *ctx, a_size *stop)
length for a pointer to string structure using UTF-8
void a_str_die(a_str *ctx)
deallocate a pointer to string structure
void a_str_rtrim(a_str *ctx, char const *s, a_size n)
trim a string by removing trailing characters. If n is non-zero, the function uses the array s to spe...
a_size a_str_len(a_str const *ctx)
length for a pointer to string structure
Definition str.h:45
a_size 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, a_size idx)
access specified character for a pointer to string structure
Definition str.h:61
int a_str_setn(a_str *ctx, a_size num)
set length for a pointer to string structure
Definition str.h:103
int a_str_cmps(a_str const *ctx, void const *str)
compare the string ctx with the C string str
int a_str_cmp(a_str const *lhs, a_str const *rhs)
compare the string lhs with the string rhs
void a_str_dtor(a_str *ctx)
destructor for string structure
int a_str_cats(a_str *ctx, void const *str)
concatenate C string to a pointer to string structure
char * a_str_at(a_str const *ctx, a_size idx)
access specified character for a pointer to string structure
Definition str.h:70
int a_str_cmp_(void const *p0, a_size n0, void const *p1, a_size n1)
compare the memory block 0 with the memory block 1
ptrdiff_t a_diff
signed integer type returned when subtracting two pointers
Definition a.h:780
#define a_size_c(x)
static cast to a_size
Definition a.h:820
unsigned long a_u32
unsigned integer type with width of exactly 32 bits
Definition a.h:538
size_t a_size
unsigned integer type returned by the sizeof operator
Definition a.h:823
instance structure for basic string
Definition str.h:27
a_size mem_
Definition str.h:30
char * ptr_
Definition str.h:28
a_size num_
Definition str.h:29