Skip to content

Commit

Permalink
feat+fix: use mbstring instead str
Browse files Browse the repository at this point in the history
  • Loading branch information
kocoten1992 committed Apr 3, 2022
1 parent c03e64a commit e6fc962
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ str_replace([12, 23], [23, 45], '1223') // '4545'
StrUtil::replaceOnce([12, 23], [23, 45], '1223') // false
```

## Requirement
Need mbstring extension

## Installation

Use the package manager [composer](https://getcomposer.org/) to install phputils.
Expand Down
22 changes: 11 additions & 11 deletions src/StrUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public static function replaceOnceIndex(
}

foreach ($indexes[$search_index] as $index) {
if (substr($subject, $index, strlen($search)) !== $search) {
if (mb_substr($subject, $index, mb_strlen($search)) !== $search) {
return false;
}
}
Expand All @@ -37,11 +37,11 @@ public static function replaceOnceIndex(
}

// check if indexes and searches overlap
$overlap_map = array_fill(0, strlen($subject), false);
$overlap_map = array_fill(0, mb_strlen($subject), false);

foreach ($searches as $search_index => $search) {
foreach ($indexes[$search_index] as $index) {
for ($i = $index; $i < $index + strlen($search); $i++) {
for ($i = $index; $i < $index + mb_strlen($search); $i++) {
if (! $overlap_map[$i]) {
$overlap_map[$i] = true;
continue;
Expand Down Expand Up @@ -121,14 +121,14 @@ public static function replaceOnce(

$pair_count = count($searches);

$k_character_set = array_unique(str_split($searches[0]));
$k_character_set = array_unique(mb_str_split($searches[0]));

for ($i = 0; $i < $pair_count; $i++) {
if ($i === 0) {
continue;
}

$chars = str_split($searches[$i]);
$chars = mb_str_split($searches[$i]);

if (! empty(array_intersect($k_character_set, $chars))) {
return false;
Expand Down Expand Up @@ -188,10 +188,10 @@ protected static function multiStrpos(string $haystack, string $needle): array

$pos_last = 0;

while (($pos_last = strpos($haystack, $needle, $pos_last)) !== false) {
while (($pos_last = mb_strpos($haystack, $needle, $pos_last)) !== false) {
$positions[] = $pos_last;

$pos_last = $pos_last + strlen($needle);
$pos_last = $pos_last + mb_strlen($needle);
}

return $positions;
Expand All @@ -215,9 +215,9 @@ protected static function replaceAtIndex(
// replaceAtIndex($search, $replace, $subject, 1) // it wrong, tripple think it boiss

return
substr($subject, 0, $index).
mb_substr($subject, 0, $index).
$replace.
substr($subject, strlen($search) + $index);
mb_substr($subject, mb_strlen($search) + $index);
}

/**
Expand Down Expand Up @@ -284,7 +284,7 @@ protected static function updateReplaceMap(

foreach ($replace_map[$search_idx] as $r_map_k => $r_map_v) {
$replace_map[$search_idx][$r_map_k] =
$r_map_v + (+ strlen($replace) - strlen($search));
$r_map_v + (+ mb_strlen($replace) - mb_strlen($search));
}

foreach ($replace_map as $r_map_k => $r_map_v) {
Expand All @@ -297,7 +297,7 @@ protected static function updateReplaceMap(
continue;
}

$replace_map[$r_map_k][$r_map_v_k] += (+ strlen($replace) - strlen($search));
$replace_map[$r_map_k][$r_map_v_k] += (+ mb_strlen($replace) - mb_strlen($search));
}
}

Expand Down

0 comments on commit e6fc962

Please sign in to comment.