forked from NpgsqlRest/NpgsqlRest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParser.cs
More file actions
128 lines (110 loc) · 4.31 KB
/
Copy pathParser.cs
File metadata and controls
128 lines (110 loc) · 4.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
using System.Buffers;
using System.Runtime.CompilerServices;
using System.Text.RegularExpressions;
namespace NpgsqlRest;
public static partial class Parser
{
// SIMD-accelerated search for wildcard characters
private static readonly SearchValues<char> WildcardChars = SearchValues.Create("*?");
[GeneratedRegex(@"^(\d*\.?\d+)\s*([a-z]*)$", RegexOptions.Compiled | RegexOptions.IgnoreCase)]
private static partial Regex IntervalRegex();
public static TimeSpan? ParsePostgresInterval(string? interval)
{
if (string.IsNullOrWhiteSpace(interval))
{
return null;
}
interval = interval.Trim().ToLowerInvariant();
// Match number (integer or decimal) followed by optional space and optional unit
var match = IntervalRegex().Match(interval);
if (!match.Success)
{
return null;
}
string numberPart = match.Groups[1].Value;
string unitPart = match.Groups[2].Value;
if (!double.TryParse(numberPart, System.Globalization.NumberStyles.Any,
System.Globalization.CultureInfo.InvariantCulture, out double value))
{
return null;
}
// If no unit provided, default to seconds
if (string.IsNullOrEmpty(unitPart))
{
return TimeSpan.FromSeconds(value);
}
// Map PostgreSQL units to TimeSpan conversions
return unitPart switch
{
"us" or "usec" or "microsecond" or "microseconds" => TimeSpan.FromMicroseconds(value),
"ms" or "msec" or "millisecond" or "milliseconds" => TimeSpan.FromMilliseconds(value),
"s" or "sec" or "second" or "seconds" => TimeSpan.FromSeconds(value),
"m" or "min" or "minute" or "minutes" => TimeSpan.FromMinutes(value),
"h" or "hour" or "hours" => TimeSpan.FromHours(value),
"d" or "day" or "days" => TimeSpan.FromDays(value),
"w" or "week" or "weeks" => TimeSpan.FromDays(value * 7),
_ => null
};
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsPatternMatch(string name, string pattern)
{
if (name == null || pattern == null) return false;
int nl = name.Length, pl = pattern.Length;
if (nl == 0 || pl == 0) return false;
// Fast path: extension matching (*.ext)
if (pl > 1 && pattern[0] == Consts.Multiply && pattern[1] == Consts.Dot)
{
ReadOnlySpan<char> ext = pattern.AsSpan(1);
return nl > ext.Length && name.AsSpan(nl - ext.Length).Equals(ext, StringComparison.OrdinalIgnoreCase);
}
// Check if pattern has wildcards using SIMD
ReadOnlySpan<char> patternSpan = pattern.AsSpan();
int firstWildcard = patternSpan.IndexOfAny(WildcardChars);
// Fast path: no wildcards - simple case-insensitive comparison
if (firstWildcard == -1)
{
return name.AsSpan().Equals(patternSpan, StringComparison.OrdinalIgnoreCase);
}
// Fast path: pattern starts with literal segment
if (firstWildcard > 0)
{
// Check if name starts with the literal prefix
if (!name.AsSpan(0, Math.Min(firstWildcard, nl)).Equals(patternSpan.Slice(0, Math.Min(firstWildcard, nl)), StringComparison.OrdinalIgnoreCase))
{
return false;
}
}
// Standard wildcard matching algorithm
int ni = 0, pi = 0;
int lastStar = -1, lastMatch = 0;
while (ni < nl)
{
if (pi < pl)
{
char pc = pattern[pi];
if (pc == Consts.Multiply)
{
lastStar = pi++;
lastMatch = ni;
continue;
}
if (pc == Consts.Question ? ni < nl : char.ToLowerInvariant(pc) == char.ToLowerInvariant(name[ni]))
{
ni++;
pi++;
continue;
}
}
if (lastStar >= 0)
{
pi = lastStar + 1;
ni = ++lastMatch;
continue;
}
return false;
}
while (pi < pl && pattern[pi] == Consts.Multiply) pi++;
return pi == pl;
}
}