Skip to content

Less memory hungry FnMatchPathNormalizer: reduced by ~35-45% #192

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

Merged
merged 3 commits into from
Apr 18, 2024
Merged
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
30 changes: 7 additions & 23 deletions src/Skipper/FileSystem/FnMatchPathNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,36 +11,20 @@
*/
final class FnMatchPathNormalizer
{
/**
* @var string
* @see https://regex101.com/r/ZB2dFV/2
*/
private const ONLY_ENDS_WITH_ASTERISK_REGEX = '#^[^*](.*?)\*$#';

/**
* @var string
* @see https://regex101.com/r/aVUDjM/2
*/
private const ONLY_STARTS_WITH_ASTERISK_REGEX = '#^\*(.*?)[^*]$#';

public function normalizeForFnmatch(string $path): string
{
// ends with *
if (Strings::match($path, self::ONLY_ENDS_WITH_ASTERISK_REGEX)) {
return '*' . $path;
}

// starts with *
if (Strings::match($path, self::ONLY_STARTS_WITH_ASTERISK_REGEX)) {
return $path . '*';
if (str_ends_with($path, '*') || str_starts_with($path, '*')) {
return '*' . trim($path, '*') . '*';
}

if (\str_contains($path, '..')) {
/** @var string|false $path */
$path = realpath($path);
if ($path === false) {
/** @var string|false $realPath */
$realPath = realpath($path);
if ($realPath === false) {
return '';
}

return $realPath;
}

return $path;
Expand Down
Loading