Changeset View
Changeset View
Standalone View
Standalone View
omnitools/src/Strings/Multibyte/OmniString.php
| <?php | <?php | ||||
| declare(strict_types=1); | declare(strict_types=1); | ||||
| namespace Keruald\OmniTools\Strings\Multibyte; | namespace Keruald\OmniTools\Strings\Multibyte; | ||||
| use Keruald\OmniTools\Collections\Vector; | use Keruald\OmniTools\Collections\Vector; | ||||
| /** | |||||
| * Represents a multibyte string and perform operations with the grapheme | |||||
| * library for UTF-8 encoding, and mbstring for other encodings. | |||||
| */ | |||||
| class OmniString { | class OmniString { | ||||
| use WithEncoding; | use WithEncoding; | ||||
| /// | /// | ||||
| /// Private members | /// Private members | ||||
| /// | /// | ||||
| /** | private string $value; | ||||
| * @var string | |||||
| */ | |||||
| private $value; | |||||
| /// | /// | ||||
| /// Constructor | /// Constructor | ||||
| /// | /// | ||||
| public function __construct (string $value = '', string $encoding = '') { | public function __construct (string $value = '', string $encoding = '') { | ||||
| $this->value = $value; | $this->value = $value; | ||||
| $this->setEncoding($encoding ?: "UTF-8"); | $this->setEncoding($encoding ?: "UTF-8"); | ||||
| Show All 28 Lines | class OmniString { | ||||
| public function startsWith (string $start) : bool { | public function startsWith (string $start) : bool { | ||||
| return str_starts_with($this->value, $start); | return str_starts_with($this->value, $start); | ||||
| } | } | ||||
| public function endsWith (string $end) : bool { | public function endsWith (string $end) : bool { | ||||
| return str_ends_with($this->value, $end); | return str_ends_with($this->value, $end); | ||||
| } | } | ||||
| /** | |||||
| * @deprecated Use more specific method to express your intent: | |||||
| * countBytes, countCodePoints or countGraphemes | |||||
| */ | |||||
| public function len () : int { | public function len () : int { | ||||
| return $this->countGraphemes(); | |||||
| } | |||||
| public function countBytes () : int { | |||||
| return strlen($this->value); | |||||
| } | |||||
| public function countCodePoints () : int { | |||||
| return mb_strlen($this->value, $this->encoding); | return mb_strlen($this->value, $this->encoding); | ||||
| } | } | ||||
| public function getChars () : array { | public function countGraphemes () : int { | ||||
| return match ($this->encoding) { | |||||
| "UTF-8" => grapheme_strlen($this->value), | |||||
| default => $this->countCodepoints(), | |||||
| }; | |||||
| } | |||||
| public function getBytes() : array { | |||||
| return str_split($this->value, 1); | |||||
| } | |||||
| public function getCodePoints () : array { | |||||
| return mb_str_split($this->value, 1, $this->encoding); | |||||
| } | |||||
| public function getGraphemes () : array { | |||||
| if ($this->encoding !== "UTF-8") { | |||||
| return $this->getCodePoints(); | |||||
| } | |||||
| $chars = []; | $chars = []; | ||||
| $len = $this->len(); | $len = grapheme_strlen($this->value); | ||||
| for ($i = 0 ; $i < $len ; $i++) { | for ($i = 0 ; $i < $len ; $i++) { | ||||
| $chars[] = mb_substr($this->value, $i, 1, $this->encoding); | $chars[] = grapheme_substr($this->value, $i, 1); | ||||
| } | } | ||||
| return $chars; | return $chars; | ||||
| } | } | ||||
| /** | |||||
| * @deprecated Use more specific method to express your intent: | |||||
| * getBytes, getCodePoints or getGraphemes | |||||
| */ | |||||
| public function getChars () : array { | |||||
| return $this->getGraphemes(); | |||||
| } | |||||
| public function getBigrams () : array { | public function getBigrams () : array { | ||||
| return match ($this->encoding) { | |||||
| "UTF-8" => $this->getBigramsFromGraphemes(), | |||||
| default => $this->getBigramsFromCodePoints(), | |||||
| }; | |||||
| } | |||||
| private function getBigramsFromGraphemes() : array { | |||||
| $bigrams = []; | |||||
| $len = grapheme_strlen($this->value); | |||||
| for ($i = 0 ; $i < $len - 1 ; $i++) { | |||||
| $bigrams[] = grapheme_substr($this->value, $i, 2); | |||||
| } | |||||
| return $bigrams; | |||||
| } | |||||
| private function getBigramsFromCodePoints() : array { | |||||
| $bigrams = []; | $bigrams = []; | ||||
| $len = $this->len(); | $len = mb_strlen($this->value, $this->encoding); | ||||
| for ($i = 0 ; $i < $len - 1 ; $i++) { | for ($i = 0 ; $i < $len - 1 ; $i++) { | ||||
| $bigrams[] = mb_substr($this->value, $i, 2, $this->encoding); | $bigrams[] = mb_substr($this->value, $i, 2, $this->encoding); | ||||
| } | } | ||||
| return $bigrams; | return $bigrams; | ||||
| } | } | ||||
| /// | /// | ||||
| Show All 35 Lines | |||||
Nasqueron DevCentral · If it had been much bigger the moon would have had a core of ice. · Powered by Phabricator