Fun API Documentation 0.42.1
The programming language that makes you have fun!
Loading...
Searching...
No Matches
pcre2.c File Reference

PCRE2 helpers for Fun VM extension opcodes (conditional build). More...

#include <pcre2.h>
#include <string.h>
Include dependency graph for pcre2.c:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define PCRE2_CODE_UNIT_WIDTH   8

Functions

static uint32_t fun_pcre2_opts_from_flags (int flags)
 Map Fun VM regex flags to PCRE2 compile options.
static int fun_pcre2_test (const char *pattern, const char *subject, int flags)
 Test whether a pattern matches a subject at least once.
static Value fun_pcre2_match (const char *pattern, const char *subject, int flags)
 Match a pattern once and return a structured result map.
static Value fun_pcre2_findall (const char *pattern, const char *subject, int flags)
 Find all non-overlapping matches of a pattern in a subject.

Detailed Description

PCRE2 helpers for Fun VM extension opcodes (conditional build).

This module centralizes the concrete PCRE2 implementation so VM opcodes in src/vm/pcre2/*.c only perform stack marshalling and delegate to these helpers. This mirrors the approach used by other extensions (e.g. SQLite, XML2) where the heavy lifting lives under src/extensions/ and the opcodes just call into small C helpers.

Build-time feature flag:

  • The code in this file is compiled only when FUN_WITH_PCRE2 is enabled. When disabled, PCRE2-dependent opcodes are built with no-op fallbacks.

PCRE2 width configuration:

  • PCRE2 requires defining PCRE2_CODE_UNIT_WIDTH before including <pcre2.h> to select 8/16/32-bit code units. We select 8-bit here. Because the Fun VM translates many opcode .c files into the same translation unit, it is important this macro is defined exactly once before the first <pcre2.h> inclusion. This file ensures that when FUN_WITH_PCRE2 is enabled.

Flags mapping used by helpers/opcodes (bitmask in the VM):

  • 1 -> PCRE2_CASELESS ("i")
  • 2 -> PCRE2_MULTILINE ("m")
  • 4 -> PCRE2_DOTALL ("s")
  • 8 -> PCRE2_UTF ("u")
  • 16 -> PCRE2_EXTENDED ("x")

Value and memory ownership:

  • The Value type and helper functions (make_map_empty, make_array_*, map_set, array_push, make_int, make_string, make_nil, string_substr, etc.) are provided by the Fun VM and are declared in the including translation unit. The arrays/maps returned from this module are owned by the caller (the VM opcode), consistent with other extension helpers.

Thread-safety:

  • These helpers are not inherently thread-safe, but they do not maintain any internal state beyond stack-local variables. Coordinate usage externally if the embedding is multi-threaded.

Definition in file pcre2.c.

Macro Definition Documentation

◆ PCRE2_CODE_UNIT_WIDTH

#define PCRE2_CODE_UNIT_WIDTH   8

Definition at line 58 of file pcre2.c.

Function Documentation

◆ fun_pcre2_findall()

Value fun_pcre2_findall ( const char * pattern,
const char * subject,
int flags )
static

Find all non-overlapping matches of a pattern in a subject.

Scans the subject from left to right and appends, for each non-overlapping match, a map with the same shape as fun_pcre2_match() to the result array. If the engine reports an empty match (start == end), the scan advances by a single code unit to prevent infinite loops.

On pattern compile failure or allocation error, returns an empty array.

Parameters
patternNUL-terminated regex pattern string.
subjectNUL-terminated subject string.
flagsVM bitmask translated by fun_pcre2_opts_from_flags().
Returns
Value An array of match maps; may be empty when no matches are found or on error.
See also
fun_pcre2_match()

Definition at line 193 of file pcre2.c.

◆ fun_pcre2_match()

Value fun_pcre2_match ( const char * pattern,
const char * subject,
int flags )
static

Match a pattern once and return a structured result map.

On success, returns a map with the following keys:

  • "full" -> string: the matched substring for group 0
  • "start" -> int: start index (0-based) of the match in the subject
  • "end" -> int: end index (exclusive)
  • "groups" -> array: strings for each captured group (1..n), empty if none

On no match, pattern compile failure, or memory allocation error, returns Nil.

Parameters
patternNUL-terminated regex pattern string.
subjectNUL-terminated subject string.
flagsVM bitmask translated by fun_pcre2_opts_from_flags().
Returns
Value A VM map Value as described above, or Nil on failure.
See also
fun_pcre2_findall()

Definition at line 138 of file pcre2.c.

◆ fun_pcre2_opts_from_flags()

uint32_t fun_pcre2_opts_from_flags ( int flags)
static

Map Fun VM regex flags to PCRE2 compile options.

The Fun VM passes a small integer bitmask controlling common regex behaviours. This function translates those bits into the corresponding PCRE2 compile options.

Bit mapping:

  • 1 -> PCRE2_CASELESS (case-insensitive)
  • 2 -> PCRE2_MULTILINE (^ and $ match start/end of line)
  • 4 -> PCRE2_DOTALL (dot matches newlines)
  • 8 -> PCRE2_UTF (treat pattern/subject as UTF-8)
  • 16 -> PCRE2_EXTENDED (ignore unescaped whitespace and allow comments)
Parameters
flagsBitmask provided by the VM.
Returns
uint32_t PCRE2 options suitable for pcre2_compile().

Definition at line 80 of file pcre2.c.

◆ fun_pcre2_test()

int fun_pcre2_test ( const char * pattern,
const char * subject,
int flags )
static

Test whether a pattern matches a subject at least once.

Compiles the given pattern with options derived from the flags bitmask and runs pcre2_match() once starting at offset 0.

Parameters
patternNUL-terminated regex pattern string.
subjectNUL-terminated subject string.
flagsVM bitmask translated by fun_pcre2_opts_from_flags().
Returns
int 1 if pcre2_match() returns a non-negative value; 0 if there is no match or an error occurs (including compile error or OOM).
Note
This helper performs only a single match attempt at offset 0; it does not search for subsequent matches. Use fun_pcre2_findall() for that.

Definition at line 105 of file pcre2.c.