2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
23#ifndef GIMBAL_STRING_VIEW_H
24#define GIMBAL_STRING_VIEW_H
26#include "../core/gimbal_ctx.h"
27#include "../algorithms/gimbal_hash.h"
28#include "../preprocessor/gimbal_macro_utils.h"
37#define GBL_STRV(...) GblStringView_fromString
(__VA_ARGS__)
41#define GBL_STRV_CSTR_ALLOCA(view) GblStringView_toCString(view, (char*)GBL_ALLOCA(view.length + 1
), view.length + 1
)
43#define GBL_SELF_TYPE GblStringView
48
49
50
51
52
53
54
55
59 size_t nullTerminated : 1,
60 size_t length :
sizeof(size_t) * 8 - 1
65
66
67
68
78
79
80
81
93
94
95
96
108
109
110
111
123
124
125
126
162
163
164
165
187
188
189
190
224#define GblStringView_fromString(...)
225 GblStringView_fromStringDefault_
(__VA_ARGS__)
226#define GblStringView_fromStringDefault_(...)
227 GblStringView_fromStringDefault__
(__VA_ARGS__, 0
)
228#define GblStringView_fromStringDefault__(string, length, ...)
229 (GblStringView_fromString
)(string, length)
231#define GblStringView_compare(...)
232 GblStringView_compareDefault_
(__VA_ARGS__)
233#define GblStringView_compareDefault_(...)
234 GblStringView_compareDefault__
(__VA_ARGS__, 0
)
235#define GblStringView_compareDefault__(view, string, length, ...)
236 (GblStringView_compare
)(view, string, length)
238#define GblStringView_compareIgnoreCase(...)
239 GblStringView_compareIgnoreCaseDefault_
(__VA_ARGS__)
240#define GblStringView_compareIgnoreCaseDefault_(...)
241 GblStringView_compareIgnoreCaseDefault__
(__VA_ARGS__, 0
)
242#define GblStringView_compareIgnoreCaseDefault__(view, string, length, ...)
243 (GblStringView_compareIgnoreCase
)(view, string, length)
245#define GblStringView_equals(...)
246 GblStringView_equalsDefault_
(__VA_ARGS__)
247#define GblStringView_equalsDefault_(...)
248 GblStringView_equalsDefault__
(__VA_ARGS__, 0
)
249#define GblStringView_equalsDefault__(view, string, length, ...)
250 (GblStringView_equals
)(view, string, length)
252#define GblStringView_equalsIgnoreCase(...)
253 GblStringView_equalsIgnoreCaseDefault_
(__VA_ARGS__)
254#define GblStringView_equalsIgnoreCaseDefault_(...)
255 GblStringView_equalsIgnoreCaseDefault__
(__VA_ARGS__, 0
)
256#define GblStringView_equalsIgnoreCaseDefault__(view, string, length, ...)
257 (GblStringView_equalsIgnoreCase
)(view, string, length)
259#define GblStringView_contains(...)
260 GblStringView_containsDefault_
(__VA_ARGS__)
261#define GblStringView_containsDefault_(...)
262 GblStringView_containsDefault__
(__VA_ARGS__, 0
)
263#define GblStringView_containsDefault__(view, string, length, ...)
264 (GblStringView_contains
)(view, string, length)
266#define GblStringView_containsIgnoreCase(...)
267 GblStringView_containsIgnoreCaseDefault_
(__VA_ARGS__)
268#define GblStringView_containsIgnoreCaseDefault_(...)
269 GblStringView_containsIgnoreCaseDefault__
(__VA_ARGS__, 0
)
270#define GblStringView_containsIgnoreCaseDefault__(view, string, length, ...)
271 (GblStringView_containsIgnoreCase
)(view, string, length)
273#define GblStringView_count(...)
274 GblStringView_countDefault_
(__VA_ARGS__)
275#define GblStringView_countDefault_(...)
276 GblStringView_countDefault__
(__VA_ARGS__, 0
)
277#define GblStringView_countDefault__(view, string, length, ...)
278 (GblStringView_count
)(view, string, length)
280#define GblStringView_startsWith(...)
281 GblStringView_startsWithDefault_
(__VA_ARGS__)
282#define GblStringView_startsWithDefault_(...)
283 GblStringView_startsWithDefault__
(__VA_ARGS__, 0
)
284#define GblStringView_startsWithDefault__(view, string, length, ...)
285 (GblStringView_startsWith
)(view, string, length)
287#define GblStringView_startsWithIgnoreCase(...)
288 GblStringView_startsWithIgnoreCaseDefault_
(__VA_ARGS__)
289#define GblStringView_startsWithIgnoreCaseDefault_(...)
290 GblStringView_startsWithIgnoreCaseDefault__
(__VA_ARGS__, 0
)
291#define GblStringView_startsWithIgnoreCaseDefault__(view, string, length, ...)
292 (GblStringView_startsWithIgnoreCase
)(view, string, length)
294#define GblStringView_endsWith(...)
295 GblStringView_endsWithDefault_
(__VA_ARGS__)
296#define GblStringView_endsWithDefault_(...)
297 GblStringView_endsWithDefault__
(__VA_ARGS__, 0
)
298#define GblStringView_endsWithDefault__(view, string, length, ...)
299 (GblStringView_endsWith
)(view, string, length)
301#define GblStringView_endsWithIgnoreCase(...)
302 GblStringView_endsWithIgnoreCaseDefault_
(__VA_ARGS__)
303#define GblStringView_endsWithIgnoreCaseDefault_(...)
304 GblStringView_endsWithIgnoreCaseDefault__
(__VA_ARGS__, 0
)
305#define GblStringView_endsWithIgnoreCaseDefault__(view, string, length, ...)
306 (GblStringView_endsWithIgnoreCase
)(view, string, length)
308#define GblStringView_find(...)
309 GblStringView_findDefault_
(__VA_ARGS__)
310#define GblStringView_findDefault_(...)
311 GblStringView_findDefault__
(__VA_ARGS__, 0
, 0
)
312#define GblStringView_findDefault__(view, string, length, offset, ...)
313 (GblStringView_find
)(view, string, length, offset)
315#define GblStringView_findIgnoreCase(...)
316 GblStringView_findIgnoreCaseDefault_
(__VA_ARGS__)
317#define GblStringView_findIgnoreCaseDefault_(...)
318 GblStringView_findIgnoreCaseDefault__
(__VA_ARGS__, 0
, 0
)
319#define GblStringView_findIgnoreCaseDefault__(view, string, length, offset, ...)
320 (GblStringView_findIgnoreCase
)(view, string, length, offset)
322#define GblStringView_rfind(...)
324#define GblStringView_rfind_2(view, str)
325 GblStringView_rfind_3
(view, str, 0
)
326#define GblStringView_rfind_3(view, str, len)
328#define GblStringView_rfind_4(view, str, len, offset)
329 (GblStringView_rfind
)(view, str, len, offset)
331#define GblStringView_rfindIgnoreCase(...)
333#define GblStringView_rfindIgnoreCase_2(view, str)
334 GblStringView_rfindIgnoreCase_3
(view, str, 0
)
335#define GblStringView_rfindIgnoreCase_3(view, str, len)
337#define GblStringView_rfindIgnoreCase_4(view, str, len, offset)
338 (GblStringView_rfindIgnoreCase
)(view, str, len, offset)
340#define GblStringView_findFirstOf(...)
341 GblStringView_findFirstOfDefault_
(__VA_ARGS__)
342#define GblStringView_findFirstOfDefault_(...)
343 GblStringView_findFirstOfDefault__
(__VA_ARGS__, 0
, 0
)
344#define GblStringView_findFirstOfDefault__(view, string, length, offset, ...)
345 (GblStringView_findFirstOf
)(view, string, length, offset)
347#define GblStringView_findFirstNotOf(...)
348 GblStringView_findFirstNotOfDefault_
(__VA_ARGS__)
349#define GblStringView_findFirstNotOfDefault_(...)
350 GblStringView_findFirstNotOfDefault__
(__VA_ARGS__, 0
, 0
)
351#define GblStringView_findFirstNotOfDefault__(view, string, length, offset, ...)
352 (GblStringView_findFirstNotOf
)(view, string, length, offset)
354#define GblStringView_findLastOf(...)
356#define GblStringView_findLastOf_2(view, str)
357 GblStringView_findLastOf_3
(view, str, 0
)
358#define GblStringView_findLastOf_3(view, str, len)
360#define GblStringView_findLastOf_4(view, str, len, offset)
361 (GblStringView_findLastOf
)(view, str, len, offset)
363#define GblStringView_findLastNotOf(...)
365#define GblStringView_findLastNotOf_2(view, str)
366 GblStringView_findLastNotOf_3
(view, str, 0
)
367#define GblStringView_findLastNotOf_3(view, str, len)
369#define GblStringView_findLastNotOf_4(view, str, len, offset)
370 (GblStringView_findLastNotOf
)(view, str, len, offset)
372#define GblStringView_toBool(...)
373 GblStringView_toBoolDefault_
(__VA_ARGS__)
374#define GblStringView_toBoolDefault_(...)
375 GblStringView_toBoolDefault__
(__VA_ARGS__, NULL)
376#define GblStringView_toBoolDefault__(self, success, ...)
377 ((GblStringView_toBool
)(self, success))
379#define GblStringView_toChar(...)
380 GblStringView_toCharDefault_
(__VA_ARGS__)
381#define GblStringView_toCharDefault_(...)
382 GblStringView_toCharDefault__
(__VA_ARGS__, NULL)
383#define GblStringView_toCharDefault__(self, success, ...)
384 ((GblStringView_toChar
)(self, success))
386#define GblStringView_toUint8(...)
387 GblStringView_toUint8Default_
(__VA_ARGS__)
388#define GblStringView_toUint8Default_(...)
389 GblStringView_toUint8Default__
(__VA_ARGS__, NULL)
390#define GblStringView_toUint8Default__(self, success, ...)
391 ((GblStringView_toUint8
)(self, success))
393#define GblStringView_toUint16(...)
394 GblStringView_toUint16Default_
(__VA_ARGS__)
395#define GblStringView_toUint16Default_(...)
396 GblStringView_toUint16Default__
(__VA_ARGS__, NULL)
397#define GblStringView_toUint16Default__(self, success, ...)
398 ((GblStringView_toUint16
)(self, success))
400#define GblStringView_toInt16(...)
401 GblStringView_toInt16Default_
(__VA_ARGS__)
402#define GblStringView_toInt16Default_(...)
403 GblStringView_toInt16Default__
(__VA_ARGS__, NULL)
404#define GblStringView_toInt16Default__(self, success, ...)
405 ((GblStringView_toInt16
)(self, success))
407#define GblStringView_toUint32(...)
408 GblStringView_toUint32Default_
(__VA_ARGS__)
409#define GblStringView_toUint32Default_(...)
410 GblStringView_toUint32Default__
(__VA_ARGS__, NULL)
411#define GblStringView_toUint32Default__(self, success, ...)
412 ((GblStringView_toUint32
)(self, success))
414#define GblStringView_toInt32(...)
415 GblStringView_toInt32Default_
(__VA_ARGS__)
416#define GblStringView_toInt32Default_(...)
417 GblStringView_toInt32Default__
(__VA_ARGS__, NULL)
418#define GblStringView_toInt32Default__(self, success, ...)
419 ((GblStringView_toInt32
)(self, success))
421#define GblStringView_toUint64(...)
422 GblStringView_toUint64Default_
(__VA_ARGS__)
423#define GblStringView_toUint64Default_(...)
424 GblStringView_toUint64Default__
(__VA_ARGS__, NULL)
425#define GblStringView_toUint64Default__(self, success, ...)
426 ((GblStringView_toUint64
)(self, success))
428#define GblStringView_toInt64(...)
429 GblStringView_toInt64Default_
(__VA_ARGS__)
430#define GblStringView_toInt64Default_(...)
431 GblStringView_toInt64Default__
(__VA_ARGS__, NULL)
432#define GblStringView_toInt64Default__(self, success, ...)
433 ((GblStringView_toInt64
)(self, success))
435#define GblStringView_toFloat(...)
436 GblStringView_toFloatDefault_
(__VA_ARGS__)
437#define GblStringView_toFloatDefault_(...)
438 GblStringView_toFloatDefault__
(__VA_ARGS__, NULL)
439#define GblStringView_toFloatDefault__(self, success, ...)
440 ((GblStringView_toFloat
)(self, success))
442#define GblStringView_toDouble(...)
443 GblStringView_toDoubleDefault_
(__VA_ARGS__)
444#define GblStringView_toDoubleDefault_(...)
445 GblStringView_toDoubleDefault__
(__VA_ARGS__, NULL)
446#define GblStringView_toDoubleDefault__(self, success, ...)
447 ((GblStringView_toDouble
)(self, success))
449#define GblStringView_toPointer(...)
450 GblStringView_toPointerDefault_
(__VA_ARGS__)
451#define GblStringView_toPointerDefault_(...)
452 GblStringView_toPointerDefault__
(__VA_ARGS__, NULL)
453#define GblStringView_toPointerDefault__(self, success, ...)
454 ((GblStringView_toPointer
)(self, success))
#define GBL_VA_OVERLOAD_CALL_ARGC(BASE,...)
#define GBL_BIT_FIELDS(...)
Used to declare an endian-independent group of bitfields.
#define GBL_STRING_VIEW_NPOS
Constant value used to signify the last position or an invalid position in a GblStringView.
#define GBL_STRV_CSTR_ALLOCA(view)
Convenience macro for creating a NULL-terminated C string from a GblStringView on the stack,...
uint32_t GblHash
Type representing a calculated numeric hash across the codebase.
uint8_t GblBool
Basic boolean type, standardized to sizeof(char)
uintptr_t GblQuark
Uniquely identifiable interned string type.
Immutable substring type.
size_t GblStringView_findIgnoreCase(GblStringView self, const char *pStr, size_t len, size_t offset)
Returns the starting index of the first occurrence of pStr appearing as a substring within the given ...
int32_t GblStringView_toInt32(GblStringView self, GblBool *pSuccess)
Converts the given GblStringView to an int32_t value, optionally returning whether the conversion suc...
char GblStringView_at(GblStringView self, size_t index)
Returns the character within the given view at index, raising an error if it is out-of-range.
GblBool GblStringView_endsWithIgnoreCase(GblStringView self, const char *pString, size_t len)
Returns GBL_TRUE if the given GblStringView ends with the substring given by pString (ignoring casing...
GblStringView GblStringView_fromString(const char *pString, size_t length)
Constructs a GblStringView from a(n optionally sized) character array.
GblBool GblStringView_toNil(GblStringView self)
Converts the given GblStringView to NIL (returns GBL_TRUE if it's empty or matches "nil")
GblBool GblStringView_blank(GblStringView self)
Returns GBL_TRUE if the given GblStringView contains only whitespace characters (spaces,...
GBL_RESULT GblStringView_copy(GblStringView self, void *pDst, size_t offset, size_t bytes)
Copies an arbitrary substring within teh view into the external buffer, pDst of size bytes starting a...
size_t GblStringView_rfindIgnoreCase(GblStringView self, const char *pStr, size_t len, size_t offset)
Returns the starting index of the last occurrence of pStr appearing as a substring within the given G...
GblQuark GblStringView_tryQuark(GblStringView self)
Attempts to return the GblQuark corresponding to the given GblStringView, retruning GBL_QUARK_INVALID...
void * GblStringView_toPointer(GblStringView self, GblBool *pSuccess)
Converts the given GblStringView to a pointer value, optionally returning whether the conversion succ...
GblStringView GblStringView_fromQuark(GblQuark quark)
Constructs a GblStringView from an existing GblQuark, or creating an empty view if it's invalid.
GblBool GblStringView_toBool(GblStringView self, GblBool *pSuccess)
Converts the given GblStringView to a boolean value, optionally returning whether the conversion succ...
GblBool GblStringView_containsIgnoreCase(GblStringView self, const char *pString, size_t len)
Returns GBL_TRUE if the given GblStringView contains the substring, pString, (ignoring case) otherwis...
int GblStringView_compare(GblStringView self, const char *pString, size_t len)
Lexicographically compares the given view to pString, returning a positive or negative for inequality...
GblQuark GblStringView_quark(GblStringView self)
Creates and returns a GblQuark interned string representation from the given GblStringView.
int16_t GblStringView_toInt16(GblStringView self, GblBool *pSuccess)
Converts the given GblStringView to an int16_t value, optionally returning whether the conversion suc...
size_t GblStringView_rfind(GblStringView self, const char *pStr, size_t len, size_t offset)
Returns the starting index of the last occurrence of pStr appearing as a substring within the given G...
int64_t GblStringView_toInt64(GblStringView self, GblBool *pSuccess)
Converts the given GblStringView to an int64_t value, optionally returning whether the conversion suc...
GblBool GblStringView_endsWith(GblStringView self, const char *pString, size_t len)
Returns GBL_TRUE if the given GblStringView ends with the substring given by pString,...
float GblStringView_toFloat(GblStringView self, GblBool *pSuccess)
Converts the given GblStringView to a float value, optionally returning whether the conversion succee...
GblBool GblStringView_empty(GblStringView self)
Returns GBL_TRUE if the given GblStringView is empty, otherwise returning GBL_FALSE.
GblHash GblStringView_hash(GblStringView self)
Returns the GblHash for the given GblStringView, using the default libGimbal hashing algorithm.
uint32_t GblStringView_toUint32(GblStringView self, GblBool *pSuccess)
Converts the given GblStringView to a uint32_t value, optionally returning whether the conversion suc...
size_t GblStringView_countIgnoreCase(GblStringView self, const char *pString, size_t len)
Returns the number of times the substring, pString, appears within the given GblStringView,...
GblBool GblStringView_contains(GblStringView self, const char *pString, size_t len)
Returns GBL_TRUE if the given GblStringView contains the substring, pString, otherwise returning GBL_...
GblBool GblStringView_startsWithIgnoreCase(GblStringView self, const char *pString, size_t len)
Returns GBL_TRUE if the given GblStringView starts with the substring given by pString (ignoring casi...
GblBool GblStringView_equals(GblStringView self, const char *pString, size_t len)
Returns GBL_TRUE if the given string view equals pString or returns GBL_FALSE otherwise.
GblStringView GblStringView_substr(GblStringView self, size_t offset, size_t length)
Retruns a new GblStringView as a subview of the given GblStringView, starting at offset of size lengt...
const char * GblStringView_intern(GblStringView self)
Interns (creates a GblQuark) from the given GblStringView, returning its C string representation.
char * GblStringView_toCString(GblStringView self, char *pDest, size_t destSize)
Converts the given GblStringView to a NULL-terminated C string, copying it to the pDest buffer of des...
GblStringView GblStringView_chomp(GblStringView self)
Returns a new GblStringView which has any terminating newline characters (linux + windows) chopped of...
char * GblStringView_strdup(GblStringView self)
Duplicates the given GblStringView as a heap-allocated NULL-terminated C string and returns it....
double GblStringView_toDouble(GblStringView self, GblBool *pSuccess)
Converts the given GblStringView to a double value, optionally returning whether the conversion succe...
const char * pData
Start address of the string being viewed.
GblStringView GblStringView_removeSuffix(GblStringView self, size_t length)
Returns a new GblStringView which has the last length characters chopped off, raising an error upon f...
size_t GblStringView_findLastNotOf(GblStringView self, const char *pChars, size_t len, size_t offset)
Returns the index of the last occurrence of a character NOT provided within pChars,...
GblStringView GblStringView_fromEmpty(void)
Constructs an empty GblStringView (same as zero initializing one)
size_t GblStringView_find(GblStringView self, const char *pStr, size_t len, size_t offset)
Returns the starting index of the first occurrence of pStr appearing as a substring within the given ...
uint8_t GblStringView_toUint8(GblStringView self, GblBool *pSuccess)
Converts the given GblStringView to a uint8_t value, optionally returning whether the conversion succ...
size_t GblStringView_findFirstNotOf(GblStringView self, const char *pChars, size_t len, size_t offset)
Returns the index of the first occurrence of a character NOT provided within pChars,...
int GblStringView_compareIgnoreCase(GblStringView self, const char *pString, size_t len)
Lexicographically compares the given view to pString, ignoring case, returning a positive or negative...
size_t GblStringView_count(GblStringView self, const char *pString, size_t len)
Returns the number of times the substring, pString, appears within the given GblStringView.
char GblStringView_last(GblStringView self)
Returns the last character within the given view, raising an error if the view is empty.
int GblStringView_scanf(GblStringView self, const char *pFmt,...)
Calls libC's sscanf() using the given GblStringView as the source buffer.
uint16_t GblStringView_toUint16(GblStringView self, GblBool *pSuccess)
Converts the given GblStringView to a uint16_t value, optionally returning whether the conversion suc...
GblStringView GblStringView_removePrefix(GblStringView self, size_t length)
! Returns a new GblStringView which has had the first length characters chopped off,...
char GblStringView_toChar(GblStringView self, GblBool *pSuccess)
Converts the given GblStringView to a char value, optionally returning whether the conversion succeed...
GblBool GblStringView_equalsIgnoreCase(GblStringView self, const char *pString, size_t len)
Returns GBL_TRUE if the given string view equals pString (ignoring casing on both operands) or return...
GblBool GblStringView_startsWith(GblStringView self, const char *pString, size_t len)
Returns GBL_TRUE if the given GblStringView starts with the substring given by pString,...
uint64_t GblStringView_toUint64(GblStringView self, GblBool *pSuccess)
Converts the given GblStringView to a uint64_t value, optionally returning whether the conversion suc...
size_t GblStringView_findLastOf(GblStringView self, const char *pChars, size_t len, size_t offset)
Returns the index of the last occurrence of one of the characters provided within pChars,...
size_t GblStringView_findFirstOf(GblStringView self, const char *pChars, size_t len, size_t offset)
Returns the index of the first occurrence of one of the characters provided within pChars,...
int GblStringView_scanfVa(GblStringView self, const char *pFmt, va_list *pVarArgs)
Calls libC's vsscanf() using the given GblStringView as the source buffer.
char GblStringView_first(GblStringView self)
Returns the first character within the given view, raising an error if the view is empty.