-
Notifications
You must be signed in to change notification settings - Fork 453
/
UniqueParametersNormalizer.cs
151 lines (129 loc) · 4.15 KB
/
UniqueParametersNormalizer.cs
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
using System;
using System.Collections.Generic;
namespace LinqToDB.DataProvider
{
/// <summary>
/// Parameter name rules, implemented by this policy:
/// <list type="bullet">
/// <item>duplicate name check is case-insensitive</item>
/// <item>max name length: 50 characters</item>
/// <item>allowed characters: ASCII digits, ASCII letters, _ (underscore).</item>
/// <item>allowed first character: ASCII letter.</item>
/// <item>default name if name missing/invalid: "p"</item>
/// <item>duplicates resolved by adding "_counter" suffix</item>
/// </list>
/// </summary>
public class UniqueParametersNormalizer : IQueryParametersNormalizer
{
private HashSet<string>? _usedParameterNames;
protected virtual StringComparer Comparer => StringComparer.OrdinalIgnoreCase;
protected virtual string DefaultName => "p";
protected virtual string CounterSeparator => "_";
protected virtual int MaxLength => 50;
/// <summary>
/// Method should validate name characters and remove or replace invalid characters.
/// Default implementation removes all characters except ASCII letters/digits and underscore.
/// </summary>
protected virtual string MakeValidName(string name)
{
var badIdx = -1;
for (var i = 0; i < name.Length; i++)
{
if (!(i == 0 ? IsValidFirstCharacter(name[i]) : IsValidCharacter(name[i])))
{
badIdx = i;
break;
}
}
if (badIdx != -1)
{
#if NET6_0_OR_GREATER
// allocate memory on the stack if possible, and prepopulate it with the original string
Span<char> newName = name.Length < 500 ? stackalloc char[name.Length] : name.ToCharArray();
if (name.Length < 500)
{
// prepopulate the stack with the original string
name.AsSpan().CopyTo(newName);
}
#else
var newName = name.ToCharArray();
#endif
var newNameLength = badIdx;
for (var i = badIdx; i < name.Length; i++)
{
var chr = name[i];
// add only allowed characters
if (newNameLength == 0 ? IsValidFirstCharacter(chr) : IsValidCharacter(chr))
newName[newNameLength++] = chr;
}
if (newNameLength > 0)
{
#if NET6_0_OR_GREATER
name = new string(newName.Slice(0, newNameLength));
#else
name = new string(newName, 0, newNameLength);
#endif
}
else
name = DefaultName;
}
return name;
}
protected virtual bool IsValidFirstCharacter(char chr)
{
#if NET8_0_OR_GREATER
return char.IsAsciiLetter(chr);
#else
return chr is >= 'a' and <= 'z'
|| chr is >= 'A' and <= 'Z';
#endif
}
protected virtual bool IsValidCharacter(char chr)
{
#if NET8_0_OR_GREATER
return chr == '_' || char.IsAsciiLetterOrDigit(chr);
#else
return chr == '_'
|| chr is >= 'a' and <= 'z'
|| chr is >= 'A' and <= 'Z'
|| chr is >= '0' and <= '9';
#endif
}
protected virtual bool IsReserved(string name) => false;
public string? Normalize(string? originalName)
{
if (string.IsNullOrEmpty(originalName))
originalName = DefaultName;
if (originalName!.Length > MaxLength)
originalName = originalName.Substring(0, MaxLength);
originalName = MakeValidName(originalName!);
string name;
while (true)
{
name = originalName;
// if the name is reserved or already in use, generate a unique name for the parameter
var cnt = 0;
while (IsReserved(name) || _usedParameterNames?.Contains(name) == true)
name = FormattableString.Invariant($"{originalName}{CounterSeparator}{++cnt}");
// if name is not too long, return it
if (name.Length <= MaxLength)
break;
// if the original name is already reduced to a single character, being the first character
// of the default name, throw an exception, so as to prevent an infinite loop
if (originalName.Length == 1)
{
originalName = originalName[0] != DefaultName[0]
? DefaultName
: throw new InvalidOperationException("Cannot sufficiently shorten original name");
}
else
{
// otherwise, shorten original name by one character and retry
originalName = originalName.Substring(0, originalName.Length - 1);
}
}
(_usedParameterNames ??= new(Comparer)).Add(name);
return name;
}
}
}