Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix incorrect split UTF-8 strings #105

Merged
merged 2 commits into from
Jul 27, 2023
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
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## 2.1.2 under development

- no changes in this release.
- Bug #105: Fix incorrect split UTF-8 strings in `StringHelper::split()` method (@vjik)

## 2.1.1 April 28, 2023

Expand Down
2 changes: 1 addition & 1 deletion src/StringHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@
*
* @return int The number of bytes in the given string.
*/
public static function byteLength(?string $input): int

Check warning on line 34 in src/StringHelper.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "PublicVisibility": --- Original +++ New @@ @@ * * @return int The number of bytes in the given string. */ - public static function byteLength(?string $input) : int + protected static function byteLength(?string $input) : int { return mb_strlen((string) $input, '8bit'); }
{
return mb_strlen((string)$input, '8bit');

Check warning on line 36 in src/StringHelper.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "MBString": --- Original +++ New @@ @@ */ public static function byteLength(?string $input) : int { - return mb_strlen((string) $input, '8bit'); + return strlen((string) $input); } /** * Returns the portion of string specified by the start and length parameters.
}

/**
Expand All @@ -51,7 +51,7 @@
*/
public static function byteSubstring(string $input, int $start, int $length = null): string
{
return mb_substr($input, $start, $length ?? mb_strlen($input, '8bit'), '8bit');

Check warning on line 54 in src/StringHelper.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "MBString": --- Original +++ New @@ @@ */ public static function byteSubstring(string $input, int $start, int $length = null) : string { - return mb_substr($input, $start, $length ?? mb_strlen($input, '8bit'), '8bit'); + return mb_substr($input, $start, $length ?? strlen($input), '8bit'); } /** * Returns the trailing name component of a path.
}

/**
Expand Down Expand Up @@ -496,7 +496,7 @@
public static function split(string $string, string $separator = '\R'): array
{
$string = preg_replace('(^\s*|\s*$)', '', $string);
return preg_split('~\s*' . $separator . '\s*~', $string, -1, PREG_SPLIT_NO_EMPTY);
return preg_split('~\s*' . $separator . '\s*~u', $string, -1, PREG_SPLIT_NO_EMPTY);
}

/**
Expand Down
4 changes: 4 additions & 0 deletions tests/StringHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,10 @@ public function dataSplit(): array
"\0\nA\nB",
["\0", 'A', 'B'],
],
[
"технический\nдолг",
['технический', 'долг'],
],
];
}

Expand Down
Loading