Optimise WriteEscapedJavaScriptString
Skip attempting write when string is empty. FirstCharToEscape was introduced for the async version and gives a faster means to identify the first block before any escaping, so may as well use it here too. Conversely, WriteEscapedStringThroughBuffer was introduced to share code between the sync and async versions, but later optimisations meant it was no longer used by the async. Inline the code back again.
This commit is contained in:
committed by
James Newton-King
parent
de82488549
commit
7a93892d03
@@ -30,6 +30,7 @@ using System.Threading;
|
|||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
#endif
|
#endif
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Diagnostics;
|
||||||
#if NET20
|
#if NET20
|
||||||
using Newtonsoft.Json.Utilities.LinqBridge;
|
using Newtonsoft.Json.Utilities.LinqBridge;
|
||||||
#else
|
#else
|
||||||
@@ -149,11 +150,29 @@ namespace Newtonsoft.Json.Utilities
|
|||||||
writer.Write(delimiter);
|
writer.Write(delimiter);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (s != null)
|
if (!string.IsNullOrEmpty(s))
|
||||||
{
|
{
|
||||||
int lastWritePosition = 0;
|
int lastWritePosition = FirstCharToEscape(s, charEscapeFlags, stringEscapeHandling);
|
||||||
|
if (lastWritePosition == -1)
|
||||||
|
{
|
||||||
|
writer.Write(s);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (lastWritePosition != 0)
|
||||||
|
{
|
||||||
|
if (writeBuffer == null || writeBuffer.Length < lastWritePosition)
|
||||||
|
{
|
||||||
|
writeBuffer = BufferUtils.EnsureBufferSize(bufferPool, lastWritePosition, writeBuffer);
|
||||||
|
}
|
||||||
|
|
||||||
for (int i = 0; i < s.Length; i++)
|
// write unchanged chars at start of text.
|
||||||
|
s.CopyTo(0, writeBuffer, 0, lastWritePosition);
|
||||||
|
writer.Write(writeBuffer, 0, lastWritePosition);
|
||||||
|
}
|
||||||
|
|
||||||
|
int length;
|
||||||
|
for (int i = lastWritePosition; i < s.Length; i++)
|
||||||
{
|
{
|
||||||
char c = s[i];
|
char c = s[i];
|
||||||
|
|
||||||
@@ -233,7 +252,7 @@ namespace Newtonsoft.Json.Utilities
|
|||||||
|
|
||||||
if (i > lastWritePosition)
|
if (i > lastWritePosition)
|
||||||
{
|
{
|
||||||
int length = i - lastWritePosition + ((isEscapedUnicodeText) ? UnicodeTextLength : 0);
|
length = i - lastWritePosition + ((isEscapedUnicodeText) ? UnicodeTextLength : 0);
|
||||||
int start = (isEscapedUnicodeText) ? UnicodeTextLength : 0;
|
int start = (isEscapedUnicodeText) ? UnicodeTextLength : 0;
|
||||||
|
|
||||||
if (writeBuffer == null || writeBuffer.Length < length)
|
if (writeBuffer == null || writeBuffer.Length < length)
|
||||||
@@ -269,19 +288,22 @@ namespace Newtonsoft.Json.Utilities
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (lastWritePosition == 0)
|
Debug.Assert(lastWritePosition != 0);
|
||||||
|
length = s.Length - lastWritePosition;
|
||||||
|
if (length > 0)
|
||||||
{
|
{
|
||||||
// no escaped text, write entire string
|
if (writeBuffer == null || writeBuffer.Length < length)
|
||||||
writer.Write(s);
|
{
|
||||||
|
writeBuffer = BufferUtils.EnsureBufferSize(bufferPool, length, writeBuffer);
|
||||||
}
|
}
|
||||||
else
|
|
||||||
{
|
s.CopyTo(lastWritePosition, writeBuffer, 0, length);
|
||||||
int length = WriteEscapedStringThroughBuffer(s, lastWritePosition, bufferPool, ref writeBuffer);
|
|
||||||
|
|
||||||
// write remaining text
|
// write remaining text
|
||||||
writer.Write(writeBuffer, 0, length);
|
writer.Write(writeBuffer, 0, length);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// trailing delimiter
|
// trailing delimiter
|
||||||
if (appendDelimiters)
|
if (appendDelimiters)
|
||||||
@@ -290,20 +312,6 @@ namespace Newtonsoft.Json.Utilities
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static int WriteEscapedStringThroughBuffer(string s, int lastWritePosition, IArrayPool<char> bufferPool, ref char[] writeBuffer)
|
|
||||||
{
|
|
||||||
int length = s.Length - lastWritePosition;
|
|
||||||
|
|
||||||
if (writeBuffer == null || writeBuffer.Length < length)
|
|
||||||
{
|
|
||||||
writeBuffer = BufferUtils.EnsureBufferSize(bufferPool, length, writeBuffer);
|
|
||||||
}
|
|
||||||
|
|
||||||
s.CopyTo(lastWritePosition, writeBuffer, 0, length);
|
|
||||||
|
|
||||||
return length;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static string ToEscapedJavaScriptString(string value, char delimiter, bool appendDelimiters, StringEscapeHandling stringEscapeHandling)
|
public static string ToEscapedJavaScriptString(string value, char delimiter, bool appendDelimiters, StringEscapeHandling stringEscapeHandling)
|
||||||
{
|
{
|
||||||
bool[] charEscapeFlags = GetCharEscapeFlags(stringEscapeHandling, delimiter);
|
bool[] charEscapeFlags = GetCharEscapeFlags(stringEscapeHandling, delimiter);
|
||||||
|
|||||||
Reference in New Issue
Block a user