Zycore 1.5.2
Zyan Core Library for C
Loading...
Searching...
No Matches
String.h
Go to the documentation of this file.
1/***************************************************************************************************
2
3 Zyan Core Library (Zycore-C)
4
5 Original Author : Florian Bernd
6
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in all
15 * copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24
25***************************************************************************************************/
26
31
32#ifndef ZYCORE_STRING_H
33#define ZYCORE_STRING_H
34
35#include <Zycore/Allocator.h>
36#include <Zycore/Status.h>
37#include <Zycore/Types.h>
38#include <Zycore/Vector.h>
39
40#ifdef __cplusplus
41extern "C" {
42#endif
43
44/* ============================================================================================== */
45/* Constants */
46/* ============================================================================================== */
47
52#define ZYAN_STRING_MIN_CAPACITY 32
53
57#define ZYAN_STRING_DEFAULT_GROWTH_FACTOR 2
58
62#define ZYAN_STRING_DEFAULT_SHRINK_THRESHOLD 4
63
64/* ============================================================================================== */
65/* Enums and types */
66/* ============================================================================================== */
67
68/* ---------------------------------------------------------------------------------------------- */
69/* String flags */
70/* ---------------------------------------------------------------------------------------------- */
71
76
80#define ZYAN_STRING_HAS_FIXED_CAPACITY 0x01 // (1 << 0)
81
82/* ---------------------------------------------------------------------------------------------- */
83/* String */
84/* ---------------------------------------------------------------------------------------------- */
85
108
109/* ---------------------------------------------------------------------------------------------- */
110/* View */
111/* ---------------------------------------------------------------------------------------------- */
112
141
142/* ---------------------------------------------------------------------------------------------- */
143
144/* ============================================================================================== */
145/* Macros */
146/* ============================================================================================== */
147
148/* ---------------------------------------------------------------------------------------------- */
149/* General */
150/* ---------------------------------------------------------------------------------------------- */
151
155#define ZYAN_STRING_INITIALIZER \
156 { \
157 /* flags */ 0, \
158 /* vector */ ZYAN_VECTOR_INITIALIZER \
159 }
160
161/* ---------------------------------------------------------------------------------------------- */
162/* Helper macros */
163/* ---------------------------------------------------------------------------------------------- */
164
168#define ZYAN_STRING_TO_VIEW(string) (const ZyanStringView*)(string)
169
175#define ZYAN_DEFINE_STRING_VIEW(string) \
176 { \
177 /* string */ \
178 { \
179 /* flags */ 0, \
180 /* vector */ \
181 { \
182 /* allocator */ ZYAN_NULL, \
183 /* growth_factor */ 1, \
184 /* shrink_threshold */ 0, \
185 /* size */ sizeof(string), \
186 /* capacity */ sizeof(string), \
187 /* element_size */ sizeof(char), \
188 /* destructor */ ZYAN_NULL, \
189 /* data */ (char*)(string) \
190 } \
191 } \
192 }
193
194/* ---------------------------------------------------------------------------------------------- */
195
196/* ============================================================================================== */
197/* Exported functions */
198/* ============================================================================================== */
199
200/* ---------------------------------------------------------------------------------------------- */
201/* Constructor and destructor */
202/* ---------------------------------------------------------------------------------------------- */
203
204#ifndef ZYAN_NO_LIBC
205
223
224#endif // ZYAN_NO_LIBC
225
247 ZyanAllocator* allocator, ZyanU8 growth_factor, ZyanU8 shrink_threshold);
248
263 ZyanUSize capacity);
264
274
275/* ---------------------------------------------------------------------------------------------- */
276/* Duplication */
277/* ---------------------------------------------------------------------------------------------- */
278
279#ifndef ZYAN_NO_LIBC
280
305 const ZyanStringView* source, ZyanUSize capacity);
306
307#endif // ZYAN_NO_LIBC
308
337 const ZyanStringView* source, ZyanUSize capacity, ZyanAllocator* allocator,
338 ZyanU8 growth_factor, ZyanU8 shrink_threshold);
339
361 const ZyanStringView* source, char* buffer, ZyanUSize capacity);
362
363/* ---------------------------------------------------------------------------------------------- */
364/* Concatenation */
365/* ---------------------------------------------------------------------------------------------- */
366
367#ifndef ZYAN_NO_LIBC
368
397 const ZyanStringView* s1, const ZyanStringView* s2, ZyanUSize capacity);
398
399#endif // ZYAN_NO_LIBC
400
433 const ZyanStringView* s2, ZyanUSize capacity, ZyanAllocator* allocator, ZyanU8 growth_factor,
434 ZyanU8 shrink_threshold);
435
460 const ZyanStringView* s1, const ZyanStringView* s2, char* buffer, ZyanUSize capacity);
461
462/* ---------------------------------------------------------------------------------------------- */
463/* Views */
464/* ---------------------------------------------------------------------------------------------- */
465
478 const ZyanStringView* source);
479
494 const ZyanStringView* source, ZyanUSize index, ZyanUSize count);
495
505
516 ZyanUSize length);
517
527
539
540/* ---------------------------------------------------------------------------------------------- */
541/* Character access */
542/* ---------------------------------------------------------------------------------------------- */
543
554 char* value);
555
566 char** value);
567
578
579/* ---------------------------------------------------------------------------------------------- */
580/* Insertion */
581/* ---------------------------------------------------------------------------------------------- */
582
593 const ZyanStringView* source);
594
609 const ZyanStringView* source, ZyanUSize source_index, ZyanUSize count);
610
611/* ---------------------------------------------------------------------------------------------- */
612/* Appending */
613/* ---------------------------------------------------------------------------------------------- */
614
624
636 ZyanUSize source_index, ZyanUSize count);
637
638/* ---------------------------------------------------------------------------------------------- */
639/* Deletion */
640/* ---------------------------------------------------------------------------------------------- */
641
652
662
671
672/* ---------------------------------------------------------------------------------------------- */
673/* Searching */
674/* ---------------------------------------------------------------------------------------------- */
675
691 const ZyanStringView* needle, ZyanISize* found_index);
692
711 const ZyanStringView* needle, ZyanISize* found_index, ZyanUSize index, ZyanUSize count);
712
728 const ZyanStringView* needle, ZyanISize* found_index);
729
748 const ZyanStringView* needle, ZyanISize* found_index, ZyanUSize index, ZyanUSize count);
749
765 const ZyanStringView* needle, ZyanISize* found_index);
766
785 const ZyanStringView* needle, ZyanISize* found_index, ZyanUSize index, ZyanUSize count);
786
802 const ZyanStringView* needle, ZyanISize* found_index);
803
822 const ZyanStringView* needle, ZyanISize* found_index, ZyanUSize index, ZyanUSize count);
823
824/* ---------------------------------------------------------------------------------------------- */
825/* Comparing */
826/* ---------------------------------------------------------------------------------------------- */
827
846 ZyanI32* result);
847
866 ZyanI32* result);
867
868/* ---------------------------------------------------------------------------------------------- */
869/* Case conversion */
870/* ---------------------------------------------------------------------------------------------- */
871
883
897 ZyanUSize count);
898
910
924 ZyanUSize count);
925
926/* ---------------------------------------------------------------------------------------------- */
927/* Memory management */
928/* ---------------------------------------------------------------------------------------------- */
929
942
955
967
968/* ---------------------------------------------------------------------------------------------- */
969/* Information */
970/* ---------------------------------------------------------------------------------------------- */
971
981
992
1001ZYCORE_EXPORT ZyanStatus ZyanStringGetData(const ZyanString* string, const char** value);
1002
1003/* ---------------------------------------------------------------------------------------------- */
1004
1005/* ============================================================================================== */
1006
1007#ifdef __cplusplus
1008}
1009#endif
1010
1011#endif // ZYCORE_STRING_H
struct ZyanAllocator_ ZyanAllocator
Defines the ZyanAllocator struct.
#define ZYAN_REQUIRES_LIBC
Marks functions that require libc (cannot be used with ZYAN_NO_LIBC).
Definition Defines.h:468
#define ZYCORE_EXPORT
Symbol is exported in shared library builds.
Definition Defines.h:330
Status code definitions and check macros.
ZyanU32 ZyanStatus
Defines the ZyanStatus data type.
Definition Status.h:48
ZYCORE_EXPORT ZyanStatus ZyanStringViewInsideViewEx(ZyanStringView *view, const ZyanStringView *source, ZyanUSize index, ZyanUSize count)
Returns a view inside an existing view/string starting from the given index.
ZYCORE_EXPORT ZyanStatus ZyanStringViewInsideView(ZyanStringView *view, const ZyanStringView *source)
Returns a view inside an existing view/string.
ZYCORE_EXPORT ZyanStatus ZyanStringToUpperCase(ZyanString *string)
Converts the given string to uppercase letters.
ZYCORE_EXPORT ZyanStatus ZyanStringInitCustomBuffer(ZyanString *string, char *buffer, ZyanUSize capacity)
Initializes the given ZyanString instance and configures it to use a custom user defined buffer with ...
ZYCORE_EXPORT ZyanStatus ZyanStringDuplicateEx(ZyanString *destination, const ZyanStringView *source, ZyanUSize capacity, ZyanAllocator *allocator, ZyanU8 growth_factor, ZyanU8 shrink_threshold)
Initializes a new ZyanString instance by duplicating an existing string and sets a custom allocator a...
ZYCORE_EXPORT ZyanStatus ZyanStringGetData(const ZyanString *string, const char **value)
Returns the C-style string of the given ZyanString instance.
ZYCORE_EXPORT ZyanStatus ZyanStringRPosI(const ZyanStringView *haystack, const ZyanStringView *needle, ZyanISize *found_index)
Performs a case-insensitive search for the first occurrence of needle in the given haystack starting ...
ZYCORE_EXPORT ZyanStatus ZyanStringConcatEx(ZyanString *destination, const ZyanStringView *s1, const ZyanStringView *s2, ZyanUSize capacity, ZyanAllocator *allocator, ZyanU8 growth_factor, ZyanU8 shrink_threshold)
Initializes a new ZyanString instance by concatenating two existing strings and sets a custom allocat...
ZYCORE_EXPORT ZyanStatus ZyanStringToUpperCaseEx(ZyanString *string, ZyanUSize index, ZyanUSize count)
Converts count characters of the given string to uppercase letters.
struct ZyanStringView_ ZyanStringView
Defines the ZyanStringView struct.
ZYCORE_EXPORT ZyanStatus ZyanStringDestroy(ZyanString *string)
Destroys the given ZyanString instance.
ZYCORE_EXPORT ZyanStatus ZyanStringToLowerCase(ZyanString *string)
Converts the given string to lowercase letters.
ZYCORE_EXPORT ZyanStatus ZyanStringCompareI(const ZyanStringView *s1, const ZyanStringView *s2, ZyanI32 *result)
Performs a case-insensitive comparison of two strings.
ZYCORE_EXPORT ZYAN_REQUIRES_LIBC ZyanStatus ZyanStringInit(ZyanString *string, ZyanUSize capacity)
Initializes the given ZyanString instance.
ZYCORE_EXPORT ZyanStatus ZyanStringRPosIEx(const ZyanStringView *haystack, const ZyanStringView *needle, ZyanISize *found_index, ZyanUSize index, ZyanUSize count)
Performs a case-insensitive search for the first occurrence of needle in the given haystack starting ...
ZYCORE_EXPORT ZyanStatus ZyanStringInsertEx(ZyanString *destination, ZyanUSize destination_index, const ZyanStringView *source, ZyanUSize source_index, ZyanUSize count)
Inserts count characters of the source string in the destination string at the given index.
ZYCORE_EXPORT ZyanStatus ZyanStringReserve(ZyanString *string, ZyanUSize capacity)
Changes the capacity of the given ZyanString instance.
struct ZyanString_ ZyanString
Defines the ZyanString struct.
ZYCORE_EXPORT ZyanStatus ZyanStringGetCapacity(const ZyanString *string, ZyanUSize *capacity)
Returns the current capacity of the string.
ZYCORE_EXPORT ZyanStatus ZyanStringViewInsideBuffer(ZyanStringView *view, const char *string)
Returns a view inside a null-terminated C-style string.
ZYCORE_EXPORT ZyanStatus ZyanStringViewInsideBufferEx(ZyanStringView *view, const char *buffer, ZyanUSize length)
Returns a view inside a character buffer with custom length.
ZYCORE_EXPORT ZyanStatus ZyanStringLPos(const ZyanStringView *haystack, const ZyanStringView *needle, ZyanISize *found_index)
Searches for the first occurrence of needle in the given haystack starting from the left.
ZYCORE_EXPORT ZyanStatus ZyanStringRPosEx(const ZyanStringView *haystack, const ZyanStringView *needle, ZyanISize *found_index, ZyanUSize index, ZyanUSize count)
Searches for the first occurrence of needle in the given haystack starting from the right.
ZYCORE_EXPORT ZyanStatus ZyanStringGetChar(const ZyanStringView *string, ZyanUSize index, char *value)
Returns the character at the given index.
ZYCORE_EXPORT ZyanStatus ZyanStringToLowerCaseEx(ZyanString *string, ZyanUSize index, ZyanUSize count)
Converts count characters of the given string to lowercase letters.
ZYCORE_EXPORT ZyanStatus ZyanStringConcatCustomBuffer(ZyanString *destination, const ZyanStringView *s1, const ZyanStringView *s2, char *buffer, ZyanUSize capacity)
Initializes a new ZyanString instance by concatenating two existing strings and configures it to use ...
ZYCORE_EXPORT ZyanStatus ZyanStringCompare(const ZyanStringView *s1, const ZyanStringView *s2, ZyanI32 *result)
Compares two strings.
ZYCORE_EXPORT ZyanStatus ZyanStringClear(ZyanString *string)
Erases the given string.
ZYCORE_EXPORT ZYAN_REQUIRES_LIBC ZyanStatus ZyanStringConcat(ZyanString *destination, const ZyanStringView *s1, const ZyanStringView *s2, ZyanUSize capacity)
Initializes a new ZyanString instance by concatenating two existing strings.
ZYCORE_EXPORT ZyanStatus ZyanStringViewGetData(const ZyanStringView *view, const char **buffer)
Returns the C-style string of the given ZyanString instance.
ZYCORE_EXPORT ZyanStatus ZyanStringLPosEx(const ZyanStringView *haystack, const ZyanStringView *needle, ZyanISize *found_index, ZyanUSize index, ZyanUSize count)
Searches for the first occurrence of needle in the given haystack starting from the left.
ZYCORE_EXPORT ZyanStatus ZyanStringResize(ZyanString *string, ZyanUSize size)
Resizes the given ZyanString instance.
ZYCORE_EXPORT ZyanStatus ZyanStringViewGetSize(const ZyanStringView *view, ZyanUSize *size)
Returns the size (number of characters) of the view.
ZYCORE_EXPORT ZyanStatus ZyanStringDuplicateCustomBuffer(ZyanString *destination, const ZyanStringView *source, char *buffer, ZyanUSize capacity)
Initializes a new ZyanString instance by duplicating an existing string and configures it to use a cu...
ZYCORE_EXPORT ZyanStatus ZyanStringLPosI(const ZyanStringView *haystack, const ZyanStringView *needle, ZyanISize *found_index)
Performs a case-insensitive search for the first occurrence of needle in the given haystack starting ...
ZYCORE_EXPORT ZyanStatus ZyanStringGetCharMutable(ZyanString *string, ZyanUSize index, char **value)
Returns a pointer to the character at the given index.
ZYCORE_EXPORT ZyanStatus ZyanStringLPosIEx(const ZyanStringView *haystack, const ZyanStringView *needle, ZyanISize *found_index, ZyanUSize index, ZyanUSize count)
Performs a case-insensitive search for the first occurrence of needle in the given haystack starting ...
ZYCORE_EXPORT ZyanStatus ZyanStringGetSize(const ZyanString *string, ZyanUSize *size)
Returns the current size (number of characters) of the string (excluding the terminating zero charact...
ZYCORE_EXPORT ZyanStatus ZyanStringAppendEx(ZyanString *destination, const ZyanStringView *source, ZyanUSize source_index, ZyanUSize count)
Appends count characters of the source string to the end of the destination string.
ZyanU8 ZyanStringFlags
Defines the ZyanStringFlags data-type.
Definition String.h:75
ZYCORE_EXPORT ZyanStatus ZyanStringInsert(ZyanString *destination, ZyanUSize index, const ZyanStringView *source)
Inserts the content of the source string in the destination string at the given index.
ZYCORE_EXPORT ZyanStatus ZyanStringSetChar(ZyanString *string, ZyanUSize index, char value)
Assigns a new value to the character at the given index.
ZYCORE_EXPORT ZYAN_REQUIRES_LIBC ZyanStatus ZyanStringDuplicate(ZyanString *destination, const ZyanStringView *source, ZyanUSize capacity)
Initializes a new ZyanString instance by duplicating an existing string.
ZYCORE_EXPORT ZyanStatus ZyanStringShrinkToFit(ZyanString *string)
Shrinks the capacity of the given string to match it's size.
ZYCORE_EXPORT ZyanStatus ZyanStringRPos(const ZyanStringView *haystack, const ZyanStringView *needle, ZyanISize *found_index)
Searches for the first occurrence of needle in the given haystack starting from the right.
ZYCORE_EXPORT ZyanStatus ZyanStringInitEx(ZyanString *string, ZyanUSize capacity, ZyanAllocator *allocator, ZyanU8 growth_factor, ZyanU8 shrink_threshold)
Initializes the given ZyanString instance and sets a custom allocator and memory allocation/deallocat...
ZYCORE_EXPORT ZyanStatus ZyanStringTruncate(ZyanString *string, ZyanUSize index)
Deletes all remaining characters from the given string, starting at index.
ZYCORE_EXPORT ZyanStatus ZyanStringDelete(ZyanString *string, ZyanUSize index, ZyanUSize count)
Deletes characters from the given string, starting at index.
ZYCORE_EXPORT ZyanStatus ZyanStringAppend(ZyanString *destination, const ZyanStringView *source)
Appends the content of the source string to the end of the destination string.
Includes and defines some default data types.
ptrdiff_t ZyanISize
Definition Types.h:225
size_t ZyanUSize
Definition Types.h:224
int32_t ZyanI32
Definition Types.h:222
uint8_t ZyanU8
Definition Types.h:216
Implements the vector container class.
struct ZyanVector_ ZyanVector
Defines the ZyanVector struct.
Defines the ZyanStringView struct.
Definition String.h:132
ZyanString string
The string data.
Definition String.h:139
Defines the ZyanString struct.
Definition String.h:98
ZyanVector vector
The vector that contains the actual string.
Definition String.h:106
ZyanStringFlags flags
String flags.
Definition String.h:102