code reformat and cleanup

This commit is contained in:
2025-04-05 05:59:22 +05:00
parent 758388cda0
commit e9c7c8f5c1
10 changed files with 357 additions and 344 deletions
+28 -32
View File
@@ -9,15 +9,15 @@ public interface ISearchExpression
public static class SearchExpressionCompiler
{
private static bool CharEqualsAndNotEscaped(char c, ReadOnlySpan<char> chars, int i) =>
chars[i] == c && (i < 1 || chars[i - 1] != '\\') && (i < 2 || chars[i - 2] != '\\');
private static bool CharEqualsAndNotEscaped(char c, ReadOnlySpan<char> chars, int i)
=> chars[i] == c && (i < 1 || chars[i - 1] != '\\') && (i < 2 || chars[i - 2] != '\\');
public static ISearchExpression Compile(ReadOnlySpan<char> query)
{
if(query.IsEmpty)
if (query.IsEmpty)
throw new ArgumentNullException(nameof(query));
if(query[0] is '(')
if (query[0] is '(')
{
var subExprs = new List<ISearchExpression>();
int supExprBegin = 1;
@@ -25,9 +25,13 @@ public static class SearchExpressionCompiler
for (int i = supExprBegin; i < query.Length && bracketBalance != 0; i++)
{
if (CharEqualsAndNotEscaped('(', query, i))
{
bracketBalance++;
}
else if (CharEqualsAndNotEscaped(')', query, i))
{
bracketBalance--;
}
else if (bracketBalance == 1 && CharEqualsAndNotEscaped('|', query, i))
{
var subPart = query.Slice(supExprBegin, i - supExprBegin);
@@ -37,14 +41,14 @@ public static class SearchExpressionCompiler
}
}
if(query[^1] != ')')
if (query[^1] != ')')
throw new NotImplementedException("Expressions after ')' are not supported");
if (bracketBalance > 0)
throw new Exception("Too many opening brackets");
if (bracketBalance < 0)
throw new Exception("Too many closing brackets");
throw new Exception("Too many closing brackets");
var subPartLast = query.Slice(supExprBegin, query.Length - supExprBegin - 1);
var subExprLast = Compile(subPartLast);
subExprs.Add(subExprLast);
@@ -54,35 +58,31 @@ public static class SearchExpressionCompiler
int partBeforePointLength = 0;
while (partBeforePointLength < query.Length)
{
if(CharEqualsAndNotEscaped('.', query, partBeforePointLength))
if (CharEqualsAndNotEscaped('.', query, partBeforePointLength))
break;
partBeforePointLength++;
}
var part = query.Slice(0, partBeforePointLength);
ReadOnlySpan<char> remaining = default;
if (partBeforePointLength < query.Length)
remaining = query.Slice(partBeforePointLength + 1);
if (part is "*")
{
return new AnyMatchExpression(remaining.IsEmpty ? null : Compile(remaining));
}
if (part is "*") return new AnyMatchExpression(remaining.IsEmpty ? null : Compile(remaining));
for (int j = 0; j < part.Length; j++)
{
if(CharEqualsAndNotEscaped('*', part, j))
if (CharEqualsAndNotEscaped('*', part, j))
throw new NotImplementedException("pattern matching other than '*' is not implemented yet");
}
if (part[0] is '[')
{
part = part.Slice(1, part.Length - 2);
return new IndexMatchExpression(int.Parse(part), remaining.IsEmpty ? null : Compile(remaining));
}
return new ExactMatchExpression(part.ToString(), remaining.IsEmpty ? null : Compile(remaining));
}
private record AnyMatchExpression(ISearchExpression? next) : ISearchExpression
{
public bool DoesMatch(SearchArgs args, out ISearchExpression? nextSearchExpression)
@@ -91,19 +91,15 @@ public static class SearchExpressionCompiler
return true;
}
}
private record MultipleMatchExpression(List<ISearchExpression> subExprs) : ISearchExpression
{
public bool DoesMatch(SearchArgs args, out ISearchExpression? nextSearchExpression)
{
foreach (var e in subExprs)
{
if(e.DoesMatch(args, out nextSearchExpression))
{
if (e.DoesMatch(args, out nextSearchExpression))
return true;
}
}
nextSearchExpression = null;
return false;
}
@@ -118,12 +114,12 @@ public static class SearchExpressionCompiler
nextSearchExpression = next;
return true;
}
nextSearchExpression = null;
return false;
}
}
private record ExactMatchExpression(string key, ISearchExpression? next) : ISearchExpression
{
public bool DoesMatch(SearchArgs args, out ISearchExpression? nextSearchExpression)
@@ -133,9 +129,9 @@ public static class SearchExpressionCompiler
nextSearchExpression = next;
return true;
}
nextSearchExpression = null;
return false;
}
}
}
}