Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit f8057cc

Browse files
authored
Temporarily removing use of ReadOnlySpan indexer. (#25881)
1 parent c73ba13 commit f8057cc

File tree

5 files changed

+201
-181
lines changed

5 files changed

+201
-181
lines changed

src/Common/src/System/Memory/FixedBufferExtensions.cs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,21 @@ internal unsafe static bool FixedBufferEqualsString(this ReadOnlySpan<char> span
3535
if (value == null || value.Length > span.Length)
3636
return false;
3737

38-
int i = 0;
39-
for (; i < value.Length; i++)
38+
fixed (char* spanPtr = &span.DangerousGetPinnableReference())
4039
{
41-
// Strings with embedded nulls can never match as the fixed buffer always null terminates.
42-
if (value[i] == '\0' || value[i] != span[i])
43-
return false;
44-
}
40+
var readWriteSpan = new Span<char>(spanPtr, span.Length);
41+
int i = 0;
42+
for (; i < value.Length; i++)
43+
{
44+
// Strings with embedded nulls can never match as the fixed buffer always null terminates.
45+
if (value[i] == '\0' || value[i] != readWriteSpan[i])
46+
return false;
47+
}
4548

46-
// If we've maxed out the buffer or reached the
47-
// null terminator, we're equal.
48-
return i == span.Length || span[i] == '\0';
49+
// If we've maxed out the buffer or reached the
50+
// null terminator, we're equal.
51+
return i == span.Length || readWriteSpan[i] == '\0';
52+
}
4953
}
5054
}
5155
}

src/System.IO.FileSystem/src/System/IO/PathHelpers.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -172,17 +172,21 @@ public static unsafe bool IsDotOrDotDot(ReadOnlySpan<char> fileName)
172172
|| (fileName.Length == 2 && fileName[1] != '.'));
173173
}
174174

175-
public static ReadOnlySpan<char> GetDirectoryNameNoChecks(ReadOnlySpan<char> path)
175+
public static unsafe ReadOnlySpan<char> GetDirectoryNameNoChecks(ReadOnlySpan<char> path)
176176
{
177177
if (path.Length == 0)
178178
return ReadOnlySpan<char>.Empty;
179179

180180
int root = PathInternal.GetRootLength(path);
181181
int i = path.Length;
182-
if (i > root)
182+
fixed (char* pathPtr = &path.DangerousGetPinnableReference())
183183
{
184-
while (i > root && !PathInternal.IsDirectorySeparator(path[--i])) ;
185-
return path.Slice(0, i);
184+
var pathSpan = new Span<char>(pathPtr, path.Length);
185+
if (i > root)
186+
{
187+
while (i > root && !PathInternal.IsDirectorySeparator(pathSpan[--i])) ;
188+
return pathSpan.Slice(0, i);
189+
}
186190
}
187191

188192
return ReadOnlySpan<char>.Empty;

src/System.Private.Xml/src/System/Xml/Core/XmlTextReaderImpl.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3212,16 +3212,20 @@ private void SetupEncoding(Encoding encoding)
32123212
}
32133213
}
32143214

3215-
private void EatPreamble()
3215+
private unsafe void EatPreamble()
32163216
{
32173217
ReadOnlySpan<byte> preamble = _ps.encoding.Preamble;
32183218
int preambleLen = preamble.Length;
32193219
int i;
3220-
for (i = 0; i < preambleLen && i < _ps.bytesUsed; i++)
3220+
fixed (byte* preamblePtr = &preamble.DangerousGetPinnableReference())
32213221
{
3222-
if (_ps.bytes[i] != preamble[i])
3222+
var preambleSpan = new Span<byte>(preamblePtr, preambleLen);
3223+
for (i = 0; i < preambleLen && i < _ps.bytesUsed; i++)
32233224
{
3224-
break;
3225+
if (_ps.bytes[i] != preambleSpan[i])
3226+
{
3227+
break;
3228+
}
32253229
}
32263230
}
32273231
if (i == preambleLen)

0 commit comments

Comments
 (0)