Drop checks for mbstring existence

Since mbstring is now a hard dependency enforced by composer, it is
no longer necessary to check for its presence and implement fallbacks.
This commit is contained in:
Adrien Crivelli 2017-02-16 11:53:52 +09:00
parent 1a52e23f08
commit 7b90bb9394
No known key found for this signature in database
GPG Key ID: B182FD79DC6DE92E
4 changed files with 73 additions and 239 deletions

View File

@ -672,37 +672,3 @@ class Functions
return $value;
}
}
//
// Strangely, PHP doesn't have a mb_str_replace multibyte function
// As we'll only ever use this function with UTF-8 characters, we can simply "hard-code" the character set
//
if ((!function_exists('mb_str_replace')) &&
(function_exists('mb_substr')) && (function_exists('mb_strlen')) && (function_exists('mb_strpos'))
) {
function mb_str_replace($search, $replace, $subject)
{
if (is_array($subject)) {
$ret = [];
foreach ($subject as $key => $val) {
$ret[$key] = mb_str_replace($search, $replace, $val);
}
return $ret;
}
foreach ((array) $search as $key => $s) {
if ($s == '' && $s !== 0) {
continue;
}
$r = !is_array($replace) ? $replace : (isset($replace[$key]) ? $replace[$key] : '');
$pos = mb_strpos($subject, $s, 0, 'UTF-8');
while ($pos !== false) {
$subject = mb_substr($subject, 0, $pos, 'UTF-8') . $r . mb_substr($subject, $pos + mb_strlen($s, 'UTF-8'), null, 'UTF-8');
$pos = mb_strpos($subject, $s, $pos + mb_strlen($r, 'UTF-8'), 'UTF-8');
}
}
return $subject;
}
}

View File

