Skip to content

Commit

Permalink
Less memory hungry FnMatchPathNormalizer: reduced by ~35-45% (#192)
Browse files Browse the repository at this point in the history
* Less memory hungry FnMatchPathNormalizer: reduce ~35-45% less consumption

* fix typo

* cs
  • Loading branch information
staabm committed Apr 18, 2024
1 parent 5002568 commit 104c1c8
Showing 1 changed file with 7 additions and 23 deletions.
30 changes: 7 additions & 23 deletions src/Skipper/FileSystem/FnMatchPathNormalizer.php
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

0 comments on commit 104c1c8

Please sign in to comment.