~ubuntu-branches/ubuntu/trusty/smuxi/trusty-proposed

« back to all changes in this revision

Viewing changes to lib/Newtonsoft.Json/Src/Newtonsoft.Json/Utilities/JavaScriptUtils.cs

  • Committer: Package Import Robot
  • Author(s): Mirco Bauer
  • Date: 2013-05-25 22:11:31 UTC
  • mfrom: (1.2.12)
  • Revision ID: package-import@ubuntu.com-20130525221131-nd2mc0kzubuwyx20
Tags: 0.8.11-1
* [22d13d5] Imported Upstream version 0.8.11
* [6d2b95a] Refreshed patches
* [89eb66e] Added ServiceStack libraries to smuxi-engine package
* [848ab10] Enable Campfire engine
* [c6dbdc7] Always build db4o for predictable build result
* [13ec489] Exclude OS X specific libraries from dh_clideps

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
  }
 
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
    private const string EscapedUnicodeText = "!";
 
39
 
 
40
    public static void WriteEscapedJavaScriptString(TextWriter writer, string s, char delimiter, bool appendDelimiters)
 
41
    {
 
42
      // leading delimiter
 
43
      if (appendDelimiters)
 
44
        writer.Write(delimiter);
 
45
 
 
46
      if (s != null)
 
47
      {
 
48
        char[] chars = null;
 
49
        char[] unicodeBuffer = null;
 
50
        int lastWritePosition = 0;
 
51
 
 
52
        for (int i = 0; i < s.Length; i++)
 
53
        {
 
54
          var c = s[i];
 
55
 
 
56
          // don't escape standard text/numbers except '\' and the text delimiter
 
57
          if (c >= ' ' && c < 128 && c != '\\' && c != delimiter)
 
58
            continue;
 
59
 
 
60
          string escapedValue;
 
61
 
 
62
          switch (c)
 
63
          {
 
64
            case '\t':
 
65
              escapedValue = @"\t";
 
66
              break;
 
67
            case '\n':
 
68
              escapedValue = @"\n";
 
69
              break;
 
70
            case '\r':
 
71
              escapedValue = @"\r";
 
72
              break;
 
73
            case '\f':
 
74
              escapedValue = @"\f";
 
75
              break;
 
76
            case '\b':
 
77
              escapedValue = @"\b";
 
78
              break;
 
79
            case '\\':
 
80
              escapedValue = @"\\";
 
81
              break;
 
82
            case '\u0085': // Next Line
 
83
              escapedValue = @"\u0085";
 
84
              break;
 
85
            case '\u2028': // Line Separator
 
86
              escapedValue = @"\u2028";
 
87
              break;
 
88
            case '\u2029': // Paragraph Separator
 
89
              escapedValue = @"\u2029";
 
90
              break;
 
91
            case '\'':
 
92
              // this charater is being used as the delimiter
 
93
              escapedValue = @"\'";
 
94
              break;
 
95
            case '"':
 
96
              // this charater is being used as the delimiter
 
97
              escapedValue = "\\\"";
 
98
              break;
 
99
            default:
 
100
              if (c <= '\u001f')
 
101
              {
 
102
                if (unicodeBuffer == null)
 
103
                  unicodeBuffer = new char[6];
 
104
 
 
105
                StringUtils.ToCharAsUnicode(c, unicodeBuffer);
 
106
 
 
107
                // slightly hacky but it saves multiple conditions in if test
 
108
                escapedValue = EscapedUnicodeText;
 
109
              }
 
110
              else
 
111
              {
 
112
                escapedValue = null;
 
113
              }
 
114
              break;
 
115
          }
 
116
 
 
117
          if (escapedValue == null)
 
118
            continue;
 
119
 
 
120
          if (i > lastWritePosition)
 
121
          {
 
122
            if (chars == null)
 
123
              chars = s.ToCharArray();
 
124
 
 
125
            // write unchanged chars before writing escaped text
 
126
            writer.Write(chars, lastWritePosition, i - lastWritePosition);
 
127
          }
 
128
 
 
129
          lastWritePosition = i + 1;
 
130
          if (!string.Equals(escapedValue, EscapedUnicodeText))
 
131
            writer.Write(escapedValue);
 
132
          else
 
133
            writer.Write(unicodeBuffer);
 
134
        }
 
135
 
 
136
        if (lastWritePosition == 0)
 
137
        {
 
138
          // no escaped text, write entire string
 
139
          writer.Write(s);
 
140
        }
 
141
        else
 
142
        {
 
143
          if (chars == null)
 
144
            chars = s.ToCharArray();
 
145
 
 
146
          // write remaining text
 
147
          writer.Write(chars, lastWritePosition, s.Length - lastWritePosition);
 
148
        }
 
149
      }
 
150
 
 
151
      // trailing delimiter
 
152
      if (appendDelimiters)
 
153
        writer.Write(delimiter);
 
154
    }
 
155
 
 
156
    public static string ToEscapedJavaScriptString(string value)
 
157
    {
 
158
      return ToEscapedJavaScriptString(value, '"', true);
 
159
    }
 
160
 
 
161
    public static string ToEscapedJavaScriptString(string value, char delimiter, bool appendDelimiters)
 
162
    {
 
163
      using (StringWriter w = StringUtils.CreateStringWriter(StringUtils.GetLength(value) ?? 16))
 
164
      {
 
165
        WriteEscapedJavaScriptString(w, value, delimiter, appendDelimiters);
 
166
        return w.ToString();
 
167
      }
 
168
    }
 
169
  }
148
170
}
 
 
b'\\ No newline at end of file'