Loading...
Searching...
No Matches
String.h
Go to the documentation of this file.
/***************************************************************************************************
Zyan Core Library (Zycore-C)
Original Author : Florian Bernd
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
***************************************************************************************************/
#ifndef ZYCORE_STRING_H
#define ZYCORE_STRING_H
#include <Zycore/Allocator.h>
#include <Zycore/Status.h>
#include <Zycore/Types.h>
#include <Zycore/Vector.h>
#ifdef __cplusplus
extern "C" {
#endif
/* ============================================================================================== */
/* Constants */
/* ============================================================================================== */
#define ZYAN_STRING_MIN_CAPACITY 32
#define ZYAN_STRING_DEFAULT_GROWTH_FACTOR 2
#define ZYAN_STRING_DEFAULT_SHRINK_THRESHOLD 4
/* ============================================================================================== */
/* Enums and types */
/* ============================================================================================== */
/* ---------------------------------------------------------------------------------------------- */
/* String flags */
/* ---------------------------------------------------------------------------------------------- */
typedef ZyanU8 ZyanStringFlags;
#define ZYAN_STRING_HAS_FIXED_CAPACITY 0x01 // (1 << 0)
/* ---------------------------------------------------------------------------------------------- */
/* String */
/* ---------------------------------------------------------------------------------------------- */
typedef struct ZyanString_
{
ZyanStringFlags flags;
ZyanVector vector;
} ZyanString;
/* ---------------------------------------------------------------------------------------------- */
/* View */
/* ---------------------------------------------------------------------------------------------- */
typedef struct ZyanStringView_
{
ZyanString string;
} ZyanStringView;
/* ---------------------------------------------------------------------------------------------- */
/* ============================================================================================== */
/* Macros */
/* ============================================================================================== */
/* ---------------------------------------------------------------------------------------------- */
/* General */
/* ---------------------------------------------------------------------------------------------- */
#define ZYAN_STRING_INITIALIZER \
{ \
/* flags */ 0, \
/* vector */ ZYAN_VECTOR_INITIALIZER \
}
/* ---------------------------------------------------------------------------------------------- */
/* Helper macros */
/* ---------------------------------------------------------------------------------------------- */
#define ZYAN_STRING_TO_VIEW(string) (const ZyanStringView*)(string)
#define ZYAN_DEFINE_STRING_VIEW(string) \
{ \
/* string */ \
{ \
/* flags */ 0, \
/* vector */ \
{ \
/* allocator */ ZYAN_NULL, \
/* growth_factor */ 1, \
/* shrink_threshold */ 0, \
/* size */ sizeof(string), \
/* capacity */ sizeof(string), \
/* element_size */ sizeof(char), \
/* destructor */ ZYAN_NULL, \
/* data */ (char*)(string) \
} \
} \
}
/* ---------------------------------------------------------------------------------------------- */
/* ============================================================================================== */
/* Exported functions */
/* ============================================================================================== */
/* ---------------------------------------------------------------------------------------------- */
/* Constructor and destructor */
/* ---------------------------------------------------------------------------------------------- */
#ifndef ZYAN_NO_LIBC
ZYCORE_EXPORT ZYAN_REQUIRES_LIBC ZyanStatus ZyanStringInit(ZyanString* string, ZyanUSize capacity);
#endif // ZYAN_NO_LIBC
ZYCORE_EXPORT ZyanStatus ZyanStringInitEx(ZyanString* string, ZyanUSize capacity,
ZyanAllocator* allocator, ZyanU8 growth_factor, ZyanU8 shrink_threshold);
ZYCORE_EXPORT ZyanStatus ZyanStringInitCustomBuffer(ZyanString* string, char* buffer,
ZyanUSize capacity);
ZYCORE_EXPORT ZyanStatus ZyanStringDestroy(ZyanString* string);
/* ---------------------------------------------------------------------------------------------- */
/* Duplication */
/* ---------------------------------------------------------------------------------------------- */
#ifndef ZYAN_NO_LIBC
ZYCORE_EXPORT ZYAN_REQUIRES_LIBC ZyanStatus ZyanStringDuplicate(ZyanString* destination,
const ZyanStringView* source, ZyanUSize capacity);
#endif // ZYAN_NO_LIBC
ZYCORE_EXPORT ZyanStatus ZyanStringDuplicateEx(ZyanString* destination,
const ZyanStringView* source, ZyanUSize capacity, ZyanAllocator* allocator,
ZyanU8 growth_factor, ZyanU8 shrink_threshold);
ZYCORE_EXPORT ZyanStatus ZyanStringDuplicateCustomBuffer(ZyanString* destination,
const ZyanStringView* source, char* buffer, ZyanUSize capacity);
/* ---------------------------------------------------------------------------------------------- */
/* Concatenation */
/* ---------------------------------------------------------------------------------------------- */
#ifndef ZYAN_NO_LIBC
ZYCORE_EXPORT ZYAN_REQUIRES_LIBC ZyanStatus ZyanStringConcat(ZyanString* destination,
const ZyanStringView* s1, const ZyanStringView* s2, ZyanUSize capacity);
#endif // ZYAN_NO_LIBC
ZYCORE_EXPORT ZyanStatus ZyanStringConcatEx(ZyanString* destination, const ZyanStringView* s1,
const ZyanStringView* s2, ZyanUSize capacity, ZyanAllocator* allocator, ZyanU8 growth_factor,
ZyanU8 shrink_threshold);
ZYCORE_EXPORT ZyanStatus ZyanStringConcatCustomBuffer(ZyanString* destination,
const ZyanStringView* s1, const ZyanStringView* s2, char* buffer, ZyanUSize capacity);
/* ---------------------------------------------------------------------------------------------- */
/* Views */
/* ---------------------------------------------------------------------------------------------- */
ZYCORE_EXPORT ZyanStatus ZyanStringViewInsideView(ZyanStringView* view,
const ZyanStringView* source);
ZYCORE_EXPORT ZyanStatus ZyanStringViewInsideViewEx(ZyanStringView* view,
const ZyanStringView* source, ZyanUSize index, ZyanUSize count);
ZYCORE_EXPORT ZyanStatus ZyanStringViewInsideBuffer(ZyanStringView* view, const char* string);
ZYCORE_EXPORT ZyanStatus ZyanStringViewInsideBufferEx(ZyanStringView* view, const char* buffer,
ZyanUSize length);
ZYCORE_EXPORT ZyanStatus ZyanStringViewGetSize(const ZyanStringView* view, ZyanUSize* size);
ZYCORE_EXPORT ZyanStatus ZyanStringViewGetData(const ZyanStringView* view, const char** buffer);
/* ---------------------------------------------------------------------------------------------- */
/* Character access */
/* ---------------------------------------------------------------------------------------------- */
ZYCORE_EXPORT ZyanStatus ZyanStringGetChar(const ZyanStringView* string, ZyanUSize index,
char* value);
ZYCORE_EXPORT ZyanStatus ZyanStringGetCharMutable(ZyanString* string, ZyanUSize index,
char** value);
ZYCORE_EXPORT ZyanStatus ZyanStringSetChar(ZyanString* string, ZyanUSize index, char value);
/* ---------------------------------------------------------------------------------------------- */
/* Insertion */
/* ---------------------------------------------------------------------------------------------- */
ZYCORE_EXPORT ZyanStatus ZyanStringInsert(ZyanString* destination, ZyanUSize index,
const ZyanStringView* source);
ZYCORE_EXPORT ZyanStatus ZyanStringInsertEx(ZyanString* destination, ZyanUSize destination_index,
const ZyanStringView* source, ZyanUSize source_index, ZyanUSize count);
/* ---------------------------------------------------------------------------------------------- */
/* Appending */
/* ---------------------------------------------------------------------------------------------- */
ZYCORE_EXPORT ZyanStatus ZyanStringAppend(ZyanString* destination, const ZyanStringView* source);
ZYCORE_EXPORT ZyanStatus ZyanStringAppendEx(ZyanString* destination, const ZyanStringView* source,
ZyanUSize source_index, ZyanUSize count);
/* ---------------------------------------------------------------------------------------------- */
/* Deletion */
/* ---------------------------------------------------------------------------------------------- */
ZYCORE_EXPORT ZyanStatus ZyanStringDelete(ZyanString* string, ZyanUSize index, ZyanUSize count);
ZYCORE_EXPORT ZyanStatus ZyanStringTruncate(ZyanString* string, ZyanUSize index);
ZYCORE_EXPORT ZyanStatus ZyanStringClear(ZyanString* string);
/* ---------------------------------------------------------------------------------------------- */
/* Searching */
/* ---------------------------------------------------------------------------------------------- */
ZYCORE_EXPORT ZyanStatus ZyanStringLPos(const ZyanStringView* haystack,
const ZyanStringView* needle, ZyanISize* found_index);
ZYCORE_EXPORT ZyanStatus ZyanStringLPosEx(const ZyanStringView* haystack,
const ZyanStringView* needle, ZyanISize* found_index, ZyanUSize index, ZyanUSize count);
ZYCORE_EXPORT ZyanStatus ZyanStringLPosI(const ZyanStringView* haystack,
const ZyanStringView* needle, ZyanISize* found_index);
ZYCORE_EXPORT ZyanStatus ZyanStringLPosIEx(const ZyanStringView* haystack,
const ZyanStringView* needle, ZyanISize* found_index, ZyanUSize index, ZyanUSize count);
ZYCORE_EXPORT ZyanStatus ZyanStringRPos(const ZyanStringView* haystack,
const ZyanStringView* needle, ZyanISize* found_index);
ZYCORE_EXPORT ZyanStatus ZyanStringRPosEx(const ZyanStringView* haystack,
const ZyanStringView* needle, ZyanISize* found_index, ZyanUSize index, ZyanUSize count);
ZYCORE_EXPORT ZyanStatus ZyanStringRPosI(const ZyanStringView* haystack,
const ZyanStringView* needle, ZyanISize* found_index);
ZYCORE_EXPORT ZyanStatus ZyanStringRPosIEx(const ZyanStringView* haystack,
const ZyanStringView* needle, ZyanISize* found_index, ZyanUSize index, ZyanUSize count);
/* ---------------------------------------------------------------------------------------------- */
/* Comparing */
/* ---------------------------------------------------------------------------------------------- */
ZYCORE_EXPORT ZyanStatus ZyanStringCompare(const ZyanStringView* s1, const ZyanStringView* s2,
ZyanI32* result);
ZYCORE_EXPORT ZyanStatus ZyanStringCompareI(const ZyanStringView* s1, const ZyanStringView* s2,
ZyanI32* result);
/* ---------------------------------------------------------------------------------------------- */
/* Case conversion */
/* ---------------------------------------------------------------------------------------------- */
ZYCORE_EXPORT ZyanStatus ZyanStringToLowerCase(ZyanString* string);
ZYCORE_EXPORT ZyanStatus ZyanStringToLowerCaseEx(ZyanString* string, ZyanUSize index,
ZyanUSize count);
ZYCORE_EXPORT ZyanStatus ZyanStringToUpperCase(ZyanString* string);
ZYCORE_EXPORT ZyanStatus ZyanStringToUpperCaseEx(ZyanString* string, ZyanUSize index,
ZyanUSize count);
/* ---------------------------------------------------------------------------------------------- */
/* Memory management */
/* ---------------------------------------------------------------------------------------------- */
ZYCORE_EXPORT ZyanStatus ZyanStringResize(ZyanString* string, ZyanUSize size);
ZYCORE_EXPORT ZyanStatus ZyanStringReserve(ZyanString* string, ZyanUSize capacity);
ZYCORE_EXPORT ZyanStatus ZyanStringShrinkToFit(ZyanString* string);
/* ---------------------------------------------------------------------------------------------- */
/* Information */
/* ---------------------------------------------------------------------------------------------- */
ZYCORE_EXPORT ZyanStatus ZyanStringGetCapacity(const ZyanString* string, ZyanUSize* capacity);
ZYCORE_EXPORT ZyanStatus ZyanStringGetSize(const ZyanString* string, ZyanUSize* size);
ZYCORE_EXPORT ZyanStatus ZyanStringGetData(const ZyanString* string, const char** value);
/* ---------------------------------------------------------------------------------------------- */
/* ============================================================================================== */
#ifdef __cplusplus
}
#endif
#endif // ZYCORE_STRING_H
Allocator.h
ZyanAllocator
struct ZyanAllocator_ ZyanAllocator
ZYAN_REQUIRES_LIBC
#define ZYAN_REQUIRES_LIBC
Definition Defines.h:382
ZYCORE_EXPORT
#define ZYCORE_EXPORT
Definition Defines.h:244
Status.h
ZyanStatus
ZyanU32 ZyanStatus
Definition Status.h:48
ZyanStringViewInsideViewEx
ZYCORE_EXPORT ZyanStatus ZyanStringViewInsideViewEx(ZyanStringView *view, const ZyanStringView *source, ZyanUSize index, ZyanUSize count)
ZyanStringViewInsideView
ZYCORE_EXPORT ZyanStatus ZyanStringViewInsideView(ZyanStringView *view, const ZyanStringView *source)
ZyanStringToUpperCase
ZYCORE_EXPORT ZyanStatus ZyanStringToUpperCase(ZyanString *string)
ZyanStringInitCustomBuffer
ZYCORE_EXPORT ZyanStatus ZyanStringInitCustomBuffer(ZyanString *string, char *buffer, ZyanUSize capacity)
ZyanStringDuplicateEx
ZYCORE_EXPORT ZyanStatus ZyanStringDuplicateEx(ZyanString *destination, const ZyanStringView *source, ZyanUSize capacity, ZyanAllocator *allocator, ZyanU8 growth_factor, ZyanU8 shrink_threshold)
ZyanStringGetData
ZYCORE_EXPORT ZyanStatus ZyanStringGetData(const ZyanString *string, const char **value)
ZyanStringRPosI
ZYCORE_EXPORT ZyanStatus ZyanStringRPosI(const ZyanStringView *haystack, const ZyanStringView *needle, ZyanISize *found_index)
ZyanStringConcatEx
ZYCORE_EXPORT ZyanStatus ZyanStringConcatEx(ZyanString *destination, const ZyanStringView *s1, const ZyanStringView *s2, ZyanUSize capacity, ZyanAllocator *allocator, ZyanU8 growth_factor, ZyanU8 shrink_threshold)
ZyanStringToUpperCaseEx
ZYCORE_EXPORT ZyanStatus ZyanStringToUpperCaseEx(ZyanString *string, ZyanUSize index, ZyanUSize count)
ZyanStringView
struct ZyanStringView_ ZyanStringView
ZyanStringDestroy
ZYCORE_EXPORT ZyanStatus ZyanStringDestroy(ZyanString *string)
ZyanStringToLowerCase
ZYCORE_EXPORT ZyanStatus ZyanStringToLowerCase(ZyanString *string)
ZyanStringCompareI
ZYCORE_EXPORT ZyanStatus ZyanStringCompareI(const ZyanStringView *s1, const ZyanStringView *s2, ZyanI32 *result)
ZyanStringInit
ZYCORE_EXPORT ZYAN_REQUIRES_LIBC ZyanStatus ZyanStringInit(ZyanString *string, ZyanUSize capacity)
ZyanStringRPosIEx
ZYCORE_EXPORT ZyanStatus ZyanStringRPosIEx(const ZyanStringView *haystack, const ZyanStringView *needle, ZyanISize *found_index, ZyanUSize index, ZyanUSize count)
ZyanStringInsertEx
ZYCORE_EXPORT ZyanStatus ZyanStringInsertEx(ZyanString *destination, ZyanUSize destination_index, const ZyanStringView *source, ZyanUSize source_index, ZyanUSize count)
ZyanStringReserve
ZYCORE_EXPORT ZyanStatus ZyanStringReserve(ZyanString *string, ZyanUSize capacity)
ZyanString
struct ZyanString_ ZyanString
ZyanStringGetCapacity
ZYCORE_EXPORT ZyanStatus ZyanStringGetCapacity(const ZyanString *string, ZyanUSize *capacity)
ZyanStringViewInsideBuffer
ZYCORE_EXPORT ZyanStatus ZyanStringViewInsideBuffer(ZyanStringView *view, const char *string)
ZyanStringViewInsideBufferEx
ZYCORE_EXPORT ZyanStatus ZyanStringViewInsideBufferEx(ZyanStringView *view, const char *buffer, ZyanUSize length)
ZyanStringLPos
ZYCORE_EXPORT ZyanStatus ZyanStringLPos(const ZyanStringView *haystack, const ZyanStringView *needle, ZyanISize *found_index)
ZyanStringRPosEx
ZYCORE_EXPORT ZyanStatus ZyanStringRPosEx(const ZyanStringView *haystack, const ZyanStringView *needle, ZyanISize *found_index, ZyanUSize index, ZyanUSize count)
ZyanStringGetChar
ZYCORE_EXPORT ZyanStatus ZyanStringGetChar(const ZyanStringView *string, ZyanUSize index, char *value)
ZyanStringToLowerCaseEx
ZYCORE_EXPORT ZyanStatus ZyanStringToLowerCaseEx(ZyanString *string, ZyanUSize index, ZyanUSize count)
ZyanStringConcatCustomBuffer
ZYCORE_EXPORT ZyanStatus ZyanStringConcatCustomBuffer(ZyanString *destination, const ZyanStringView *s1, const ZyanStringView *s2, char *buffer, ZyanUSize capacity)
ZyanStringCompare
ZYCORE_EXPORT ZyanStatus ZyanStringCompare(const ZyanStringView *s1, const ZyanStringView *s2, ZyanI32 *result)
ZyanStringClear
ZYCORE_EXPORT ZyanStatus ZyanStringClear(ZyanString *string)
ZyanStringConcat
ZYCORE_EXPORT ZYAN_REQUIRES_LIBC ZyanStatus ZyanStringConcat(ZyanString *destination, const ZyanStringView *s1, const ZyanStringView *s2, ZyanUSize capacity)
ZyanStringViewGetData
ZYCORE_EXPORT ZyanStatus ZyanStringViewGetData(const ZyanStringView *view, const char **buffer)
ZyanStringLPosEx
ZYCORE_EXPORT ZyanStatus ZyanStringLPosEx(const ZyanStringView *haystack, const ZyanStringView *needle, ZyanISize *found_index, ZyanUSize index, ZyanUSize count)
ZyanStringResize
ZYCORE_EXPORT ZyanStatus ZyanStringResize(ZyanString *string, ZyanUSize size)
ZyanStringViewGetSize
ZYCORE_EXPORT ZyanStatus ZyanStringViewGetSize(const ZyanStringView *view, ZyanUSize *size)
ZyanStringDuplicateCustomBuffer
ZYCORE_EXPORT ZyanStatus ZyanStringDuplicateCustomBuffer(ZyanString *destination, const ZyanStringView *source, char *buffer, ZyanUSize capacity)
ZyanStringLPosI
ZYCORE_EXPORT ZyanStatus ZyanStringLPosI(const ZyanStringView *haystack, const ZyanStringView *needle, ZyanISize *found_index)
ZyanStringGetCharMutable
ZYCORE_EXPORT ZyanStatus ZyanStringGetCharMutable(ZyanString *string, ZyanUSize index, char **value)
ZyanStringLPosIEx
ZYCORE_EXPORT ZyanStatus ZyanStringLPosIEx(const ZyanStringView *haystack, const ZyanStringView *needle, ZyanISize *found_index, ZyanUSize index, ZyanUSize count)
ZyanStringGetSize
ZYCORE_EXPORT ZyanStatus ZyanStringGetSize(const ZyanString *string, ZyanUSize *size)
ZyanStringAppendEx
ZYCORE_EXPORT ZyanStatus ZyanStringAppendEx(ZyanString *destination, const ZyanStringView *source, ZyanUSize source_index, ZyanUSize count)
ZyanStringFlags
ZyanU8 ZyanStringFlags
Definition String.h:75
ZyanStringInsert
ZYCORE_EXPORT ZyanStatus ZyanStringInsert(ZyanString *destination, ZyanUSize index, const ZyanStringView *source)
ZyanStringSetChar
ZYCORE_EXPORT ZyanStatus ZyanStringSetChar(ZyanString *string, ZyanUSize index, char value)
ZyanStringDuplicate
ZYCORE_EXPORT ZYAN_REQUIRES_LIBC ZyanStatus ZyanStringDuplicate(ZyanString *destination, const ZyanStringView *source, ZyanUSize capacity)
ZyanStringShrinkToFit
ZYCORE_EXPORT ZyanStatus ZyanStringShrinkToFit(ZyanString *string)
ZyanStringRPos
ZYCORE_EXPORT ZyanStatus ZyanStringRPos(const ZyanStringView *haystack, const ZyanStringView *needle, ZyanISize *found_index)
ZyanStringInitEx
ZYCORE_EXPORT ZyanStatus ZyanStringInitEx(ZyanString *string, ZyanUSize capacity, ZyanAllocator *allocator, ZyanU8 growth_factor, ZyanU8 shrink_threshold)
ZyanStringTruncate
ZYCORE_EXPORT ZyanStatus ZyanStringTruncate(ZyanString *string, ZyanUSize index)
ZyanStringDelete
ZYCORE_EXPORT ZyanStatus ZyanStringDelete(ZyanString *string, ZyanUSize index, ZyanUSize count)
ZyanStringAppend
ZYCORE_EXPORT ZyanStatus ZyanStringAppend(ZyanString *destination, const ZyanStringView *source)
Types.h
Vector.h
ZyanVector
struct ZyanVector_ ZyanVector
ZyanStringView_
Definition String.h:132
ZyanStringView_::string
ZyanString string
Definition String.h:139
ZyanString_
Definition String.h:98
ZyanString_::vector
ZyanVector vector
Definition String.h:106
ZyanString_::flags
ZyanStringFlags flags
Definition String.h:102