@ -124,19 +124,12 @@ class TextData
}
$character = $characters;
if ((function_exists('mb_strlen')) && (function_exists('mb_substr'))) {
if (mb_strlen($characters, 'UTF-8') > 1) {
$character = mb_substr($characters, 0, 1, 'UTF-8');
}
return self::unicodeToOrd($character);
}
if (strlen($characters) > 0) {
$character = substr($characters, 0, 1);
}
return ord($character);
}
/**
* CONCATENATE.
@ -225,11 +218,8 @@ class TextData
if (\PhpOffice\PhpSpreadsheet\Shared\StringHelper::countCharacters($needle) == 0) {
return $offset;
}
if (function_exists('mb_strpos')) {
$pos = mb_strpos($haystack, $needle, --$offset, 'UTF-8');
} else {
$pos = strpos($haystack, $needle, --$offset);
}
if ($pos !== false) {
return ++$pos;
}
@ -263,11 +253,8 @@ class TextData
if (\PhpOffice\PhpSpreadsheet\Shared\StringHelper::countCharacters($needle) == 0) {
return $offset;
}
if (function_exists('mb_stripos')) {
$pos = mb_stripos($haystack, $needle, --$offset, 'UTF-8');
} else {
$pos = stripos($haystack, $needle, --$offset);
}
if ($pos !== false) {
return ++$pos;
}
@ -330,13 +317,9 @@ class TextData
$value = ($value) ? \PhpOffice\PhpSpreadsheet\Calculation::getTRUE() : \PhpOffice\PhpSpreadsheet\Calculation::getFALSE();
}
if (function_exists('mb_substr')) {
return mb_substr($value, 0, $chars, 'UTF-8');
}
return substr($value, 0, $chars);
}
/**
* MID.
*
@ -363,11 +346,8 @@ class TextData
if (empty($chars)) {
return '';
}
if (function_exists('mb_substr')) {
return mb_substr($value, --$start, $chars, 'UTF-8');
}
return substr($value, --$start, $chars);
return mb_substr($value, --$start, $chars, 'UTF-8');
}
/**
@ -391,13 +371,9 @@ class TextData
$value = ($value) ? \PhpOffice\PhpSpreadsheet\Calculation::getTRUE() : \PhpOffice\PhpSpreadsheet\Calculation::getFALSE();
}
if ((function_exists('mb_substr')) && (function_exists('mb_strlen'))) {
return mb_substr($value, mb_strlen($value, 'UTF-8') - $chars, $chars, 'UTF-8');
}
return substr($value, strlen($value) - $chars);
}
/**
* STRINGLENGTH.
*
@ -413,13 +389,9 @@ class TextData
$value = ($value) ? \PhpOffice\PhpSpreadsheet\Calculation::getTRUE() : \PhpOffice\PhpSpreadsheet\Calculation::getFALSE();
}
if (function_exists('mb_strlen')) {
return mb_strlen($value, 'UTF-8');
}
return strlen($value);
}
/**
* LOWERCASE.
*
@ -521,30 +493,20 @@ class TextData
$instance = floor(Functions::flattenSingleValue($instance));
if ($instance == 0) {
if (function_exists('mb_str_replace')) {
return mb_str_replace($fromText, $toText, $text);
return \PhpOffice\PhpSpreadsheet\Shared\StringHelper::mbStrReplace($fromText, $toText, $text);
}
return str_replace($fromText, $toText, $text);
}
$pos = -1;
while ($instance > 0) {
if (function_exists('mb_strpos')) {
$pos = mb_strpos($text, $fromText, $pos + 1, 'UTF-8');
} else {
$pos = strpos($text, $fromText, $pos + 1);
}
if ($pos === false) {
break;
}
--$instance;
}
if ($pos !== false) {
if (function_exists('mb_strlen')) {
return self::REPLACE($text, ++$pos, mb_strlen($fromText, 'UTF-8'), $toText);
}
return self::REPLACE($text, ++$pos, strlen($fromText), $toText);
if ($pos !== false) {
return self::REPLACE($text, ++$pos, mb_strlen($fromText, 'UTF-8'), $toText);
}
return $text;

View File

@ -66,13 +66,6 @@ class StringHelper
*/
private static $currencyCode;
/**
* Is mbstring extension avalable?
*
* @var bool
*/
private static $isMbstringEnabled;
/**
* Is iconv extension avalable?
*
@ -259,23 +252,6 @@ class StringHelper
];
}
/**
* Get whether mbstring extension is available.
*
* @return bool
*/
public static function getIsMbstringEnabled()
{
if (isset(self::$isMbstringEnabled)) {
return self::$isMbstringEnabled;
}
self::$isMbstringEnabled = function_exists('mb_convert_encoding') ?
true : false;
return self::$isMbstringEnabled;
}
/**
* Get whether iconv extension is available.
*
@ -387,16 +363,11 @@ class StringHelper
return $value;
}
if (self::getIsMbstringEnabled()) {
$value = mb_convert_encoding($value, 'UTF-8', 'UTF-8');
return $value;
}
// else, no conversion
return $value;
}
/**
* Check if a string contains UTF8 data.
*
@ -444,9 +415,7 @@ class StringHelper
$ln = self::countCharacters($value, 'UTF-8');
// option flags
if (empty($arrcRuns)) {
$opt = (self::getIsIconvEnabled() || self::getIsMbstringEnabled()) ?
0x0001 : 0x0000;
$data = pack('CC', $ln, $opt);
$data = pack('CC', $ln, 0x0001);
// characters
$data .= self::convertEncoding($value, 'UTF-16LE', 'UTF-8');
} else {
@ -479,20 +448,16 @@ class StringHelper
// character count
$ln = self::countCharacters($value, 'UTF-8');
// option flags
$opt = (self::getIsIconvEnabled() || self::getIsMbstringEnabled()) ?
0x0001 : 0x0000;
// characters
$chars = self::convertEncoding($value, 'UTF-16LE', 'UTF-8');
$data = pack('vC', $ln, $opt) . $chars;
$data = pack('vC', $ln, 0x0001) . $chars;
return $data;
}
/**
* Convert string from one encoding to another. First try mbstring, then iconv, finally strlen.
* Convert string from one encoding to another.
*
* @param string $value
* @param string $to Encoding to convert to, e.g. 'UTF-8'
@ -506,68 +471,11 @@ class StringHelper
return iconv($from, $to . '//IGNORE//TRANSLIT', $value);
}
if (self::getIsMbstringEnabled()) {
return mb_convert_encoding($value, $to, $from);
}
if ($from == 'UTF-16LE') {
return self::utf16Decode($value, false);
} elseif ($from == 'UTF-16BE') {
return self::utf16Decode($value);
}
// else, no conversion
return $value;
}
/**
* Decode UTF-16 encoded strings.
*
* Can handle both BOM'ed data and un-BOM'ed data.
* Assumes Big-Endian byte order if no BOM is available.
* This function was taken from http://php.net/manual/en/function.utf8-decode.php
* and $bom_be parameter added.
*
* @param string $str uTF-16 encoded data to decode
* @param mixed $bom_be
*
* @return string uTF-8 / ISO encoded data
*
* @version 0.2 / 2010-05-13
*
* @author Rasmus Andersson {@link http://rasmusandersson.se/}
* @author vadik56
*/
public static function utf16Decode($str, $bom_be = true)
{
if (strlen($str) < 2) {
return $str;
}
$c0 = ord($str[0]);
$c1 = ord($str[1]);
if ($c0 == 0xfe && $c1 == 0xff) {
$str = substr($str, 2);
} elseif ($c0 == 0xff && $c1 == 0xfe) {
$str = substr($str, 2);
$bom_be = false;
}
$len = strlen($str);
$newstr = '';
for ($i = 0; $i < $len; $i += 2) {
if ($bom_be) {
$val = ord($str[$i]) << 4;
$val += ord($str[$i + 1]);
} else {
$val = ord($str[$i + 1]) << 4;
$val += ord($str[$i]);
}
$newstr .= ($val == 0x228) ? "\n" : chr($val);
}
return $newstr;
}
/**
* Get character count. First try mbstring, then iconv, finally strlen.
* Get character count.
*
* @param string $value
* @param string $enc Encoding
@ -576,20 +484,11 @@ class StringHelper
*/
public static function countCharacters($value, $enc = 'UTF-8')
{
if (self::getIsMbstringEnabled()) {
return mb_strlen($value, $enc);
}
if (self::getIsIconvEnabled()) {
return iconv_strlen($value, $enc);
}
// else strlen
return strlen($value);
}
/**
* Get a substring of a UTF-8 encoded string. First try mbstring, then iconv, finally strlen.
* Get a substring of a UTF-8 encoded string.
*
* @param string $pValue UTF-8 encoded string
* @param int $pStart Start offset
@ -599,18 +498,9 @@ class StringHelper
*/
public static function substring($pValue = '', $pStart = 0, $pLength = 0)
{
if (self::getIsMbstringEnabled()) {
return mb_substr($pValue, $pStart, $pLength, 'UTF-8');
}
if (self::getIsIconvEnabled()) {
return iconv_substr($pValue, $pStart, $pLength, 'UTF-8');
}
// else substr
return substr($pValue, $pStart, $pLength);
}
/**
* Convert a UTF-8 encoded string to upper case.
*
@ -620,13 +510,9 @@ class StringHelper
*/
public static function strToUpper($pValue = '')
{
if (function_exists('mb_convert_case')) {
return mb_convert_case($pValue, MB_CASE_UPPER, 'UTF-8');
}
return strtoupper($pValue);
}
/**
* Convert a UTF-8 encoded string to lower case.
*
@ -636,13 +522,9 @@ class StringHelper
*/
public static function strToLower($pValue = '')
{
if (function_exists('mb_convert_case')) {
return mb_convert_case($pValue, MB_CASE_LOWER, 'UTF-8');
}
return strtolower($pValue);
}
/**
* Convert a UTF-8 encoded string to title/proper case
* (uppercase every first character in each word, lower case all other characters).
@ -653,13 +535,9 @@ class StringHelper
*/
public static function strToTitle($pValue = '')
{
if (function_exists('mb_convert_case')) {
return mb_convert_case($pValue, MB_CASE_TITLE, 'UTF-8');
}
return ucwords($pValue);
}
public static function mbIsUpper($char)
{
return mb_strtolower($char, 'UTF-8') != $char;
@ -672,6 +550,44 @@ class StringHelper
return preg_split('/(?<!^)(?!$)/u', $string);
}
/**
* Replace into multi-bytes string.
*
* Strangely, PHP doesn't have a mb_str_replace multibyte function
* As we'll only ever use this function with UTF-8 characters, we can simply "hard-code" the character set
*
* @param string|string[] $search
* @param string|string[] $replace
* @param string $subject
*
* @return string
*/
public static function mbStrReplace($search, $replace, $subject)
{
if (is_array($subject)) {
$ret = [];
foreach ($subject as $key => $val) {
$ret[$key] = self::mbStrReplace($search, $replace, $val);
}
return $ret;
}
foreach ((array) $search as $key => $s) {
if ($s == '' && $s !== 0) {
continue;
}
$r = !is_array($replace) ? $replace : (isset($replace[$key]) ? $replace[$key] : '');
$pos = mb_strpos($subject, $s, 0, 'UTF-8');
while ($pos !== false) {
$subject = mb_substr($subject, 0, $pos, 'UTF-8') . $r . mb_substr($subject, $pos + mb_strlen($s, 'UTF-8'), null, 'UTF-8');
$pos = mb_strpos($subject, $s, $pos + mb_strlen($r, 'UTF-8'), 'UTF-8');
}
}
return $subject;
}
/**
* Reverse the case of a string, so that all uppercase characters become lowercase
* and all lowercase characters become uppercase.
@ -682,7 +598,6 @@ class StringHelper
*/
public static function strCaseReverse($pValue = '')
{
if (self::getIsMbstringEnabled()) {
$characters = self::mbStrSplit($pValue);
foreach ($characters as &$character) {
if (self::mbIsUpper($character)) {
@ -695,9 +610,6 @@ class StringHelper
return implode('', $characters);
}
return strtolower($pValue) ^ strtoupper($pValue) ^ $pValue;
}
/**
* Identify whether a string contains a fractional numeric value,
* and convert it to a numeric if it is.

View File

@ -14,12 +14,6 @@ class StringTest extends \PHPUnit_Framework_TestCase
StringHelper::setCurrencyCode(null);
}
public function testGetIsMbStringEnabled()
{
$result = StringHelper::getIsMbstringEnabled();
$this->assertTrue($result);
}
public function testGetIsIconvEnabled()
{
$result = StringHelper::getIsIconvEnabled();