Rust

pcre

Module

PHP's PCRE functions: preg_match, preg_match_all, preg_replace, preg_split and preg_quote, on PCRE2 10.44 with its JIT, the library and version PHP 8.5 bundles.

PHPRust
preg_match($p, $s, $m, $flags, $offset)preg_match
preg_match($p, $s)preg_match_bare
preg_match_all($p, $s, $m, $flags, $offset)preg_match_all
preg_replace($p, $r, $s, $limit, $count)preg_replace
preg_split($p, $s, $limit, $flags)preg_split
preg_quote($s, $delimiter)preg_quote
preg_match($p, $s, $m) === 1 on a pattern compiled onceRegex::compiled, Regex::captures, Regex::is_match
preg_last_error() / preg_last_error_msg()PregError in every result

User patterns must behave exactly as in PHP, down to where pcre.backtrack_limit and the JIT stack run out, and only PCRE2 itself does that: build.rs compiles the sources PHP 8.5 bundles (ext/pcre/pcre2lib), and this module calls them as ext/pcre/php_pcre.c does. Patterns are delimited and take modifiers exactly as pcre_get_compiled_regex_cache() parses them (/i m n s x A D r S X U u J), then go through PHP's per-process cache of compiled patterns, which JIT-compiles them while pcre.jit is on (a JIT failure warns and turns it off for the process). Matches run with pcre.backtrack_limit and pcre.recursion_limit (their defaults) as the match and depth limits and the 32 KiB–192 KiB JIT stack, through pcre2_jit_match() when no UTF-8 check is needed and pcre2_match() otherwise, and an empty match is retried with PCRE2_NOTEMPTY_ATSTART | PCRE2_ANCHORED, as PHP does.

Every function here is checked against the real PHP function by bin/compat fuzz php-std (operations pcre.*).

Contents

3 submodules, and what the module defines itself.

use php_std::pcre::ArgumentError;
Rust
pub struct ArgumentError

A ValueError/TypeError thrown for an invalid argument.

Also implements Display.
use php_std::Array;
Rust
pub struct Array

An ordered PHP array.

use php_std::pcre::CompileFailure;
Rust
pub struct CompileFailure

A pattern that pcre_get_compiled_regex_cache() rejected: the warning text, without the function name.

use php_std::pcre::MatchResult;
Rust
pub struct MatchResult

The result of preg_match and [preg_match_all]: the return value (Int or Bool(false)) and $matches (None when the pattern did not compile, which leaves $matches untouched).

use php_std::pcre::Preg;
Rust
pub struct Preg<T>

What a preg_* call returned, with preg_last_error() and the last warning it emitted ("preg_match(): Compilation failed: ...").

use php_std::pcre::Regex;
Rust
pub struct Regex

A compiled PHP regular expression (delimiters and modifiers included): a pcre_cache_entry.

Also implements Debug.
use php_std::pcre::ReplaceResult;
Rust
pub struct ReplaceResult

What preg_replace returns: the result (Str, Array or Null) and $count.

use php_std::pcre::Key;
Rust
pub enum Key

A PHP array key.

use php_std::pcre::PregError;
Rust
pub enum PregError

preg_last_error().

use php_std::pcre::StrOrArray;
Rust
pub enum StrOrArray<'a>

A string|array argument of preg_replace().

use php_std::Value;
Rust
pub enum Value

The PHP values the preg_* functions build.

use php_std::pcre::preg_last_error;
Rust
pub fn preg_last_error() -> PregError

preg_last_error(): the error of the last preg_* call that set one.

use php_std::pcre::preg_match;
Rust
pub fn preg_match(pattern: &[u8], subject: &[u8], flags: i64, offset: i64) -> Result<Preg<MatchResult>, ArgumentError>

preg_match($pattern, $subject, $matches, $flags, $offset).

use php_std::pcre::preg_match_all;
Rust
pub fn preg_match_all(pattern: &[u8], subject: &[u8], flags: i64, offset: i64) -> Result<Preg<MatchResult>, ArgumentError>

