-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathstr.h
More file actions
280 lines (229 loc) · 7.14 KB
/
str.h
File metadata and controls
280 lines (229 loc) · 7.14 KB
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/* Copyright 2008-2026 Carsten Elton Sorensen
This file is part of ASMotor.
ASMotor is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
ASMotor is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with ASMotor. If not, see <http://www.gnu.org/licenses/>.
*/
#if !defined(UTIL_STR_H_INCLUDED_)
#define UTIL_STR_H_INCLUDED_
#include <assert.h>
#include <stdbool.h>
#include <string.h>
#include "util.h"
#if defined(_MSC_VER)
#pragma warning(push)
#pragma warning(disable : 4200)
#endif
typedef struct {
uint32_t refCount;
uint32_t length;
char data[];
} string;
#if defined(_MSC_VER)
#pragma warning(pop)
#endif
#if defined(_MSC_VER)
#define strncpy(dest, src, len) strncpy_s(dest, len, src, len)
#endif
extern string*
#if defined(_DEBUG)
str_CreateLengthDebug(const char* data, size_t length, const char* file, int lineNumber);
#define str_CreateLength(data, length) str_CreateLengthDebug(data, length, __FILE__, __LINE__)
#else
str_CreateLength(const char* data, size_t length);
#endif
extern string*
#if defined(_DEBUG)
str_CreateStreamDebug(char (*nextChar)(void), size_t length, const char* filename, int lineNumber);
#define str_CreateStream(nextChar, length) str_CreateStreamDebug(nextChar, length, __FILE__, __LINE__)
#else
str_CreateStream(char (*nextChar)(void), size_t length);
#endif
extern string*
#if defined(_DEBUG)
str_CreateSpacesDebug(uint32_t count, const char* filename, int lineNumber);
#define str_CreateSpaces(count) str_CreateSpacesDebug(count, __FILE__, __LINE__)
#else
str_CreateSpaces(uint32_t count);
#endif
extern string*
#if defined(_DEBUG)
str_CreateArgsDebug(const char* filename, int lineNumber, const char* format, va_list args);
#define str_CreateArgs(format, args) str_CreateArgsDebug(__FILE__, __LINE__, format, args)
#else
str_CreateArgs(const char* format, va_list args);
#endif
extern string*
#if defined(_DEBUG)
str_CreateFormatDebug(const char* filename, int lineNumber, const char* format, ...);
#define str_CreateFormat(format, ...) str_CreateFormatDebug(__FILE__, __LINE__, format, __VA_ARGS__)
#else
str_CreateFormat(const char* format, ...);
#endif
INLINE string*
#if defined(_DEBUG)
str_CreateDebug(const char* data, const char* filename, int lineNumber) {
return str_CreateLengthDebug(data, strlen(data), filename, lineNumber);
#define str_Create(data) str_CreateDebug(data, __FILE__, __LINE__)
#else
str_Create(const char* data) {
return str_CreateLength(data, strlen(data));
#endif
}
extern string*
str_Empty(void);
extern void
str_Free(string* str);
extern string*
#if defined(_DEBUG)
str_ConcatDebug(const string* str1, const string* str2, const char* file, int lineNumber);
#define str_Concat(str1, str2) str_ConcatDebug(str1, str2, __FILE__, __LINE__)
#else
str_Concat(const string* str1, const string* str2);
#endif
extern string*
#if defined(_DEBUG)
str_SliceDebug(const string* str1, ssize_t index, ssize_t length, const char* file, int lineNumber);
#define str_Slice(str1, index, length) str_SliceDebug(str1, index, length, __FILE__, __LINE__)
#else
str_Slice(const string* str1, ssize_t index, ssize_t length);
#endif
extern uint32_t
str_Find(const string* haystack, const string* needle);
extern uint32_t
str_FindChar(const string* haystack, char needle);
extern bool
str_Equal(const string* str1, const string* str2);
extern int
str_Compare(const string* str1, const string* str2);
extern bool
str_EqualConst(const string* str1, const char* str2);
extern string*
#if defined(_DEBUG)
str_ReplaceDebug(const string* str, char search, char replace, const char* filename, int lineNumber);
#define str_Replace(str, search, replace) str_ReplaceDebug(str, search, replace, __FILE__, __LINE__)
#else
str_Replace(const string* str, char search, char replace);
#endif
extern string*
#if defined(_DEBUG)
str_ToLowerDebug(const string* str, const char* file, int lineNumber);
#define str_ToLower(str) str_ToLowerDebug(str, __FILE__, __LINE__)
#else
str_ToLower(const string* str);
#endif
extern void
str_TransformReplace(string** str, char (*transform)(char));
extern void
str_ToUpperReplace(string** str);
extern void
str_ToLowerReplace(string** str);
extern string*
str_Align(string* str, int32_t alignment);
INLINE bool
str_NotEqual(const string* str1, const string* str2) {
return !str_Equal(str1, str2);
}
INLINE size_t
str_Count(const string* str, char ch) {
size_t count = 0;
for (size_t i = 0; i < str->length; ++i) {
if (str->data[i] == ch)
count += 1;
}
return count;
}
INLINE string*
str_Copy(const string* str) {
if (str != NULL)
++((string*) str)->refCount;
return (string*) str;
}
INLINE size_t
str_Length(const string* str) {
return str->length;
}
INLINE const char*
str_String(const string* str) {
assert(str != NULL);
return str->data;
}
INLINE char
str_CharAt(const string* str, ssize_t index) {
assert(str != NULL);
if (index < 0)
index = str_Length(str) + index;
return str->data[index];
}
INLINE void
str_Assign(string** dest, const string* src) {
str_Free(*dest);
*dest = str_Copy(src);
}
INLINE void
str_Move(string** dest, string** src) {
str_Free(*dest);
*dest = *src;
*src = NULL;
}
extern uint32_t
str_JenkinsHashLength(const void* str, size_t length);
INLINE uint32_t
str_JenkinsHash(const string* str) {
return str_JenkinsHashLength(str_String(str), str_Length(str));
}
extern uint32_t
str_JenkinsHashLengthI(const void* str, size_t length);
INLINE uint32_t
str_JenkinsHashI(const string* str) {
return str_JenkinsHashLengthI(str_String(str), str_Length(str));
}
extern string*
#if defined(_DEBUG)
str_ReadFileDebug(FILE* fileHandle, size_t count, const char* file, int lineNumber);
#define str_ReadFile(file, count) str_ReadFileDebug(file, count, __FILE__, __LINE__)
#else
str_ReadFile(FILE* fileHandle, size_t count);
#endif
extern string*
#if defined(_DEBUG)
str_ReadLineFromFileDebug(FILE* fileHandle, const char* file, int lineNumber);
#define str_ReadLineFromFile(file) str_ReadLineFromFileDebug(file, __FILE__, __LINE__)
#else
str_ReadLineFromFile(FILE* fileHandle);
#endif
extern string*
#if defined(_DEBUG)
str_CanonicalizeLineEndingsDebug(string* srcString, const char* file, int lineNumber);
#define str_CanonicalizeLineEndings(srcString) str_CanonicalizeLineEndingsDebug(srcString, __FILE__, __LINE__);
#else
str_CanonicalizeLineEndings(string* srcString);
#endif
INLINE bool
hexToInt(const char* text, uint32_t* result) {
#if defined(_MSC_VER)
int count = sscanf_s(text, "%x", result);
#else
int count = sscanf(text, "%x", result);
#endif
return count == 1;
}
INLINE bool
decimalToInt(const char* text, int32_t* result) {
#if defined(_MSC_VER)
int count = sscanf_s(text, "%d", result);
#else
int count = sscanf(text, "%d", result);
#endif
return count == 1;
}
#define STR_ASSIGN(p, str) str_Assign(&(p), (str))
#define STR_MOVE(p, str) str_Move(&(p), &(str))
#endif