pcre
ModulePHP'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.
| PHP | Rust |
|---|---|
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 once | Regex::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;pub struct ArgumentErrorA ValueError/TypeError thrown for an invalid argument.
Display.use php_std::Array;pub struct ArrayAn ordered PHP array.
use php_std::pcre::CompileFailure;pub struct CompileFailureA pattern that pcre_get_compiled_regex_cache() rejected: the warning
text, without the function name.
use php_std::pcre::MatchResult;pub struct MatchResultThe 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;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;pub struct RegexA compiled PHP regular expression (delimiters and modifiers included):
a pcre_cache_entry.
Debug.use php_std::pcre::ReplaceResult;pub struct ReplaceResultWhat preg_replace returns: the result (Str, Array or Null) and
$count.
use php_std::pcre::Key;pub enum KeyA PHP array key.
use php_std::pcre::PregError;pub enum PregErrorpreg_last_error().
use php_std::pcre::StrOrArray;pub enum StrOrArray<'a>A string|array argument of preg_replace().
use php_std::Value;pub enum ValueThe PHP values the preg_* functions build.
use php_std::pcre::preg_last_error;pub fn preg_last_error() -> PregErrorpreg_last_error(): the error of the last preg_* call that set one.
use php_std::pcre::preg_match;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;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;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;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;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;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).
pub const PREG_OFFSET_CAPTURE: i64 = 1 << 8Returns each match with its byte offset, as PHP's PREG_OFFSET_CAPTURE.
pub const PREG_PATTERN_ORDER: i64 = 1Orders preg_match_all() results by group, as PHP's PREG_PATTERN_ORDER.
pub const PREG_SET_ORDER: i64 = 2Orders preg_match_all() results by match, as PHP's PREG_SET_ORDER.
pub const PREG_SPLIT_DELIM_CAPTURE: i64 = 2Returns the delimiter's captured groups from preg_split() too, as PHP's PREG_SPLIT_DELIM_CAPTURE.
pub const PREG_SPLIT_NO_EMPTY: i64 = 1Leaves empty pieces out of preg_split(), as PHP's PREG_SPLIT_NO_EMPTY.
pub const PREG_SPLIT_OFFSET_CAPTURE: i64 = 4Returns each preg_split() piece with its byte offset, as PHP's PREG_SPLIT_OFFSET_CAPTURE.
pub const PREG_UNMATCHED_AS_NULL: i64 = 1 << 9Reports unmatched groups as null, as PHP's PREG_UNMATCHED_AS_NULL.
pub const UNICODE_VERSION: &str = "15.0.0"The Unicode version of PCRE2's tables (PHP's "PCRE Unicode Version").
pcre::compiled
Own pageA 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).
| PHP | Rust |
|---|---|
preg_match($re, $s, $m) === 1, then $m[$n] | Regex::compiled, Regex::captures, Captures::get |
preg_match($re, $s) === 1 | Regex::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;pub struct Captures<'s>The groups of one match of Regex::captures, as preg_match() puts
them in $matches.
pcre::engine
Own pagePCRE2 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 pageThe 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;pub struct Codepcre2_code.
Drop.use php_std::pcre::ffi::CompileContext;pub struct CompileContextpcre2_compile_context.
use php_std::pcre::ffi::GeneralContext;pub struct GeneralContextpcre2_general_context (always NULL here: PCRE2's default allocator).
use php_std::pcre::ffi::JitStack;pub struct JitStackpcre2_jit_stack.
use php_std::pcre::ffi::MatchContext;pub struct MatchContextpcre2_match_context.
use php_std::pcre::ffi::MatchData;pub struct MatchDatapcre2_match_data.
pub type JitCallback = Option<unsafe extern "C" fn(*mut c_void) -> *mut JitStack>pcre2_jit_callback.
pub const ANCHORED: u32 = 0x8000_0000Undocumented.
pub const CASELESS: u32 = 0x0000_0008Undocumented.
pub const DOLLAR_ENDONLY: u32 = 0x0000_0010Undocumented.
pub const DOTALL: u32 = 0x0000_0020Undocumented.
pub const DUPNAMES: u32 = 0x0000_0040Undocumented.
pub const ERROR_BACKSLASH_C_CALLER_DISABLED: c_int = 183Undocumented.
pub const ERROR_BADOFFSET: c_int = -33Undocumented.
pub const ERROR_BADUTFOFFSET: c_int = -36Undocumented.
pub const ERROR_DEPTHLIMIT: c_int = -53Undocumented.
pub const ERROR_JIT_STACKLIMIT: c_int = -46Undocumented.
pub const ERROR_MATCHLIMIT: c_int = -47Undocumented.
pub const ERROR_NOMATCH: c_int = -1Undocumented.
pub const ERROR_NOMEMORY: c_int = -48Undocumented.
pub const ERROR_UTF8_ERR1: c_int = -3Undocumented.
pub const ERROR_UTF8_ERR21: c_int = -23Undocumented.
pub const EXTENDED: u32 = 0x0000_0080Undocumented.
pub const EXTRA_CASELESS_RESTRICT: u32 = 0x0000_0080Undocumented.
pub const INFO_CAPTURECOUNT: u32 = 4Undocumented.
pub const INFO_JITSIZE: u32 = 10Undocumented.
pub const INFO_NAMECOUNT: u32 = 17Undocumented.
pub const INFO_NAMEENTRYSIZE: u32 = 18Undocumented.
pub const INFO_NAMETABLE: u32 = 19Undocumented.
pub const JIT_COMPLETE: u32 = 0x0000_0001Undocumented.
pub const MULTILINE: u32 = 0x0000_0400Undocumented.
pub const NEVER_BACKSLASH_C: u32 = 0x0010_0000Undocumented.
pub const NO_AUTO_CAPTURE: u32 = 0x0000_2000Undocumented.
pub const NO_UTF_CHECK: u32 = 0x4000_0000Undocumented.
pub const NOTEMPTY_ATSTART: u32 = 0x0000_0008Undocumented.
pub const UCP: u32 = 0x0002_0000Undocumented.
pub const UNGREEDY: u32 = 0x0004_0000Undocumented.
pub const UNSET: usize = usize::MAXPCRE2_UNSET: an unset capture offset.
pub const UTF: u32 = 0x0008_0000Undocumented.