Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/mono/Newtonsoft.Json.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'Src/Newtonsoft.Json/Utilities/JavaScriptUtils.cs')
-rw-r--r--Src/Newtonsoft.Json/Utilities/JavaScriptUtils.cs64
1 files changed, 38 insertions, 26 deletions
diff --git a/Src/Newtonsoft.Json/Utilities/JavaScriptUtils.cs b/Src/Newtonsoft.Json/Utilities/JavaScriptUtils.cs
index b502394..d4ee35e 100644
--- a/Src/Newtonsoft.Json/Utilities/JavaScriptUtils.cs
+++ b/Src/Newtonsoft.Json/Utilities/JavaScriptUtils.cs
@@ -35,21 +35,29 @@ namespace Newtonsoft.Json.Utilities
{
internal static class JavaScriptUtils
{
- public static void WriteEscapedJavaScriptString(TextWriter writer, string value, char delimiter, bool appendDelimiters)
+ public static void WriteEscapedJavaScriptString(TextWriter writer, string s, char delimiter, bool appendDelimiters)
{
// leading delimiter
if (appendDelimiters)
writer.Write(delimiter);
- if (value != null)
+ if (s != null)
{
- int lastWritePosition = 0;
- int skipped = 0;
char[] chars = null;
+ int lastWritePosition = -1;
- for (int i = 0; i < value.Length; i++)
+ for (var index = 0; index < s.Length; ++index)
{
- char c = value[i];
+ var c = s[index];
+
+ if (c >= ' ' && c < 128 && c != '\"' && c != '\\' && c != '\'')
+ {
+ if (lastWritePosition == -1)
+ lastWritePosition = index;
+
+ continue;
+ }
+
string escapedValue;
switch (c)
@@ -94,35 +102,39 @@ namespace Newtonsoft.Json.Utilities
break;
}
- if (escapedValue != null)
+ if (escapedValue == null)
{
- if (chars == null)
- chars = value.ToCharArray();
-
- // write skipped text
- if (skipped > 0)
- {
- writer.Write(chars, lastWritePosition, skipped);
- skipped = 0;
- }
-
- // write escaped value and note position
- writer.Write(escapedValue);
- lastWritePosition = i + 1;
+ if (lastWritePosition == -1)
+ lastWritePosition = index;
+
+ continue;
}
- else
+
+ if (lastWritePosition != -1)
{
- skipped++;
+ if (chars == null)
+ chars = s.ToCharArray();
+
+ writer.Write(chars, lastWritePosition, index - lastWritePosition);
+ lastWritePosition = -1;
}
+
+ writer.Write(escapedValue);
}
- // write any remaining skipped text
- if (skipped > 0)
+ if (lastWritePosition != -1)
{
if (lastWritePosition == 0)
- writer.Write(value);
+ {
+ writer.Write(s);
+ }
else
- writer.Write(chars, lastWritePosition, skipped);
+ {
+ if (chars == null)
+ chars = s.ToCharArray();
+
+ writer.Write(chars, lastWritePosition, s.Length - lastWritePosition);
+ }
}
}