#pragma once #include #define PCRE2_CODE_UNIT_WIDTH 8 #include /** * Regex structure. */ typedef struct { pcre2_code *expr; unsigned nmatches; char **matches; } ksregex; /** * Create a ksregex from an expression. * @param expression - the expression to compile. * @param nmatches - the number of matches groups to get, 0 if you want none. * @param options - PCRE2 primary option bits. * @return - the created regex, NULL if an error happen. */ ksregex *ksregex_new(PCRE2_SPTR expression, unsigned nmatches, int options); /** * Create a ksregex from an expression with 0 match group. * @param expression - the expression to compile. * @param options - PCRE2 primary option bits. */ static inline ksregex *ksregex_new_nogroup(PCRE2_SPTR expression, int options) { return ksregex_new(expression, 0, options); } /** * Try to match the regex with a tested string. * @param regex - the regex to match. * @param tested - the string that should match the regex. * @return - true if the string matches, false otherwise. */ bool ksregex_matches(ksregex *regex, const char *tested); /** * Free ksregex matches. */ static inline void _ksregex_free_matches(ksregex *regex) { if (regex->matches) { // free all matches. for (unsigned i = 0; i < regex->nmatches; i++) free(regex->matches[i]); // free matches array. free(regex->matches); regex->matches = NULL; } } /** * Free a ksregex. */ static inline void ksregex_free(ksregex *regex) { // free matches groups. _ksregex_free_matches(regex); // free pcre expr. pcre2_code_free(regex->expr); free(regex); // free regex data. }