Fun 0.41.5
The programming language that makes You have fun
Loading...
Searching...
No Matches
findall.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
43
44/* PCRE2_FINDALL */
46#ifdef FUN_WITH_PCRE2
47 Value vflags = pop_value(vm);
48 Value vtext = pop_value(vm);
49 Value vpat = pop_value(vm);
50 int flags = 0;
51 if (vflags.type == VAL_INT || vflags.type == VAL_BOOL) flags = (int)vflags.i;
52 char *pattern = value_to_string_alloc(&vpat);
53 char *subject = value_to_string_alloc(&vtext);
54 free_value(vflags);
55 free_value(vtext);
56 free_value(vpat);
57 if (!pattern || !subject) {
58 if (pattern) free(pattern);
59 if (subject) free(subject);
61 break;
62 }
63#ifndef PCRE2_CODE_UNIT_WIDTH
64#define PCRE2_CODE_UNIT_WIDTH 8
65#endif
66#include <pcre2.h>
67 int errorcode;
68 PCRE2_SIZE erroff;
69 uint32_t opt = 0;
70 if (flags & 1) opt |= PCRE2_CASELESS; /* I */
71 if (flags & 2) opt |= PCRE2_MULTILINE; /* M */
72 if (flags & 4) opt |= PCRE2_DOTALL; /* S */
73 if (flags & 8) opt |= PCRE2_UTF; /* U */
74 if (flags & 16) opt |= PCRE2_EXTENDED; /* X */
75 pcre2_code *re = pcre2_compile((PCRE2_SPTR)pattern, PCRE2_ZERO_TERMINATED, opt, &errorcode, &erroff, NULL);
76 if (!re) {
78 free(subject);
80 break;
81 }
82 pcre2_match_data *mdata = pcre2_match_data_create_from_pattern(re, NULL);
84 size_t subj_len = strlen(subject);
85 size_t start_off = 0;
86 int gcount = 0;
87 while (1) {
88 int rc = pcre2_match(re, (PCRE2_SPTR)subject, (PCRE2_SIZE)subj_len, start_off, 0, mdata, NULL);
89 if (rc <= 0) break;
90 PCRE2_SIZE *ov = pcre2_get_ovector_pointer(mdata);
91 int s0 = (int)ov[0];
92 int e0 = (int)ov[1];
93 /* result map for this match */
95 char *full = string_substr(subject, s0, e0 - s0);
96 (void)map_set(&res, "full", make_string(full ? full : ""));
97 if (full) free(full);
98 (void)map_set(&res, "start", make_int(s0));
99 (void)map_set(&res, "end", make_int(e0));
100 Value groups = make_array_from_values(NULL, 0);
101 for (int i = 1; i < rc; ++i) {
102 int s = (int)ov[2 * i];
103 int e = (int)ov[2 * i + 1];
104 char *gstr = (s >= 0 && e >= s) ? string_substr(subject, s, e - s) : NULL;
105 Value gv = make_string(gstr ? gstr : "");
106 if (gstr) free(gstr);
107 (void)array_push(&groups, gv);
108 }
109 (void)map_set(&res, "groups", groups);
110 (void)array_push(&out, res);
111 /* advance start offset; guard against empty match */
112 if (e0 == s0) {
113 if ((size_t)e0 < subj_len) {
114 start_off = e0 + 1;
115 } else {
116 break;
117 }
118 } else {
119 start_off = e0;
120 }
121 gcount = rc;
122 }
123 pcre2_match_data_free(mdata);
124 pcre2_code_free(re);
126 free(subject);
127 push_value(vm, out);
128#else
129 Value a = pop_value(vm);
131 Value b = pop_value(vm);
133 Value c = pop_value(vm);
136#endif
137 break;
138}
Value a
Definition add.c:37
int res
Definition and.c:34
Value out
Definition apop.c:38
uint32_t b
Definition band.c:32
@ OP_PCRE2_FINDALL
Definition bytecode.h:194
int rc
free_value(a)
push_value(vm, make_array_from_values(NULL, 0))
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
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_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