-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathFormatter.cs
More file actions
283 lines (250 loc) · 9.42 KB
/
Copy pathFormatter.cs
File metadata and controls
283 lines (250 loc) · 9.42 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
using System.Buffers;
namespace NpgsqlRest;
public static class Formatter
{
// SIMD-accelerated search for brace characters
private static readonly SearchValues<char> BraceChars = SearchValues.Create("{}");
public static ReadOnlySpan<char> FormatString(ReadOnlySpan<char> input, Dictionary<string, string> replacements)
{
if (replacements is null || replacements.Count == 0)
{
return input;
}
int inputLength = input.Length;
if (inputLength == 0)
{
return input;
}
return FormatString(input, replacements.GetAlternateLookup<ReadOnlySpan<char>>());
}
public static ReadOnlySpan<char> FormatString(ReadOnlySpan<char> input, Dictionary<string, string>.AlternateLookup<ReadOnlySpan<char>> lookup)
{
int inputLength = input.Length;
if (inputLength == 0)
{
return input;
}
// First pass: calculate result length using SIMD-accelerated search
int resultLength = 0;
bool inside = false;
int startIndex = 0;
int i = 0;
while (i < inputLength)
{
// Use SIMD to find the next brace character
var remaining = input.Slice(i);
int nextBraceOffset = remaining.IndexOfAny(BraceChars);
if (nextBraceOffset == -1)
{
// No more braces, count remaining chars
if (!inside)
{
resultLength += remaining.Length;
}
else
{
// Unclosed brace, will be handled at the end
}
break;
}
int braceIndex = i + nextBraceOffset;
char ch = input[braceIndex];
if (ch == Consts.OpenBrace)
{
if (!inside)
{
// Count chars before the brace
resultLength += nextBraceOffset;
}
else
{
// Nested open brace - treat previous content as literal
resultLength += braceIndex - startIndex;
}
inside = true;
startIndex = braceIndex;
i = braceIndex + 1;
}
else // CloseBrace
{
if (inside)
{
inside = false;
if (lookup.TryGetValue(input[(startIndex + 1)..braceIndex], out var value))
{
resultLength += value.Length;
}
else
{
resultLength += braceIndex - startIndex + 1;
}
i = braceIndex + 1;
}
else
{
// Unmatched close brace, count chars including brace
resultLength += nextBraceOffset + 1;
i = braceIndex + 1;
}
}
}
if (inside)
{
resultLength += inputLength - startIndex;
}
// Second pass: build result using SIMD-accelerated search
char[] resultArray = new char[resultLength];
Span<char> result = resultArray;
int resultPos = 0;
inside = false;
startIndex = 0;
i = 0;
while (i < inputLength)
{
var remaining = input.Slice(i);
int nextBraceOffset = remaining.IndexOfAny(BraceChars);
if (nextBraceOffset == -1)
{
if (!inside)
{
remaining.CopyTo(result.Slice(resultPos));
resultPos += remaining.Length;
}
break;
}
int braceIndex = i + nextBraceOffset;
char ch = input[braceIndex];
if (ch == Consts.OpenBrace)
{
if (!inside)
{
// Copy chars before the brace
if (nextBraceOffset > 0)
{
remaining.Slice(0, nextBraceOffset).CopyTo(result.Slice(resultPos));
resultPos += nextBraceOffset;
}
}
else
{
// Nested open brace - copy previous content as literal
var literal = input.Slice(startIndex, braceIndex - startIndex);
literal.CopyTo(result.Slice(resultPos));
resultPos += literal.Length;
}
inside = true;
startIndex = braceIndex;
i = braceIndex + 1;
}
else // CloseBrace
{
if (inside)
{
inside = false;
if (lookup.TryGetValue(input[(startIndex + 1)..braceIndex], out var value))
{
value.AsSpan().CopyTo(result.Slice(resultPos));
resultPos += value.Length;
}
else
{
var literal = input.Slice(startIndex, braceIndex - startIndex + 1);
literal.CopyTo(result.Slice(resultPos));
resultPos += literal.Length;
}
i = braceIndex + 1;
}
else
{
// Unmatched close brace, copy chars including brace
remaining.Slice(0, nextBraceOffset + 1).CopyTo(result.Slice(resultPos));
resultPos += nextBraceOffset + 1;
i = braceIndex + 1;
}
}
}
if (inside)
{
input.Slice(startIndex).CopyTo(result.Slice(resultPos));
}
return resultArray;
}
/// <summary>
/// Converts a SQL expression template with {placeholder} syntax into a parameterized SQL string
/// with $N positional parameters. Returns the parameterized SQL and a list of (name, value) pairs.
/// Placeholders are looked up from the command parameters by ActualName or ConvertedName.
/// Uses SIMD-accelerated brace search via the shared BraceChars SearchValues.
/// </summary>
internal static (string Sql, List<(string Name, object? Value)> Parameters) ParameterizeSqlExpression(
string expression,
Npgsql.NpgsqlParameterCollection commandParams)
{
ReadOnlySpan<char> span = expression;
var result = new System.Text.StringBuilder(expression.Length);
var sqlParams = new List<(string Name, object? Value)>();
int paramIndex = 0;
int pos = 0;
while (pos < span.Length)
{
var remaining = span.Slice(pos);
int braceOffset = remaining.IndexOfAny(BraceChars);
if (braceOffset == -1)
{
result.Append(remaining);
break;
}
int braceIndex = pos + braceOffset;
if (span[braceIndex] == Consts.OpenBrace)
{
// Append everything before the open brace
if (braceOffset > 0)
{
result.Append(remaining[..braceOffset]);
}
// Find closing brace
var afterBrace = span[(braceIndex + 1)..];
int closeOffset = afterBrace.IndexOf(Consts.CloseBrace);
if (closeOffset == -1)
{
// No closing brace, append open brace as literal
result.Append(Consts.OpenBrace);
pos = braceIndex + 1;
continue;
}
var placeholderSpan = span.Slice(braceIndex + 1, closeOffset);
// Look up value from command parameters using span comparison (no allocation)
object? value = DBNull.Value;
string? matchedName = null;
for (int p = 0; p < commandParams.Count; p++)
{
var param = (NpgsqlRestParameter)commandParams[p];
if (placeholderSpan.Equals(param.ActualName, StringComparison.OrdinalIgnoreCase))
{
value = param.Value ?? DBNull.Value;
matchedName = param.ActualName;
break;
}
if (placeholderSpan.Equals(param.ConvertedName, StringComparison.OrdinalIgnoreCase))
{
value = param.Value ?? DBNull.Value;
matchedName = param.ConvertedName;
break;
}
}
paramIndex++;
result.Append('$');
result.Append(paramIndex);
// Reuse matched parameter name string to avoid allocation; fall back to ToString only if unmatched
sqlParams.Add((matchedName ?? placeholderSpan.ToString(), value));
pos = braceIndex + 1 + closeOffset + 1;
}
else
{
// Stray close brace - append everything up to and including it
result.Append(remaining[..(braceOffset + 1)]);
pos = braceIndex + 1;
}
}
return (result.ToString(), sqlParams);
}
}