-
Notifications
You must be signed in to change notification settings - Fork 252
/
Copy pathsc_str.h
201 lines (179 loc) · 6.63 KB
/
sc_str.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*
* BSD-3-Clause
*
* Copyright 2021 Ozan Tezcan
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef SC_STR_H
#define SC_STR_H
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#define SC_STR_VERSION "2.0.0"
#ifdef SC_HAVE_CONFIG_H
#include "config.h"
#else
#define sc_str_malloc malloc
#define sc_str_realloc realloc
#define sc_str_free free
#endif
/**
* length prefixed C strings, length is at the start of the allocated memory
* e.g.,
* -----------------------------------------------
* | 0 | 0 | 0 | 4 | 'T' | 'E' | 'S' | 'T' | '\0'|
* -----------------------------------------------
* ^
* return
* User can keep pointer to first character, so it's like C style strings with
* additional functionality when it's used with these functions here.
*/
/**
* @param str '\0' terminated C string, can be NULL.
* @return length prefixed string. NULL on out of memory or if 'str' is NULL.
*/
char *sc_str_create(const char *str);
/**
* @param str string bytes, no need for '\0' termination.
* @param len length of the 'str'.
* @return length prefixed string. NULL on out of memory or if 'str' is NULL.
*/
char *sc_str_create_len(const char *str, uint32_t len);
/**
* @param fmt format
* @param ... arguments
* @return length prefixed string. NULL on out of memory.
*/
char *sc_str_create_fmt(const char *fmt, ...);
/**
* @param fmt format
* @param va va_list
* @return length prefixed string. NULL on out of memory.
*/
char *sc_str_create_va(const char *fmt, va_list va);
/**
* Deallocate length prefixed string.
* @param str length prefixed string. str may be NULL.
*/
void sc_str_destroy(char **str);
/**
* @param str length prefixed string. NULL values are accepted.
* @return length of the string. If NULL, returns -1.
*/
int64_t sc_str_len(const char *str);
/**
* @param str length prefixed string. NULL values are accepted.
* @return duplicate string. NULL on out of memory or if 'str' is NULL.
*/
char *sc_str_dup(const char *str);
/**
* @param str Pointer to length prefixed string, '*str' may change.
* @param param New value to set.
* @return 'true' on success, 'false' on out of memory or if '*str' is NULL
*/
bool sc_str_set(char **str, const char *param);
/**
* @param str pointer to length prefixed string, '*str' may change.
* @param fmt format
* @param ... arguments
* @return 'true' on success, 'false' on out of memory
*/
bool sc_str_set_fmt(char **str, const char *fmt, ...);
/**
* @param str pointer to length prefixed string, '*str' may change.
* @param text text to append.
* @return 'true' on success, 'false' on out of memory or if '*str' is NULL.
*/
bool sc_str_append(char **str, const char *text);
/**
* @param str pointer to length prefixed string. (char**).'*str' may change.
* @param fmt format
* @param ... arguments
* @return 'true' on success, 'false' on out of memory or if '*str' is NULL.
*/
#define sc_str_append_fmt(str, fmt, ...) \
((*str) ? (sc_str_set_fmt(str, "%s" fmt, *str, __VA_ARGS__)) : \
(sc_str_set_fmt(str, fmt, __VA_ARGS__)))
/**
* Compare two length prefixed strings. To compare with C string, use strcmp().
*
* @param str length prefixed string, must not be NULL.
* @param other length prefixed string, must not be NULL.
* @return 'true' if equals.
*/
bool sc_str_cmp(const char *str, const char *other);
/**
* @param str length prefixed string, '*str' may change
* @param list character list to trim.
* @return 'true' on success or if '*str' is NULL. 'false' on out of memory
*/
bool sc_str_trim(char **str, const char *list);
/**
* @param str length prefixed string, *str' may change.
* @param start start index.
* @param end end index.
* @return 'false' on out of range, on out of memory or if '*str' is NULL.
* 'true' on success.
*/
bool sc_str_substring(char **str, uint32_t start, uint32_t end);
/**
* @param str length prefixed string, '*str' may change.
* @param rep string to be replaced
* @param with string to replace with
* @return 'true' on success or if '*str' is NULL. 'false' on out of memory
*/
bool sc_str_replace(char **str, const char *rep, const char *with);
/**
* Tokenization is zero-copy but a bit tricky. This function will mutate 'str',
* but it is temporary. On each 'sc_str_token_begin' call, this function will
* place '\0' character at the end of a token and put delimiter at the end of
* the 'str'.
* e.g., user1-user2\0 after first iteration will be user1\0user2-
*
* sc_str_token_end() will fix original string if necessary.
*
* usage:
*
* char *str = sc_str_create("user1-user2-user3");
* char *save = NULL; // Must be initialized with NULL.
* const char *token;
*
* while ((token = sc_str_token_begin(str, &save, "-")) != NULL) {
* printf("token : %s \n", token);
* }
*
* sc_str_token_end(str, &save);
*
*
* @param str length prefixed string, must not be NULL.
* @param save helper variable for tokenizer code.
* @param delim delimiter list.
* @return token.
*/
const char *sc_str_token_begin(char *str, char **save, const char *delim);
void sc_str_token_end(char *str, char **save);
#endif