47 fprintf(stderr,
"Runtime type error: REGEX_REPLACE expects (string, string, string)\n");
71 const char *
s =
str.s ?
str.s :
"";
72 const char *
r = repl.
s ? repl.
s :
"";
74 size_t out_cap = strlen(
s) + 1;
75 char *outbuf = (
char *)malloc(out_cap);
79 enum { MAX_CAP = 16 };
80 regmatch_t caps[MAX_CAP];
83 if (regexec(&rx,
s + pos, MAX_CAP, caps, 0) != 0) {
85 size_t rest = strlen(
s + pos);
86 if (out_len + rest + 1 > out_cap) {
87 out_cap = out_len + rest + 1;
88 outbuf = (
char *)realloc(outbuf, out_cap);
90 memcpy(outbuf + out_len,
s + pos, rest + 1);
94 int mstart = (int)caps[0].rm_so;
95 int mend = (int)caps[0].rm_eo;
96 if (mstart < 0 || mend < mstart) {
101 size_t pre_len = (size_t)mstart;
102 if (out_len + pre_len + 1 > out_cap) {
103 out_cap = (out_len + pre_len + 1) * 2;
104 outbuf = (
char *)realloc(outbuf, out_cap);
106 memcpy(outbuf + out_len,
s + pos, pre_len);
110 size_t rlen = strlen(
r);
111 if (out_len +
rlen + 1 > out_cap) {
112 out_cap = (out_len +
rlen + 1) * 2;
113 outbuf = (
char *)realloc(outbuf, out_cap);
115 memcpy(outbuf + out_len,
r,
rlen);
121 if (pos < strlen(
s)) {
122 if (out_len + 1 > out_cap) {
123 out_cap = out_len + 2;
124 outbuf = (
char *)realloc(outbuf, out_cap);
126 outbuf[out_len++] =
s[pos++];
134 if (outbuf)
free(outbuf);
Tagged union representing a Fun value.
Value make_string(const char *s)
Construct a string Value by duplicating the given C string.
static Value pop_value(VM *vm)
Pop a Value from the VM operand stack.