Skip to content

Commit

Permalink
[+]: add "UTF8::strlen_in_byte()"
Browse files Browse the repository at this point in the history
[+]: fix typo in "UTF8::strpos()"
  • Loading branch information
voku committed Oct 8, 2017
1 parent f678bdd commit 162de9c
Showing 1 changed file with 24 additions and 25 deletions.
49 changes: 24 additions & 25 deletions src/voku/helper/UTF8.php
Original file line number Diff line number Diff line change
Expand Up @@ -2545,16 +2545,11 @@ public static function is_utf8($str, $strict = false)
self::checkForSupport();
}

if (self::$SUPPORT['mbstring_func_overload'] === true) {
$len = \mb_strlen($str, '8BIT');
} else {
$len = \strlen($str);
}

if (self::$ORD === null) {
self::$ORD = self::getData('ord');
}

$len = self::strlen_in_byte($str);
/** @noinspection ForeachInvariantsInspection */
for ($i = 0; $i < $len; $i++) {
$in = self::$ORD[$str[$i]];
Expand Down Expand Up @@ -3730,11 +3725,7 @@ public static function split($str, $length = 1, $cleanUtf8 = false)
self::checkForSupport();
}

if (self::$SUPPORT['mbstring_func_overload'] === true) {
$len = \mb_strlen($str, '8BIT');
} else {
$len = strlen($str);
}
$len = self::strlen_in_byte($str);

/** @noinspection ForeachInvariantsInspection */
for ($i = 0; $i < $len; $i++) {
Expand Down Expand Up @@ -4895,6 +4886,24 @@ public static function strlen($str, $encoding = 'UTF-8', $cleanUtf8 = false)
return \mb_strlen($str, $encoding);
}

/**
* Get string length in byte.
*
* @param string $str
*
* @return int
*/
public static function strlen_in_byte($str)
{
if (self::$SUPPORT['mbstring_func_overload'] === true) {
$len = \mb_strlen($str, '8BIT');
} else {
$len = \strlen($str);
}

return $len;
}

/**
* Case insensitive string comparisons using a "natural order" algorithm.
*
Expand Down Expand Up @@ -5061,8 +5070,8 @@ public static function strpos($haystack, $needle, $offset = 0, $encoding = 'UTF-

if (
$encoding !== 'UTF-8'
&
self::$SUPPORT['iconv'] === true
&&
self::$SUPPORT['iconv'] === false
&&
self::$SUPPORT['mbstring'] === false
) {
Expand Down Expand Up @@ -6663,12 +6672,7 @@ public static function to_utf8($str, $decodeHtmlEntityToUtf8 = false)
self::checkForSupport();
}

if (self::$SUPPORT['mbstring_func_overload'] === true) {
$max = \mb_strlen($str, '8BIT');
} else {
$max = strlen($str);
}

$max = self::strlen_in_byte($str);
$buf = '';

/** @noinspection ForeachInvariantsInspection */
Expand Down Expand Up @@ -7249,12 +7253,7 @@ public static function utf8_decode($str, $keepUtf8Chars = false)

// save for later comparision
$str_backup = $str;

if (self::$SUPPORT['mbstring_func_overload'] === true) {
$len = \mb_strlen($str, '8BIT');
} else {
$len = \strlen($str);
}
$len = self::strlen_in_byte($str);

if (self::$ORD === null) {
self::$ORD = self::getData('ord');
Expand Down

0 comments on commit 162de9c

Please sign in to comment.