-
Notifications
You must be signed in to change notification settings - Fork 22
/
stringbuilder.h
62 lines (50 loc) · 1.57 KB
/
stringbuilder.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#ifndef STRINGBUILDER_H
#define STRINGBUILDER_H
typedef struct stringbuilder_tag {
char* cstr; /* Must be first member in the struct! */
int pos;
int size;
int reallocs; /* Performance metric to record the number of string reallocations */
} stringbuilder;
/**
* Creates a new stringbuilder with the default chunk size
*
*/
stringbuilder* sb_new();
/**
* Destroys the given stringbuilder. Pass 1 to free_string if the underlying c string should also be freed
*/
void sb_destroy(stringbuilder* sb, int free_string);
/**
* Creates a new stringbuilder with initial size at least the given size
*/
stringbuilder* sb_new_with_size(int size);
/**
* Resets the stringbuilder to empty
*/
void sb_reset(stringbuilder* sb);
/**
* Appends the given character to the string builder
*/
void sb_append_ch(stringbuilder* sb, const char ch);
/**
* Appends at most length of the given src string to the string buffer
*/
void sb_append_strn(stringbuilder* sb, const char* src, int length);
/**
* Appends the given src string to the string builder
*/
void sb_append_str(stringbuilder* sb, const char* src);
/**
* Appends the formatted string to the given string builder
*/
void sb_append_strf(stringbuilder* sb, const char* fmt, ...);
/**
* Allocates and copies a new cstring based on the current stringbuilder contents
*/
char* sb_make_cstring(stringbuilder* sb);
/**
* Returns the stringbuilder as a regular C String
*/
#define sb_cstring(sb) ((sb)->cstr)
#endif // STRINGBUILDER_H