Skip to content

Commit 76dbb27

Browse files
authored
Use SearchValues in Uri.CheckForUnicodeOrEscapedUnreserved (dotnet#107357)
1 parent 149d4bb commit 76dbb27

File tree

1 file changed

+34
-16
lines changed
  • src/libraries/System.Private.Uri/src/System

1 file changed

+34
-16
lines changed

src/libraries/System.Private.Uri/src/System/UriExt.cs

+34-16
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System.Buffers;
45
using System.Diagnostics;
56
using System.Diagnostics.CodeAnalysis;
67
using System.Globalization;
@@ -216,32 +217,49 @@ private void InitializeUri(ParsingError err, UriKind uriKind, out UriFormatExcep
216217
}
217218
}
218219

219-
// Unescapes entire string and checks if it has unicode chars
220-
// Also checks for sequences that are 3986 Unreserved characters as these should be un-escaped
220+
/// <summary>SearchValues for all ASCII characters other than %</summary>
221+
private static readonly SearchValues<char> s_asciiOtherThanPercent = SearchValues.Create(
222+
"\u0000\u0001\u0002\u0003\u0004\u0005\u0006\u0007\u0008\u0009\u000A\u000B\u000C\u000D\u000E\u000F" +
223+
"\u0010\u0011\u0012\u0013\u0014\u0015\u0016\u0017\u0018\u0019\u001A\u001B\u001C\u001D\u001E\u001F" +
224+
"\u0020\u0021\u0022\u0023\u0024" + "\u0026\u0027\u0028\u0029\u002A\u002B\u002C\u002D\u002E\u002F" +
225+
"\u0030\u0031\u0032\u0033\u0034\u0035\u0036\u0037\u0038\u0039\u003A\u003B\u003C\u003D\u003E\u003F" +
226+
"\u0040\u0041\u0042\u0043\u0044\u0045\u0046\u0047\u0048\u0049\u004A\u004B\u004C\u004D\u004E\u004F" +
227+
"\u0050\u0051\u0052\u0053\u0054\u0055\u0056\u0057\u0058\u0059\u005A\u005B\u005C\u005D\u005E\u005F" +
228+
"\u0060\u0061\u0062\u0063\u0064\u0065\u0066\u0067\u0068\u0069\u006A\u006B\u006C\u006D\u006E\u006F" +
229+
"\u0070\u0071\u0072\u0073\u0074\u0075\u0076\u0077\u0078\u0079\u007A\u007B\u007C\u007D\u007E\u007F");
230+
231+
/// <summary>
232+
/// Unescapes entire string and checks if it has unicode chars.Also checks for sequences that are 3986 Unreserved characters as these should be un-escaped
233+
/// </summary>
221234
private static bool CheckForUnicodeOrEscapedUnreserved(string data)
222235
{
223-
for (int i = 0; i < data.Length; i++)
236+
int i = data.AsSpan().IndexOfAnyExcept(s_asciiOtherThanPercent);
237+
if (i >= 0)
224238
{
225-
char c = data[i];
226-
if (c == '%')
239+
for ( ; i < data.Length; i++)
227240
{
228-
if ((uint)(i + 2) < (uint)data.Length)
241+
char c = data[i];
242+
if (c == '%')
229243
{
230-
char value = UriHelper.DecodeHexChars(data[i + 1], data[i + 2]);
231-
232-
if (!char.IsAscii(value) || UriHelper.Unreserved.Contains(value))
244+
if ((uint)(i + 2) < (uint)data.Length)
233245
{
234-
return true;
235-
}
246+
char value = UriHelper.DecodeHexChars(data[i + 1], data[i + 2]);
247+
248+
if (!char.IsAscii(value) || UriHelper.Unreserved.Contains(value))
249+
{
250+
return true;
251+
}
236252

237-
i += 2;
253+
i += 2;
254+
}
255+
}
256+
else if (c > 0x7F)
257+
{
258+
return true;
238259
}
239-
}
240-
else if (c > 0x7F)
241-
{
242-
return true;
243260
}
244261
}
262+
245263
return false;
246264
}
247265

0 commit comments

Comments
 (0)