Skip to content

Commit

Permalink
Use IndexOfAnyValues in Uri.EscapeString helper (#79024)
Browse files Browse the repository at this point in the history
  • Loading branch information
MihaZupan committed Nov 30, 2022
1 parent 8cc8392 commit c01ad86
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 142 deletions.
2 changes: 1 addition & 1 deletion src/libraries/System.Private.Uri/src/System/IriHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ internal static unsafe string EscapeUnescapeIri(char* pInput, int start, int end

foreach (byte b in encodedBytes)
{
UriHelper.EscapeAsciiChar(b, ref dest);
UriHelper.PercentEncodeByte(b, ref dest);
}
}

Expand Down
18 changes: 9 additions & 9 deletions src/libraries/System.Private.Uri/src/System/Uri.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1797,7 +1797,7 @@ private static bool CheckForColonInFirstPathSegment(string uriString)

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

//
// This method is called first to figure out the scheme or a simple file path
Expand Down Expand Up @@ -2359,7 +2359,7 @@ private unsafe void CreateHostString()
flags |= Flags.E_HostNotCanonical;
if (NotAny(Flags.UserEscaped))
{
host = UriHelper.EscapeString(host, checkExistingEscaped: !IsImplicitFile, UriHelper.UnreservedReservedTable, '?', '#');
host = UriHelper.EscapeString(host, checkExistingEscaped: !IsImplicitFile, UriHelper.UnreservedReservedExceptQuestionMarkHash);
}
else
{
Expand Down Expand Up @@ -2656,7 +2656,7 @@ private string ReCreateParts(UriComponents parts, ushort nonCanonical, UriFormat
case UriFormat.UriEscaped:
if (NotAny(Flags.UserEscaped))
{
UriHelper.EscapeString(slice, ref dest, checkExistingEscaped: true, '?', '#');
UriHelper.EscapeString(slice, ref dest, checkExistingEscaped: true, UriHelper.UnreservedReservedExceptQuestionMarkHash);
}
else
{
Expand Down Expand Up @@ -2802,7 +2802,7 @@ private string ReCreateParts(UriComponents parts, ushort nonCanonical, UriFormat
{
UriHelper.EscapeString(
str.AsSpan(offset, _info.Offset.Fragment - offset),
ref dest, checkExistingEscaped: true, '#');
ref dest, checkExistingEscaped: true, UriHelper.UnreservedReservedExceptHash);

goto AfterQuery;
}
Expand Down Expand Up @@ -2841,7 +2841,7 @@ private string ReCreateParts(UriComponents parts, ushort nonCanonical, UriFormat
{
UriHelper.EscapeString(
str.AsSpan(offset, _info.Offset.End - offset),
ref dest, checkExistingEscaped: true);
ref dest, checkExistingEscaped: true, UriHelper.UnreservedReserved);

goto AfterFragment;
}
Expand Down Expand Up @@ -4452,7 +4452,7 @@ private unsafe void GetCanonicalPath(ref ValueStringBuilder dest, UriFormat form

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

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

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

//
// CheckSecurity
Expand Down
8 changes: 4 additions & 4 deletions src/libraries/System.Private.Uri/src/System/UriExt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ private static bool CheckForUnicodeOrEscapedUnreserved(string data)
{
char value = UriHelper.DecodeHexChars(data[i + 1], data[i + 2]);

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

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

//
// Cleans up the specified component according to Iri rules
Expand Down Expand Up @@ -766,7 +766,7 @@ private unsafe string GetRelativeSerializationString(UriFormat format)
{
if (format == UriFormat.UriEscaped)
{
return UriHelper.EscapeString(_string, checkExistingEscaped: true, UriHelper.UnreservedReservedTable);
return UriHelper.EscapeString(_string, checkExistingEscaped: true, UriHelper.UnreservedReserved);
}
else if (format == UriFormat.Unescaped)
{
Expand Down
Loading

0 comments on commit c01ad86

Please sign in to comment.