Page MenuHomePhabricator
Diviner libphutil Tech Docs PhutilUTF8StringTruncator

final class PhutilUTF8StringTruncator
libphutil Technical Documentation (Core Utilities)

Truncate a UTF-8 string to a some maximum number of bytes, codepoints, or glyphs.

This class takes some UTF-8 string as input, and emits a valid UTF-8 string which is no longer than given byte, codepoint, or glyph limits.

$short = id(new PhutilUTF8StringTruncator())
  ->setMaximumGlyphs(80)
  ->truncateString($long);

Byte limits restrict the number of bytes the result may contain. They are appropriate when you care about how much storage a string requires.

Codepoint limits restrict the number of codepoints the result may contain. Since codepoints may have up to 4 bytes, the resulting strings may require have more than this many bytes. This kind of limit is appropriate when you are using UTF-8 storage.

Glyph limits restrict the display size of the string. Because a single glyph may have an arbitrary number of combining characters, this does not impose a storage size limit on the string: a string with only one glyph may require an arbitrarily large number of bytes.

You can set more than one limit; the smallest limit will be used.

NOTE: This function makes a best effort to apply some reasonable rules but will not work well for the full range of unicode languages.

Methods

public function setMaximumBytes($maximum_bytes)

This method is not documented.
Parameters
$maximum_bytes
Return
wild

public function setMaximumCodepoints($maximum_codepoints)

This method is not documented.
Parameters
$maximum_codepoints
Return
wild

public function setMaximumGlyphs($maximum_glyphs)

This method is not documented.
Parameters
$maximum_glyphs
Return
wild

private function didUpdateMaxima()

This method is not documented.
Return
wild

public function setTerminator($terminator)

This method is not documented.
Parameters
$terminator
Return
wild

public function truncateString($string)

This method is not documented.
Parameters
$string
Return
wild