• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Genfic / Ogma / 34907662747

14 Sep 2026 11:10PM UTC coverage: 1.032% (-0.2%) from 1.195%
34907662747

push

github

Atulin
Nuggie uppies

350 of 33906 relevant lines covered (1.03%)

0.01 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/Ogma3/Infrastructure/SearchQueryParser/SearchQueryParser.cs
1
using System.Collections.Frozen;
2
using Utils.Extensions;
3

4
namespace Ogma3.Infrastructure.SearchQueryParser;
5

6
using LookupDict = FrozenDictionary<string, Func<string, bool, SearchToken>>;
7
using AliasLookup = Dictionary<string,string>.AlternateLookup<ReadOnlySpan<char>>;
8

9
public static class SearchQueryParser
10
{
11
        private static readonly LookupDict FieldParsers =
×
12
                new Dictionary<string, Func<string, bool, SearchToken>>(StringComparer.OrdinalIgnoreCase)
×
13
                {
×
14
                        ["author"] = (val, neg) => new AuthorToken(val, neg),
×
15
                        ["status"] = (val, neg) => new StatusToken(val, neg),
×
16
                        ["rating"] = (val, neg) => new RatingToken(val, neg),
×
17
                }.ToFrozenDictionary();
×
18

19
        private static readonly LookupDict.AlternateLookup<ReadOnlySpan<char>> FieldLookup =
×
20
                FieldParsers.GetAlternateLookup<ReadOnlySpan<char>>();
×
21

22
        public static IReadOnlyList<SearchToken> Parse(ReadOnlySpan<char> query, Dictionary<string, string> namespaceAliases)
23
        {
24
                var tokens = new List<SearchToken>();
×
25

26
                var aliasLookup = namespaceAliases.GetAlternateLookup<ReadOnlySpan<char>>();
×
27

28
                var start = 0;
×
29
                var quoted = false;
×
30

31
                for (var i = 0; i < query.Length; i++)
×
32
                {
33
                        var curr = query[i];
×
34
                        switch (curr)
×
35
                        {
36
                                case '"':
37
                                        quoted = !quoted;
×
38
                                        continue;
×
39
                                case ',' when !quoted:
×
40
                                        TryAddToken(tokens, query[start..i], aliasLookup);
×
41
                                        start = i + 1;
×
42
                                        break;
43
                        }
44

45
                }
46

47
                // trailing
48
                TryAddToken(tokens, query[start..], aliasLookup);
×
49

50
                return tokens.AsReadOnly();
×
51
        }
52

53
        private static void TryAddToken(List<SearchToken> tokens, ReadOnlySpan<char> rawSegment, AliasLookup aliasLookup)
54
        {
55
                var segment = rawSegment.Trim();
×
56
                if (segment.IsEmpty)
×
57
                {
58
                        return;
×
59
                }
60

61
                tokens.Add(ParseSegment(segment, aliasLookup));
×
62
        }
×
63

64

65
        private static SearchToken ParseSegment(ReadOnlySpan<char> segment, AliasLookup aliasLookup)
66
        {
67
                var negated = segment.StartsWith('-');
×
68
                segment = negated ? segment[1..] : segment;
×
69

70
                var colon = segment.IndexOf(':');
×
71

72
                // "some title"
73
                if (segment is ['"', .. var title, '"'])
×
74
                {
75
                        return new TitleToken(title.Trim().ToString());
×
76
                }
77

78
                // plain tag
79
                if (colon <= 0)
×
80
                {
81
                        return new TagToken(null, segment.Trim().ToString(), negated);
×
82
                }
83

84
                // key:value
85
                var key = segment[..colon].Trim();
×
86
                var val = segment[(colon + 1)..].Trim();
×
87

88
                // my:<something>
89
                if (key is "my" && MyContentTypeExtensions.TryParse(val, out var myType, true))
×
90
                {
91
                        return new MyContentToken(myType);
×
92
                }
93

94
                if (FieldLookup.TryGetValue(key, out var fac))
×
95
                {
96
                        return fac(val.ToString(), negated);
×
97
                }
98

99
                // plain tag with namespace
100
                var trimmed = segment[..colon].Trim();
×
101

102
                // fast path: no spaces to remove, skip the stackalloc copy entirely
103
                if (trimmed.IndexOf(' ') < 0)
×
104
                {
105
                        var subbed = aliasLookup.TryGetValue(trimmed, out var sub) ? sub : trimmed;
×
106
                        return new TagToken(subbed.ToString(), segment[(colon + 1)..].Trim().ToString(), negated);
×
107
                }
108

109
                // slow path: contains spaces, perform the stackalloc strip
110
                Span<char> buffer = stackalloc char[trimmed.Length];
×
111
                var ns = trimmed.RemoveChar(' ', buffer);
×
112

113
                var subbedClean = aliasLookup.TryGetValue(ns, out var subClean) ? subClean : ns;
×
114
                return new TagToken(subbedClean.ToString(), segment[(colon + 1)..].Trim().ToString(), negated);
×
115
        }
116
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc