Changeset View
Changeset View
Standalone View
Standalone View
src/utils/utf8.php
Show First 20 Lines • Show All 308 Lines • ▼ Show 20 Lines | |||||
* NOTE: We currently assume width 1 for East-Asian ambiguous characters. | * NOTE: We currently assume width 1 for East-Asian ambiguous characters. | ||||
* | * | ||||
* NOTE: This function is VERY slow. | * NOTE: This function is VERY slow. | ||||
* | * | ||||
* @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_console_strlen($string) { | ||||
$string = phutil_string_cast($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); | ||||
} | } | ||||
▲ Show 20 Lines • Show All 660 Lines • Show Last 20 Lines |
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