Skip to content

Commit 4bc6737

Browse files
authored
Merge pull request #23 from Rohansi/fix-windows-searchpattern
Remove SearchPattern special case for Windows
2 parents 65bd6c5 + fbc9603 commit 4bc6737

File tree

3 files changed

+22
-24
lines changed

3 files changed

+22
-24
lines changed

src/Zio.Tests/TestSearchPattern.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public class TestSearchPattern
4949
[InlineData("/a/b/c", "x*.txt", "xyoyo.txt", true)]
5050
[InlineData("/a/b/c", "x*.txt", "x.txt", true)]
5151
[InlineData("/a/b/c", "*.txt", "x.txt", true)]
52-
[InlineData("/a/b/c", "*.txt", "x.txt1", true)]
52+
[InlineData("/a/b/c", "*.txt", "x.txt1", false)] // No 8.3 truncating
5353
[InlineData("/a/b/c", "*.i", "x.i", true)]
5454
[InlineData("/a/b/c", "*.i", "x.i1", false)]
5555
[InlineData("/a/b/c", "x*.txt", "x_txt", false)]

src/Zio/FileSystems/PhysicalFileSystem.cs

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -476,20 +476,34 @@ protected override IEnumerable<UPath> EnumeratePathsImpl(UPath path, string sear
476476
}
477477
}
478478

479+
IEnumerable<string> results;
479480
switch (searchTarget)
480481
{
481482
case SearchTarget.File:
482-
foreach (var subPath in Directory.EnumerateFiles(ConvertPathToInternal(path), searchPattern, searchOption))
483-
yield return ConvertPathFromInternal(subPath);
483+
results = Directory.EnumerateFiles(ConvertPathToInternal(path), searchPattern, searchOption);
484484
break;
485+
485486
case SearchTarget.Directory:
486-
foreach (var subPath in Directory.EnumerateDirectories(ConvertPathToInternal(path), searchPattern, searchOption))
487-
yield return ConvertPathFromInternal(subPath);
487+
results = Directory.EnumerateDirectories(ConvertPathToInternal(path), searchPattern, searchOption);
488488
break;
489+
489490
case SearchTarget.Both:
490-
foreach (var subPath in Directory.EnumerateFileSystemEntries(ConvertPathToInternal(path), searchPattern, searchOption))
491-
yield return ConvertPathFromInternal(subPath);
491+
results = Directory.EnumerateFileSystemEntries(ConvertPathToInternal(path), searchPattern, searchOption);
492492
break;
493+
494+
default:
495+
yield break;
496+
}
497+
498+
foreach (var subPath in results)
499+
{
500+
// Windows will truncate the search pattern's extension to three characters if the filesystem
501+
// has 8.3 paths enabled. This means searching for *.docx will list *.doc as well which is
502+
// not what we want. Check against the search pattern again to filter out those false results.
503+
if (!IsOnWindows || search.Match(Path.GetFileName(subPath)))
504+
{
505+
yield return ConvertPathFromInternal(subPath);
506+
}
493507
}
494508
}
495509

src/Zio/SearchPattern.cs

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,6 @@ private SearchPattern(ref UPath path, ref string searchPattern)
104104
}
105105
}
106106

107-
bool appendSpecialCaseForExt3Chars = false;
108107
var startIndex = 0;
109108
int nextIndex;
110109
StringBuilder builder = null;
@@ -129,16 +128,6 @@ private SearchPattern(ref UPath path, ref string searchPattern)
129128
var regexPatternPart = c == '*' ? "[^/]*" : "[^/]";
130129
builder.Append(regexPatternPart);
131130

132-
// If the specified extension is exactly three characters long,
133-
// the method returns files with extensions that begin with the specified extension.
134-
// For example, "*.xls" returns both "book.xls" and "book.xlsx".
135-
// 012345
136-
// *.txt
137-
if (c == '*' && nextIndex + 5 == searchPattern.Length && searchPattern[nextIndex + 1] == '.' && searchPattern.IndexOf('.', nextIndex + 2) < 0)
138-
{
139-
appendSpecialCaseForExt3Chars = true;
140-
}
141-
142131
startIndex = nextIndex + 1;
143132
}
144133
if (builder == null)
@@ -154,11 +143,6 @@ private SearchPattern(ref UPath path, ref string searchPattern)
154143
builder.Append(toEscape);
155144
}
156145

157-
if (appendSpecialCaseForExt3Chars)
158-
{
159-
builder.Append("[^/]*");
160-
}
161-
162146
builder.Append("$");
163147

164148
var regexPattern = builder.ToString();
@@ -174,4 +158,4 @@ private SearchPattern(ref UPath path, ref string searchPattern)
174158
}
175159
}
176160
}
177-
}
161+
}

0 commit comments

Comments
 (0)