Skip to content

Commit 388f4b8

Browse files
committed
refined filtering
1 parent 1c70833 commit 388f4b8

File tree

3 files changed

+69
-15
lines changed

3 files changed

+69
-15
lines changed

src/Globbing.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using SpanExtensions;
2+
3+
namespace LineCount;
4+
5+
public static class Globbing
6+
{
7+
public static string ToRegex(string pattern)
8+
{
9+
if (!pattern.All(IsValidGlobPatternChar))
10+
{
11+
return pattern;
12+
}
13+
14+
if (pattern.StartsWith('*') && pattern.EndsWith('*'))
15+
{
16+
return pattern[1..^1];
17+
}
18+
19+
if (pattern.StartsWith('*'))
20+
{
21+
return $"{pattern.Skip(1)}$";
22+
}
23+
24+
if (pattern.EndsWith('*'))
25+
{
26+
return $"^{pattern.SkipLast(1)}";
27+
}
28+
29+
return $"^{pattern}$";
30+
}
31+
32+
static bool IsValidGlobPatternChar(char c)
33+
{
34+
return c == '*' || c == '_' || c == '-' || c == '.' || char.IsLetterOrDigit(c);
35+
}
36+
}

src/LineCount.cs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
using System.Globalization;
2-
using System.Security;
3-
using System.Text.RegularExpressions;
4-
using LineCount.Errors;
1+
using LineCount.Errors;
52
using LineCount.Logging;
63
using LineCount.Result;
4+
using System.Globalization;
5+
using System.Linq;
6+
using System.Security;
7+
using System.Text.RegularExpressions;
78

89
namespace LineCount;
910

@@ -27,7 +28,7 @@ public static class LineCount
2728
{
2829
return null;
2930
}
30-
}
31+
}
3132

3233
static async Task<ReportResult> GetLineCount(string path, LineCountData data, PathPatterns excludeFilePatterns, PathPatterns excludeDirectoryPatterns, CancellationToken cancellationToken = default)
3334
{
@@ -245,12 +246,19 @@ static async Task<ReportResult> CountInFiles(string path, LineCountData data, Pa
245246

246247
static IEnumerable<string> GetFilterFilePaths(string path, LineCountData data)
247248
{
248-
if (data.Filter is null)
249+
var files = Directory.EnumerateFiles(path);
250+
251+
if (data.Filter is not null)
249252
{
250-
return Directory.EnumerateFiles(path).Select(Path.GetFullPath);
253+
files = files.Where(line => data.Filter.IsMatch(line));
251254
}
252-
253-
return Directory.EnumerateFiles(path, data.Filter).Select(Path.GetFullPath);
255+
256+
if (data.ExcludeFilter is not null)
257+
{
258+
files = files.Where(line => !data.ExcludeFilter.IsMatch(line));
259+
}
260+
261+
return files.Select(Path.GetFullPath);
254262
}
255263

256264
static Task<LineCountReport> GetSingleFileLineCountReport(string path, LineCountData data, CancellationToken cancellationToken = default)

src/LineCountData.cs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
using System.Diagnostics.CodeAnalysis;
2+
using System.Security.Cryptography;
23
using System.Text.RegularExpressions;
34

45
namespace LineCount;
56

67
public class LineCountData
78
{
8-
public string? Filter { get; }
9+
public Regex? Filter { get; }
910
public Regex? LineFilter { get; }
10-
public string? ExcludeFilter { get; }
11+
public Regex? ExcludeFilter { get; }
1112
public Regex? ExcludeLineFilter { get; }
1213

1314
public FilterType FilterType { get; } = (FilterType)(-1);
@@ -17,17 +18,26 @@ public class LineCountData
1718

1819
public LineCountData(string? filter, string? lineFilter, string? filterNot, string? lineFilterNot)
1920
{
20-
Filter = filter;
21-
ExcludeFilter = filterNot;
22-
21+
if(filter is not null)
22+
{
23+
string filterPattern = Globbing.ToRegex(filter);
24+
Filter = new Regex(filterPattern, RegexOptions.Singleline | RegexOptions.Compiled, TimeOut);
25+
}
26+
27+
if (filterNot is not null)
28+
{
29+
string filterNotPattern = Globbing.ToRegex(filterNot);
30+
ExcludeFilter = new Regex(filterNotPattern, RegexOptions.Singleline | RegexOptions.Compiled, TimeOut);
31+
}
32+
2333
if (lineFilter is null)
2434
{
2535
FilterType = lineFilterNot is null ? FilterType.None : FilterType.FilteredExcept;
2636
}
2737
else
2838
{
29-
LineFilter = new Regex(lineFilter, RegexOptions.Singleline | RegexOptions.Compiled, TimeOut);
3039
FilterType = lineFilterNot is null ? FilterType.Filtered : FilterType.FilteredBoth;
40+
LineFilter = new Regex(lineFilter, RegexOptions.Singleline | RegexOptions.Compiled, TimeOut);
3141
}
3242

3343
if (lineFilterNot is not null)

0 commit comments

Comments
 (0)