Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Tests/StringHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,14 @@ public static function seedCompliantStrings(): array
["\xFF ABC", false],
["0xfffd ABC", true],
['', true],
'truncated 2-byte sequence at the end' => ["abc\xC3", false],
'truncated 3-byte sequence at the end' => ["abc\xE2\x82", false],
'truncated 4-byte sequence at the end' => ["abc\xF0\x9F\x98", false],
'truncated 5-byte sequence at the end' => ["\xDF\xA3\xFB\x88", false],
'overlong encoding' => ["\xC0\xAF", false],
'surrogate' => ["\xED\xA0\x80", false],
'code point above U+10FFFF' => ["\xF4\x90\x80\x80", false],
'complete 4-byte sequence' => ["\xF0\x9F\x98\x80", true],
];
}

Expand Down
7 changes: 7 additions & 0 deletions docs/overview.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# String package

TODO

## `StringHelper::valid()`

`StringHelper::valid($str)` returns whether `$str` is valid UTF-8, using `mb_check_encoding($str, 'UTF-8')`.
Overlong forms, UTF-16 surrogates, code points above U+10FFFF, five- and six-byte sequences and
strings that end in an incomplete multi-byte sequence are rejected. Earlier releases reported a string
that ends in an incomplete sequence, such as `"abc\xC3"`, as valid.
118 changes: 2 additions & 116 deletions src/StringHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@
*/
public static function str_pad($input, $length, $padStr = ' ', $type = STR_PAD_RIGHT)
{
return mb_str_pad($input, $length, $padStr, $type);

Check failure on line 333 in src/StringHelper.php

View workflow job for this annotation

GitHub Actions / framework-ci / Run PHPstan

Function mb_str_pad not found.
}

