Fun 0.41.5
The programming language that makes You have fun
Loading...
Searching...
No Matches
regex_replace.c File Reference

VM opcode snippet for OP_REGEX_REPLACE (POSIX global replace). More...

Go to the source code of this file.

Functions

 if (str.type !=VAL_STRING||pattern.type !=VAL_STRING||repl.type !=VAL_STRING)
 free_value (repl)
 free_value (pattern)
 free_value (str)
 push_value (vm, out)

Variables

case OP_REGEX_REPLACE
Value pattern = pop_value(vm)
Value str = pop_value(vm)
Value out = make_string(str.s ? str.s : "")
 break

Detailed Description

VM opcode snippet for OP_REGEX_REPLACE (POSIX global replace).

Performs a global search-and-replace on the input string using a POSIX regular expression pattern and a replacement string. Backreferences in the replacement are not expanded (simple literal replacement).

Behavior (stack effects):

  • Pops: replacement (string), pattern (string), input (string)
  • Pushes: output (string) — the transformed string after replacing all matches; on invalid regex, the original input is returned unchanged.

Platform notes:

  • On non-UNIX platforms (no POSIX regex), the original input string is returned unchanged.

Errors:

  • If operands have invalid types, the VM prints a runtime type error and exits.

Example:

  • pattern = "[0-9]+", repl = "#", input = "a1b22c" -> "a#b#c"

Definition in file regex_replace.c.

Function Documentation

◆ free_value() [1/3]

free_value(pattern)

◆ free_value() [2/3]

free_value(repl)

◆ free_value() [3/3]

free_value(str)

◆ if()

if(str.type ! = VAL_STRING || pattern.type != VAL_STRING || repl.type != VAL_STRING)

Definition at line 45 of file regex_replace.c.

◆ push_value()

push_value(vm,
out )

Variable Documentation

◆ break

break

Definition at line 56 of file regex_replace.c.

◆ OP_REGEX_REPLACE

case OP_REGEX_REPLACE

Definition at line 41 of file regex_replace.c.

◆ out

Definition at line 51 of file regex_replace.c.

◆ pattern

Value pattern = pop_value(vm)

Definition at line 43 of file regex_replace.c.

◆ str

Value str = pop_value(vm)

Definition at line 44 of file regex_replace.c.