Fun
0.41.5
The programming language that makes You have fun
Main Page
Data Structures
Files
File List
Globals
Loading...
Searching...
No Matches
src
vm
strings
regex_match.c
Go to the documentation of this file.
1
/*
2
* This file is part of the Fun programming language.
3
* https://fun-lang.xyz/
4
*
5
* Copyright 2025 Johannes Findeisen <you@hanez.org>
6
* Licensed under the terms of the Apache-2.0 license.
7
* https://opensource.org/license/apache-2-0
8
*/
9
34
/* Regex full-match opcode using POSIX regex */
35
#ifdef __unix__
36
#include <regex.h>
37
#endif
38
39
case
OP_REGEX_MATCH
: {
40
Value
pattern
= pop_value(vm);
41
Value
str
= pop_value(vm);
42
if
(
str
.type !=
VAL_STRING
||
pattern
.type !=
VAL_STRING
) {
43
fprintf
(stderr,
"Runtime type error: REGEX_MATCH expects (string, string)\n"
);
44
exit
(1);
45
}
46
#ifndef __unix__
47
/* Not supported on non-UNIX: return 0 gracefully */
48
free_value
(
pattern
);
49
int
truth
= 0;
50
free_value
(
str
);
51
push_value
(vm,
make_int
(
truth
));
52
break
;
53
#else
54
regex_t rx;
55
int
rc
= regcomp(&rx,
pattern
.s ?
pattern
.s :
""
, REG_EXTENDED);
56
if
(
rc
!= 0) {
57
/* invalid regex -> false */
58
free_value
(
pattern
);
59
free_value
(
str
);
60
push_value
(vm,
make_int
(0));
61
break
;
62
}
63
regmatch_t
m
;
64
int
ok
= regexec(&rx,
str
.s ?
str
.s :
""
, 1, &
m
, 0) == 0;
65
int
truth
= 0;
66
if
(
ok
) {
67
/* full match means the match spans whole string */
68
if
(
m
.rm_so == 0 &&
str
.s) {
69
size_t
slen = strlen(
str
.s);
70
truth
= (
m
.rm_eo == (regoff_t)slen) ? 1 : 0;
71
}
72
}
73
regfree(&rx);
74
free_value
(
pattern
);
75
free_value
(
str
);
76
push_value
(vm,
make_int
(
truth
));
77
break
;
78
#endif
79
}
OP_REGEX_MATCH
@ OP_REGEX_MATCH
Definition
bytecode.h:107
ok
int ok
Definition
contains.c:38
rc
int rc
Definition
fd_poll_read.c:26
m
Value m
Definition
has_key.c:27
free_value
free_value(pattern)
str
Value str
Definition
regex_match.c:41
push_value
push_value(vm, make_int(truth))
truth
int truth
Definition
regex_match.c:49
pattern
Value pattern
Definition
regex_replace.c:43
Value
Tagged union representing a Fun value.
Definition
value.h:68
make_int
Value make_int(int64_t v)
Construct a Value representing a 64-bit integer.
Definition
value.c:51
VAL_STRING
@ VAL_STRING
Definition
value.h:53
fprintf
#define fprintf
Definition
vm.c:200
exit
#define exit(code)
Definition
vm.c:230
Generated on
for Fun by
1.16.1