Skip to content

Commit

Permalink
Revert "Fix and optimize EscapeUnescapeIri (#32025)"
Browse files Browse the repository at this point in the history
This reverts commit dda29ff.
  • Loading branch information
jkotas authored Feb 15, 2020
1 parent 739a0d5 commit e3dd082
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 71 deletions.
77 changes: 40 additions & 37 deletions src/libraries/System.Private.Uri/src/System/IriHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,19 @@ internal static unsafe string EscapeUnescapeIri(char* pInput, int start, int end
ValueStringBuilder dest = new ValueStringBuilder(size);
byte[]? bytes = null;

const int percentEncodingLen = 3; // Escaped UTF-8 will take 3 chars: %AB.
int bufferRemaining = 0;

int next = start;
char ch;
bool escape = false;
bool surrogatePair = false;

for (; next < end; ++next)
{
escape = false;
surrogatePair = false;

if ((ch = pInput[next]) == '%')
{
if (next + 2 < end)
Expand Down Expand Up @@ -218,62 +226,57 @@ internal static unsafe string EscapeUnescapeIri(char* pInput, int start, int end
{
// unicode

bool escape;
bool surrogatePair = false;

char ch2 = '\0';
char ch2;

if ((char.IsHighSurrogate(ch)) && (next + 1 < end))
{
ch2 = pInput[next + 1];
escape = !CheckIriUnicodeRange(ch, ch2, ref surrogatePair, component == UriComponents.Query);
}
else
{
escape = !CheckIriUnicodeRange(ch, component == UriComponents.Query);
}

if (escape)
{
Span<byte> encodedBytes = stackalloc byte[4];

Rune rune;
if (surrogatePair)
{
rune = new Rune(ch, ch2);
}
else if (!Rune.TryCreate(ch, out rune))
if (!escape)
{
rune = Rune.ReplacementChar;
}

int bytesWritten = rune.EncodeToUtf8(encodedBytes);
encodedBytes = encodedBytes.Slice(0, bytesWritten);

foreach (byte b in encodedBytes)
{
UriHelper.EscapeAsciiChar(b, ref dest);
// copy the two chars
dest.Append(pInput[next++]);
dest.Append(pInput[next]);
}
}
else
{
dest.Append(ch);
if (surrogatePair)
if (CheckIriUnicodeRange(ch, component == UriComponents.Query))
{
dest.Append(ch2);
// copy it
dest.Append(pInput[next]);
}
else
{
// escape it
escape = true;
}
}

if (surrogatePair)
{
next++;
}
}
else
{
// just copy the character
dest.Append(pInput[next]);
}

if (escape)
{
const int MaxNumberOfBytesEncoded = 4;

byte[] encodedBytes = new byte[MaxNumberOfBytesEncoded];
fixed (byte* pEncodedBytes = &encodedBytes[0])
{
int encodedBytesCount = Encoding.UTF8.GetBytes(pInput + next, surrogatePair ? 2 : 1, pEncodedBytes, MaxNumberOfBytesEncoded);
Debug.Assert(encodedBytesCount <= MaxNumberOfBytesEncoded, "UTF8 encoder should not exceed specified byteCount");

bufferRemaining -= encodedBytesCount * percentEncodingLen;

for (int count = 0; count < encodedBytesCount; ++count)
{
UriHelper.EscapeAsciiChar(encodedBytes[count], ref dest);
}
}
}
}

string result = dest.ToString();
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
</PropertyGroup>
<ItemGroup>
<Compile Include="AppxUriValue.cs" />
<Compile Include="EscapeUnescapeIriTests.cs" />
<Compile Include="IdnCheckHostNameTest.cs" />
<Compile Include="IdnDnsSafeHostTest.cs" />
<Compile Include="IdnHostNameValidationTest.cs" />
Expand Down

0 comments on commit e3dd082

Please sign in to comment.