Fun 0.41.5
The programming language that makes you have fun!
Loading...
Searching...
No Matches
test.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
10/**
11 * @file test.c
12 * @brief Implements the OP_PCRE2_TEST opcode (conditional build).
13 *
14 * Tests whether a PCRE2 pattern matches a subject string and returns 1 on
15 * success or 0 otherwise when FUN_WITH_PCRE2 is enabled. When PCRE2 support
16 * is disabled at build time, the opcode always returns 0.
17 */
18
19/**
20 * OP_PCRE2_TEST: (pattern:any, text:any, flags:int|bool=0) -> int
21 *
22 * Behavior when FUN_WITH_PCRE2 is enabled:
23 * - Pops: pattern, text, flags. Converts pattern/text to strings.
24 * - Flags bits:
25 * - 1 = PCRE2_CASELESS (I)
26 * - 2 = PCRE2_MULTILINE (M)
27 * - 4 = PCRE2_DOTALL (S)
28 * - 8 = PCRE2_UTF (U)
29 * - 16 = PCRE2_EXTENDED (X)
30 * - Returns 1 if pcre2_match() returns a non-negative result, else 0.
31 * - On compile error or allocation failure, returns 0.
32 *
33 * Behavior when FUN_WITH_PCRE2 is disabled:
34 * - Pops three values and returns 0.
35 */
36
37/* PCRE2_TEST */
39#ifdef FUN_WITH_PCRE2
40 Value vflags = pop_value(vm);
43 int flags = 0;
44 if (vflags.type == VAL_INT || vflags.type == VAL_BOOL) flags = (int)vflags.i;
47 free_value(vflags);
50 if (!pattern || !subject) {
51 if (pattern) free(pattern);
52 if (subject) free(subject);
53 push_value(vm, make_int(0));
54 break;
55 }
56#ifndef PCRE2_CODE_UNIT_WIDTH
57#define PCRE2_CODE_UNIT_WIDTH 8
58#endif
59#include <pcre2.h>
61 PCRE2_SIZE erroff;
62 uint32_t opt = 0;
63 if (flags & 1) opt |= PCRE2_CASELESS; /* I */
64 if (flags & 2) opt |= PCRE2_MULTILINE; /* M */
65 if (flags & 4) opt |= PCRE2_DOTALL; /* S */
66 if (flags & 8) opt |= PCRE2_UTF; /* U */
67 if (flags & 16) opt |= PCRE2_EXTENDED; /* X */
68 pcre2_code *re = pcre2_compile((PCRE2_SPTR)pattern, PCRE2_ZERO_TERMINATED, opt, &errorcode, &erroff, NULL);
69 if (!re) {
72 push_value(vm, make_int(0));
73 break;
74 }
75 pcre2_match_data *mdata = pcre2_match_data_create_from_pattern(re, NULL);
76 int rc = pcre2_match(re, (PCRE2_SPTR)subject, (PCRE2_SIZE)strlen(subject), 0, 0, mdata, NULL);
81 push_value(vm, make_int(rc >= 0 ? 1 : 0));
82#else
83 /* pop args and return 0 when PCRE2 disabled */
84 Value a = pop_value(vm);
86 Value b = pop_value(vm);
88 Value c = pop_value(vm);
90 push_value(vm, make_int(0));
91#endif
92 break;
93}
Value a
Definition add.c:37
uint32_t b
Definition band.c:32
@ OP_PCRE2_TEST
Definition bytecode.h:192
CURLcode rc
Definition download.c:71
uint32_t opt
Definition findall.c:69
Value vtext
Definition findall.c:48
pcre2_code * re
Definition findall.c:75
Value vpat
Definition findall.c:49
pcre2_match_data * mdata
Definition findall.c:82
int errorcode
Definition findall.c:67
PCRE2_SIZE erroff
Definition findall.c:68
char * subject
Definition findall.c:53
char * pattern
Definition findall.c:52
Value c
Definition load_const.c:31
int flags
Definition stringify.c:38
Tagged union representing a Fun value.
Definition value.h:68
int64_t i
Definition value.h:71
ValueType type
Definition value.h:69
pcre2_match_data_free(mdata)
free_value(vflags)
push_value(vm, make_int(rc >=0 ? 1 :0))
free(pattern)
pcre2_code_free(re)
char * value_to_string_alloc(const Value *v)
Allocate a printable C string for a Value.
Definition value.c:641
Value make_int(int64_t v)
Construct a Value representing a 64-bit integer.
Definition value.c:51
@ VAL_BOOL
Definition value.h:52
@ VAL_INT
Definition value.h:51
static Value pop_value(VM *vm)
Pop a Value from the VM operand stack.
Definition vm.c:580