Skip to content

Commit

Permalink
Revert "Temporarily removing use of ReadOnlySpan indexer in Runtime.E…
Browse files Browse the repository at this point in the history
…xtensions (dotnet#25326)"

This reverts commit 1d95739.
  • Loading branch information
ahsonkhan committed Dec 16, 2017
1 parent 4110bfa commit 2e5a18a
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 16 deletions.
16 changes: 6 additions & 10 deletions src/System.Runtime.Extensions/src/System/IO/StreamReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,7 @@ private void DetectEncoding()
// there is no match. If there is no match, every byte read previously will be available
// for further consumption. If there is a match, we will compress the buffer for the
// leading preamble bytes
private unsafe bool IsPreamble()
private bool IsPreamble()
{
if (!_checkPreamble)
{
Expand All @@ -571,17 +571,13 @@ private unsafe bool IsPreamble()
Debug.Assert(_bytePos <= preamble.Length, "_compressPreamble was called with the current bytePos greater than the preamble buffer length. Are two threads using this StreamReader at the same time?");
int len = (_byteLen >= (preamble.Length)) ? (preamble.Length - _bytePos) : (_byteLen - _bytePos);

fixed (byte* preamblePtr = &preamble.DangerousGetPinnableReference())
for (int i = 0; i < len; i++, _bytePos++)
{
var preambleSpan = new Span<byte>(preamblePtr, preamble.Length);
for (int i = 0; i < len; i++, _bytePos++)
if (_byteBuffer[_bytePos] != preamble[_bytePos])
{
if (_byteBuffer[_bytePos] != preambleSpan[_bytePos])
{
_bytePos = 0;
_checkPreamble = false;
break;
}
_bytePos = 0;
_checkPreamble = false;
break;
}
}

Expand Down
8 changes: 2 additions & 6 deletions src/System.Runtime.Extensions/src/System/IO/StreamWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -375,13 +375,9 @@ private unsafe void WriteCore(ReadOnlySpan<char> buffer, bool autoFlush)
{
// For very short buffers and when we don't need to worry about running out of space
// in the char buffer, just copy the chars individually.
fixed (char* bufferPtr = &buffer.DangerousGetPinnableReference())
for (int i = 0; i < buffer.Length; i++)
{
Span<char> bufferSpan = new Span<char>(bufferPtr, buffer.Length);
for (int i = 0; i < buffer.Length; i++)
{
_charBuffer[_charPos++] = bufferSpan[i];
}
_charBuffer[_charPos++] = buffer[i];
}
}
else
Expand Down

0 comments on commit 2e5a18a

Please sign in to comment.