diff --git a/CHANGELOG.md b/CHANGELOG.md index 364b536..27a560f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ - New #118: Add `findBetween()`, `findBetweenFirst()` and `findBetweenLast()` methods to `StringHelper` to retrieve a substring that lies between two strings (@salehhashemi1992) +- Enh #121: Don't use regexp if there is no delimeter in the path in `StringHelper::parsePath()` (@viktorprogger) ## 2.3.1 October 30, 2023 diff --git a/src/StringHelper.php b/src/StringHelper.php index 1715d0a..75a1bf4 100644 --- a/src/StringHelper.php +++ b/src/StringHelper.php @@ -509,6 +509,14 @@ public static function parsePath( return []; } + if (!str_contains($path, $delimiter)) { + if ($preserveDelimiterEscaping) { + return [$path]; + } + + return [str_replace($escapeCharacter . $escapeCharacter, $escapeCharacter, $path)]; + } + /** @psalm-var non-empty-list $matches */ $matches = preg_split( sprintf( diff --git a/tests/StringHelperTest.php b/tests/StringHelperTest.php index 7895078..d412145 100644 --- a/tests/StringHelperTest.php +++ b/tests/StringHelperTest.php @@ -447,6 +447,7 @@ public function dataParsePath(): array ['key1\.', '.', '\\', false, ['key1.']], ['key1~.', '.', '~', false, ['key1.']], ['key1~~', '.', '~', false, ['key1~']], + ['key1~~', '.', '~', true, ['key1~~']], ['key1\\\\', '.', '\\', false, ['key1\\']], ['key1~~.key2', '.', '~', false, ['key1~', 'key2']], ['key1\\\\.key2', '.', '\\', false, ['key1\\', 'key2']],