Fun 0.41.5
The programming language that makes you have fun!
Loading...
Searching...
No Matches
match.c File Reference

Implements the OP_PCRE2_MATCH opcode (conditional build). More...

#include <pcre2.h>
Include dependency graph for match.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

 if (vflags.type==VAL_INT||vflags.type==VAL_BOOL) flags
 free_value (vflags)
 free_value (vtext)
 free_value (vpat)
 if (!pattern||!subject)
 if (flags &1) opt|
 if (!re)
 if (rc<=0)
void map_setres (full ? full :"")
 if (full)
 pcre2_match_data_free (mdata)
 pcre2_code_free (re)
 free (pattern)
 free (subject)
 push_value (vm, res)

Variables

case OP_PCRE2_MATCH
Value vtext = pop_value(vm)
Value vpat = pop_value(vm)
int flags = 0
char * pattern = value_to_string_alloc(&vpat)
char * subject = value_to_string_alloc(&vtext)
int errorcode
PCRE2_SIZE erroff
uint32_t opt = 0
pcre2_code * re = pcre2_compile((PCRE2_SPTR)pattern, PCRE2_ZERO_TERMINATED, opt, &errorcode, &erroff, NULL)
pcre2_match_data * mdata = pcre2_match_data_create_from_pattern(re, NULL)
int rc = pcre2_match(re, (PCRE2_SPTR)subject, (PCRE2_SIZE)strlen(subject), 0, 0, mdata, NULL)
PCRE2_SIZE * ov = pcre2_get_ovector_pointer(mdata)
Value res = make_map_empty()
int start0 = (int)ov[0]
int end0 = (int)ov[1]
char * full = string_substr(subject, start0, end0 - start0)
 break

Detailed Description

Implements the OP_PCRE2_MATCH opcode (conditional build).

Executes a single PCRE2 pattern match against a subject string and returns a map describing the first match and its capture groups when FUN_WITH_PCRE2 is enabled. Returns Nil when there is no match or on error. When PCRE2 support is disabled at build time, the opcode always returns Nil.

Definition in file match.c.

Macro Definition Documentation

◆ PCRE2_CODE_UNIT_WIDTH

#define PCRE2_CODE_UNIT_WIDTH   8

Definition at line 62 of file match.c.

Function Documentation

◆ free() [1/2]

free ( pattern )

◆ free() [2/2]

free ( subject )

◆ free_value() [1/3]

free_value ( vflags )

◆ free_value() [2/3]

free_value ( vpat )

◆ free_value() [3/3]

free_value ( vtext )

◆ if() [1/6]

if ( !pattern||! subject)

Definition at line 55 of file match.c.

◆ if() [2/6]

if ( ! re)

Definition at line 74 of file match.c.

◆ if() [3/6]

if ( flags & 1)

◆ if() [4/6]

if ( full )

Definition at line 97 of file match.c.

◆ if() [5/6]

if ( rc<= 0)

Definition at line 82 of file match.c.

◆ if() [6/6]

if ( vflags. type = =VAL_INT||vflags.type==VAL_BOOL)

◆ pcre2_code_free()

pcre2_code_free ( re )

◆ pcre2_match_data_free()

pcre2_match_data_free ( mdata )

◆ push_value()

push_value ( vm ,
res  )

◆ res()

void map_set & res ( full ? full :"" )

Variable Documentation

◆ break

break

Definition at line 125 of file match.c.

◆ end0

int end0 = (int)ov[1]

Definition at line 94 of file match.c.

◆ erroff

PCRE2_SIZE erroff

Definition at line 66 of file match.c.

◆ errorcode

int errorcode

Definition at line 65 of file match.c.

◆ flags

int flags = 0

Definition at line 48 of file match.c.

◆ full

char* full = string_substr(subject, start0, end0 - start0)

Definition at line 95 of file match.c.

◆ mdata

pcre2_match_data* mdata = pcre2_match_data_create_from_pattern(re, NULL)

Definition at line 80 of file match.c.

◆ OP_PCRE2_MATCH

case OP_PCRE2_MATCH

OP_PCRE2_MATCH: (pattern:any, text:any, flags:int|bool=0) -> map|Nil

Behavior when FUN_WITH_PCRE2 is enabled:

  • Pops: pattern, text, flags. Converts pattern/text to strings.
  • Flags bits:
    • 1 = PCRE2_CASELESS (I)
    • 2 = PCRE2_MULTILINE (M)
    • 4 = PCRE2_DOTALL (S)
    • 8 = PCRE2_UTF (U)
    • 16 = PCRE2_EXTENDED (X)
  • On successful match, returns a map with keys:
    • "full": matched substring (group 0)
    • "start": start index (int)
    • "end": end index (int, exclusive)
    • "groups": array of captured group strings (excluding group 0)
  • On no match or compile error, returns Nil.

Behavior when FUN_WITH_PCRE2 is disabled:

  • Pops three values and returns Nil.

Definition at line 43 of file match.c.

◆ opt

uint32_t opt = 0

Definition at line 67 of file match.c.

◆ ov

PCRE2_SIZE* ov = pcre2_get_ovector_pointer(mdata)

Definition at line 90 of file match.c.

◆ pattern

char* pattern = value_to_string_alloc(&vpat)

Definition at line 50 of file match.c.

◆ rc

int rc = pcre2_match(re, (PCRE2_SPTR)subject, (PCRE2_SIZE)strlen(subject), 0, 0, mdata, NULL)

Definition at line 81 of file match.c.

◆ re

pcre2_code* re = pcre2_compile((PCRE2_SPTR)pattern, PCRE2_ZERO_TERMINATED, opt, &errorcode, &erroff, NULL)

Definition at line 73 of file match.c.

◆ res

void map_set& res = make_map_empty()

Definition at line 92 of file match.c.

◆ start0

int start0 = (int)ov[0]

Definition at line 93 of file match.c.

◆ subject

char* subject = value_to_string_alloc(&vtext)

Definition at line 51 of file match.c.

◆ vpat

Value vpat = pop_value(vm)

Definition at line 47 of file match.c.

◆ vtext

Value vtext = pop_value(vm)

Definition at line 46 of file match.c.