libGimbal 0.1.0
C17-Based Extended Standard Library and Cross-Language Runtime Framework
Loading...
Searching...
No Matches
gimbal_pattern.h
Go to the documentation of this file.
1/*! \file
2 * \brief GblPattern: RegExp-style pattern matching
3 * \ingroup strings
4 *
5 * This file contains the API and opaque structure declaration
6 * for GblPattern, which represents a compiled regular expression,
7 * used for string matching operations.
8 *
9 * \warning
10 * LibGimbal uses a highly modified fork of the the Tiny C RegEx
11 * library for its back-end, lowest-level regular expression
12 * pattern matching, which prioritizes being lightweight and
13 * efficient over having complete support for all that RegEx
14 * has to offer. You may find some features missing, with others
15 * getting added at a later date.
16 *
17 * \todo
18 * - Wrap Tiny C Regex tests in gimbal Unit tests
19 * - `\b` word-boundary character class
20 *
21 * \author 2023, 2025 Falco Girgis
22 * \copyright MIT License
23 */
24#ifndef GIMBAL_PATTERN_H
25#define GIMBAL_PATTERN_H
26
27#include "../core/gimbal_decls.h"
28
29#define GBL_SELF_TYPE GblPattern
30
32
33GBL_FORWARD_DECLARE_STRUCT(GblStringView);
34GBL_FORWARD_DECLARE_STRUCT(GblStringBuffer);
35
36/*! \struct GblPattern
37 * \brief Opaque structure containing a compiled regular expression
38 *
39 * GblPattern is the compiled version of a regular expression
40 * string, produced by GblPattern_create(). It is faster
41 * than working with raw regular expression strings, since
42 * preprocessing has already been done.
43 *
44 * It is advised that when you are repeatedly using the same
45 * regular expression, you store it and use it as a GblPattern.
46 *
47 * \note
48 * GblPattern is a reference-counted shared pointer type.
49 *
50 * \ingroup strings
51 */
52struct GblPattern;
53typedef struct GblPattern GblPattern;
54
55/*! \name Lifetime Management
56 * \brief Methods for creating, referencing, and unreferencing patterns
57 * \relatesalso GblPattern
58 * @{
59 */
60//! Compiles the given regular expression into a pre-processed GblPattern
61GBL_EXPORT const GblPattern* GblPattern_create (const char* pRegExp) GBL_NOEXCEPT;
62//! Returns a new reference to an existing pattern, incrementing its refcount
64//! Releases a reference to a pattern, deallocating it upon reaching zero
66//! Returns the number of active references held to the given compiled pattern
68//! Returns the total number of active, allocated, compiled regex patterns
70//! @}
71
72/*! \name Properties and Operators
73 * \brief Methods for getters and basic operations
74 * \relatesalso GblPattern
75 * @{
76 */
77//! Compares two different compiled patterns to see if they are equivalent regexes
79//! Compares two different compiled patterns for exact value equality of their regexes
81//! Counts the total size of a compiled pattern and returns it in bytes
83//! Reconstructs an approximate string representation of the compiled pattern
84GBL_EXPORT const char* GblPattern_string (GBL_CSELF, GblStringBuffer* pBuff) GBL_NOEXCEPT;
85//! @}
86
87/*! \name Matching
88 * \brief Methods for generic pattern matching
89 * \relatesalso GblPattern
90 * @{
91 */
92/*! Finds the numbered match given by \p pCount, or 1 if NULL.
93 *
94 * GblPattern_match() searches through \p pString for substrings
95 * which match the pattern given by \p pSelf. When it has found
96 * the number of matches equal to the value given by \p pCount
97 * (or 1 if \p pCount is NULL), \p pMatch is set to the substring
98 * of the final match (or ignored when NULL) and GBL_TRUE is
99 * returned. If the match could not be found, GBL_FALSE is returned.
100 *
101 * \note
102 * When \p pCount points to a value of 0, no matches are expected
103 * to be found. If a match is found, GBL_FALSE is returned, otherwise
104 * GBL_TRUE is returned (inverted from normal).
105 *
106 * \note
107 * When \p pCount points to a value of -1, all matches are searched,
108 * with \p pMatch being updated to point to the final one and \p pCount
109 * being updated to store the final number of matches.
110 *
111 * \param pSelf The precompiled regular expression pattern
112 * \param pString The input string to search for matches within
113 * \param pMatch (Optional) Output used to hold the final match substring
114 * \param pCount (Optional, defaults to 1) Number of matches to find
115 *
116 * \retval GBL_FALSE The requested match number wasn't found.
117 * \retval GBL_TRUE The requested match number was found.
118 *
119 * \sa GblPattern_matchNot()
120 */
122 const char* pString,
123 GblStringView* pMatch/*=NULL*/,
124 int* pCount/*=NULL*/) GBL_NOEXCEPT;
125//! Dynamically-compiled string-based version of GblPattern_match()
127 const char* pString,
128 GblStringView* pMatch/*=NULL*/,
129 int* pCount/*=NULL*/) GBL_NOEXCEPT;
130//! @}
131
132/*! \name Inverse Matching
133 * \brief Methods for returning non matches
134 * \relatesalso GblPattern
135 * @{
136 */
137//! Behaves like GblPattern_match() except searching for NON-MATCHES
139 const char* pString,
140 GblStringView* pMatch/*=NULL*/,
141 int* pCount/*=NULL*/) GBL_NOEXCEPT;
142//! Dynamically-compiled string-based version of GblPattern_matchNot()
144 const char* pString,
145 GblStringView* pMatch/*=NULL*/,
146 int* pCount/*=NULL*/) GBL_NOEXCEPT;
147//! @}
148
149
150/*! \name Exact Matching
151 * \brief Methods for checking exact matches
152 * \relatesalso GblPattern
153 * @{
154 */
155 //! Returns GBL_TRUE if the given string EXACTLY matches the given pattern or GBL_FALSE otherwise
157//! Dynamically compiled string-based version of GblPattern_matchExact()
159 const char* pString) GBL_NOEXCEPT;
160//! @}
161
162/*! \name Match Counting
163 * \brief Methods used for counting matches
164 * \relatesalso GblPattern
165 * @{
166 */
167//! Returns the number of pattern matches found in \p pString
169//! Dynamically compiled, string-based version of GblPattern_matchExact()
170GBL_EXPORT size_t GblPattern_matchCountStr (const char* pRegExp,
171 const char* pString) GBL_NOEXCEPT;
172//! @}
173
175
176//! \cond
177#define GblPattern_match(...)
178 (GblPattern_matchDefault_(__VA_ARGS__))
179#define GblPattern_matchDefault_(...)
180 (GblPattern_matchDefault__(__VA_ARGS__, GBL_NULL, GBL_NULL))
181#define GblPattern_matchDefault__(pat, str, match, count, ...)
182 ((GblPattern_match)(pat, str, match, count))
183
184#define GblPattern_matchStr(...)
185 (GblPattern_matchStrDefault_(__VA_ARGS__))
186#define GblPattern_matchStrDefault_(...)
187 (GblPattern_matchStrDefault__(__VA_ARGS__, GBL_NULL, GBL_NULL))
188#define GblPattern_matchStrDefault__(exp, str, match, count, ...)
189 ((GblPattern_matchStr)(exp, str, match, count))
190
191#define GblPattern_matchNot(...)
192 (GblPattern_matchNotDefault_(__VA_ARGS__))
193#define GblPattern_matchNotDefault_(...)
194 (GblPattern_matchNotDefault__(__VA_ARGS__, GBL_NULL, GBL_NULL))
195#define GblPattern_matchNotDefault__(pat, str, match, count, ...)
196 ((GblPattern_matchNot)(pat, str, match, count))
197
198#define GblPattern_matchNotStr(...)
199 (GblPattern_matchNotStrDefault_(__VA_ARGS__))
200#define GblPattern_matchNotStrDefault_(...)
201 (GblPattern_matchNotStrDefault__(__VA_ARGS__, GBL_NULL, GBL_NULL))
202#define GblPattern_matchNotStrDefault__(exp, str, match, count, ...)
203 ((GblPattern_matchNotStr)(exp, str, match, count))
204//! \endcond
205
206#undef GBL_SELF_TYPE
207
208#endif // GIMBAL_PATTERN_H
#define GBL_NULL
#define GBL_NOEXCEPT
#define GBL_DECLS_BEGIN
#define GBL_FORWARD_DECLARE_STRUCT(S)
#define GBL_EXPORT
uint8_t GblBool
Basic boolean type, standardized to sizeof(char)
uint16_t GblRefCount
Type able to hold a reference counter across the codebase.
const GblPattern * GblPattern_ref(const GblPattern *pSelf)
Returns a new reference to an existing pattern, incrementing its refcount.
GblRefCount GblPattern_totalCount(void)
Returns the total number of active, allocated, compiled regex patterns.
GblRefCount GblPattern_refCount(const GblPattern *pSelf)
Returns the number of active references held to the given compiled pattern.
size_t GblPattern_matchCount(const GblPattern *pSelf, const char *pString)
Returns the number of pattern matches found in pString.
GblBool GblPattern_matchStr(const char *pRegExp, const char *pString, GblStringView *pMatch, int *pCount)
Dynamically-compiled string-based version of GblPattern_match()
GblBool GblPattern_match(const GblPattern *pSelf, const char *pString, GblStringView *pMatch, int *pCount)
Finds the numbered match given by pCount, or 1 if NULL.
GblBool GblPattern_equals(const GblPattern *pSelf, const GblPattern *pRhs)
Compares two different compiled patterns for exact value equality of their regexes.
GblBool GblPattern_matchNot(const GblPattern *pSelf, const char *pString, GblStringView *pMatch, int *pCount)
Behaves like GblPattern_match() except searching for NON-MATCHES.
GblBool GblPattern_matchNotStr(const char *pRegExp, const char *pString, GblStringView *pMatch, int *pCount)
Dynamically-compiled string-based version of GblPattern_matchNot()
GblBool GblPattern_matchExact(const GblPattern *pSelf, const char *pString)
Returns GBL_TRUE if the given string EXACTLY matches the given pattern or GBL_FALSE otherwise.
size_t GblPattern_matchCountStr(const char *pRegExp, const char *pString)
Dynamically compiled, string-based version of GblPattern_matchExact()
const char * GblPattern_string(const GblPattern *pSelf, GblStringBuffer *pBuff)
Reconstructs an approximate string representation of the compiled pattern.
const GblPattern * GblPattern_create(const char *pRegExp)
Compiles the given regular expression into a pre-processed GblPattern.
int GblPattern_compare(const GblPattern *pSelf, const GblPattern *pRhs)
Compares two different compiled patterns to see if they are equivalent regexes.
size_t GblPattern_bytes(const GblPattern *pSelf)
Counts the total size of a compiled pattern and returns it in bytes.
GblRefCount GblPattern_unref(const GblPattern *pSelf)
Releases a reference to a pattern, deallocating it upon reaching zero.
GblBool GblPattern_matchExactStr(const char *pRegExp, const char *pString)
Dynamically compiled string-based version of GblPattern_matchExact()