GFXprim
2D bitmap graphics library with emphasis on speed and correctness
Loading...
Searching...
No Matches
gp_vec_str.h
Go to the documentation of this file.
1//SPDX-License-Identifier: LGPL-2.0-or-later
2/*
3
4 Copyright (C) 2022 Cyril Hrubis <metan@ucw.cz>
5
6*/
7
13#ifndef GP_VEC_STR_H
14#define GP_VEC_STR_H
15
16#include <string.h>
17#include <core/gp_compiler.h>
18#include <utils/gp_utf.h>
19#include <utils/gp_vec.h>
20
27static inline char *gp_vec_str_new(void)
28{
29 return gp_vec_new(1, 1);
30}
31
39static inline size_t gp_vec_strlen(char *self)
40{
41 return gp_vec_len(self) - 1;
42}
43
51static inline size_t gp_vec_strsize(char *self)
52{
53 return gp_vec_len(self);
54}
55
63static inline char *gp_vec_strdup(const char *src)
64{
65 char *ret = gp_vec_new(strlen(src)+1, 1);
66
67 if (!ret)
68 return NULL;
69
70 strcpy(ret, src);
71
72 return ret;
73}
74
84GP_WUR static inline char *gp_vec_strins(char *self, size_t off, const char *src)
85{
86 char *ret = gp_vec_ins(self, off, strlen(src));
87
88 if (!ret)
89 return NULL;
90
91 memcpy(ret+off, src, strlen(src));
92
93 return ret;
94}
95
104GP_WUR static inline char *gp_vec_str_append(char *self, const char *str)
105{
106 return gp_vec_strins(self, gp_vec_len(self)-1, str);
107}
108
109#define GP_VEC_STR_APPEND(self, str) ({\
110 char *gp_ret__ = gp_vec_str_append(self, str); \
111 if (gp_ret__) \
112 self = gp_ret__; \
113 gp_ret__; \
114 })
115
125GP_WUR static inline char *gp_vec_chins(char *self, size_t off, char ch)
126{
127 char *ret = gp_vec_ins(self, off, 1);
128
129 if (!ret)
130 return NULL;
131
132 ret[off] = ch;
133
134 return ret;
135}
136
146GP_WUR static inline char *gp_vec_ins_utf8(char *self, size_t off, uint32_t unicode)
147{
148 char *ret = gp_vec_ins(self, off, gp_utf8_bytes(unicode));
149
150 if (!ret)
151 return NULL;
152
153 gp_to_utf8(unicode, ret+off);
154
155 return ret;
156}
157
168GP_WUR
169static inline char *gp_vec_strdel(char *self, size_t off, size_t len)
170{
171 return gp_vec_del(self, off, len);
172}
173
181GP_WUR static inline char *gp_vec_strclr(char *self)
182{
183 char *ret = gp_vec_resize(self, 1);
184
185 ret[0] = 0;
186
187 return ret;
188}
189
199GP_WUR char *gp_vec_printf(char *self, const char *fmt, ...) GP_FMT_PRINTF(2, 3);
200
210GP_WUR char *gp_vec_vprintf(char *self, const char *fmt, va_list va);
211
212#endif /* GP_VEC_STR_H */
A compiler dependent macros.
#define GP_WUR
Expands to warn_unused_result attribute when supported by the compiler.
Definition gp_compiler.h:26
#define GP_FMT_PRINTF(fmt, list)
Expands to format printf attribute when supported by the compiler.
Definition gp_compiler.h:30
Unicode helper macros and functions.
static unsigned int gp_utf8_bytes(uint32_t unicode)
Returns a number of bytes needed to store unicode character into UTF-8.
Definition gp_utf.h:118
static int gp_to_utf8(uint32_t unicode, char *buf)
Writes an unicode character into a UTF-8 buffer.
Definition gp_utf.h:141
Vector a growable array.
void * gp_vec_resize(void *self, size_t len)
Resize vector.
static size_t gp_vec_len(const void *self)
Returns vector lenght.
Definition gp_vec.h:111
void * gp_vec_del(void *self, size_t off, size_t len)
Deletes a range from the vector.
void * gp_vec_new(size_t len, size_t unit)
Allocates a new vector.
void * gp_vec_ins(void *self, size_t off, size_t len)
Insert a gap into the vec of new elements, reallocating the underlying memory if more capacity is nee...
static char * gp_vec_str_new(void)
Allocated new string vector.
Definition gp_vec_str.h:27
static char * gp_vec_chins(char *self, size_t off, char ch)
Inserts a string into a string vector at a given offset.
Definition gp_vec_str.h:125
static char * gp_vec_strins(char *self, size_t off, const char *src)
Inserts a string into a string vector at a given offset.
Definition gp_vec_str.h:84
char * gp_vec_printf(char *self, const char *fmt,...)
Printf into a string vector.
static char * gp_vec_str_append(char *self, const char *str)
Appends a string to a string vector.
Definition gp_vec_str.h:104
static char * gp_vec_ins_utf8(char *self, size_t off, uint32_t unicode)
Inserts an unicode character into an UTF-8 string.
Definition gp_vec_str.h:146
static char * gp_vec_strclr(char *self)
Clears the string.
Definition gp_vec_str.h:181
static size_t gp_vec_strlen(char *self)
Returns string vector len.
Definition gp_vec_str.h:39
static char * gp_vec_strdup(const char *src)
Copies a string into newly allocated string vector.
Definition gp_vec_str.h:63
static char * gp_vec_strdel(char *self, size_t off, size_t len)
Deletes len characters from a string vector at a given offset.
Definition gp_vec_str.h:169
char * gp_vec_vprintf(char *self, const char *fmt, va_list va)
Printf va_list into a string vector.
static size_t gp_vec_strsize(char *self)
Returns string vector size.
Definition gp_vec_str.h:51