Changeset View
Changeset View
Standalone View
Standalone View
src/utils/PhutilUTF8StringTruncator.php
Context not available. | |||||
public function setTerminator($terminator) { | public function setTerminator($terminator) { | ||||
$this->terminator = $terminator; | $this->terminator = $terminator; | ||||
$this->terminatorBytes = strlen($terminator); | $this->terminatorBytes = @strlen($terminator); | ||||
$this->terminatorCodepoints = count(phutil_utf8v($terminator)); | $this->terminatorCodepoints = count(phutil_utf8v($terminator)); | ||||
$this->terminatorGlyphs = count(phutil_utf8v_combined($terminator)); | $this->terminatorGlyphs = count(phutil_utf8v_combined($terminator)); | ||||
return $this; | return $this; | ||||
Context not available. | |||||
// First, check if the string has fewer bytes than the most restrictive | // First, check if the string has fewer bytes than the most restrictive | ||||
// limit. Codepoints and glyphs always take up at least one byte, so we can | // limit. Codepoints and glyphs always take up at least one byte, so we can | ||||
// just return the string unmodified if we're under all of the limits. | // just return the string unmodified if we're under all of the limits. | ||||
$byte_len = strlen($string); | $byte_len = @strlen($string); | ||||
if ($byte_len <= $this->minimumLimit) { | if ($byte_len <= $this->minimumLimit) { | ||||
return $string; | return $string; | ||||
} | } | ||||
Context not available. | |||||
} else { | } else { | ||||
$bytes = $this->terminatorBytes; | $bytes = $this->terminatorBytes; | ||||
for ($ii = 0; $ii < $glyph_len; $ii++) { | for ($ii = 0; $ii < $glyph_len; $ii++) { | ||||
$bytes += strlen($string_gv[$ii]); | $bytes += @strlen($string_gv[$ii]); | ||||
if ($bytes > $this->maximumBytes) { | if ($bytes > $this->maximumBytes) { | ||||
$cutoff = $ii; | $cutoff = $ii; | ||||
break; | break; | ||||
Context not available. | |||||
} else { | } else { | ||||
$points = 0; | $points = 0; | ||||
for ($ii = 0; $ii < $glyph_len; $ii++) { | for ($ii = 0; $ii < $glyph_len; $ii++) { | ||||
$glyph_bytes = strlen($string_gv[$ii]); | $glyph_bytes = @strlen($string_gv[$ii]); | ||||
while ($points < $point_len) { | while ($points < $point_len) { | ||||
$glyph_bytes -= strlen($string_pv[$points]); | $glyph_bytes -= @strlen($string_pv[$points]); | ||||
$points++; | $points++; | ||||
if ($glyph_bytes <= 0) { | if ($glyph_bytes <= 0) { | ||||
break; | break; | ||||
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