Changeset View
Changeset View
Standalone View
Standalone View
src/utils/utf8.php
Context not available. | |||||
$offset = 0; | $offset = 0; | ||||
$matches = null; | $matches = null; | ||||
while (preg_match($regex, $string, $matches, 0, $offset)) { | while (@preg_match($regex, $string, $matches, 0, $offset)) { | ||||
if (!isset($matches[2])) { | if (!isset($matches[2])) { | ||||
$match = $matches[1]; | $match = $matches[1]; | ||||
Context not available. | |||||
+ ((ord($match[1]) & 0x3F) << 6) | + ((ord($match[1]) & 0x3F) << 6) | ||||
+ ((ord($match[2]) & 0x3F)); | + ((ord($match[2]) & 0x3F)); | ||||
if ($codepoint >= 0xD800 && $codepoint <= 0xDFFF) { | if ($codepoint >= 0xD800 && $codepoint <= 0xDFFF) { | ||||
$result[] = str_repeat($replacement, strlen($match)); | $result[] = str_repeat($replacement, @strlen($match)); | ||||
$offset += strlen($matches[0]); | $offset += @strlen($matches[0]); | ||||
continue; | continue; | ||||
} | } | ||||
} | } | ||||
Context not available. | |||||
$result[] = $replacement; | $result[] = $replacement; | ||||
} | } | ||||
$offset += strlen($matches[0]); | $offset += @strlen($matches[0]); | ||||
} | } | ||||
return implode('', $result); | return implode('', $result); | ||||
Context not available. | |||||
function phutil_is_utf8_slowly($string, $only_bmp = false) { | function phutil_is_utf8_slowly($string, $only_bmp = false) { | ||||
// First, check the common case of normal ASCII strings. We're fine if | // First, check the common case of normal ASCII strings. We're fine if | ||||
// the string contains no bytes larger than 127. | // the string contains no bytes larger than 127. | ||||
if (preg_match('/^[\x01-\x7F]+\z/', $string)) { | if (@preg_match('/^[\x01-\x7F]+\z/', $string)) { | ||||
return true; | return true; | ||||
} | } | ||||
Context not available. | |||||
// input limit is extremely low (less than 50KB on my system), do this check | // input limit is extremely low (less than 50KB on my system), do this check | ||||
// very very slowly in PHP instead. See also T5316. | // very very slowly in PHP instead. See also T5316. | ||||
$len = strlen($string); | $len = @strlen($string); | ||||
for ($ii = 0; $ii < $len; $ii++) { | for ($ii = 0; $ii < $len; $ii++) { | ||||
$chr = ord($string[$ii]); | $chr = ord($string[$ii]); | ||||
if ($chr >= 0x01 && $chr <= 0x7F) { | if ($chr >= 0x01 && $chr <= 0x7F) { | ||||
Context not available. | |||||
* @param string A valid utf-8 string. | * @param string A valid utf-8 string. | ||||
* @return int The character length of the string. | * @return int The character length of the string. | ||||
*/ | */ | ||||
function phutil_utf8_strlen($string) { | function phutil_utf8strlen($string) { | ||||
if (function_exists('utf8_decode')) { | if (function_exists('utf8_decode')) { | ||||
return strlen(utf8_decode($string)); | return @strlen(utf8_decode($string)); | ||||
} | } | ||||
return count(phutil_utf8v($string)); | return count(phutil_utf8v($string)); | ||||
} | } | ||||
Context not available. | |||||
* @param string A valid UTF-8 string. | * @param string A valid UTF-8 string. | ||||
* @return int The console display length of the string. | * @return int The console display length of the string. | ||||
*/ | */ | ||||
function phutil_utf8_console_strlen($string) { | function phutil_utf8_consolestrlen($string) { | ||||
// Formatting and colors don't contribute any width in the console. | // Formatting and colors don't contribute any width in the console. | ||||
$string = preg_replace("/\x1B\[\d*m/", '', $string); | $string = preg_replace("/\x1B\[\d*m/", '', $string); | ||||
// In the common case of an ASCII string, just return the string length. | // In the common case of an ASCII string, just return the string length. | ||||
if (preg_match('/^[\x01-\x7F]*\z/', $string)) { | if (@preg_match('/^[\x01-\x7F]*\z/', $string)) { | ||||
return strlen($string); | return @strlen($string); | ||||
} | } | ||||
$len = 0; | $len = 0; | ||||
Context not available. | |||||
$string = phutil_string_cast($string); | $string = phutil_string_cast($string); | ||||
$res = array(); | $res = array(); | ||||
$len = strlen($string); | $len = @strlen($string); | ||||
$ii = 0; | $ii = 0; | ||||
while ($ii < $len) { | while ($ii < $len) { | ||||
Context not available. | |||||
pht('Invalid UTF-8 string passed to %s.', __FUNCTION__)); | pht('Invalid UTF-8 string passed to %s.', __FUNCTION__)); | ||||
} | } | ||||
} | } | ||||
$res[] = substr($string, $ii, $seq_len); | $res[] = @substr($string, $ii, $seq_len); | ||||
$ii += $seq_len; | $ii += $seq_len; | ||||
if ($byte_limit && ($ii >= $byte_limit)) { | if ($byte_limit && ($ii >= $byte_limit)) { | ||||
Context not available. | |||||
} | } | ||||
} | } | ||||
if (strlen($string)) { | if (@strlen($string)) { | ||||
$result[] = $string; | $result[] = $string; | ||||
} | } | ||||
Context not available. | |||||
} | } | ||||
} | } | ||||
if (strlen($buffer)) { | if (@strlen($buffer)) { | ||||
$result[] = $buffer; | $result[] = $buffer; | ||||
} | } | ||||
} | } | ||||
Context not available. |
Content licensed under Creative Commons Attribution-ShareAlike 4.0 (CC-BY-SA) unless otherwise noted; code licensed under Apache 2.0 or other open source licenses. · CC BY-SA 4.0 · Apache 2.0