/**
Expand Down Expand Up @@ -605,10 +605,10 @@
}

if ($charlist === false) {
return mb_ltrim($str);

Check failure on line 608 in src/StringHelper.php

View workflow job for this annotation

GitHub Actions / framework-ci / Run PHPstan

Function mb_ltrim not found.
}

return mb_ltrim($str, $charlist);

Check failure on line 611 in src/StringHelper.php

View workflow job for this annotation

GitHub Actions / framework-ci / Run PHPstan

Function mb_ltrim not found.
}

/**
Expand All @@ -632,10 +632,10 @@
}

if ($charlist === false) {
return mb_rtrim($str);

Check failure on line 635 in src/StringHelper.php

View workflow job for this annotation

GitHub Actions / framework-ci / Run PHPstan

Function mb_rtrim not found.
}

return mb_rtrim($str, $charlist);

Check failure on line 638 in src/StringHelper.php

View workflow job for this annotation

GitHub Actions / framework-ci / Run PHPstan

Function mb_rtrim not found.
}

/**
Expand All @@ -659,10 +659,10 @@
}

if ($charlist === false) {
return mb_trim($str);

Check failure on line 662 in src/StringHelper.php

View workflow job for this annotation

GitHub Actions / framework-ci / Run PHPstan

Function mb_trim not found.
}

return mb_trim($str, $charlist);

Check failure on line 665 in src/StringHelper.php

View workflow job for this annotation

GitHub Actions / framework-ci / Run PHPstan

Function mb_trim not found.
}

/**
Expand All @@ -684,14 +684,14 @@
public static function ucfirst($str, $delimiter = null, $newDelimiter = null)
{
if ($delimiter === null) {
return mb_ucfirst($str);

Check failure on line 687 in src/StringHelper.php

View workflow job for this annotation

GitHub Actions / framework-ci / Run PHPstan

Function mb_ucfirst not found.
}

if ($newDelimiter === null) {
$newDelimiter = $delimiter;
}

return implode($newDelimiter, array_map('mb_ucfirst', explode($delimiter, $str)));

Check failure on line 694 in src/StringHelper.php

View workflow job for this annotation

GitHub Actions / framework-ci / Run PHPstan

Parameter #1 $callback of function array_map expects (callable(string): mixed)|null, 'mb_ucfirst' given.
}

/**
Expand Down Expand Up @@ -749,132 +749,18 @@
/**
* Tests a string as to whether it's valid UTF-8 and supported by the Unicode standard.
*
* Note: this function has been modified to simple return true or false.
* Overlong forms, surrogates, code points above U+10FFFF and incomplete multi-byte sequences are rejected.
*
* @param string $str UTF-8 encoded string.
*
* @return boolean true if valid
*
* @author <hsivonen@iki.fi>
* @link https://hsivonen.fi/php-utf8/
* @see compliant
* @since 1.3.0
*/
public static function valid($str)
{
$mState = 0; // cached expected number of octets after the current octet
// until the beginning of the next UTF8 character sequence
$mUcs4 = 0; // cached Unicode character
$mBytes = 1; // cached expected number of octets in the current sequence

$len = strlen($str);

for ($i = 0; $i < $len; $i++) {
/*
* Joomla modification - As of PHP 7.4, curly brace access has been deprecated. As a result the line below has
* been modified to use square brace syntax
* See https://github.com/php/php-src/commit/d574df63dc375f5fc9202ce5afde23f866b6450a
* for additional references
*/
$in = ord($str[$i]);

if ($mState == 0) {
// When mState is zero we expect either a US-ASCII character or a
// multi-octet sequence.
if (0 == (0x80 & ($in))) {
// US-ASCII, pass straight through.
$mBytes = 1;
} elseif (0xC0 == (0xE0 & ($in))) {
// First octet of 2 octet sequence
$mUcs4 = ($in);
$mUcs4 = ($mUcs4 & 0x1F) << 6;
$mState = 1;
$mBytes = 2;
} elseif (0xE0 == (0xF0 & ($in))) {
// First octet of 3 octet sequence
$mUcs4 = ($in);
$mUcs4 = ($mUcs4 & 0x0F) << 12;
$mState = 2;
$mBytes = 3;
} elseif (0xF0 == (0xF8 & ($in))) {
// First octet of 4 octet sequence
$mUcs4 = ($in);
$mUcs4 = ($mUcs4 & 0x07) << 18;
$mState = 3;
$mBytes = 4;
} elseif (0xF8 == (0xFC & ($in))) {
/* First octet of 5 octet sequence.
*
* This is illegal because the encoded codepoint must be either
* (a) not the shortest form or
* (b) outside the Unicode range of 0-0x10FFFF.
* Rather than trying to resynchronize, we will carry on until the end
* of the sequence and let the later error handling code catch it.
*/
$mUcs4 = ($in);
$mUcs4 = ($mUcs4 & 0x03) << 24;
$mState = 4;
$mBytes = 5;
} elseif (0xFC == (0xFE & ($in))) {
// First octet of 6 octet sequence, see comments for 5 octet sequence.
$mUcs4 = ($in);
$mUcs4 = ($mUcs4 & 1) << 30;
$mState = 5;
$mBytes = 6;
} else {
/* Current octet is neither in the US-ASCII range nor a legal first
* octet of a multi-octet sequence.
*/
return false;
}
} else {
// When mState is non-zero, we expect a continuation of the multi-octet
// sequence
if (0x80 == (0xC0 & ($in))) {
// Legal continuation.
$shift = ($mState - 1) * 6;
$tmp = $in;
$tmp = ($tmp & 0x0000003F) << $shift;
$mUcs4 |= $tmp;

/**
* End of the multi-octet sequence. mUcs4 now contains the final
* Unicode codepoint to be output
*/
if (0 == --$mState) {
/*
* Check for illegal sequences and codepoints.
*/
// From Unicode 3.1, non-shortest form is illegal
if (
((2 == $mBytes) && ($mUcs4 < 0x0080)) ||
((3 == $mBytes) && ($mUcs4 < 0x0800)) ||
((4 == $mBytes) && ($mUcs4 < 0x10000)) ||
(4 < $mBytes) ||
// From Unicode 3.2, surrogate characters are illegal
(($mUcs4 & 0xFFFFF800) == 0xD800) ||
// Codepoints outside the Unicode range are illegal
($mUcs4 > 0x10FFFF)
) {
return false;
}

//initialize UTF8 cache
$mState = 0;
$mUcs4 = 0;
$mBytes = 1;
}
} else {
/**
*((0xC0 & (*in) != 0x80) && (mState != 0))
* Incomplete multi-octet sequence.
*/

return false;
}
}
}
return true;
return mb_check_encoding((string) $str, 'UTF-8');
}

/**
Expand Down
Loading