sstr  1.0.1
sstr_t represent sequences of characters.
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 
41 #ifdef __cplusplus
42 extern "C" {
43 #endif
44 
45 #define SHORT_STR_CAPACITY 25
46 #define CAP_ADD_DELTA 256
47 
48 struct sstr_s {
49  size_t length; // MUST FIRST, see sstr_length at sstr.h
50  char type;
51  union {
52  // short string store datas in short_str
53  char short_str[SHORT_STR_CAPACITY + 1];
54  // long string allocate a buffer and store datas in long_str
55  struct {
56  size_t capacity;
57  char* data;
58  } long_str;
59  // reference to a memory buffer
60  struct {
61  char* data;
62  } ref_str;
63  } un;
64 };
65 
66 #define SSTR_TYPE_SHORT 0
67 #define SSTR_TYPE_LONG 1
68 #define SSTR_TYPE_REF 2
69 
73 typedef void* sstr_t;
74 
81 
87 void sstr_free(sstr_t s);
88 
98 sstr_t sstr_of(const void* data, size_t length);
99 
111 sstr_t sstr_ref(const void* data, size_t length);
112 
121 sstr_t sstr(const char* cstr);
122 
135 char* sstr_cstr(sstr_t s);
136 
150 #define sstr_length(s) ((struct sstr_s*)s)->length
151 
168 int sstr_compare(sstr_t a, sstr_t b);
169 
176 int sstr_compare_c(sstr_t a, const char* b);
177 
185 void sstr_append_zero(sstr_t s, size_t length);
186 
195 void sstr_append_of(sstr_t s, const void* data, size_t length);
196 
204 void sstr_append(sstr_t dst, sstr_t src);
205 
213 void sstr_append_cstr(sstr_t dst, const char* src);
214 
222 
232 sstr_t sstr_substr(sstr_t s, size_t index, size_t len);
233 
239 void sstr_clear(sstr_t s);
240 
267 sstr_t sstr_vslprintf(const char* fmt, va_list args);
268 
278 sstr_t sstr_vslprintf_append(sstr_t buf, const char* fmt, va_list args);
279 
287 sstr_t sstr_printf(const char* fmt, ...);
288 
298 sstr_t sstr_printf_append(sstr_t buf, const char* fmt, ...);
299 
301 
308 void sstr_append_int_str(sstr_t s, int i);
316 int sstr_parse_long(sstr_t s, long* v);
324 int sstr_parse_int(sstr_t* s, int* v);
331 void sstr_append_long_str(sstr_t s, long l);
339 void sstr_append_float_str(sstr_t s, float f, int precission);
347 void sstr_append_double_str(sstr_t s, double f, int precision);
355 int sstr_parse_double(sstr_t s, double* v);
356 
365 void sstr_append_of_if(sstr_t s, const void* data, size_t length, int cond);
372 #define sstr_append_cstr_if(dst, src, cond) \
373  sstr_append_of_if(dst, src, strlen(src), cond)
374 
375 // escape string to json string format
376 int sstr_json_escape_string_append(sstr_t out, sstr_t in);
377 
384 void sstr_append_indent(sstr_t s, size_t indent);
385 
391 const char* sstr_version();
392 
393 #ifdef __cplusplus
394 }
395 #endif
396 
397 #endif /* SSTR_H_ */
sstr_t sstr_vslprintf(const char *fmt, va_list args)
Printf implement.
Definition: sstr.c:236
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:226
void sstr_append_of_if(sstr_t s, const void *data, size_t length, int cond)
Append if cond is true, otherwise do nothing.
Definition: sstr.c:813
int sstr_parse_long(sstr_t s, long *v)
convert sstr_t string to long
Definition: sstr.c:625
int sstr_compare_c(sstr_t a, const char *b)
compare sstr_t a and c-style string b
Definition: sstr.c:110
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:178
int sstr_parse_double(sstr_t s, double *v)
parse sstr_t string to double
Definition: sstr.c:723
void sstr_append_float_str(sstr_t s, float f, int precission)
convert float to sstr_t
Definition: sstr.c:679
void sstr_append_long_str(sstr_t s, long l)
convert long to sstr_t
Definition: sstr.c:658
const char * sstr_version()
return version string.
Definition: sstr.c:597
char * sstr_cstr(sstr_t s)
Return C-style string representation of s.
Definition: sstr.c:85
sstr_t sstr_dup(sstr_t s)
Duplicate s and return.
Definition: sstr.c:176
void * sstr_t
sstr_t are objects that represent sequences of characters.
Definition: sstr.h:73
void sstr_append_double_str(sstr_t s, double f, int precision)
convert double to sstr_t
Definition: sstr.c:684
void sstr_append_cstr(sstr_t dst, const char *src)
Extends the sstr_t by appending additional characters contained in src.
Definition: sstr.c:172
sstr_t sstr(const char *cstr)
Create a sstr_t from C-style (NULL-terminated) string str.
Definition: sstr.c:83
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:75
sstr_t sstr_printf(const char *fmt,...)
printf implement.
Definition: sstr.c:216
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:161
void sstr_append_indent(sstr_t s, size_t indent)
append spaces at the end of the sstr_t.
Definition: sstr.c:822
void sstr_clear(sstr_t s)
clear the sstr_t. After this call, the sstr_t is empty.
Definition: sstr.c:190
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:87
sstr_t sstr_of(const void *data, size_t length)
Create a sstr_t from data with length bytes.
Definition: sstr.c:58
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:242
void sstr_append(sstr_t dst, sstr_t src)
Extends the sstr_t by appending additional characters contained in src.
Definition: sstr.c:168
int sstr_parse_int(sstr_t *s, int *v)
convert sstr_t string to int
Definition: sstr.c:651
sstr_t sstr_new()
Create an empty sstr_t.
Definition: sstr.c:41
void sstr_free(sstr_t s)
delete a sstr_t.
Definition: sstr.c:47
void sstr_append_int_str(sstr_t s, int i)
convert sstr <-> int,long,float,double
Definition: sstr.c:602
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:124
Definition: sstr.h:48