Was.OrcSearch – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | using System.Text.RegularExpressions; |
2 | |||
3 | namespace Was.OrcSearch.Extensions |
||
4 | { |
||
5 | public static class StringExtensions |
||
6 | { |
||
7 | public static string PrepareOrcSearchFilter(this string filter) |
||
8 | { |
||
9 | if (filter.StartsWith("\"") && filter.EndsWith("\"")) |
||
10 | return filter.Length == 2 |
||
11 | ? string.Empty |
||
12 | : filter; |
||
13 | |||
14 | if (!filter.Contains("*") && |
||
15 | !filter.Contains(":") && |
||
16 | !filter.Contains(" ") && |
||
17 | !filter.Contains("AND") && |
||
18 | !filter.Contains("OR")) |
||
19 | filter += "*"; |
||
20 | |||
21 | return filter; |
||
22 | } |
||
23 | |||
24 | public static bool IsValidOrcSearchFilter(this string filter) |
||
25 | { |
||
26 | if (string.IsNullOrWhiteSpace(filter)) return false; |
||
27 | |||
28 | filter = filter.Trim(); |
||
29 | if (filter.EndsWith(":")) return false; |
||
30 | |||
31 | return true; |
||
32 | } |
||
33 | |||
34 | public static string ExtractRegexString(this string filter) |
||
35 | { |
||
36 | if (!filter.StartsWith("/") || !filter.EndsWith("/")) return string.Empty; |
||
37 | |||
38 | filter = filter.Substring(1, filter.Length - 2); |
||
39 | if (!filter.IsValidRegexPattern()) return string.Empty; |
||
40 | |||
41 | return filter; |
||
42 | } |
||
43 | |||
44 | public static bool IsValidRegexPattern(this string pattern) |
||
45 | { |
||
46 | try |
||
47 | { |
||
48 | // ReSharper disable once ObjectCreationAsStatement |
||
49 | new Regex(pattern); |
||
50 | return true; |
||
51 | } |
||
52 | catch |
||
53 | { |
||
54 | // ignored |
||
55 | } |
||
56 | |||
57 | return false; |
||
58 | } |
||
59 | } |
||
60 | } |