49 fprintf(stderr,
"Runtime type error: REGEX_SEARCH expects (string, string)\n");
81 enum { MAX_CAP = 16 };
82 regmatch_t caps[MAX_CAP];
83 int ok = regexec(&rx,
str.s ?
str.s :
"", MAX_CAP, caps, 0) == 0;
92 int s = (int)caps[0].rm_so;
93 int e = (int)caps[0].rm_eo;
94 char *matchStr = NULL;
95 if (
str.s &&
s >= 0 &&
e >=
s) {
97 matchStr = (
char *)malloc((
size_t)
len + 1);
99 memcpy(matchStr,
str.s +
s, (
size_t)
len);
100 matchStr[
len] =
'\0';
104 if (matchStr)
free(matchStr);
111 for (
int i = 1; i < MAX_CAP; ++i) {
112 if (caps[i].rm_so == -1 || caps[i].rm_eo == -1)
break;
115 if (groupCount > 0) {
118 for (
int i = 1; i <= groupCount; ++i) {
119 int gs = (int)caps[i].rm_so;
120 int ge = (int)caps[i].rm_eo;
122 if (
str.s && gs >= 0 && ge >= gs) {
124 gstr = (
char *)malloc((
size_t)gl + 1);
126 memcpy(gstr,
str.s + gs, (
size_t)gl);
131 if (gstr)
free(gstr);
134 for (
int i = 0; i < groupCount; ++i)
138 (void)
map_set(&outMap,
"groups", groupsArr);
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.
Tagged union representing a Fun value.
Value make_string(const char *s)
Construct a string Value by duplicating the given C string.
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.
static Value pop_value(VM *vm)
Pop a Value from the VM operand stack.