#65342 closed enhancement (fixed)
Charset: Polyfill mb_ord() and mb_chr() for UTF-8
| Reported by: | dmsnell | Owned by: | dmsnell |
|---|---|---|---|
| Priority: | normal | Milestone: | 7.1 |
| Component: | Charset | Version: | |
| Severity: | normal | Keywords: | has-patch has-unit-tests |
| Cc: | Focuses: |
Description
These functions are useful primitives but missing when the mbstring extension isn’t available. This patch adds polyfills for those few environments where this is the case so that WordPress code can unconditionally call them.
Only UTF-8 is supported for practical reasons. Code in unsupported environments should convert first to UTF-8 and then call these functions. See also #62172
This work is part of #31992
Change History (11)
This ticket was mentioned in PR #11965 on WordPress/wordpress-develop by @dmsnell.
5 months ago
#1
- Keywords has-patch has-unit-tests added
This ticket was mentioned in PR #12020 on WordPress/wordpress-develop by @westonruter.
5 months ago
#4
This is a follow-up to https://github.com/WordPress/wordpress-develop/pull/11965.
There is a PHPStan rule level 0 error introduced in that PR:
------ -----------------------------------------------------------------------------
Line src/wp-includes/compat.php
------ -----------------------------------------------------------------------------
227 Function _mb_ord() should return int|false but return statement is missing.
🪪 return.missing
at src/wp-includes/compat.php:227
------ -----------------------------------------------------------------------------
While it doesn't seem that $byte_length can ever be anything other than int<1, 4> when 1 !== $found_count, this is not picked up by PHPStan, leading ot the error.
An alternative to this would be to declare the type:
-
src/wp-includes/compat.php
diff --git a/src/wp-includes/compat.php b/src/wp-includes/compat.php index 5eb467280a..d67cea2d0c 100644
a b function _mb_ord( $string, $encoding = null ) { 221 221 return false; 222 222 } 223 223 224 /** @var int<1, 4> $byte_length */ 225 224 226 // These are valid code points, so no further validation is required. 225 227 $b0 = ord( $string[0] );
But this seems less clean.
Trac ticket: https://core.trac.wordpress.org/ticket/65342
## Use of AI Tools
None
#5
@
5 months ago
Follow-up with PHPStan fix: https://github.com/WordPress/wordpress-develop/pull/12020
@dmsnell commented on PR #12020:
5 months ago
#6
Thanks @westonruter — the inline comment is probably better semantically but the return false would potentially make up for any regressions that would creep into _wp_scan_utf8(), as unlikely as those are.
@westonruter commented on PR #12020:
5 months ago
#8
#9
@
3 months ago
@dmsnell @westonruter: native mb_chr() and mb_ord() accept alternative UTF-8 names such as utf-8 and utf8. Polyfills require exact UTF-8, returning false for these valid names. Should both checks use _is_utf8_charset()? We may also want to add tests for those.
#10
@
3 months ago
@tyxla it’s probably fine if you want to do that. my thinking at the time was about normalizing UTF-8 and removing the confusion from supporting different labels. I believe that all existing uses in Core are either called without the charset parameter or the string literal UTF-8.
Did you encounter this as a bug anywhere?
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)
Trac ticket: Core-65342
See also Core-62172
Preparation for #11567
These functions are useful primitives but missing when the mbstring extension isn’t available. This patch adds polyfills for those few environments where this is the case so that WordPress code can unconditionally call them.