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.
Contents
use utopia_php_std::pcre::ArgumentError;pub struct ArgumentError {
// private fields
}A ValueError/TypeError thrown for an invalid argument.
Display.use utopia_php_std::pcre::Array;pub struct Array {
// private fields
}An ordered PHP array.
use utopia_php_std::pcre::Captures;pub struct Captures<'s> {
// private fields
}The groups of one match of Regex::captures, as preg_match() puts
them in $matches.
use utopia_php_std::pcre::CompileFailure;pub struct CompileFailure {
pub message: Vec<u8>,
}A pattern that pcre_get_compiled_regex_cache() rejected: the warning
text, without the function name.
use utopia_php_std::pcre::MatchResult;pub struct MatchResult {
pub result: Value,
pub matches: Option<Array>,
}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 utopia_php_std::pcre::Preg;pub struct Preg<T> {
pub value: T,
pub error: PregError,
pub warning: Option<Vec<u8>>,
}What a preg_* call returned, with preg_last_error() and the last
warning it emitted ("preg_match(): Compilation failed: ...").
use utopia_php_std::pcre::Regex;pub struct Regex {
// private fields
}A compiled PHP regular expression (delimiters and modifiers included):
a pcre_cache_entry.
Debug.use utopia_php_std::pcre::ReplaceResult;pub struct ReplaceResult {
pub result: Value,
pub count: i64,
}What preg_replace returns: the result (Str, Array or Null) and
$count.
use utopia_php_std::pcre::Key;pub enum Key {
Int(i64),
Str(Vec<u8>),
}A PHP array key.
use utopia_php_std::pcre::PregError;pub enum PregError {
None,
Internal,
BacktrackLimit,
RecursionLimit,
BadUtf8,
BadUtf8Offset,
JitStackLimit,
}preg_last_error().
use utopia_php_std::pcre::StrOrArray;pub enum StrOrArray<'a> {
Str(&'a [u8]),
Array(Vec<(Key, Vec<u8>)>),
}A string|array argument of preg_replace().
use utopia_php_std::pcre::Value;pub enum Value {
Null,
Bool(bool),
Int(i64),
Str(Vec<u8>),
Array(Array),
}The PHP values the preg_* functions build.
use utopia_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 utopia_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 utopia_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 utopia_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 utopia_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 utopia_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 utopia_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 = 256i64;Returns each match with its byte offset, as PHP's PREG_OFFSET_CAPTURE.
pub const PREG_PATTERN_ORDER: i64 = 1i64;Orders preg_match_all() results by group, as PHP's PREG_PATTERN_ORDER.
pub const PREG_SET_ORDER: i64 = 2i64;Orders preg_match_all() results by match, as PHP's PREG_SET_ORDER.
pub const PREG_SPLIT_DELIM_CAPTURE: i64 = 2i64;Returns the delimiter's captured groups from preg_split() too, as PHP's PREG_SPLIT_DELIM_CAPTURE.
pub const PREG_SPLIT_NO_EMPTY: i64 = 1i64;Leaves empty pieces out of preg_split(), as PHP's PREG_SPLIT_NO_EMPTY.
pub const PREG_SPLIT_OFFSET_CAPTURE: i64 = 4i64;Returns each preg_split() piece with its byte offset, as PHP's PREG_SPLIT_OFFSET_CAPTURE.
pub const PREG_UNMATCHED_AS_NULL: i64 = 512i64;Reports 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").