Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
strategy:
fail-fast: true
matrix:
php: [7.4, 8.0, 8.1, 8.2, 8.3, 8.4]
php: [7.4, 8.0, 8.1, 8.2, 8.3, 8.4, 8.5]

name: PHP ${{ matrix.php }}

Expand All @@ -32,4 +32,4 @@ jobs:
run: composer update --no-interaction --no-progress

- name: Execute tests
run: vendor/bin/phpunit --verbose
run: composer run tests
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
CHANGELOG
-------------

### v2.1.0, 2025.10.17

* Added static `getStringLength()`

### v2.0.0, 2021.04.13

* The library was fully refactored
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ The full documentation for this library can be found [here][documentation].

## Installation

**Opis String** is available on [Packagist] and it can be installed from a
**Opis String** is available on [Packagist], and it can be installed from a
command line interface by using [Composer].

```bash
Expand All @@ -40,7 +40,7 @@ Or you could directly reference it into your `composer.json` file as a dependenc
```json
{
"require": {
"opis/string": "^2.0"
"opis/string": "^2.1"
}
}
```
Expand Down
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
"Opis\\String\\Test\\": "tests/"
}
},
"scripts": {
"tests": "./vendor/bin/phpunit --verbose --color"
},
"extra": {
"branch-alias": {
"dev-master": "2.x-dev"
Expand Down
105 changes: 105 additions & 0 deletions src/UnicodeString.php
Original file line number Diff line number Diff line change
Expand Up @@ -1274,6 +1274,111 @@ public static function walkString(string $str): iterable
}
}

/**
* Compute string length
* @param string $str
* @return int
* @throws InvalidStringException
*/
public static function getStringLength(string $str): int
{
$count = 0;
$length = strlen($str);

$i = 0;
while ($i < $length) {
$ord0 = ord($str[$i++]);

if ($ord0 < 0x80) {
$count++;
continue;
}

if ($i === $length || $ord0 < 0xC2 || $ord0 > 0xF4) {
throw new InvalidStringException($str, $i - 1);
}

$ord1 = ord($str[$i++]);

if ($ord0 < 0xE0) {
if ($ord1 < 0x80 || $ord1 >= 0xC0) {
throw new InvalidStringException($str, $i - 1);
}

// $ord1 = ($ord0 - 0xC0) * 64 + $ord1 - 0x80;
$count++;

continue;
}

if ($i === $length) {
throw new InvalidStringException($str, $i - 1);
}

$ord2 = ord($str[$i++]);

if ($ord0 < 0xF0) {
if ($ord0 === 0xE0) {
if ($ord1 < 0xA0 || $ord1 >= 0xC0) {
throw new InvalidStringException($str, $i - 2);
}
} elseif ($ord0 === 0xED) {
if ($ord1 < 0x80 || $ord1 >= 0xA0) {
throw new InvalidStringException($str, $i - 2);
}
} elseif ($ord1 < 0x80 || $ord1 >= 0xC0) {
throw new InvalidStringException($str, $i - 2);
}

if ($ord2 < 0x80 || $ord2 >= 0xC0) {
throw new InvalidStringException($str, $i - 1);
}

// $ord2 = ($ord0 - 0xE0) * 0x1000 + ($ord1 - 0x80) * 64 + $ord2 - 0x80;
$count++;

continue;
}

if ($i === $length) {
throw new InvalidStringException($str, $i - 1);
}

$ord3 = ord($str[$i++]);

if ($ord0 < 0xF5) {
if ($ord0 === 0xF0) {
if ($ord1 < 0x90 || $ord1 >= 0xC0) {
throw new InvalidStringException($str, $i - 3);
}
} elseif ($ord0 === 0xF4) {
if ($ord1 < 0x80 || $ord1 >= 0x90) {
throw new InvalidStringException($str, $i - 3);
}
} elseif ($ord1 < 0x80 || $ord1 >= 0xC0) {
throw new InvalidStringException($str, $i - 3);
}

if ($ord2 < 0x80 || $ord2 >= 0xC0) {
throw new InvalidStringException($str, $i - 2);
}

if ($ord3 < 0x80 || $ord3 >= 0xC0) {
throw new InvalidStringException($str, $i - 1);
}

// $ord3 = ($ord0 - 0xF0) * 0x40000 + ($ord1 - 0x80) * 0x1000 + ($ord2 - 0x80) * 64 + $ord3 - 0x80;
$count++;

continue;
}

throw new InvalidStringException($str, $i - 1);
}

return $count;
}

/**
* Converts each code point to a char
* @param array $codes
Expand Down
11 changes: 11 additions & 0 deletions tests/UnicodeStringTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,22 @@ public function testLength()
{
$this->assertEquals(0, wstr::from('')->length());
$this->assertEquals(1, wstr::from(' ')->length());
$this->assertEquals(1, wstr::from("\x00")->length());
$this->assertEquals(5, wstr::from('abcde')->length());
$this->assertEquals(10, wstr::from('ăĂâÂîÎșȘțȚ')->length());
$this->assertEquals(15, wstr::from('abcdeăĂâÂîÎșȘțȚ')->length());
}

public function testStaticLength()
{
$this->assertEquals(0, wstr::getStringLength(''));
$this->assertEquals(1, wstr::getStringLength(' '));
$this->assertEquals(1, wstr::getStringLength("\x00"));
$this->assertEquals(5, wstr::getStringLength('abcde'));
$this->assertEquals(10, wstr::getStringLength('ăĂâÂîÎșȘțȚ'));
$this->assertEquals(15, wstr::getStringLength('abcdeăĂâÂîÎșȘțȚ'));
}

public function testIsEmpty()
{
$this->assertTrue(wstr::from('')->isEmpty());
Expand Down