-
Notifications
You must be signed in to change notification settings - Fork 9.8k
/
QueryStringEnumerable.cs
231 lines (204 loc) · 7.99 KB
/
QueryStringEnumerable.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
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Buffers;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Intrinsics;
using System.Runtime.Intrinsics.X86;
#if QueryStringEnumerable_In_WebUtilities
namespace Microsoft.AspNetCore.WebUtilities;
#else
namespace Microsoft.AspNetCore.Internal;
#endif
/// <summary>
/// An enumerable that can supply the name/value pairs from a URI query string.
/// </summary>
#if QueryStringEnumerable_In_WebUtilities
public
#else
internal
#endif
readonly struct QueryStringEnumerable
{
private readonly ReadOnlyMemory<char> _queryString;
/// <summary>
/// Constructs an instance of <see cref="QueryStringEnumerable"/>.
/// </summary>
/// <param name="queryString">The query string.</param>
public QueryStringEnumerable(string? queryString)
: this(queryString.AsMemory())
{
}
/// <summary>
/// Constructs an instance of <see cref="QueryStringEnumerable"/>.
/// </summary>
/// <param name="queryString">The query string.</param>
public QueryStringEnumerable(ReadOnlyMemory<char> queryString)
{
_queryString = queryString;
}
/// <summary>
/// Retrieves an object that can iterate through the name/value pairs in the query string.
/// </summary>
/// <returns>An object that can iterate through the name/value pairs in the query string.</returns>
public Enumerator GetEnumerator()
=> new Enumerator(_queryString);
/// <summary>
/// Represents a single name/value pair extracted from a query string during enumeration.
/// </summary>
public readonly struct EncodedNameValuePair
{
/// <summary>
/// Gets the name from this name/value pair in its original encoded form.
/// To get the decoded string, call <see cref="DecodeName"/>.
/// </summary>
public readonly ReadOnlyMemory<char> EncodedName { get; }
/// <summary>
/// Gets the value from this name/value pair in its original encoded form.
/// To get the decoded string, call <see cref="DecodeValue"/>.
/// </summary>
public readonly ReadOnlyMemory<char> EncodedValue { get; }
internal EncodedNameValuePair(ReadOnlyMemory<char> encodedName, ReadOnlyMemory<char> encodedValue)
{
EncodedName = encodedName;
EncodedValue = encodedValue;
}
/// <summary>
/// Decodes the name from this name/value pair.
/// </summary>
/// <returns>Characters representing the decoded name.</returns>
public ReadOnlyMemory<char> DecodeName()
=> Decode(EncodedName);
/// <summary>
/// Decodes the value from this name/value pair.
/// </summary>
/// <returns>Characters representing the decoded value.</returns>
public ReadOnlyMemory<char> DecodeValue()
=> Decode(EncodedValue);
private static ReadOnlyMemory<char> Decode(ReadOnlyMemory<char> chars)
{
// If the value is short, it's cheap to check up front if it really needs decoding. If it doesn't,
// then we can save some allocations.
return chars.Length < 16 && chars.Span.IndexOfAny('%', '+') < 0
? chars
: Uri.UnescapeDataString(SpanHelper.ReplacePlusWithSpace(chars.Span)).AsMemory();
}
}
/// <summary>
/// An enumerator that supplies the name/value pairs from a URI query string.
/// </summary>
public struct Enumerator
{
private ReadOnlyMemory<char> _query;
internal Enumerator(ReadOnlyMemory<char> query)
{
Current = default;
_query = query.IsEmpty || query.Span[0] != '?'
? query
: query.Slice(1);
}
/// <summary>
/// Gets the currently referenced key/value pair in the query string being enumerated.
/// </summary>
public EncodedNameValuePair Current { get; private set; }
/// <summary>
/// Moves to the next key/value pair in the query string being enumerated.
/// </summary>
/// <returns>True if there is another key/value pair, otherwise false.</returns>
public bool MoveNext()
{
while (!_query.IsEmpty)
{
// Chomp off the next segment
ReadOnlyMemory<char> segment;
var delimiterIndex = _query.Span.IndexOf('&');
if (delimiterIndex >= 0)
{
segment = _query.Slice(0, delimiterIndex);
_query = _query.Slice(delimiterIndex + 1);
}
else
{
segment = _query;
_query = default;
}
// If it's nonempty, emit it
var equalIndex = segment.Span.IndexOf('=');
if (equalIndex >= 0)
{
Current = new EncodedNameValuePair(
segment.Slice(0, equalIndex),
segment.Slice(equalIndex + 1));
return true;
}
else if (!segment.IsEmpty)
{
Current = new EncodedNameValuePair(segment, default);
return true;
}
}
Current = default;
return false;
}
}
private static class SpanHelper
{
private static readonly SpanAction<char, IntPtr> s_replacePlusWithSpace = ReplacePlusWithSpaceCore;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe string ReplacePlusWithSpace(ReadOnlySpan<char> span)
{
fixed (char* ptr = &MemoryMarshal.GetReference(span))
{
return string.Create(span.Length, (IntPtr)ptr, s_replacePlusWithSpace);
}
}
private static unsafe void ReplacePlusWithSpaceCore(Span<char> buffer, IntPtr state)
{
fixed (char* ptr = &MemoryMarshal.GetReference(buffer))
{
var input = (ushort*)state.ToPointer();
var output = (ushort*)ptr;
var i = (nint)0;
var n = (nint)(uint)buffer.Length;
if (Vector256.IsHardwareAccelerated && n >= Vector256<ushort>.Count)
{
var vecPlus = Vector256.Create((ushort)'+');
var vecSpace = Vector256.Create((ushort)' ');
do
{
var vec = Vector256.Load(input + i);
var mask = Vector256.Equals(vec, vecPlus);
var res = Vector256.ConditionalSelect(mask, vecSpace, vec);
res.Store(output + i);
i += Vector256<ushort>.Count;
} while (i <= n - Vector256<ushort>.Count);
}
if (Vector128.IsHardwareAccelerated && n - i >= Vector128<ushort>.Count)
{
var vecPlus = Vector128.Create((ushort)'+');
var vecSpace = Vector128.Create((ushort)' ');
do
{
var vec = Vector128.Load(input + i);
var mask = Vector128.Equals(vec, vecPlus);
var res = Vector128.ConditionalSelect(mask, vecSpace, vec);
res.Store(output + i);
i += Vector128<ushort>.Count;
} while (i <= n - Vector128<ushort>.Count);
}
for (; i < n; ++i)
{
if (input[i] != '+')
{
output[i] = input[i];
}
else
{
output[i] = ' ';
}
}
}
}
}
}