preg_match_all($pattern, $subject, $matches, $flags, $offset).

use php_std::pcre::preg_match_bare;
Rust
pub fn preg_match_bare(pattern: &[u8], subject: &[u8]) -> Result<Preg<Value>, ArgumentError>

preg_match($pattern, $subject): preg_match() without $matches. PHP then treats an empty match like the next match of preg_match_all(): it retries at the same position with PCRE2_NOTEMPTY_ATSTART | PCRE2_ANCHORED, and an error there (a limit) makes the result false. Passing flags: or offset: by name gives preg_match() a $matches to fill, so that is preg_match.

use php_std::pcre::preg_quote;
Rust
pub fn preg_quote(s: &[u8], delimiter: Option<&[u8]>) -> Vec<u8>

preg_quote($str, $delimiter): only the first byte of the delimiter is used.

use php_std::pcre::preg_replace;
Rust
pub fn preg_replace(pattern: &StrOrArray<'_>, replacement: &StrOrArray<'_>, subject: &StrOrArray<'_>, limit: i64) -> Result<Preg<ReplaceResult>, ArgumentError>

preg_replace($pattern, $replacement, $subject, $limit, $count).

use php_std::pcre::preg_split;
Rust
pub fn preg_split(pattern: &[u8], subject: &[u8], limit: i64, flags: i64) -> Preg<Value>

preg_split($pattern, $subject, $limit, $flags): an array, or Bool(false).

Rust
pub const PREG_OFFSET_CAPTURE: i64 = 1 << 8

Returns each match with its byte offset, as PHP's PREG_OFFSET_CAPTURE.

Rust
pub const PREG_PATTERN_ORDER: i64 = 1

Orders preg_match_all() results by group, as PHP's PREG_PATTERN_ORDER.

Rust
pub const PREG_SET_ORDER: i64 = 2

Orders preg_match_all() results by match, as PHP's PREG_SET_ORDER.

Rust
pub const PREG_SPLIT_DELIM_CAPTURE: i64 = 2

Returns the delimiter's captured groups from preg_split() too, as PHP's PREG_SPLIT_DELIM_CAPTURE.

Rust
pub const PREG_SPLIT_NO_EMPTY: i64 = 1

Leaves empty pieces out of preg_split(), as PHP's PREG_SPLIT_NO_EMPTY.

Rust
pub const PREG_SPLIT_OFFSET_CAPTURE: i64 = 4

Returns each preg_split() piece with its byte offset, as PHP's PREG_SPLIT_OFFSET_CAPTURE.

Rust
pub const PREG_UNMATCHED_AS_NULL: i64 = 1 << 9

Reports unmatched groups as null, as PHP's PREG_UNMATCHED_AS_NULL.

Rust
pub const UNICODE_VERSION: &str = "15.0.0"

The Unicode version of PCRE2's tables (PHP's "PCRE Unicode Version").

pcre::compiled

Own page

A PHP regex compiled once and matched many times: preg_match() without the per-call cache lookup, for libraries that run fixed patterns on a hot path (utopia-user-agent runs its tables on every request).

PHPRust
preg_match($re, $s, $m) === 1, then $m[$n]Regex::compiled, Regex::captures, Captures::get
preg_match($re, $s) === 1Regex::is_match

Both run exactly the calls preg_match() makes, so a match that fails (pcre.backtrack_limit, the JIT stack) is no match, as preg_match() returning false is not 1. They do not set preg_last_error().

use php_std::pcre::compiled::Captures;
Rust
pub struct Captures<'s>

The groups of one match of Regex::captures, as preg_match() puts them in $matches.

pcre::engine

Own page

PCRE2 as ext/pcre holds it: compiled patterns shared by every thread, and per thread the match context (pcre.backtrack_limit, pcre.recursion_limit and the 32 KiB–192 KiB JIT stack) and the preallocated match data php_pcre_init_pcre2() creates.

PHP keeps that state per thread too (ZEND_TLS), so a Rust thread running several patterns behaves like one PHP process.

