libGimbal 0.1.0
C17-Based Extended Standard Library and Cross-Language Runtime Framework
Loading...
Searching...
No Matches
gimbal_string_ref.h
Go to the documentation of this file.
1/*! \file
2 * \brief ::GblStringRef type and related functions
3 * \ingroup strings
4 *
5 * ::GblStringRef is a read-only, reference-counted string used to
6 * efficiently store, reference, and copy strings. It is also
7 * type-compatible with const char*, so it masquerades as a regular
8 * C-string to conveniently support regular C string APIs and character
9 * array accesses.
10 *
11 * \note
12 * ::GblStringRef also stores its length internally, so
13 * the GblStringRef_length() function and GblStringRef_view()
14 * functions are extremely efficient. Favor this over strlen(),
15 * when you know you're working with a ::GblStringRef.
16 *
17 * The following example illustrates how it may be used:
18 *
19 * GblStringRef* pRef = GblStringRef_create("lolol");
20 *
21 * // Look, I'm a regular C string!
22 * assert(pRef[0] == 'l');
23 * assert(strlen(pRef) == 5);
24 * assert(strcmp(pRef, "lolol") == 0);
25 *
26 * // owait, no, I'm not.
27 * GblStringRef* pRef2 = GblStringRef_ref(pRef);
28 * assert(GblStringRef_refCount(pRef2) == 2);
29 *
30 * // even stores length to save on strlen() / StringView time
31 * assert(GblStringRef_length(pRef2) == 5);
32 *
33 * // no strlen() happening here, fast.
34 * GblStringView view = GblStringRef_view(pRef);
35 *
36 * GblStringRef_unref(pRef); // now refCount is 1
37 * GblStringRef_unref(pRef2); // now it's deleted
38 *
39 * \author 2023 Falco Girgis
40 * \copyright MIT License
41 */
42
43#ifndef GIMBAL_STRING_REF_H
44#define GIMBAL_STRING_REF_H
45
47
48#define GBL_SELF_TYPE GblStringRef
49
51
52/*! \brief Reference-counted, const char*-compatible string type
53 * \ingroup strings
54 *
55 * GblStringRef is a const char*-compatible reference-counted,
56 * read-only string type allowing for the sharing of a single
57 * string allocation between multiple different locations.
58 *
59 * \sa gimbal_string_ref.h
60 */
61typedef const char GblStringRef;
62
63/*! \name Lifetime Management
64 * \brief Methods for managing GblStringRef references
65 * @{
66 */
67//! Creates and returns a reference containing \p pString, with optional length and context
69 size_t len/*=0*/,
70 GblContext* pCtx/*=NULL*/) GBL_NOEXCEPT;
71//! Returns a new reference to \p pRef, incrementing its internal reference count rather than actually copying
73//! Releases a reference to \p pRef, freeing the allocation if it was the last, returning the new refCount
75//! @}
76
77/*! \name Properties
78 * \brief Methods for reading stored and derived data
79 * @{
80 */
81//! Returns the GblContext that was created with the given GblStringRef
83//! Returns the number of active references remaining to the given GblStringRef
85//! Returns the cached length of the given GblStringRef
87//! Returns whether the given GblStringRef is valid (not NULL)
89//! Returns whether the given GblStringRef is empty, with nothing but a NULL terminator
91//! Returns whether the given GblStringRef is blank, containing only NULL or spacing characters
93//! Calculates and returns the 32-bit hash value associated with the givne GblStringRef
95//! @}
96
97/*! \name Accessors
98 * \brief Methods for accessing character and substrings
99 * @{
100 */
101//! Returns the character located at position \p idx, raising an error upon out-of-range
103//! Returns a GblStringView containing the character window given by \p offset and \p len
105 size_t offset/*=0*/,
106 size_t len/*=0*/) GBL_NOEXCEPT;
107//! @}
108
110
111//! \cond
112#define GblStringRef_create(...)
113 GblStringRef_createDefault_(__VA_ARGS__)
114#define GblStringRef_createDefault_(...)
115 GblStringRef_createDefault__(__VA_ARGS__, 0, NULL)
116#define GblStringRef_createDefault__(str, len, ctx, ...)
117 ((GblStringRef_create)(str, len, ctx))
118
119#define GblStringRef_view(...)
120 GblStringRef_viewDefault_(__VA_ARGS__)
121#define GblStringRef_viewDefault_(...)
122 GblStringRef_viewDefault__(__VA_ARGS__, 0, 0)
123#define GblStringRef_viewDefault__(ref, offset, len, ...)
124 ((GblStringRef_view)(ref, offset, len))
125//! \endcond
126
127#undef GBL_SELF_TYPE
128
129#endif // GIMBAL_STRING_REF_H
#define GBL_NOEXCEPT
#define GBL_DECLS_BEGIN
#define GBL_EXPORT
size_t GblStringRef_length(const GblStringRef *pSelf)
Returns the cached length of the given GblStringRef.
GblStringRef * GblStringRef_create(const char *pString, size_t len, GblContext *pCtx)
Creates and returns a reference containing pString, with optional length and context.
GblBool GblStringRef_blank(const GblStringRef *pSelf)
Returns whether the given GblStringRef is blank, containing only NULL or spacing characters.
GblBool GblStringRef_valid(const GblStringRef *pSelf)
Returns whether the given GblStringRef is valid (not NULL)
GblBool GblStringRef_empty(const GblStringRef *pSelf)
Returns whether the given GblStringRef is empty, with nothing but a NULL terminator.
char GblStringRef_at(const GblStringRef *pSelf, size_t idx)
Returns the character located at position idx, raising an error upon out-of-range.
GblContext * GblStringRef_context(const GblStringRef *pSelf)
Returns the GblContext that was created with the given GblStringRef.
GblRefCount GblStringRef_refCount(const GblStringRef *pSelf)
Returns the number of active references remaining to the given GblStringRef.
GblStringView GblStringRef_view(const GblStringRef *pSelf, size_t offset, size_t len)
Returns a GblStringView containing the character window given by offset and len.
GblRefCount GblStringRef_unref(const GblStringRef *pRef)
Releases a reference to pRef, freeing the allocation if it was the last, returning the new refCount.
GblHash GblStringRef_hash(const GblStringRef *pSelf)
Calculates and returns the 32-bit hash value associated with the givne GblStringRef.
GblStringRef * GblStringRef_ref(const GblStringRef *pRef)
Returns a new reference to pRef, incrementing its internal reference count rather than actually copyi...
uint32_t GblHash
Type representing a calculated numeric hash across the codebase.
uint8_t GblBool
Basic boolean type, standardized to sizeof(char)
uint16_t GblRefCount
Type able to hold a reference counter across the codebase.
const char GblStringRef
Reference-counted, const char*-compatible string type.