Skip to content

Commit

Permalink
Fixes #29 - star star match not correctly handling path seperator.
Browse files Browse the repository at this point in the history
  • Loading branch information
dazinator committed Jun 30, 2017
1 parent d8bc2c9 commit 1589ec6
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 21 deletions.
1 change: 1 addition & 0 deletions src/DotNet.Glob.Tests/GlobTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class GlobTests
[InlineData("*ave 12", "HKEY_LOCAL_MACHINE\\SOFTWARE\\Adobe\\Shockwave 12")]
[InlineData("*ave 12", "wave 12/")]
[InlineData("C:\\THIS_IS_A_DIR\\**\\somefile.txt", "C:\\THIS_IS_A_DIR\\awesomefile.txt")] // Regression Test for https://github.com/dazinator/DotNet.Glob/issues/27
[InlineData("C:\\name\\**", "C:\\name.ext", "C:\\name_longer.ext")] // Regression Test for https://github.com/dazinator/DotNet.Glob/issues/29
public void Does_Not_Match(string pattern, params string[] testStrings)
{
var glob = Globbing.Glob.Parse(pattern);
Expand Down
32 changes: 18 additions & 14 deletions src/DotNet.Glob/Evaluation/WildcardDirectoryTokenEvaluator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,25 +33,20 @@ public bool IsMatch(string allChars, int currentPosition, out int newPosition)
}



// otherwise we can consume a variable amount of characters but we can't match more characters than the amount that will take
// us past the min required length required by the sub evaluator tokens, and as we are a directory wildcard, we
// can go past a path seperators but we can only match complete segments, not partial segments.

// The max pos we can match upto in the string, because of subtoken match requirements.
var maxPos = (allChars.Length - _subEvaluator.ConsumesMinLength);
// we can only match full segments at a time, where as match pos could point to a char in the middle of a segment.

if (!_subEvaluator.ConsumesVariableLength)
{
// must match a fixed length of the string (upto max pos), and we only match entire segments, so for a successful
// match, the char before maxpos must either be current pos, or a seperator.
if (maxPos > currentPosition)
// must match a fixed length of the string (must match all to max pos), but we can only match entire segments,
// so for a successful match, the char before maxpos must either be current pos (we match nothing), or a seperator.
if (maxPos -1 != currentPosition)
{
var precedingchar = allChars[maxPos - 1];
if (precedingchar != '/' && precedingchar != '\\')
{
if (maxPos - 1 != currentPosition)
{
return false;
}
return false;
}
}

Expand All @@ -67,6 +62,14 @@ public bool IsMatch(string allChars, int currentPosition, out int newPosition)

var currentChar = allChars[pos];

var isSeprator = currentChar == '/' || currentChar == '\\';

if (this._token.LeadingPathSeperator != null && !isSeprator)
{
// must begin with seperator.
return false;
}

if (currentChar == '/' || currentChar == '\\')
{
if (pos == maxPos)
Expand All @@ -76,16 +79,17 @@ public bool IsMatch(string allChars, int currentPosition, out int newPosition)
pos = pos + 1;
}


while (pos <= maxPos)
{


isMatch = _subEvaluator.IsMatch(allChars, pos, out newPosition);
if (isMatch)
{
return isMatch;
}

currentChar = allChars[pos];
while (currentChar != '/' && currentChar != '\\' && pos + 1 < maxPos)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ private bool AppendRandomPaths(StringBuilder builder)
// append a random number of random literals, between 0 characters and 10 in length,
// seperated by path seperators.
var numberOfSegments = _random.Next(_minSegments, _maxSegments);
if (numberOfSegments == 0)
{
return false;
}
//if (numberOfSegments == 0)
//{
// return false;
//}

if (token.LeadingPathSeperator != null)
{
Expand Down
2 changes: 1 addition & 1 deletion src/DotNet.Glob/GlobBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public IGlobBuilder DirectoryWildcard(PathSeperatorKind? leadingSeperatorKind =
}
}

_tokens.Add(new WildcardDirectoryToken(trailingSep, leadingSep));
_tokens.Add(new WildcardDirectoryToken(leadingSep, trailingSep));


return this;
Expand Down
2 changes: 1 addition & 1 deletion src/DotNet.Glob/GlobTokeniser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ private IGlobToken ReadDirectoryWildcardToken(GlobStringReader reader, PathSeper
return new WildcardDirectoryToken(leadingPathSeperatorToken, trailingSeperator);
}

return new WildcardDirectoryToken(null, leadingPathSeperatorToken); // this shouldn't happen unless a pattern ends with ** which is weird. **sometext is not legal.
return new WildcardDirectoryToken(leadingPathSeperatorToken, null); // this shouldn't happen unless a pattern ends with ** which is weird. **sometext is not legal.

}

Expand Down
2 changes: 1 addition & 1 deletion src/DotNet.Glob/Token/WildcardDirectoryToken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
public class WildcardDirectoryToken : IGlobToken
{

public WildcardDirectoryToken(PathSeperatorToken trailingPathSeperator, PathSeperatorToken leadingPathSeperator)
public WildcardDirectoryToken(PathSeperatorToken leadingPathSeperator, PathSeperatorToken trailingPathSeperator)
{
TrailingPathSeperator = trailingPathSeperator;
LeadingPathSeperator = leadingPathSeperator;
Expand Down

0 comments on commit 1589ec6

Please sign in to comment.