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 * \note
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.
13 *
14 * \todo
15 * - Wrap Tiny C Regex tests in gimbal Unit tests
16 * - `\b` word-boundary character class
17 *
18 * \author 2023 Falco Girgis
19 * \copyright MIT License
20 */
21#ifndef GIMBAL_PATTERN_H
22#define GIMBAL_PATTERN_H
23
24#include "../core/gimbal_decls.h"
25
26#define GBL_SELF_TYPE GblPattern
27
29
30GBL_FORWARD_DECLARE_STRUCT(GblStringView);
31GBL_FORWARD_DECLARE_STRUCT(GblStringBuffer);
32
33/*! \struct GblPattern
34 * \brief Opaque structure containing a compiled regular expression
35 *
36 * GblPattern is the compiled version of a regular expression
37 * string, produced by GblPattern_create(). It is faster
38 * than working with raw regular expression strings, since
39 * preprocessing has already been done.
40 *
41 * It is advised that when you are repeatedly using the same
42 * regular expression, you store it and use it as a GblPattern.
43 *
44 * \note
45 * GblPattern is a reference-counted shared pointer type.
46 *
47 * \ingroup strings
48 */
49struct GblPattern;
50typedef struct GblPattern GblPattern;
51
52/*! \name Lifetime Management
53 * \brief Methods for creating, referencing, and unreferencing patterns
54 * \relatesalso GblPattern
55 * @{
56 */
57//! Compiles the given regular expression into a pre-processed GblPattern
58GBL_EXPORT const GblPattern* GblPattern_create (const char* pRegExp) GBL_NOEXCEPT;
59//! Returns a new reference to an existing pattern, incrementing its refcount
61//! Releases a reference to a pattern, deallocating it upon reaching zero
63//! Returns the number of active references held to the given compiled pattern
65//! Returns the total number of active, allocated, compiled regex patterns
67//! @}
68
69/*! \name Properties and Operators
70 * \brief Methods for getters and basic operations
71 * \relatesalso GblPattern
72 * @{
73 */
74//! Compares two different compiled patterns to see if they are equivalent regexes
76//! Compares two different compiled patterns for exact value equality of their regexes
78//! Counts the total size of a compiled pattern and returns it in bytes
80//! Reconstructs an approximate string representation of the compiled pattern
81GBL_EXPORT const char* GblPattern_string (GBL_CSELF, GblStringBuffer* pBuff) GBL_NOEXCEPT;
82//! @}
83
84/*! \name Matching
85 * \brief Methods for generic pattern matching
86 * \relatesalso GblPattern
87 * @{
88 */
89/*! Finds the numbered match given by \p pCount, or 1 if NULL.
90 *
91 * GblPattern_match() searches through \p pString for substrings
92 * which match the pattern given by \p pSelf. When it has found
93 * the number of matches equal to the value given by \p pCount
94 * (or 1 if \p pCount is NULL), \p pMatch is set to the substring
95 * of the final match (or ignored when NULL) and GBL_TRUE is
96 * returned. If the match could not be found, GBL_FALSE is returned.
97 *
98 * \note
99 * When \p pCount points to a value of 0, no matches are expected
100 * to be found. If a match is found, GBL_FALSE is returned, otherwise
101 * GBL_TRUE is returned (inverted from normal).
102 *
103 * \note
104 * When \p pCount points to a value of -1, all matches are searched,
105 * with \p pMatch being updated to point to the final one and \p pCount
106 * being updated to store the final number of matches.
107 *
108 * \param pSelf The precompiled regular expression pattern
109 * \param pString The input string to search for matches within
110 * \param pMatch (Optional) Output used to hold the final match substring
111 * \param pCount (Optional, defaults to 1) Number of matches to find
112 *
113 * \retval GBL_FALSE The requested match number wasn't found.
114 * \retval GBL_TRUE The requested match number was found.
115 *
116 * \sa GblPattern_matchNot()
117 */
119 const char* pString,
120 GblStringView* pMatch/*=NULL*/,
121 int* pCount/*=NULL*/) GBL_NOEXCEPT;
122//! Dynamically-compiled string-based version of GblPattern_match()
124 const char* pString,
125 GblStringView* pMatch/*=NULL*/,
126 int* pCount/*=NULL*/) GBL_NOEXCEPT;
127//! @}
128
129/*! \name Inverse Matching
130 * \brief Methods for returning non matches
131 * \relatesalso GblPattern
132 * @{
133 */
134//! Behaves like GblPattern_match() except searching for NON-MATCHES
136 const char* pString,
137 GblStringView* pMatch/*=NULL*/,
138 int* pCount/*=NULL*/) GBL_NOEXCEPT;
139//! Dynamically-compiled string-based version of GblPattern_matchNot()
141 const char* pString,
142 GblStringView* pMatch/*=NULL*/,
143 int* pCount/*=NULL*/) GBL_NOEXCEPT;
144//! @}
145
146
147/*! \name Exact Matching
148 * \brief Methods for checking exact matches
149 * \relatesalso GblPattern
150 * @{
151 */
152 //! Returns GBL_TRUE if the given string EXACTLY matches the given pattern or GBL_FALSE otherwise
154//! Dynamically compiled string-based version of GblPattern_matchExact()
156 const char* pString) GBL_NOEXCEPT;
157//! @}
158
159/*! \name Match Counting
160 * \brief Methods used for counting matches
161 * \relatesalso GblPattern
162 * @{
163 */
164//! Returns the number of pattern matches found in \p pString
166//! Dynamically compiled, string-based version of GblPattern_matchExact()
167GBL_EXPORT size_t GblPattern_matchCountStr (const char* pRegExp,
168 const char* pString) GBL_NOEXCEPT;
169//! @}
170
172
173//! \cond
174#define GblPattern_match(...)
175 (GblPattern_matchDefault_(__VA_ARGS__))
176#define GblPattern_matchDefault_(...)
177 (GblPattern_matchDefault__(__VA_ARGS__, GBL_NULL, GBL_NULL))
178#define GblPattern_matchDefault__(pat, str, match, count, ...)
179 ((GblPattern_match)(pat, str, match, count))
180
181#define GblPattern_matchStr(...)
182 (GblPattern_matchStrDefault_(__VA_ARGS__))
183#define GblPattern_matchStrDefault_(...)
184 (GblPattern_matchStrDefault__(__VA_ARGS__, GBL_NULL, GBL_NULL))
185#define GblPattern_matchStrDefault__(exp, str, match, count, ...)
186 ((GblPattern_matchStr)(exp, str, match, count))
187
188#define GblPattern_matchNot(...)
189 (GblPattern_matchNotDefault_(__VA_ARGS__))
190#define GblPattern_matchNotDefault_(...)
191 (GblPattern_matchNotDefault__(__VA_ARGS__, GBL_NULL, GBL_NULL))
192#define GblPattern_matchNotDefault__(pat, str, match, count, ...)
193 ((GblPattern_matchNot)(pat, str, match, count))
194
195#define GblPattern_matchNotStr(...)
196 (GblPattern_matchNotStrDefault_(__VA_ARGS__))
197#define GblPattern_matchNotStrDefault_(...)
198 (GblPattern_matchNotStrDefault__(__VA_ARGS__, GBL_NULL, GBL_NULL))
199#define GblPattern_matchNotStrDefault__(exp, str, match, count, ...)
200 ((GblPattern_matchNotStr)(exp, str, match, count))
201//! \endcond
202
203#undef GBL_SELF_TYPE
204
205#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()