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
18
36
37/* PCRE2_TEST */
39#ifdef FUN_WITH_PCRE2
40 Value vflags = pop_value(vm);
41 Value vtext = pop_value(vm);
42 Value vpat = pop_value(vm);
43 int flags = 0;
44 if (vflags.type == VAL_INT || vflags.type == VAL_BOOL) flags = (int)vflags.i;
45 char *pattern = value_to_string_alloc(&vpat);
46 char *subject = value_to_string_alloc(&vtext);
47 free_value(vflags);
48 free_value(vtext);
49 free_value(vpat);
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>
60 int errorcode;
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) {
71 free(subject);
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);
77 pcre2_match_data_free(mdata);
78 pcre2_code_free(re);
80 free(subject);
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);
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
int rc
Value c
Definition load_const.c:31
free(vals)
Value pattern
Tagged union representing a Fun value.
Definition value.h:68
int64_t i
Definition value.h:71
ValueType type
Definition value.h:69
free_value(a)
push_value(vm, make_int(0))
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