private static Task WriteEscapedJavaScriptStringWithoutDelimitersAsync(TextWriter writer, string s, bool[] charEscapeFlags, StringEscapeHandling stringEscapeHandling, JsonTextWriter client, char[] writeBuffer, CancellationToken cancellationToken) { int escape = JavaScriptUtils.FirstCharToEscape(s, charEscapeFlags, stringEscapeHandling); if (escape == -1) { return(writer.WriteAsync(s, cancellationToken)); } return(JavaScriptUtils.WriteDefinitelyEscapedJavaScriptStringWithoutDelimitersAsync(writer, s, escape, charEscapeFlags, stringEscapeHandling, client, writeBuffer, cancellationToken)); }
public static void WriteEscapedJavaScriptString([Nullable(1)] TextWriter writer, string s, char delimiter, bool appendDelimiters, [Nullable(1)] bool[] charEscapeFlags, StringEscapeHandling stringEscapeHandling, IArrayPool <char> bufferPool, ref char[] writeBuffer) { if (appendDelimiters) { writer.Write(delimiter); } if (!StringUtils.IsNullOrEmpty(s)) { int num = JavaScriptUtils.FirstCharToEscape(s, charEscapeFlags, stringEscapeHandling); if (num == -1) { writer.Write(s); } else { if (num != 0) { if (writeBuffer == null || writeBuffer.Length < num) { writeBuffer = BufferUtils.EnsureBufferSize(bufferPool, num, writeBuffer); } s.CopyTo(0, writeBuffer, 0, num); writer.Write(writeBuffer, 0, num); } int num2; for (int i = num; i < s.Length; i++) { char c = s[i]; if ((int)c >= charEscapeFlags.Length || charEscapeFlags[(int)c]) { string text; if (c <= '\\') { switch (c) { case '\b': text = "\\b"; break; case '\t': text = "\\t"; break; case '\n': text = "\\n"; break; case '\v': goto IL_154; case '\f': text = "\\f"; break; case '\r': text = "\\r"; break; default: if (c != '\\') { goto IL_154; } text = "\\\\"; break; } } else if (c != '\u0085') { if (c != '\u2028') { if (c != '\u2029') { goto IL_154; } text = "\\u2029"; } else { text = "\\u2028"; } } else { text = "\\u0085"; } IL_1D3: if (text == null) { goto IL_295; } bool flag = string.Equals(text, "!", StringComparison.Ordinal); if (i > num) { num2 = i - num + (flag ? 6 : 0); int num3 = flag ? 6 : 0; if (writeBuffer == null || writeBuffer.Length < num2) { char[] array = BufferUtils.RentBuffer(bufferPool, num2); if (flag) { Array.Copy(writeBuffer, array, 6); } BufferUtils.ReturnBuffer(bufferPool, writeBuffer); writeBuffer = array; } s.CopyTo(num, writeBuffer, num3, num2 - num3); writer.Write(writeBuffer, num3, num2 - num3); } num = i + 1; if (!flag) { writer.Write(text); goto IL_295; } writer.Write(writeBuffer, 0, 6); goto IL_295; IL_154: if ((int)c >= charEscapeFlags.Length && stringEscapeHandling != StringEscapeHandling.EscapeNonAscii) { text = null; goto IL_1D3; } if (c == '\'' && stringEscapeHandling != StringEscapeHandling.EscapeHtml) { text = "\\'"; goto IL_1D3; } if (c == '"' && stringEscapeHandling != StringEscapeHandling.EscapeHtml) { text = "\\\""; goto IL_1D3; } if (writeBuffer == null || writeBuffer.Length < 6) { writeBuffer = BufferUtils.EnsureBufferSize(bufferPool, 6, writeBuffer); } StringUtils.ToCharAsUnicode(c, writeBuffer); text = "!"; goto IL_1D3; } IL_295 :; } num2 = s.Length - num; if (num2 > 0) { if (writeBuffer == null || writeBuffer.Length < num2) { writeBuffer = BufferUtils.EnsureBufferSize(bufferPool, num2, writeBuffer); } s.CopyTo(num, writeBuffer, 0, num2); writer.Write(writeBuffer, 0, num2); } } } if (appendDelimiters) { writer.Write(delimiter); } }
public static void WriteEscapedJavaScriptString(TextWriter writer, string s, char delimiter, bool appendDelimiters, bool[] charEscapeFlags, StringEscapeHandling stringEscapeHandling, IArrayPool <char> bufferPool, ref char[] writeBuffer) { int length; char chr; string str; if (appendDelimiters) { writer.Write(delimiter); } if (!string.IsNullOrEmpty(s)) { int escape = JavaScriptUtils.FirstCharToEscape(s, charEscapeFlags, stringEscapeHandling); if (escape != -1) { if (escape != 0) { if (writeBuffer == null || (int)writeBuffer.Length < escape) { writeBuffer = BufferUtils.EnsureBufferSize(bufferPool, escape, writeBuffer); } s.CopyTo(0, writeBuffer, 0, escape); writer.Write(writeBuffer, 0, escape); } for (int i = escape; i < s.Length; i++) { chr = s[i]; if (chr >= (char)((int)charEscapeFlags.Length) || charEscapeFlags[chr]) { if (chr <= '\\') { switch (chr) { case '\b': { str = "\\b"; break; } case '\t': { str = "\\t"; break; } case '\n': { str = "\\n"; break; } case '\v': { goto Label0; } case '\f': { str = "\\f"; break; } case '\r': { str = "\\r"; break; } default: { if (chr == '\\') { str = "\\\\"; break; } else { goto Label0; } } } } else if (chr == '\u0085') { str = "\\u0085"; } else if (chr == '\u2028') { str = "\\u2028"; } else { if (chr != '\u2029') { goto Label0; } str = "\\u2029"; } Label1: if (str != null) { bool flag = string.Equals(str, "!"); if (i > escape) { length = i - escape + (flag ? 6 : 0); int num = (flag ? 6 : 0); if (writeBuffer == null || (int)writeBuffer.Length < length) { char[] chrArray = BufferUtils.RentBuffer(bufferPool, length); if (flag) { Array.Copy(writeBuffer, chrArray, 6); } BufferUtils.ReturnBuffer(bufferPool, writeBuffer); writeBuffer = chrArray; } s.CopyTo(escape, writeBuffer, num, length - num); writer.Write(writeBuffer, num, length - num); } escape = i + 1; if (flag) { writer.Write(writeBuffer, 0, 6); } else { writer.Write(str); } } } } length = s.Length - escape; if (length > 0) { if (writeBuffer == null || (int)writeBuffer.Length < length) { writeBuffer = BufferUtils.EnsureBufferSize(bufferPool, length, writeBuffer); } s.CopyTo(escape, writeBuffer, 0, length); writer.Write(writeBuffer, 0, length); } } else { writer.Write(s); } } if (appendDelimiters) { writer.Write(delimiter); } return; Label0: if (chr >= (char)((int)charEscapeFlags.Length)) { if (stringEscapeHandling == StringEscapeHandling.EscapeNonAscii) { goto Label3; } str = null; goto Label1; } if (chr == '\'' && stringEscapeHandling != StringEscapeHandling.EscapeHtml) { str = "\\'"; goto Label1; } else if (chr != '\"' || stringEscapeHandling == StringEscapeHandling.EscapeHtml) { if (writeBuffer == null || (int)writeBuffer.Length < 6) { writeBuffer = BufferUtils.EnsureBufferSize(bufferPool, 6, writeBuffer); } StringUtils.ToCharAsUnicode(chr, writeBuffer); str = "!"; goto Label1; } else { str = "\\\""; goto Label1; } }