solidc
Robust collection of general-purpose cross-platform C libraries and data structures designed for rapid and safe development in C
Loading...
Searching...
No Matches
regex.h
Go to the documentation of this file.
1
18#pragma once
19
20#include <stdbool.h> /* for bool */
21#include <stddef.h> /* for size_t */
22#include <stdint.h> /* for uint32_t */
23#include <stdio.h>
24
25/* Pull in PCRE2 with the 8-bit code unit width we target. */
26#define PCRE2_CODE_UNIT_WIDTH 8
27#include <pcre2.h>
28
29#ifdef __cplusplus
30extern "C" {
31#endif
32
33/* ---------------------------------------------------------------------------
34 * Limits & constants
35 * ------------------------------------------------------------------------- */
36
40#define REGEX_MAX_GROUPS 64
41
42/* ---------------------------------------------------------------------------
43 * Error codes
44 * ------------------------------------------------------------------------- */
45
55
56/* ---------------------------------------------------------------------------
57 * Compile flags (thin alias over PCRE2_* options for API isolation)
58 * ------------------------------------------------------------------------- */
59
61typedef uint32_t regex_flags_t;
62
63#define REGEX_FLAG_NONE (0u) /*< No special compilation flags. */
64#define REGEX_FLAG_CASELESS (PCRE2_CASELESS)
65#define REGEX_FLAG_MULTILINE (PCRE2_MULTILINE)
66#define REGEX_FLAG_DOTALL (PCRE2_DOTALL)
67#define REGEX_FLAG_EXTENDED (PCRE2_EXTENDED)
68#define REGEX_FLAG_UTF (PCRE2_UTF)
69#define REGEX_FLAG_UCP (PCRE2_UCP)
70#define REGEX_FLAG_UNGREEDY (PCRE2_UNGREEDY)
71#define REGEX_FLAG_ANCHORED (PCRE2_ANCHORED)
73/* ---------------------------------------------------------------------------
74 * Core types
75 * ------------------------------------------------------------------------- */
76
83typedef struct regex_s regex_t;
84
92typedef struct regex_ctx_s regex_ctx_t;
93
101typedef struct {
102 size_t start;
103 size_t end;
105
113typedef struct {
115 uint32_t count;
117
124typedef struct regex_iter_s regex_iter_t;
125
126/* ---------------------------------------------------------------------------
127 * Compilation
128 * ------------------------------------------------------------------------- */
129
144regex_status_t regex_compile(const char* pattern, regex_flags_t flags, regex_t** out, char* errbuf, size_t errbuf_len);
145
153
159void regex_free(regex_t* re);
160
161/* ---------------------------------------------------------------------------
162 * Execution context (per-thread)
163 * ------------------------------------------------------------------------- */
164
178
184void regex_ctx_free(regex_ctx_t* ctx);
185
186/* ---------------------------------------------------------------------------
187 * Matching
188 * ------------------------------------------------------------------------- */
189
203regex_status_t regex_exec(const regex_t* re, regex_ctx_t* ctx, const char* subject, size_t len, size_t offset,
204 regex_match_t* match);
205
217regex_status_t regex_match(const regex_t* re, regex_ctx_t* ctx, const char* subject, regex_match_t* match);
218
230bool regex_is_match(const regex_t* re, regex_ctx_t* ctx, const char* subject, size_t len);
231
232/* ---------------------------------------------------------------------------
233 * Iterator: find all non-overlapping matches
234 * ------------------------------------------------------------------------- */
235
250regex_status_t regex_iter_init(regex_t* re, regex_ctx_t* ctx, const char* subject, size_t len, regex_iter_t** out);
251
261
267void regex_iter_free(regex_iter_t* iter);
268
269/* ---------------------------------------------------------------------------
270 * Substitution
271 * ------------------------------------------------------------------------- */
272
293regex_status_t regex_sub(const regex_t* re, regex_ctx_t* ctx, const char* subject, size_t subject_len,
294 const char* replacement, char* out_buf, size_t* out_len);
295
311regex_status_t regex_gsub(const regex_t* re, regex_ctx_t* ctx, const char* subject, size_t subject_len,
312 const char* replacement, char* out_buf, size_t* out_len);
313
314/* ---------------------------------------------------------------------------
315 * Introspection
316 * ------------------------------------------------------------------------- */
317
324uint32_t regex_group_count(const regex_t* re);
325
332const char* regex_pattern(const regex_t* re);
333
341void regex_strerror(regex_status_t status, char* buf, size_t buf_len);
342
349static inline regex_t* regex_must_compile(const char* pattern, regex_flags_t flags) {
350 char errbuf[256];
351 regex_t* re = NULL;
352 regex_status_t st = regex_compile(pattern, flags, &re, errbuf, sizeof(errbuf));
353 if (st != REGEX_OK) {
354 fprintf(stderr, "%s:%d: regex_compile(\"%s\") failed: %s\n", __FILE__, __LINE__, pattern, errbuf);
355 exit(1);
356 }
357 return re;
358}
359
363static inline regex_ctx_t* regex_ctx_must_create(void) {
364 regex_ctx_t* ctx = NULL;
366 if (st != REGEX_OK) {
367 fprintf(stderr, "%s:%d: regex_ctx_create failed\n", __FILE__, __LINE__);
368 exit(1);
369 }
370 return ctx;
371}
372
373#ifdef __cplusplus
374}
375#endif
void regex_iter_free(regex_iter_t *iter)
Definition regex.c:321
regex_t * regex_retain(regex_t *re)
Definition regex.c:159
struct regex_ctx_s regex_ctx_t
Definition regex.h:92
struct regex_iter_s regex_iter_t
Definition regex.h:124
#define REGEX_MAX_GROUPS
Definition regex.h:40
uint32_t regex_flags_t
Definition regex.h:61
uint32_t regex_group_count(const regex_t *re)
Definition regex.c:380
const char * regex_pattern(const regex_t *re)
Definition regex.c:387
regex_status_t
Definition regex.h:47
@ REGEX_ERROR_NOMEM
Definition regex.h:51
@ REGEX_OK
Definition regex.h:48
@ REGEX_ERROR
Definition regex.h:50
@ REGEX_ERROR_LIMIT
Definition regex.h:53
@ REGEX_ERROR_ARGS
Definition regex.h:52
@ REGEX_NO_MATCH
Definition regex.h:49
regex_status_t regex_ctx_create(regex_ctx_t **out)
Definition regex.c:183
regex_status_t regex_sub(const regex_t *re, regex_ctx_t *ctx, const char *subject, size_t subject_len, const char *replacement, char *out_buf, size_t *out_len)
Definition regex.c:366
struct regex_s regex_t
Definition regex.h:83
void regex_strerror(regex_status_t status, char *buf, size_t buf_len)
Definition regex.c:394
regex_status_t regex_compile(const char *pattern, regex_flags_t flags, regex_t **out, char *errbuf, size_t errbuf_len)
Definition regex.c:93
regex_status_t regex_match(const regex_t *re, regex_ctx_t *ctx, const char *subject, regex_match_t *match)
Definition regex.c:243
regex_status_t regex_iter_next(regex_iter_t *iter, regex_match_t *match)
Definition regex.c:287
regex_status_t regex_gsub(const regex_t *re, regex_ctx_t *ctx, const char *subject, size_t subject_len, const char *replacement, char *out_buf, size_t *out_len)
Definition regex.c:371
regex_status_t regex_exec(const regex_t *re, regex_ctx_t *ctx, const char *subject, size_t len, size_t offset, regex_match_t *match)
Definition regex.c:220
void regex_ctx_free(regex_ctx_t *ctx)
Definition regex.c:208
bool regex_is_match(const regex_t *re, regex_ctx_t *ctx, const char *subject, size_t len)
Definition regex.c:250
void regex_free(regex_t *re)
Definition regex.c:166
regex_status_t regex_iter_init(regex_t *re, regex_ctx_t *ctx, const char *subject, size_t len, regex_iter_t **out)
Definition regex.c:264
uint32_t count
Definition regex.h:115
size_t start
Definition regex.h:102
size_t end
Definition regex.h:103