mb
ModuleMultibyte (mbstring) functions on UTF-8.
Ported from PHP 8.5.10's ext/mbstring for the UTF-8 encoding (PHP's
default internal encoding), including its handling of invalid UTF-8:
the decoder turns each invalid sequence into one error marker (one
"character" for mb_strlen), and functions that re-encode text write
? (mbstring.substitute_character's default) for it. Some functions
work on bytes and never re-encode (mb_strpos); each function's docs say
which.
Lower-casing uses PHP's own Unicode tables (unicode, generated from
unicode_data.h), with PHP's final sigma rule.
| PHP | Rust |
|---|---|
mb_strlen | mb_strlen, mb_strlen_in with Encoding ('8bit') |
mb_substr | mb_substr |
mb_strtolower | mb_strtolower |
mb_strpos | mb_strpos |
mb_check_encoding, mb_scrub, mb_convert_encoding($s, 'UTF-8') | mb_check_encoding, mb_scrub |
mb_strimwidth($s, 0, $width) | mb_strimwidth |
Every function here is checked against the real PHP function by
bin/compat fuzz php-std (operations mb.*).
Unicode data
mb/unicode.rs is generated by data/unicode.py from php-src's
ext/mbstring/unicode_data.h at tag php-8.5.10: the lower case table
(_uccase_lower_table, sorted by code point), _uccase_extra_table, and
the UC_CASED and UC_CASE_IGNORABLE ranges of _ucprop_ranges.
Regenerate it when PHP updates its Unicode version, and re-run
bin/compat fuzz php-std --op mb.strtolower.
Contents
2 submodules, and what the module defines itself.
use php_std::mb::Encoding;pub enum EncodingAn encoding mb_strlen accepts.
use php_std::mb::mb_check_encoding;pub fn mb_check_encoding(s: &[u8]) -> boolPHP mb_check_encoding($value, 'UTF-8') on a string: strict UTF-8
(no overlong forms, surrogates or code points above U+10FFFF).
use php_std::mb::mb_scrub;pub fn mb_scrub(s: &[u8]) -> Cow<'_, [u8]>PHP mb_scrub($string) and mb_convert_encoding($string, 'UTF-8')
from UTF-8: each invalid sequence becomes ?. Borrows valid input.
use php_std::mb::mb_strimwidth;pub fn mb_strimwidth(s: &[u8], width: usize) -> Cow<'_, [u8]>PHP mb_strimwidth($string, 0, $width) (no trim marker): the longest
leading part of the string that fits in width columns. A string that
fits is returned as it is, except that invalid sequences become ?, as
they do in a cut one.
use php_std::mb::mb_strimwidth;
assert_eq!(mb_strimwidth("你好".as_bytes(), 3).as_ref(), "你".as_bytes());
assert_eq!(mb_strimwidth(b"abc", 5).as_ref(), b"abc");
assert_eq!(mb_strimwidth(b"abc\xff\xfedef", 5).as_ref(), b"abc??");use php_std::mb::mb_strlen;pub fn mb_strlen(s: &[u8]) -> usizePHP mb_strlen($string): the number of characters; each invalid
sequence counts as one.
use php_std::mb::mb_strlen_in;pub fn mb_strlen_in(s: &[u8], encoding: Encoding) -> usizePHP mb_strlen($string, $encoding) for the encodings in Encoding.
use php_std::mb::mb_strpos;pub fn mb_strpos(haystack: &[u8], needle: &[u8], offset: i64) -> Result<Option<usize>, Error>PHP mb_strpos($haystack, $needle, $offset): character position of the
first occurrence at or after offset. Matches bytes; positions count
lead bytes. None is PHP's false.
use php_std::mb::mb_strtolower;pub fn mb_strtolower(s: &[u8]) -> Vec<u8>PHP mb_strtolower($string): full lower case with the final sigma rule
(MB_CASE_LOWER).
use php_std::mb::mb_substr;pub fn mb_substr(s: &[u8], start: i64, length: Option<i64>) -> Result<Vec<u8>, Error>PHP mb_substr($string, $start, $length): characters from start
(negative: from the end) for length characters (negative: stop that
many before the end; None: to the end). Re-encodes: invalid sequences
in the result become ?. PHP_INT_MIN for either argument is PHP's
ValueError.
mb::unicode
Own pageUnicode case data of PHP's mbstring, generated from php-src
ext/mbstring/unicode_data.h (tag php-8.5.10) by data/unicode.py.
Do not edit by hand.
The case table maps a code point to its simple mapping, or, when the value
is above 0xFFFFFF, to (length << 24) | index into EXTRA, where
EXTRA[index] is the simple mapping and the length code points after it
are the full (special casing) mapping.
mb::width
Own pageThe code points mbstring counts two columns wide (mb_strwidth,
mb_strimwidth): its East Asian Width table, as (first, last)
ranges.
Generated by dumping mb_strwidth(mb_chr($cp)) for every code point on
the dev image's PHP 8.5.10 (data/mb_width.php).