All files
[pithos-ms-client] / trunk / Libraries / Json40r2 / Source / Src / Newtonsoft.Json / Utilities / JavaScriptUtils.cs
1 #region License
2 // Copyright (c) 2007 James Newton-King
3 //
4 // Permission is hereby granted, free of charge, to any person
5 // obtaining a copy of this software and associated documentation
6 // files (the "Software"), to deal in the Software without
7 // restriction, including without limitation the rights to use,
8 // copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the
10 // Software is furnished to do so, subject to the following
11 // conditions:
12 //
13 // The above copyright notice and this permission notice shall be
14 // included in all copies or substantial portions of the Software.
15 //
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18 // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20 // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21 // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 // OTHER DEALINGS IN THE SOFTWARE.
24 #endregion
25
26 using System;
27 using System.Collections;
28 using System.Globalization;
29 using System.IO;
30 using System.Text;
31 using System.Text.RegularExpressions;
32 using System.Collections.Generic;
33
34 namespace Newtonsoft.Json.Utilities
35 {
36   internal static class JavaScriptUtils
37   {
38     public static void WriteEscapedJavaScriptString(TextWriter writer, string value, char delimiter, bool appendDelimiters)
39     {
40       // leading delimiter
41       if (appendDelimiters)
42         writer.Write(delimiter);
43
44       if (value != null)
45       {
46         int lastWritePosition = 0;
47         int skipped = 0;
48         char[] chars = null;
49
50         for (int i = 0; i < value.Length; i++)
51         {
52           char c = value[i];
53           string escapedValue;
54
55           switch (c)
56           {
57             case '\t':
58               escapedValue = @"\t";
59               break;
60             case '\n':
61               escapedValue = @"\n";
62               break;
63             case '\r':
64               escapedValue = @"\r";
65               break;
66             case '\f':
67               escapedValue = @"\f";
68               break;
69             case '\b':
70               escapedValue = @"\b";
71               break;
72             case '\\':
73               escapedValue = @"\\";
74               break;
75             case '\u0085': // Next Line
76               escapedValue = @"\u0085";
77               break;
78             case '\u2028': // Line Separator
79               escapedValue = @"\u2028";
80               break;
81             case '\u2029': // Paragraph Separator
82               escapedValue = @"\u2029";
83               break;
84             case '\'':
85               // only escape if this charater is being used as the delimiter
86               escapedValue = (delimiter == '\'') ? @"\'" : null;
87               break;
88             case '"':
89               // only escape if this charater is being used as the delimiter
90               escapedValue = (delimiter == '"') ? "\\\"" : null;
91               break;
92             default:
93               escapedValue = (c <= '\u001f') ? StringUtils.ToCharAsUnicode(c) : null;
94               break;
95           }
96
97           if (escapedValue != null)
98           {
99             if (chars == null)
100               chars = value.ToCharArray();
101
102             // write skipped text
103             if (skipped > 0)
104             {
105               writer.Write(chars, lastWritePosition, skipped);
106               skipped = 0;
107             }
108
109             // write escaped value and note position
110             writer.Write(escapedValue);
111             lastWritePosition = i + 1;
112           }
113           else
114           {
115             skipped++;
116           }
117         }
118
119         // write any remaining skipped text
120         if (skipped > 0)
121         {
122           if (lastWritePosition == 0)
123             writer.Write(value);
124           else
125             writer.Write(chars, lastWritePosition, skipped);
126         }
127       }
128
129       // trailing delimiter
130       if (appendDelimiters)
131         writer.Write(delimiter);
132     }
133
134     public static string ToEscapedJavaScriptString(string value)
135     {
136       return ToEscapedJavaScriptString(value, '"', true);
137     }
138
139     public static string ToEscapedJavaScriptString(string value, char delimiter, bool appendDelimiters)
140     {
141       using (StringWriter w = StringUtils.CreateStringWriter(StringUtils.GetLength(value) ?? 16))
142       {
143         WriteEscapedJavaScriptString(w, value, delimiter, appendDelimiters);
144         return w.ToString();
145       }
146     }
147   }
148 }