Skip to content

Commit

Permalink
glob() avoids some allocs
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubmisek committed Jun 14, 2024
1 parent 2459513 commit de11a45
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions src/Peachpie.Library/FileSystem.Glob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ private void DoGlob(string/*!*/ baseDirectory, int position)

int patternEnd = FindNextSeparator(position, true, out var containsWildcard);
bool isLastPathSegment = (patternEnd == _pattern.Length);
string dirSegment = _pattern.Substring(position, patternEnd - position);
var dirSegment = _pattern.AsSpan(position, patternEnd - position);

if (!isLastPathSegment)
{
Expand All @@ -301,7 +301,7 @@ private void DoGlob(string/*!*/ baseDirectory, int position)

if (!containsWildcard)
{
var path = Path.Combine(baseDirectory, dirSegment);
var path = Path.Combine(baseDirectory, dirSegment.ToString());
TestPath(path, patternEnd, isLastPathSegment);
return;
}
Expand Down Expand Up @@ -329,7 +329,7 @@ private void DoGlob(string/*!*/ baseDirectory, int position)
{
if (fnmatch(dirSegment, ".", _fnMatchFlags))
{
string directory = baseDirectory + CurrentPlatform.DirectorySeparatorString + ".";
var directory = baseDirectory + CurrentPlatform.DirectorySeparatorString + ".";
if (_dirOnly)
{
directory += CurrentPlatform.DirectorySeparatorString;
Expand All @@ -338,7 +338,7 @@ private void DoGlob(string/*!*/ baseDirectory, int position)
}
if (fnmatch(dirSegment, "..", _fnMatchFlags))
{
string directory = baseDirectory + CurrentPlatform.DirectorySeparatorString + "..";
var directory = baseDirectory + CurrentPlatform.DirectorySeparatorString + "..";
if (_dirOnly)
{
directory += CurrentPlatform.DirectorySeparatorString;
Expand Down Expand Up @@ -629,9 +629,9 @@ static void AppendExplicitRegexChar(StringBuilder/*!*/ builder, char c)
builder.Append(']');
}

static string/*!*/ PatternToRegex(string/*!*/ pattern, bool pathName, bool noEscape)
static string/*!*/ PatternToRegex(ReadOnlySpan<char>/*!*/pattern, bool pathName, bool noEscape)
{
var result = new StringBuilder(pattern.Length);
var result = ObjectPools.GetStringBuilder();
result.Append("\\G");

bool inEscape = false;
Expand Down Expand Up @@ -701,7 +701,15 @@ static void AppendExplicitRegexChar(StringBuilder/*!*/ builder, char c)
}
}

return (charClass == null) ? result.ToString() : string.Empty;
if (charClass == null)
{
return ObjectPools.GetStringAndReturn(result);
}
else
{
ObjectPools.Return(result);
return string.Empty;
}
}

static ValueList<string> UngroupGlobs(string/*!*/ pattern, bool noEscape, bool brace)
Expand Down Expand Up @@ -804,9 +812,9 @@ static ValueList<string> UngroupGlobs(string/*!*/ pattern, bool noEscape, bool b
/// <param name="flags">Additional flags.</param>
/// <returns><c>true</c> if the <paramref name="path"/> matches with the given
/// wildcard <paramref name="pattern"/>.</returns>
public static bool fnmatch(string/*!*/ pattern, string/*!*/ path, FnMatchOptions flags = FnMatchOptions.None)
public static bool fnmatch(ReadOnlySpan<char>/*!*/pattern, string/*!*/ path, FnMatchOptions flags = FnMatchOptions.None)
{
if (string.IsNullOrEmpty(pattern))
if (pattern.IsEmpty)
{
return string.IsNullOrEmpty(path);
}
Expand Down

0 comments on commit de11a45

Please sign in to comment.