sort
ModulePHP's array sorting and set functions: sort, ksort, array_unique,
array_diff, array_intersect, in_array and array_search, with the
SORT_* flags.
Every function here is checked against the engine by
bin/compat fuzz php-std (operations sort.*).
| PHP | Rust |
|---|---|
sort($a, $flags) | sort |
ksort($a, $flags) | ksort |
array_unique($a, $flags) | array_unique |
array_diff($a, ...$b), array_intersect | array_diff, array_intersect |
in_array($n, $h, $strict), array_search | in_array, array_search |
SORT_REGULAR, SORT_NUMERIC, SORT_STRING, SORT_FLAG_CASE, SORT_NATURAL, SORT_LOCALE_STRING | SortFlags |
PHP 8 sorts are stable: zend_sort (insertion sort up to 16 elements,
then a quicksort) with the original position breaking ties. PHP's
comparisons are not always transitive (null == 0, null == "", but
0 > ""), so the result depends on the algorithm: zend_sort is a
port of it, comparison for comparison. SORT_LOCALE_STRING compares
like strcoll in the C locale (byte order of the C strings, so up to the
first NUL byte), which is what the engine
uses without setlocale. Functions that turn values into strings fail
like PHP when they meet an object (Error), so the value sorts and set
functions return Result (the skeleton in rfc/rust-database.md has ()).
Contents
use php_std::sort::SortFlags;pub enum SortFlagsThe $flags of the sort functions.
use php_std::sort::array_diff;pub fn array_diff<X: Extension>(a: &Array<X>, others: &[&Array<X>]) -> Result<Array<X>, EngineError>array_diff($a, ...$others): the entries of a whose string value is
in none of others, keys kept.
use php_std::sort::array_intersect;pub fn array_intersect<X: Extension>(a: &Array<X>, others: &[&Array<X>]) -> Result<Array<X>, EngineError>array_intersect($a, ...$others): the entries of a whose string value
is in every one of others, keys kept (php_array_intersect: sorted
copies walked together).
use php_std::sort::array_search;pub fn array_search<'a, X: Extension>(needle: &Value<X>, haystack: &'a Array<X>, strict: bool) -> Option<KeyRef<'a>>array_search($needle, $haystack, $strict): the key of the first match.
use php_std::sort::array_unique;pub fn array_unique<X: Extension>(a: &Array<X>, flags: SortFlags) -> Result<Array<X>, EngineError>array_unique($a, $flags) (PHP's default flag is SORT_STRING): the
first of each group of equal values, keys kept.
use php_std::sort::in_array;pub fn in_array<X: Extension>(needle: &Value<X>, haystack: &Array<X>, strict: bool) -> boolin_array($needle, $haystack, $strict).
use php_std::sort::ksort;pub fn ksort<X: Extension>(a: &mut Array<X>, flags: SortFlags)ksort($a, $flags).
use php_std::sort::sort;pub fn sort<X: Extension>(a: &mut Array<X>, flags: SortFlags) -> Result<(), EngineError>sort($a, $flags): values in order, keys renumbered.
use php_std::sort::strcasecmp;pub fn strcasecmp(a: &[u8], b: &[u8]) -> i32zend_binary_strcasecmp_l: ASCII case-insensitive.
use php_std::sort::strcoll;pub fn strcoll(a: &[u8], b: &[u8]) -> i32strcoll in the C locale: byte order of the C strings (up to the first NUL).
use php_std::sort::strnatcmp;pub fn strnatcmp(a: &[u8], b: &[u8], fold_case: bool) -> i32strnatcmp_ex: natural order (strnatcmp, or strnatcasecmp with fold_case).
use php_std::sort::zend_sort;pub fn zend_sort<T>(v: &mut [T], cmp: &mut impl FnMut(&T, &T) -> i32)zend_sort: the engine's hybrid insertion sort / quicksort. cmp
returns a C comparison result; only > 0 matters, as in the engine.