Fun 0.41.5
The programming language that makes You have fun
Loading...
Searching...
No Matches
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
19
41
42/* PCRE2_MATCH */
44#ifdef FUN_WITH_PCRE2
45 Value vflags = pop_value(vm);
46 Value vtext = pop_value(vm);
47 Value vpat = pop_value(vm);
48 int flags = 0;
49 if (vflags.type == VAL_INT || vflags.type == VAL_BOOL) flags = (int)vflags.i;
50 char *pattern = value_to_string_alloc(&vpat);
51 char *subject = value_to_string_alloc(&vtext);
52 free_value(vflags);
53 free_value(vtext);
54 free_value(vpat);
55 if (!pattern || !subject) {
56 if (pattern) free(pattern);
57 if (subject) free(subject);
58 push_value(vm, make_nil());
59 break;
60 }
61#ifndef PCRE2_CODE_UNIT_WIDTH
62#define PCRE2_CODE_UNIT_WIDTH 8
63#endif
64#include <pcre2.h>
65 int errorcode;
66 PCRE2_SIZE erroff;
67 uint32_t opt = 0;
68 if (flags & 1) opt |= PCRE2_CASELESS; /* I */
69 if (flags & 2) opt |= PCRE2_MULTILINE; /* M */
70 if (flags & 4) opt |= PCRE2_DOTALL; /* S */
71 if (flags & 8) opt |= PCRE2_UTF; /* U */
72 if (flags & 16) opt |= PCRE2_EXTENDED; /* X */
73 pcre2_code *re = pcre2_compile((PCRE2_SPTR)pattern, PCRE2_ZERO_TERMINATED, opt, &errorcode, &erroff, NULL);
74 if (!re) {
76 free(subject);
77 push_value(vm, make_nil());
78 break;
79 }
80 pcre2_match_data *mdata = pcre2_match_data_create_from_pattern(re, NULL);
81 int rc = pcre2_match(re, (PCRE2_SPTR)subject, (PCRE2_SIZE)strlen(subject), 0, 0, mdata, NULL);
82 if (rc <= 0) {
83 pcre2_match_data_free(mdata);
84 pcre2_code_free(re);
86 free(subject);
87 push_value(vm, make_nil());
88 break;
89 }
90 PCRE2_SIZE *ov = pcre2_get_ovector_pointer(mdata);
91 /* Build result map */
93 int start0 = (int)ov[0];
94 int end0 = (int)ov[1];
95 char *full = string_substr(subject, start0, end0 - start0);
96 (void)map_set(&res, "full", make_string(full ? full : ""));
97 if (full) free(full);
98 (void)map_set(&res, "start", make_int(start0));
99 (void)map_set(&res, "end", make_int(end0));
100 /* groups array (excluding group 0) */
101 Value groups = make_array_from_values(NULL, 0);
102 for (int i = 1; i < rc; ++i) {
103 int s = (int)ov[2 * i];
104 int e = (int)ov[2 * i + 1];
105 char *gstr = (s >= 0 && e >= s) ? string_substr(subject, s, e - s) : NULL;
106 Value gv = make_string(gstr ? gstr : "");
107 if (gstr) free(gstr);
108 (void)array_push(&groups, gv);
109 }
110 (void)map_set(&res, "groups", groups);
111 pcre2_match_data_free(mdata);
112 pcre2_code_free(re);
114 free(subject);
115 push_value(vm, res);
116#else
117 Value a = pop_value(vm);
119 Value b = pop_value(vm);
121 Value c = pop_value(vm);
124#endif
125 break;
126}
Value a
Definition add.c:37
int res
Definition and.c:34
uint32_t b
Definition band.c:32
@ OP_PCRE2_MATCH
Definition bytecode.h:193
int rc
Value c
Definition load_const.c:31
free(vals)
int map_set(Value *vm, const char *key, Value v)
Insert or replace a key in the map.
Definition map.c:79
Value make_map_empty(void)
Construct a new empty map Value.
Definition map.c:35
free_value(a)
push_value(vm, make_nil())
Value pattern
uint32_t s
Definition rol.c:31
char * string_substr(const char *s, int start, int len)
Create a newly allocated substring of s.
Definition str_utils.c:35
Tagged union representing a Fun value.
Definition value.h:68
int64_t i
Definition value.h:71
ValueType type
Definition value.h:69
Value make_nil(void)
Construct a nil Value.
Definition value.c:126
Value make_string(const char *s)
Construct a string Value by duplicating the given C string.
Definition value.c:95
char * value_to_string_alloc(const Value *v)
Allocate a printable C string for a Value.
Definition value.c:641
int array_push(Value *v, Value newElem)
Append a Value to an array.
Definition value.c:257
Value make_int(int64_t v)
Construct a Value representing a 64-bit integer.
Definition value.c:51
Value make_array_from_values(const Value *vals, int count)
Create an array Value by copying items from an input span.
Definition value.c:142
@ VAL_BOOL
Definition value.h:52
@ VAL_INT
Definition value.h:51