Skip to content

Commit

Permalink
bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
jszobody committed Jan 29, 2025
1 parent d492806 commit ba39d66
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,8 @@ private function loopRegexes($regexes, $str, $mode = "match", $highestPosition =
$currentMatch = [];
if ($mode === "replace") {
$currentMatch = preg_replace($regex, "", $str);

// A successful replace must return a smaller string than the input
if (strlen($currentMatch) <= $maxLength) {
$match = $currentMatch;
break;
Expand All @@ -439,28 +441,37 @@ private function loopRegexes($regexes, $str, $mode = "match", $highestPosition =
if ($mode === "split") {
$currentMatch = preg_split($regex, $str, -1, PREG_SPLIT_DELIM_CAPTURE);
} else {
preg_match($regex, $str, $currentMatch);
preg_match($regex, $str, $currentMatch, PREG_OFFSET_CAPTURE);
}

// A successful split must return an array with at least two items
if (count((array) $currentMatch) > $minLength) {
if ($highestPosition) {
// No previous match?
if (!$match) {
// Save match and continue looking for other matches
$match = $currentMatch;
} else {
$higher = false;
if ($mode === "match") {
$higher = $match['index'] > $currentMatch['index'];
// This line SHOULD be equivalent to the original Javascript code,
// but it doesn't work as expected. Skipping it entirely gives us all green
// tests, and expected results. We'll revisit this later.
//$higher = count($currentMatch) >= 3 && $match[2][1] > $currentMatch[2][1];
} else {
if ($mode === "split") {
$higher = strlen($match[0]) > strlen($currentMatch[0]);
}
}

// Match positioned higher than previous one?
if ($higher) {
// Replace match and continue looking for other matches
$match = $currentMatch;
}
}
} else {
// Save match and stop
$match = $currentMatch;
break;
}
Expand All @@ -469,9 +480,13 @@ private function loopRegexes($regexes, $str, $mode = "match", $highestPosition =
}
}

return $mode === "replace"
? ($match ?? "")
: ($match ?? []);
return match($mode) {
// This should always return a string
"replace" => ($match ?? ""),
// Using PREG_OFFSET_CAPTURE returns an array of arrays, so we need to flatten it
"match" => is_array($match) ? array_map(fn($item) => $item[0], $match) : [],
default => ($match ?? []),
};
}

private function reconciliateSplitMatch($match, $minSubstrings, $defaultSubstrings, $fnExclude = null)
Expand Down

0 comments on commit ba39d66

Please sign in to comment.