PHP functions
php_std reimplements, where it lives in Rust, and how it is proven against the real PHP.Why php-std exists#
A Utopia crate has to behave exactly like its PHP library, and much of that behaviour comes from the PHP engine, not the library: how "10" == "1e1" compares, how a float prints as a string, which bytes strtolower touches, how mb_strtolower handles a final sigma, what json_encode escapes. Rust's own functions differ in these details, and every difference is a response that changes byte for byte.
So each engine function is reimplemented once, here, and checked against the real one by bin/compat: recorded cases, then fuzzing with generated inputs on every run. Crates call these functions instead of approximations of their own. Quirks PHP has are kept on purpose and pinned by a case; inputs a Rust type cannot represent are recorded as deviations.
Size
| Part | Code | Comments | Blank | Total | Files |
|---|---|---|---|---|---|
| Rust source | 33,127 | 3,282 | 1,585 | 37,994 | 48 |
| Rust tests | 1,549 | 29 | 118 | 1,696 | 32 |
Inventory#
Cases and fuzz inputs are those of the compat operations exercising each entry; an operation exercising several entries counts for each.
Strings
Byte-string functions of ext/standard/string.c and ext/ctype, plus crypt(), password_hash() / password_verify() and hash().
| PHP | Rust | Cases | Fuzz | Quirks | Used by |
|---|---|---|---|---|---|
addcslashes() | 1 | 2,000 | |||
crypt() | 2 | 4,000 | |||
ctype_alnum() | 24 | 5,000 | |||
ctype_digit() | 17 | 5,000 | |||
ctype_xdigit() | 16 | 5,000 | |||
escapeshellarg() | 4 | 3,000 | |||
explode() | 6 | 3,000 | |||
hash() | 2 | 8,000 | |||
lcfirst() | 1 | 2,000 | |||
ltrim() | 1 | 3,000 | |||
password_hash() | 2 | — | |||
password_verify() | 2 | 3,000 | |||
rtrim() | 1 | 3,000 | |||
str_contains() | 1 | 2,000 | |||
str_replace() | 5 | 8,000 | |||
str_starts_with() | 6 | 2,000 | |||
strcasecmp() | 1 | 2,000 | |||
strcmp() | 2 | 2,000 | |||
strip_tags() | 4 | 10,000 | |||
stripcslashes() | 1 | 3,000 | |||
stripos() | 1 | 3,000 | |||
strtolower() | 1 | 3,000 | |||
strtoupper() | 1 | 3,000 | |||
strtr() | 4 | 6,000 | |||
strval() | 4 | 4,000 | — | ||
substr() | 5 | 3,000 | |||
trim() | 6 | 3,000 | |||
ucfirst() | 1 | 2,000 | |||
ucwords() | 3 | 3,000 |
Multibyte strings
Multibyte (mbstring) functions on UTF-8.
| PHP | Rust | Cases | Fuzz | Quirks | Used by |
|---|---|---|---|---|---|
mb_check_encoding() | 2 | 3,000 | |||
mb_convert_encoding() | 1 | 2,000 | |||
mb_scrub() | 1 | 3,000 | |||
mb_strimwidth() | 5 | 3,000 | |||
mb_strlen() | 3 | 5,000 | |||
mb_strpos() | 3 | 4,000 | |||
mb_strtolower() | 5 | 9,000 | |||
mb_substr() | 3 | 4,000 |
Encoding
Encodings: URL encoding, http_build_query, base64, hex, HTML entities and quoted-printable.
| PHP | Rust | Cases | Fuzz | Quirks | Used by |
|---|---|---|---|---|---|
base64_decode() | 8 | 5,000 | |||
base64_encode() | 1 | 3,000 | |||
bin2hex() | 1 | 2,000 | |||
hex2bin() | 3 | 3,000 | |||
htmlentities() | 4 | 8,000 | |||
htmlspecialchars() | 8 | 8,000 | |||
http_build_query() | 8 | 4,000 | |||
parse_str() | 19 | 5,000 | |||
quoted_printable_encode() | 1 | 3,000 | |||
rawurldecode() | 1 | 3,000 | |||
rawurlencode() | 1 | 3,000 | |||
urldecode() | 2 | 3,000 | |||
urlencode() | 1 | 3,000 |
JSON
json_encode, json_decode, json_validate, json_last_error and json_last_error_msg, as PHP 8.5's ext/json implements them.
| PHP | Rust | Cases | Fuzz | Quirks | Used by |
|---|---|---|---|---|---|
json_decode() | 62 | 16,000 | |||
json_encode() | 49 | 14,000 | |||
json_last_error() | 109 | 20,000 | |||
json_last_error_msg() | 105 | 17,000 | |||
json_validate() | 15 | 3,000 |
Numbers and formatting
Number formatting and casts: sprintf, number_format, round/ceil, intval/floatval and the (int) / (float) casts, fmod, max/min of numbers.
| PHP | Rust | Cases | Fuzz | Quirks | Used by |
|---|---|---|---|---|---|
** exponentiationbehaviour | 12 | 8,000 | |||
+ - * / arithmeticbehaviour | 8 | 4,000 | |||
array_sum() | 2 | 2,000 | |||
ceil() | 1 | 2,000 | |||
floatval() | 5 | 6,000 | |||
fmod() | 2 | 2,000 | |||
intval() | 15 | 12,000 | appwrite-coremongo | ||
max() | 11 | 10,000 | |||
min() | 8 | 10,000 | |||
number_format() | 12 | 6,000 | |||
round() | 13 | 8,000 | |||
sprintf() | 28 | 19,000 |
Types, juggling and comparison
PHP value semantics (type juggling) on top of serde_json::Value.
| PHP | Rust | Cases | Fuzz | Quirks | Used by |
|---|---|---|---|---|---|
(bool) castbehaviour | 25 | 5,000 | |||
(float) castbehaviour | 36 | 11,000 | |||
(string) castbehaviour | 27 | 6,000 | |||
< <= > >= orderingbehaviour | 64 | 15,000 | |||
<=> comparisonbehaviour | 61 | 10,000 | |||
== loose comparisonbehaviour | 70 | 23,000 | |||
=== strict comparisonbehaviour | 61 | 10,000 | |||
Array keys and writesbehaviour | 16 | 4,000 | |||
The value model holds every PHP value of the wirebehaviour | — | 4 | 3,000 | — | |
Utopia\Http\Request decoding behaviour | 3 | 2,000 | |||
empty() | 1 | 2,000 | |||
gettype() | 24 | 3,000 | |||
is_numeric() | 3 | 6,000 | |||
unset() | 16 | 4,000 |
Arrays and sorting
PHP's array sorting and set functions: sort, ksort, array_unique, array_diff, array_intersect, in_array and array_search, with the SORT_* flags.
| PHP | Rust | Cases | Fuzz | Quirks | Used by |
|---|---|---|---|---|---|
array_diff() | 4 | 3,000 | |||
array_intersect() | 4 | 5,000 | |||
array_search() | 2 | 3,000 | |||
array_unique() | 6 | 3,000 | |||
in_array() | 4 | 3,000 | |||
ksort() | 5 | 3,020 | |||
sort() | 17 | 8,020 |
Dates and times
Date and time: date()/DateTime::format, DateTime parsing and timezones, strtotime, microtime.
| PHP | Rust | Cases | Fuzz | Quirks | Used by |
|---|---|---|---|---|---|
DateInterval::createFromDateStringmethod | 11 | 4,000 | |||
DateTime::__constructmethod | 11 | 7,000 | |||
DateTime::createFromFormatmethod | 16 | 4,000 | |||
DateTime::formatmethod | 27 | 11,000 | |||
DateTime::getLastErrorsmethod | datetime::DateTime::from_formatdatetime::Messages::to_php | 16 | 4,000 | ||
DateTime::modifymethod | 2 | 2,000 | |||
DateTime::setTimemethod | 9 | 5,000 | |||
DateTimeImmutable::__constructmethod | 123 | 33,000 | |||
DateTimeImmutable::addmethod | 11 | 4,000 | |||
DateTimeImmutable::formatmethod | 81 | 24,000 | |||
DateTimeImmutable::getMicrosecondmethod | 19 | 3,000 | |||
DateTimeImmutable::getOffsetmethod | 19 | 3,000 | |||
DateTimeImmutable::getTimestampmethod | 19 | 3,000 | |||
DateTimeImmutable::getTimezonemethod | 19 | 3,000 | |||
DateTimeImmutable::modifymethod | 16 | 6,000 | |||
DateTimeImmutable::setDatemethod | 9 | 5,000 | |||
DateTimeImmutable::setTimezonemethod | 21 | 5,000 | |||
DateTimeZone::__constructmethod | 42 | 9,000 | — | ||
date() | 75 | 3,000 | |||
date_parse() | 155 | 11,000 | — | ||
gmdate() | 1 | 2,000 | |||
microtime() | 1 | — | |||
strtotime() | 26 | 8,000 | |||
time() | 1 | — |
Regular expressions
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.
| PHP | Rust | Cases | Fuzz | Quirks | Used by |
|---|---|---|---|---|---|
preg_last_error() | 84 | 21,000 | |||
preg_match() | 97 | 29,000 | |||
preg_match_all() | 14 | 15,000 | |||
preg_quote() | 5 | 3,000 | |||
preg_replace() | 12 | 15,000 | |||
preg_split() | 8 | 13,000 |
URLs
parse_url and the PHP 8.5 URI extension (Uri\Rfc3986\Uri).
| PHP | Rust | Cases | Fuzz | Quirks | Used by |
|---|---|---|---|---|---|
Uri\Rfc3986\Uri::getHostmethod | 21 | 15,000 | |||
Uri\Rfc3986\Uri::getRawHostmethod | 21 | 15,000 | |||
Uri\Rfc3986\Uri::getRawPasswordmethod | 21 | 15,000 | |||
Uri\Rfc3986\Uri::getRawPathmethod | 21 | 15,000 | |||
Uri\Rfc3986\Uri::getRawQuerymethod | 21 | 15,000 | |||
Uri\Rfc3986\Uri::getRawSchememethod | 21 | 15,000 | |||
Uri\Rfc3986\Uri::getRawUserInfomethod | 21 | 15,000 | |||
Uri\Rfc3986\Uri::getRawUsernamemethod | 21 | 15,000 | |||
Uri\Rfc3986\Uri::getSchememethod | 21 | 15,000 | |||
Uri\Rfc3986\Uri::parsemethod | 21 | 15,000 | |||
parse_url() | 35 | 14,000 |
Filters
filter_var(): the validation filters (FILTER_VALIDATE_INT, BOOL, FLOAT, REGEXP, URL, EMAIL, IP, MAC, DOMAIN), the sanitizing filters (FILTER_DEFAULT/FILTER_UNSAFE_RAW, FILTER_SANITIZE_*) and FILTER_CALLBACK, with their flags and options, ported from ext/filter (logical_filters.c, sanitizing_filters.c, callback_filter.c, filter.c) of PHP 8.5.
| PHP | Rust | Cases | Fuzz | Quirks | Used by |
|---|---|---|---|---|---|
filter_var() | 139 | 49,000 |
Network
Network helpers: inet_pton/inet_ntop.
| PHP | Rust | Cases | Fuzz | Quirks | Used by |
|---|---|---|---|---|---|
inet_ntop() | 9 | 4,000 | |||
inet_pton() | 10 | 8,000 |
Serialization
serialize, unserialize and var_export, in PHP 8.5's formats.
| PHP | Rust | Cases | Fuzz | Quirks | Used by |
|---|---|---|---|---|---|
igbinary_serialize() | 30 | 6,230 | |||
igbinary_unserialize() | 49 | — | |||
serialize() | 15 | 9,000 | |||
unserialize() | 88 | 14,000 | |||
var_export() | 10 | 5,000 |
YAML
yaml_parse() (ext-yaml 2.3 over libyaml 0.2.5) with the default ini settings: timestamps, !!binary and !php/object stay strings.
| PHP | Rust | Cases | Fuzz | Quirks | Used by |
|---|---|---|---|---|---|
yaml_parse() | 10 | 8,000 |
Paths
Path functions.
| PHP | Rust | Cases | Fuzz | Quirks | Used by |
|---|---|---|---|---|---|
basename() | 14 | 6,000 | |||
fnmatch() | 10 | 9,000 | |||
pathinfo() | 8 | 3,000 |
Random numbers
Random numbers: PHP's Mersenne Twister, the engine behind mt_rand(), rand(), array_rand() and shuffle().
| PHP | Rust | Cases | Fuzz | Quirks | Used by |
|---|---|---|---|---|---|
array_rand() | 3 | 1,000 | |||
mt_rand() | 5 | 2,300 | |||
mt_srand() | 5 | 2,300 |