Skip to content

Commit 8667bfc

Browse files
committed
Use IndexOfAnyValues in Uri.EscapeString helper
1 parent b49fcc6 commit 8667bfc

File tree

4 files changed

+85
-142
lines changed

4 files changed

+85
-142
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ internal static unsafe string EscapeUnescapeIri(char* pInput, int start, int end
184184

185185
foreach (byte b in encodedBytes)
186186
{
187-
UriHelper.EscapeAsciiChar(b, ref dest);
187+
UriHelper.PercentEncodeByte(b, ref dest);
188188
}
189189
}
190190

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1797,7 +1797,7 @@ private static bool CheckForColonInFirstPathSegment(string uriString)
17971797

17981798
internal static string InternalEscapeString(string rawString) =>
17991799
rawString is null ? string.Empty :
1800-
UriHelper.EscapeString(rawString, checkExistingEscaped: true, UriHelper.UnreservedReservedTable, '?', '#');
1800+
UriHelper.EscapeString(rawString, checkExistingEscaped: true, UriHelper.UnreservedReservedExceptQuestionMarkHash);
18011801

18021802
//
18031803
// This method is called first to figure out the scheme or a simple file path
@@ -2359,7 +2359,7 @@ private unsafe void CreateHostString()
23592359
flags |= Flags.E_HostNotCanonical;
23602360
if (NotAny(Flags.UserEscaped))
23612361
{
2362-
host = UriHelper.EscapeString(host, checkExistingEscaped: !IsImplicitFile, UriHelper.UnreservedReservedTable, '?', '#');
2362+
host = UriHelper.EscapeString(host, checkExistingEscaped: !IsImplicitFile, UriHelper.UnreservedReservedExceptQuestionMarkHash);
23632363
}
23642364
else
23652365
{
@@ -2656,7 +2656,7 @@ private string ReCreateParts(UriComponents parts, ushort nonCanonical, UriFormat
26562656
case UriFormat.UriEscaped:
26572657
if (NotAny(Flags.UserEscaped))
26582658
{
2659-
UriHelper.EscapeString(slice, ref dest, checkExistingEscaped: true, '?', '#');
2659+
UriHelper.EscapeString(slice, ref dest, checkExistingEscaped: true, UriHelper.UnreservedReservedExceptQuestionMarkHash);
26602660
}
26612661
else
26622662
{
@@ -2802,7 +2802,7 @@ private string ReCreateParts(UriComponents parts, ushort nonCanonical, UriFormat
28022802
{
28032803
UriHelper.EscapeString(
28042804
str.AsSpan(offset, _info.Offset.Fragment - offset),
2805-
ref dest, checkExistingEscaped: true, '#');
2805+
ref dest, checkExistingEscaped: true, UriHelper.UnreservedReservedExceptHash);
28062806

28072807
goto AfterQuery;
28082808
}
@@ -2841,7 +2841,7 @@ private string ReCreateParts(UriComponents parts, ushort nonCanonical, UriFormat
28412841
{
28422842
UriHelper.EscapeString(
28432843
str.AsSpan(offset, _info.Offset.End - offset),
2844-
ref dest, checkExistingEscaped: true);
2844+
ref dest, checkExistingEscaped: true, UriHelper.UnreservedReserved);
28452845

28462846
goto AfterFragment;
28472847
}
@@ -4452,7 +4452,7 @@ private unsafe void GetCanonicalPath(ref ValueStringBuilder dest, UriFormat form
44524452

44534453
UriHelper.EscapeString(
44544454
str.Slice(_info.Offset.Path, _info.Offset.Query - _info.Offset.Path),
4455-
ref dest, checkExistingEscaped: !IsImplicitFile, '?', '#');
4455+
ref dest, checkExistingEscaped: !IsImplicitFile, UriHelper.UnreservedReservedExceptQuestionMarkHash);
44564456
}
44574457
else
44584458
{
@@ -4472,7 +4472,7 @@ private unsafe void GetCanonicalPath(ref ValueStringBuilder dest, UriFormat form
44724472
// CS8350 & CS8352: We can't pass `copy` and `dest` as arguments together as that could leak the scope of the above stackalloc
44734473
// As a workaround, re-create the Span in a way that avoids analysis
44744474
ReadOnlySpan<char> copySpan = MemoryMarshal.CreateReadOnlySpan(ref copy.GetPinnableReference(), copy.Length);
4475-
UriHelper.EscapeString(copySpan, ref dest, checkExistingEscaped: true, '\\');
4475+
UriHelper.EscapeString(copySpan, ref dest, checkExistingEscaped: true, UriHelper.UnreservedReserved);
44764476
start = dest.Length;
44774477

44784478
copy.Dispose();
@@ -4534,7 +4534,7 @@ private unsafe void GetCanonicalPath(ref ValueStringBuilder dest, UriFormat form
45344534
// CS8350 & CS8352: We can't pass `copy` and `dest` as arguments together as that could leak the scope of the above stackalloc
45354535
// As a workaround, re-create the Span in a way that avoids analysis
45364536
ReadOnlySpan<char> copySpan = MemoryMarshal.CreateReadOnlySpan(ref copy.GetPinnableReference(), copy.Length);
4537-
UriHelper.EscapeString(copySpan, ref dest, checkExistingEscaped: !IsImplicitFile, '?', '#');
4537+
UriHelper.EscapeString(copySpan, ref dest, checkExistingEscaped: !IsImplicitFile, UriHelper.UnreservedReservedExceptQuestionMarkHash);
45384538
start = dest.Length;
45394539

45404540
copy.Dispose();
@@ -5149,7 +5149,7 @@ protected virtual string Unescape(string path)
51495149
[Obsolete("Uri.EscapeString has been deprecated. Use GetComponents() or Uri.EscapeDataString to escape a Uri component or a string.")]
51505150
protected static string EscapeString(string? str) =>
51515151
str is null ? string.Empty :
5152-
UriHelper.EscapeString(str, checkExistingEscaped: true, UriHelper.UnreservedReservedTable, '?', '#');
5152+
UriHelper.EscapeString(str, checkExistingEscaped: true, UriHelper.UnreservedReservedExceptQuestionMarkHash);
51535153

51545154
//
51555155
// CheckSecurity

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ private static bool CheckForUnicodeOrEscapedUnreserved(string data)
228228
{
229229
char value = UriHelper.DecodeHexChars(data[i + 1], data[i + 2]);
230230

231-
if (value >= UriHelper.UnreservedTable.Length || UriHelper.UnreservedTable[value])
231+
if (!char.IsAscii(value) || UriHelper.Unreserved.Contains(value))
232232
{
233233
return true;
234234
}
@@ -581,12 +581,12 @@ public static string UnescapeDataString(string stringToUnescape)
581581
// This method will escape any character that is not a reserved or unreserved character, including percent signs.
582582
[Obsolete(Obsoletions.EscapeUriStringMessage, DiagnosticId = Obsoletions.EscapeUriStringDiagId, UrlFormat = Obsoletions.SharedUrlFormat)]
583583
public static string EscapeUriString(string stringToEscape) =>
584-
UriHelper.EscapeString(stringToEscape, checkExistingEscaped: false, UriHelper.UnreservedReservedTable);
584+
UriHelper.EscapeString(stringToEscape, checkExistingEscaped: false, UriHelper.UnreservedReserved);
585585

586586
// Where stringToEscape is intended to be URI data, but not an entire URI.
587587
// This method will escape any character that is not an unreserved character, including percent signs.
588588
public static string EscapeDataString(string stringToEscape) =>
589-
UriHelper.EscapeString(stringToEscape, checkExistingEscaped: false, UriHelper.UnreservedTable);
589+
UriHelper.EscapeString(stringToEscape, checkExistingEscaped: false, UriHelper.Unreserved);
590590

591591
//
592592
// Cleans up the specified component according to Iri rules
@@ -766,7 +766,7 @@ private unsafe string GetRelativeSerializationString(UriFormat format)
766766
{
767767
if (format == UriFormat.UriEscaped)
768768
{
769-
return UriHelper.EscapeString(_string, checkExistingEscaped: true, UriHelper.UnreservedReservedTable);
769+
return UriHelper.EscapeString(_string, checkExistingEscaped: true, UriHelper.UnreservedReserved);
770770
}
771771
else if (format == UriFormat.Unescaped)
772772
{

0 commit comments

Comments
 (0)