pcre::ffi

Own page

The PCRE2 10.44 API (pcre2.h, 8-bit library) that ext/pcre uses, linked from the sources build.rs compiles.

use php_std::pcre::ffi::Code;
Rust
pub struct Code

pcre2_code.

Also implements Drop.
use php_std::pcre::ffi::CompileContext;
Rust
pub struct CompileContext

pcre2_compile_context.

use php_std::pcre::ffi::GeneralContext;
Rust
pub struct GeneralContext

pcre2_general_context (always NULL here: PCRE2's default allocator).

use php_std::pcre::ffi::JitStack;
Rust
pub struct JitStack

pcre2_jit_stack.

use php_std::pcre::ffi::MatchContext;
Rust
pub struct MatchContext

pcre2_match_context.

use php_std::pcre::ffi::MatchData;
Rust
pub struct MatchData

pcre2_match_data.

Rust
pub type JitCallback = Option<unsafe extern "C" fn(*mut c_void) -> *mut JitStack>

pcre2_jit_callback.

Rust
pub const ANCHORED: u32 = 0x8000_0000

Undocumented.

Rust
pub const CASELESS: u32 = 0x0000_0008

Undocumented.

Rust
pub const DOLLAR_ENDONLY: u32 = 0x0000_0010

Undocumented.

Rust
pub const DOTALL: u32 = 0x0000_0020

Undocumented.

Rust
pub const DUPNAMES: u32 = 0x0000_0040

Undocumented.

Rust
pub const ERROR_BACKSLASH_C_CALLER_DISABLED: c_int = 183

Undocumented.

Rust
pub const ERROR_BADOFFSET: c_int = -33

Undocumented.

Rust
pub const ERROR_BADUTFOFFSET: c_int = -36

Undocumented.

Rust
pub const ERROR_DEPTHLIMIT: c_int = -53

Undocumented.

Rust
pub const ERROR_JIT_STACKLIMIT: c_int = -46

Undocumented.

Rust
pub const ERROR_MATCHLIMIT: c_int = -47

Undocumented.

Rust
pub const ERROR_NOMATCH: c_int = -1

Undocumented.

Rust
pub const ERROR_NOMEMORY: c_int = -48

Undocumented.

Rust
pub const ERROR_UTF8_ERR1: c_int = -3

Undocumented.

Rust
pub const ERROR_UTF8_ERR21: c_int = -23

Undocumented.

Rust
pub const EXTENDED: u32 = 0x0000_0080

Undocumented.

Rust
pub const EXTRA_CASELESS_RESTRICT: u32 = 0x0000_0080

Undocumented.

Rust
pub const INFO_CAPTURECOUNT: u32 = 4

Undocumented.

Rust
pub const INFO_JITSIZE: u32 = 10

Undocumented.

Rust
pub const INFO_NAMECOUNT: u32 = 17

Undocumented.

Rust
pub const INFO_NAMEENTRYSIZE: u32 = 18

Undocumented.

Rust
pub const INFO_NAMETABLE: u32 = 19

Undocumented.

Rust
pub const JIT_COMPLETE: u32 = 0x0000_0001

Undocumented.

Rust
pub const MULTILINE: u32 = 0x0000_0400

Undocumented.

Rust
pub const NEVER_BACKSLASH_C: u32 = 0x0010_0000

Undocumented.

Rust
pub const NO_AUTO_CAPTURE: u32 = 0x0000_2000

Undocumented.

Rust
pub const NO_UTF_CHECK: u32 = 0x4000_0000

Undocumented.

Rust
pub const NOTEMPTY_ATSTART: u32 = 0x0000_0008

Undocumented.

Rust
pub const UCP: u32 = 0x0002_0000

Undocumented.

Rust
pub const UNGREEDY: u32 = 0x0004_0000

Undocumented.

Rust
pub const UNSET: usize = usize::MAX

PCRE2_UNSET: an unset capture offset.

Rust
pub const UTF: u32 = 0x0008_0000

Undocumented.