57#ifndef PCRE2_CODE_UNIT_WIDTH
58#define PCRE2_CODE_UNIT_WIDTH 8
82 if (flags & 1) opt |= PCRE2_CASELESS;
83 if (flags & 2) opt |= PCRE2_MULTILINE;
84 if (flags & 4) opt |= PCRE2_DOTALL;
85 if (flags & 8) opt |= PCRE2_UTF;
86 if (flags & 16) opt |= PCRE2_EXTENDED;
106 if (!pattern || !subject)
return 0;
107 int errorcode = 0; PCRE2_SIZE erroff = 0;
109 pcre2_code *re = pcre2_compile((PCRE2_SPTR)pattern, PCRE2_ZERO_TERMINATED, opt, &errorcode, &erroff, NULL);
111 pcre2_match_data *mdata = pcre2_match_data_create_from_pattern(re, NULL);
112 int rc = pcre2_match(re, (PCRE2_SPTR)subject, (PCRE2_SIZE)strlen(subject), 0, 0, mdata, NULL);
113 pcre2_match_data_free(mdata);
115 return rc >= 0 ? 1 : 0;
139 if (!pattern || !subject)
return make_nil();
140 int errorcode = 0; PCRE2_SIZE erroff = 0;
142 pcre2_code *re = pcre2_compile((PCRE2_SPTR)pattern, PCRE2_ZERO_TERMINATED, opt, &errorcode, &erroff, NULL);
144 pcre2_match_data *mdata = pcre2_match_data_create_from_pattern(re, NULL);
145 int rc = pcre2_match(re, (PCRE2_SPTR)subject, (PCRE2_SIZE)strlen(subject), 0, 0, mdata, NULL);
147 pcre2_match_data_free(mdata);
151 PCRE2_SIZE *ov = pcre2_get_ovector_pointer(mdata);
153 int start0 = (int)ov[0];
154 int end0 = (int)ov[1];
157 if (full) free(full);
161 for (
int i = 1; i < rc; ++i) {
162 int s = (int)ov[2 * i];
163 int e = (int)ov[2 * i + 1];
164 char *gstr = (s >= 0 && e >= s) ?
string_substr(subject, s, e - s) : NULL;
166 if (gstr) free(gstr);
169 (void)
map_set(&res,
"groups", groups);
170 pcre2_match_data_free(mdata);
195 if (!pattern || !subject)
return out;
196 int errorcode = 0; PCRE2_SIZE erroff = 0;
198 pcre2_code *re = pcre2_compile((PCRE2_SPTR)pattern, PCRE2_ZERO_TERMINATED, opt, &errorcode, &erroff, NULL);
200 pcre2_match_data *mdata = pcre2_match_data_create_from_pattern(re, NULL);
201 size_t subj_len = strlen(subject);
202 size_t start_off = 0;
204 int rc = pcre2_match(re, (PCRE2_SPTR)subject, (PCRE2_SIZE)subj_len, start_off, 0, mdata, NULL);
206 PCRE2_SIZE *ov = pcre2_get_ovector_pointer(mdata);
212 if (full) free(full);
216 for (
int i = 1; i < rc; ++i) {
217 int s = (int)ov[2 * i];
218 int e = (int)ov[2 * i + 1];
219 char *gstr = (s >= 0 && e >= s) ?
string_substr(subject, s, e - s) : NULL;
221 if (gstr) free(gstr);
224 (void)
map_set(&res,
"groups", groups);
228 if ((
size_t)e0 < subj_len) start_off = e0 + 1;
else break;
233 pcre2_match_data_free(mdata);
int map_set(Value *vm, const char *key, Value v)
Insert or replace a key in the map.
Value make_map_empty(void)
Construct a new empty map Value.
static Value fun_pcre2_findall(const char *pattern, const char *subject, int flags)
Find all non-overlapping matches of a pattern in a subject.
static int fun_pcre2_test(const char *pattern, const char *subject, int flags)
Test whether a pattern matches a subject at least once.
static uint32_t fun_pcre2_opts_from_flags(int flags)
Map Fun VM regex flags to PCRE2 compile options.
static Value fun_pcre2_match(const char *pattern, const char *subject, int flags)
Match a pattern once and return a structured result map.
char * string_substr(const char *s, int start, int len)
Create a newly allocated substring of s.
Tagged union representing a Fun value.
Value make_nil(void)
Construct a nil Value.
Value make_string(const char *s)
Construct a string Value by duplicating the given C string.
int array_push(Value *v, Value newElem)
Append a Value to an array.
Value make_int(int64_t v)
Construct a Value representing a 64-bit integer.
Value make_array_from_values(const Value *vals, int count)
Create an array Value by copying items from an input span.