Skip to content

Commit 3aca4e7

Browse files
committed
fix tests
1 parent adf6b14 commit 3aca4e7

File tree

4 files changed

+19
-26
lines changed

4 files changed

+19
-26
lines changed

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,7 @@ internal static unsafe string EscapeUnescapeIri(char* pInput, int start, int end
113113
{
114114
// possibly utf8 encoded sequence of unicode
115115
int charactersRead = PercentEncodingHelper.UnescapePercentEncodedUTF8Sequence(
116-
pInput + i,
117-
end - i,
116+
new ReadOnlySpan<char>(pInput + i, end - i),
118117
ref dest,
119118
isQuery,
120119
iriParsing: true);

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@
33

44
using System.Buffers;
55
using System.Diagnostics;
6+
using System.Runtime.InteropServices;
67
using System.Text;
78

89
namespace System
910
{
1011
internal static class PercentEncodingHelper
1112
{
12-
public static unsafe int UnescapePercentEncodedUTF8Sequence(char* input, int length, ref ValueStringBuilder dest, bool isQuery, bool iriParsing)
13+
public static int UnescapePercentEncodedUTF8Sequence(ReadOnlySpan<char> input, ref ValueStringBuilder dest, bool isQuery, bool iriParsing)
1314
{
15+
int length = input.Length;
1416
// The following assertions rely on the input not mutating mid-operation, as is the case currently since callers are working with strings
1517
// If we start accepting input such as spans, this method must be audited to ensure no buffer overruns/infinite loops could occur
1618

@@ -85,15 +87,18 @@ public static unsafe int UnescapePercentEncodedUTF8Sequence(char* input, int len
8587
Debug.Assert(bytesLeftInBuffer < 4 || (fourByteBuffer & (BitConverter.IsLittleEndian ? 0x80000000 : 0x00000080)) != 0);
8688

8789
uint temp = fourByteBuffer; // make a copy so that the *copy* (not the original) is marked address-taken
88-
if (Rune.DecodeFromUtf8(new ReadOnlySpan<byte>(&temp, bytesLeftInBuffer), out Rune rune, out bytesConsumed) == OperationStatus.Done)
90+
91+
92+
93+
if (Rune.DecodeFromUtf8(MemoryMarshal.AsBytes(new ReadOnlySpan<uint>(ref temp))[..bytesLeftInBuffer], out Rune rune, out bytesConsumed) == OperationStatus.Done)
8994
{
9095
Debug.Assert(bytesConsumed >= 2, $"Rune.DecodeFromUtf8 consumed {bytesConsumed} bytes, likely indicating input was modified concurrently during UnescapePercentEncodedUTF8Sequence's execution");
9196

9297
if (!iriParsing || IriHelper.CheckIriUnicodeRange((uint)rune.Value, isQuery))
9398
{
9499
if (charsToCopy != 0)
95100
{
96-
dest.Append(new ReadOnlySpan<char>(input + totalCharsConsumed - charsToCopy, charsToCopy));
101+
dest.Append(input.Slice(totalCharsConsumed - charsToCopy, charsToCopy));
97102
charsToCopy = 0;
98103
}
99104

@@ -167,7 +172,8 @@ public static unsafe int UnescapePercentEncodedUTF8Sequence(char* input, int len
167172
return totalCharsConsumed;
168173

169174
bytesLeftInBuffer *= 3;
170-
dest.Append(new ReadOnlySpan<char>(input + totalCharsConsumed - charsToCopy, charsToCopy + bytesLeftInBuffer));
175+
176+
dest.Append(input.Slice(totalCharsConsumed - charsToCopy, charsToCopy + bytesLeftInBuffer));
171177
return totalCharsConsumed + bytesLeftInBuffer;
172178
}
173179
}

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

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1027,17 +1027,7 @@ internal bool IsBaseOfHelper(Uri uriLink)
10271027
string self = GetParts(ComponentsToCompare, UriFormat.SafeUnescaped);
10281028
string other = uriLink.GetParts(ComponentsToCompare, UriFormat.SafeUnescaped);
10291029

1030-
unsafe
1031-
{
1032-
fixed (char* selfPtr = self)
1033-
{
1034-
fixed (char* otherPtr = other)
1035-
{
1036-
return UriHelper.TestForSubPath(selfPtr, self.Length, otherPtr, other.Length,
1037-
IsUncOrDosPath || uriLink.IsUncOrDosPath);
1038-
}
1039-
}
1040-
}
1030+
return UriHelper.TestForSubPath(self, other, IsUncOrDosPath || uriLink.IsUncOrDosPath);
10411031
}
10421032

10431033
//

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

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,19 +56,18 @@ public static string NormalizeAndConcat(string? start, ReadOnlySpan<char> toNorm
5656
// ASSUMES that strings like http://host/Path/Path/MoreDir/../../ have been canonicalized before going to this method.
5757
// ASSUMES that back slashes already have been converted if applicable.
5858
//
59-
internal static unsafe bool TestForSubPath(char* selfPtr, int selfLength, char* otherPtr, int otherLength,
60-
bool ignoreCase)
59+
internal static bool TestForSubPath(ReadOnlySpan<char> self, ReadOnlySpan<char> other, bool ignoreCase)
6160
{
6261
int i = 0;
6362
char chSelf;
6463
char chOther;
6564

6665
bool AllSameBeforeSlash = true;
6766

68-
for (; i < selfLength && i < otherLength; ++i)
67+
for (; i < self.Length && i < other.Length; ++i)
6968
{
70-
chSelf = *(selfPtr + i);
71-
chOther = *(otherPtr + i);
69+
chSelf = self[i];
70+
chOther = other[i];
7271

7372
if (chSelf == '?' || chSelf == '#')
7473
{
@@ -118,9 +117,9 @@ internal static unsafe bool TestForSubPath(char* selfPtr, int selfLength, char*
118117
}
119118

120119
// If self is longer then it must not have any more path segments
121-
for (; i < selfLength; ++i)
120+
for (; i < self.Length; ++i)
122121
{
123-
if ((chSelf = *(selfPtr + i)) == '?' || chSelf == '#')
122+
if ((chSelf = self[i]) == '?' || chSelf == '#')
124123
{
125124
return true;
126125
}
@@ -507,8 +506,7 @@ internal static unsafe void UnescapeString(char* pStr, int start, int end, ref V
507506
{
508507
// Unicode
509508
int charactersRead = PercentEncodingHelper.UnescapePercentEncodedUTF8Sequence(
510-
pStr + next,
511-
end - next,
509+
new ReadOnlySpan<char>(pStr + next, end - next),
512510
ref dest,
513511
isQuery,
514512
iriParsing);

0 commit comments

Comments
 (0)