json-gen-c  0.1.5
json-gen-c generate C code for json manipulation
sstr.h
Go to the documentation of this file.
1
35#ifndef SSTR_H_
36#define SSTR_H_
37
38#include <stdarg.h>
39#include <stddef.h>
40#include <stdbool.h>
41#include <stdint.h>
42
43#ifdef __cplusplus
44extern "C" {
45#endif
46
47#define SHORT_STR_CAPACITY 25
48#define CAP_ADD_DELTA 256
49
50struct sstr_s {
51 size_t length; // MUST FIRST, see sstr_length at sstr.h
52 char type;
53 union {
54 // short string store datas in short_str
55 char short_str[SHORT_STR_CAPACITY + 1];
56 // long string allocate a buffer and store datas in long_str
57 struct {
58 size_t capacity;
59 char* data;
60 } long_str;
61 // reference to a memory buffer
62 struct {
63 char* data;
64 } ref_str;
65 } un;
66};
67
68#define SSTR_TYPE_SHORT 0
69#define SSTR_TYPE_LONG 1
70#define SSTR_TYPE_REF 2
71
75typedef void* sstr_t;
76
82extern sstr_t sstr_new(void);
83
89extern void sstr_free(sstr_t s);
90
100extern sstr_t sstr_of(const void* data, size_t length);
101
113extern sstr_t sstr_ref(const void* data, size_t length);
114
123extern sstr_t sstr(const char* cstr);
124
137extern char* sstr_cstr(sstr_t s);
138
152#define sstr_length(s) ((struct sstr_s*)s)->length
153
170extern int sstr_compare(sstr_t a, sstr_t b);
171
178extern int sstr_compare_c(sstr_t a, const char* b);
179
187extern void sstr_append_zero(sstr_t s, size_t length);
188
197extern void sstr_append_of(sstr_t s, const void* data, size_t length);
198
206extern void sstr_append(sstr_t dst, sstr_t src);
207
215extern void sstr_append_cstr(sstr_t dst, const char* src);
216
223extern sstr_t sstr_dup(sstr_t s);
224
234extern sstr_t sstr_substr(sstr_t s, size_t index, size_t len);
235
241extern void sstr_clear(sstr_t s);
242
269extern sstr_t sstr_vslprintf(const char* fmt, va_list args);
270
280extern sstr_t sstr_vslprintf_append(sstr_t buf, const char* fmt, va_list args);
281
289extern sstr_t sstr_printf(const char* fmt, ...);
290
300extern sstr_t sstr_printf_append(sstr_t buf, const char* fmt, ...);
301
305
311extern void sstr_append_int_str(sstr_t s, int i);
312
319extern int sstr_parse_long(sstr_t s, long* v);
320
327extern int sstr_parse_int(sstr_t* s, int* v);
328
334extern void sstr_append_long_str(sstr_t s, long l);
335
341extern void sstr_append_uint32_str(sstr_t s, uint32_t u);
342
348extern void sstr_append_uint64_str(sstr_t s, uint64_t u);
349
356extern void sstr_append_float_str(sstr_t s, float f, int precission);
357
364extern void sstr_append_double_str(sstr_t s, double f, int precision);
365
372extern int sstr_parse_double(sstr_t s, double* v);
373
375
384extern void sstr_append_of_if(sstr_t s, const void* data, size_t length, bool cond);
391#define sstr_append_cstr_if(dst, src, cond) \
392 sstr_append_of_if(dst, src, strlen(src), cond)
393
405
412extern void sstr_append_indent(sstr_t s, size_t indent);
413
419extern const char* sstr_version(void);
420
421#ifdef __cplusplus
422}
423#endif
424
425#endif /* SSTR_H_ */
void sstr_append_uint32_str(sstr_t s, uint32_t u)
Append the decimal string representation of a uint32_t to a string.
Definition sstr.c:733
sstr_t sstr_vslprintf(const char *fmt, va_list args)
Printf implement.
Definition sstr.c:289
const char * sstr_version(void)
return version string.
Definition sstr.c:651
sstr_t sstr_printf_append(sstr_t buf, const char *fmt,...)
Same as sstr_printf(), but but print to buf instead of create a new one.
Definition sstr.c:279
int sstr_parse_long(sstr_t s, long *v)
Parse a string as a long integer.
Definition sstr.c:679
int sstr_compare_c(sstr_t a, const char *b)
compare sstr_t a and c-style string b
Definition sstr.c:128
void sstr_append_uint64_str(sstr_t s, uint64_t u)
Append the decimal string representation of a uint64_t to a string.
Definition sstr.c:742
sstr_t sstr_substr(sstr_t s, size_t index, size_t len)
Get substring of s starting at index with length bytes.
Definition sstr.c:231
int sstr_parse_double(sstr_t s, double *v)
Parse a string as a double.
Definition sstr.c:782
sstr_t sstr_new(void)
Create an empty sstr_t.
Definition sstr.c:58
void sstr_append_float_str(sstr_t s, float f, int precission)
Append the string representation of a float to a string.
Definition sstr.c:751
int sstr_json_escape_string_append(sstr_t out, sstr_t in)
Escape a string for JSON output and append it to a buffer.
Definition sstr.c:818
void sstr_append_long_str(sstr_t s, long l)
Append the decimal string representation of a long to a string.
Definition sstr.c:712
sstr_t sstr_dup(sstr_t s)
Duplicate s and return.
Definition sstr.c:229
void * sstr_t
sstr_t are objects that represent sequences of characters.
Definition sstr.h:75
void sstr_append_double_str(sstr_t s, double f, int precision)
Append the string representation of a double to a string.
Definition sstr.c:767
void sstr_append_cstr(sstr_t dst, const char *src)
Extends the sstr_t by appending additional characters contained in src.
Definition sstr.c:225
sstr_t sstr(const char *cstr)
Create a sstr_t from C-style (NULL-terminated) string str.
Definition sstr.c:100
sstr_t sstr_ref(const void *data, size_t length)
Create a sstr_t from data with length bytes. The data is not copied, but have a pointer to data.
Definition sstr.c:92
sstr_t sstr_printf(const char *fmt,...)
printf implement.
Definition sstr.c:269
void sstr_append_of(sstr_t s, const void *data, size_t length)
Extends the sstr_t by appending additional characters in data with length of length at the end of its...
Definition sstr.c:180
void sstr_append_indent(sstr_t s, size_t indent)
append spaces at the end of the sstr_t.
Definition sstr.c:896
void sstr_clear(sstr_t s)
clear the sstr_t. After this call, the sstr_t is empty.
Definition sstr.c:243
int sstr_compare(sstr_t a, sstr_t b)
Compare a and b return 0 if equal, <0 if a < b, >0 if a > b.
Definition sstr.c:104
sstr_t sstr_of(const void *data, size_t length)
Create a sstr_t from data with length bytes.
Definition sstr.c:75
sstr_t sstr_vslprintf_append(sstr_t buf, const char *fmt, va_list args)
Same as sstr_vslprintf, but print to buf instead of create a new one.
Definition sstr.c:295
void sstr_append(sstr_t dst, sstr_t src)
Extends the sstr_t by appending additional characters contained in src.
Definition sstr.c:221
int sstr_parse_int(sstr_t *s, int *v)
Parse a string as an int, consuming the parsed portion.
Definition sstr.c:705
void sstr_free(sstr_t s)
delete a sstr_t.
Definition sstr.c:64
char * sstr_cstr(sstr_t s)
Return C-style string representation of s.
Definition sstr.c:102
void sstr_append_of_if(sstr_t s, const void *data, size_t length, bool cond)
Append if cond is true, otherwise do nothing.
Definition sstr.c:890
void sstr_append_int_str(sstr_t s, int i)
Append the decimal string representation of an int to a string.
Definition sstr.c:656
void sstr_append_zero(sstr_t s, size_t length)
Extends the sstr_t by appending additional '\0' characters at the end of its current value.
Definition sstr.c:143
Definition sstr.h:50