Stop using fatal errors
This commit is contained in:
@@ -7,7 +7,13 @@ namespace CxxBuilder;
|
||||
|
||||
static class FileMatcher
|
||||
{
|
||||
public static void HandleMatches(Span<StringView> patterns, StringView directory, delegate void(StringView match) callback)
|
||||
public enum Error
|
||||
{
|
||||
ExpectedEscapeSequence,
|
||||
CharacterClassNotClosed,
|
||||
}
|
||||
|
||||
public static Result<void, Error> HandleMatches(Span<StringView> patterns, StringView directory, delegate void(StringView match) callback)
|
||||
{
|
||||
Runtime.Assert(Directory.Exists(directory), scope $"No such directory {directory}");
|
||||
void Dir(StringView dir)
|
||||
@@ -38,13 +44,14 @@ static class FileMatcher
|
||||
}
|
||||
}
|
||||
Dir(directory);
|
||||
return .Ok;
|
||||
}
|
||||
|
||||
public static bool IsMatch(StringView pattern, StringView path)
|
||||
public static Result<bool, Error> IsMatch(StringView pattern, StringView path)
|
||||
{
|
||||
var pattern, path;
|
||||
|
||||
bool Matches(StringView pattern, char8 c)
|
||||
Result<bool, Error> Matches(StringView pattern, char8 c)
|
||||
{
|
||||
if (pattern.IsEmpty) return false;
|
||||
|
||||
@@ -56,7 +63,7 @@ static class FileMatcher
|
||||
return !Path.IsDirectorySeparatorChar(c);
|
||||
case '?': return true;
|
||||
case '\\':
|
||||
Runtime.Assert(pattern.Length > 1, "Expected escape sequence");
|
||||
if (pattern.Length <= 1) return .Err(.ExpectedEscapeSequence);
|
||||
return c == pattern[1];
|
||||
case '/': return Path.IsDirectorySeparatorChar(c);
|
||||
case '[':
|
||||
@@ -64,13 +71,14 @@ static class FileMatcher
|
||||
char8 Next()
|
||||
{
|
||||
if (pattern.Length <= ++i)
|
||||
Runtime.FatalError("Character class not closed");
|
||||
return (.)7;
|
||||
return pattern[i];
|
||||
}
|
||||
bool negated = false;
|
||||
char8 current;
|
||||
switch (Next())
|
||||
{
|
||||
case (.)7: return .Err(.CharacterClassNotClosed);
|
||||
case '^', '!': negated = true; fallthrough;
|
||||
case '\\': current = Next();
|
||||
case ']': return false;
|
||||
@@ -100,7 +108,12 @@ static class FileMatcher
|
||||
{
|
||||
if (path.IsEmpty) return true;
|
||||
if (pattern.IsEmpty) return false;
|
||||
if (!Matches(pattern, path[0])) return false;
|
||||
switch (Matches(pattern, path[0]))
|
||||
{
|
||||
case .Err: return _;
|
||||
case .Ok(false): return false;
|
||||
case .Ok:
|
||||
}
|
||||
switch (pattern[0])
|
||||
{
|
||||
case '*':
|
||||
@@ -114,8 +127,8 @@ static class FileMatcher
|
||||
defer { pattern = lazy; }
|
||||
while (true)
|
||||
{
|
||||
if (IsMatch(lazy, path)) continue reduce;
|
||||
if (!Matches(pattern, path[0])) continue reduce;
|
||||
if (Try!(IsMatch(lazy, path))) continue reduce;
|
||||
if (!Try!(Matches(pattern, path[0]))) continue reduce;
|
||||
path.RemoveFromStart(1);
|
||||
if (path.IsEmpty) return lazy.IsEmpty;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user