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

« back to all changes in this revision

Viewing changes to lib/Newtonsoft.Json/Src/Newtonsoft.Json/Utilities/StringUtils.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.Generic;
28
 
using System.IO;
29
 
using System.Text;
30
 
using System.Text.RegularExpressions;
31
 
using System.Linq;
32
 
using System.Globalization;
33
 
 
34
 
namespace Newtonsoft.Json.Utilities
35
 
{
36
 
  internal static class StringUtils
37
 
  {
38
 
    public const string CarriageReturnLineFeed = "\r\n";
39
 
    public const string Empty = "";
40
 
    public const char CarriageReturn = '\r';
41
 
    public const char LineFeed = '\n';
42
 
    public const char Tab = '\t';
43
 
 
44
 
    //public static string FormatWith(this string format, params object[] args)
45
 
    //{
46
 
    //  return FormatWith(format, null, args);
47
 
    //}
48
 
 
49
 
    public static string FormatWith(this string format, IFormatProvider provider, params object[] args)
50
 
    {
51
 
      ValidationUtils.ArgumentNotNull(format, "format");
52
 
 
53
 
      return string.Format(provider, format, args);
54
 
    }
55
 
 
56
 
    /// <summary>
57
 
    /// Determines whether the string contains white space.
58
 
    /// </summary>
59
 
    /// <param name="s">The string to test for white space.</param>
60
 
    /// <returns>
61
 
    ///         <c>true</c> if the string contains white space; otherwise, <c>false</c>.
62
 
    /// </returns>
63
 
    public static bool ContainsWhiteSpace(string s)
64
 
    {
65
 
      if (s == null)
66
 
        throw new ArgumentNullException("s");
67
 
 
68
 
      for (int i = 0; i < s.Length; i++)
69
 
      {
70
 
        if (char.IsWhiteSpace(s[i]))
71
 
          return true;
72
 
      }
73
 
      return false;
74
 
    }
75
 
 
76
 
    /// <summary>
77
 
    /// Determines whether the string is all white space. Empty string will return false.
78
 
    /// </summary>
79
 
    /// <param name="s">The string to test whether it is all white space.</param>
80
 
    /// <returns>
81
 
    ///         <c>true</c> if the string is all white space; otherwise, <c>false</c>.
82
 
    /// </returns>
83
 
    public static bool IsWhiteSpace(string s)
84
 
    {
85
 
      if (s == null)
86
 
        throw new ArgumentNullException("s");
87
 
 
88
 
      if (s.Length == 0)
89
 
        return false;
90
 
 
91
 
      for (int i = 0; i < s.Length; i++)
92
 
      {
93
 
        if (!char.IsWhiteSpace(s[i]))
94
 
          return false;
95
 
      }
96
 
 
97
 
      return true;
98
 
    }
99
 
 
100
 
    /// <summary>
101
 
    /// Ensures the target string ends with the specified string.
102
 
    /// </summary>
103
 
    /// <param name="target">The target.</param>
104
 
    /// <param name="value">The value.</param>
105
 
    /// <returns>The target string with the value string at the end.</returns>
106
 
    public static string EnsureEndsWith(string target, string value)
107
 
    {
108
 
      if (target == null)
109
 
        throw new ArgumentNullException("target");
110
 
 
111
 
      if (value == null)
112
 
        throw new ArgumentNullException("value");
113
 
 
114
 
      if (target.Length >= value.Length)
115
 
      {
116
 
        if (string.Compare(target, target.Length - value.Length, value, 0, value.Length, StringComparison.OrdinalIgnoreCase) ==
117
 
                        0)
118
 
          return target;
119
 
 
120
 
        string trimmedString = target.TrimEnd(null);
121
 
 
122
 
        if (string.Compare(trimmedString, trimmedString.Length - value.Length, value, 0, value.Length,
123
 
                        StringComparison.OrdinalIgnoreCase) == 0)
124
 
          return target;
125
 
      }
126
 
 
127
 
      return target + value;
128
 
    }
129
 
 
130
 
    public static bool IsNullOrEmptyOrWhiteSpace(string s)
131
 
    {
132
 
      if (string.IsNullOrEmpty(s))
133
 
        return true;
134
 
      else if (IsWhiteSpace(s))
135
 
        return true;
136
 
      else
137
 
        return false;
138
 
    }
139
 
 
140
 
    /// <summary>
141
 
    /// Perform an action if the string is not null or empty.
142
 
    /// </summary>
143
 
    /// <param name="value">The value.</param>
144
 
    /// <param name="action">The action to perform.</param>
145
 
    public static void IfNotNullOrEmpty(string value, Action<string> action)
146
 
    {
147
 
      IfNotNullOrEmpty(value, action, null);
148
 
    }
149
 
 
150
 
    private static void IfNotNullOrEmpty(string value, Action<string> trueAction, Action<string> falseAction)
151
 
    {
152
 
      if (!string.IsNullOrEmpty(value))
153
 
      {
154
 
        if (trueAction != null)
155
 
          trueAction(value);
156
 
      }
157
 
      else
158
 
      {
159
 
        if (falseAction != null)
160
 
          falseAction(value);
161
 
      }
162
 
    }
163
 
 
164
 
    /// <summary>
165
 
    /// Indents the specified string.
166
 
    /// </summary>
167
 
    /// <param name="s">The string to indent.</param>
168
 
    /// <param name="indentation">The number of characters to indent by.</param>
169
 
    /// <returns></returns>
170
 
    public static string Indent(string s, int indentation)
171
 
    {
172
 
      return Indent(s, indentation, ' ');
173
 
    }
174
 
 
175
 
    /// <summary>
176
 
    /// Indents the specified string.
177
 
    /// </summary>
178
 
    /// <param name="s">The string to indent.</param>
179
 
    /// <param name="indentation">The number of characters to indent by.</param>
180
 
    /// <param name="indentChar">The indent character.</param>
181
 
    /// <returns></returns>
182
 
    public static string Indent(string s, int indentation, char indentChar)
183
 
    {
184
 
      if (s == null)
185
 
        throw new ArgumentNullException("s");
186
 
 
187
 
      if (indentation <= 0)
188
 
        throw new ArgumentException("Must be greater than zero.", "indentation");
189
 
 
190
 
      StringReader sr = new StringReader(s);
191
 
      StringWriter sw = new StringWriter(CultureInfo.InvariantCulture);
192
 
 
193
 
      ActionTextReaderLine(sr, sw, delegate(TextWriter tw, string line)
194
 
      {
195
 
        tw.Write(new string(indentChar, indentation));
196
 
        tw.Write(line);
197
 
      });
198
 
 
199
 
      return sw.ToString();
200
 
    }
201
 
 
202
 
    private delegate void ActionLine(TextWriter textWriter, string line);
203
 
 
204
 
    private static void ActionTextReaderLine(TextReader textReader, TextWriter textWriter, ActionLine lineAction)
205
 
    {
206
 
      string line;
207
 
      bool firstLine = true;
208
 
      while ((line = textReader.ReadLine()) != null)
209
 
      {
210
 
        if (!firstLine)
211
 
          textWriter.WriteLine();
212
 
        else
213
 
          firstLine = false;
214
 
 
215
 
        lineAction(textWriter, line);
216
 
      }
217
 
    }
218
 
 
219
 
    /// <summary>
220
 
    /// Numbers the lines.
221
 
    /// </summary>
222
 
    /// <param name="s">The string to number.</param>
223
 
    /// <returns></returns>
224
 
    public static string NumberLines(string s)
225
 
    {
226
 
      if (s == null)
227
 
        throw new ArgumentNullException("s");
228
 
 
229
 
      StringReader sr = new StringReader(s);
230
 
      StringWriter sw = new StringWriter(CultureInfo.InvariantCulture);
231
 
 
232
 
      int lineNumber = 1;
233
 
 
234
 
      ActionTextReaderLine(sr, sw, delegate(TextWriter tw, string line)
235
 
      {
236
 
        tw.Write(lineNumber.ToString(CultureInfo.InvariantCulture).PadLeft(4));
237
 
        tw.Write(". ");
238
 
        tw.Write(line);
239
 
 
240
 
        lineNumber++;
241
 
      });
242
 
 
243
 
      return sw.ToString();
244
 
    }
245
 
 
246
 
    /// <summary>
247
 
    /// Nulls an empty string.
248
 
    /// </summary>
249
 
    /// <param name="s">The string.</param>
250
 
    /// <returns>Null if the string was null, otherwise the string unchanged.</returns>
251
 
    public static string NullEmptyString(string s)
252
 
    {
253
 
      return (string.IsNullOrEmpty(s)) ? null : s;
254
 
    }
255
 
 
256
 
    public static string ReplaceNewLines(string s, string replacement)
257
 
    {
258
 
      StringReader sr = new StringReader(s);
259
 
      StringBuilder sb = new StringBuilder();
260
 
 
261
 
      bool first = true;
262
 
 
263
 
      string line;
264
 
      while ((line = sr.ReadLine()) != null)
265
 
      {
266
 
        if (first)
267
 
          first = false;
268
 
        else
269
 
          sb.Append(replacement);
270
 
 
271
 
        sb.Append(line);
272
 
      }
273
 
 
274
 
      return sb.ToString();
275
 
    }
276
 
 
277
 
    public static string Truncate(string s, int maximumLength)
278
 
    {
279
 
      return Truncate(s, maximumLength, "...");
280
 
    }
281
 
 
282
 
    public static string Truncate(string s, int maximumLength, string suffix)
283
 
    {
284
 
      if (suffix == null)
285
 
        throw new ArgumentNullException("suffix");
286
 
 
287
 
      if (maximumLength <= 0)
288
 
        throw new ArgumentException("Maximum length must be greater than zero.", "maximumLength");
289
 
 
290
 
      int subStringLength = maximumLength - suffix.Length;
291
 
 
292
 
      if (subStringLength <= 0)
293
 
        throw new ArgumentException("Length of suffix string is greater or equal to maximumLength");
294
 
 
295
 
      if (s != null && s.Length > maximumLength)
296
 
      {
297
 
        string truncatedString = s.Substring(0, subStringLength);
298
 
        // incase the last character is a space
299
 
        truncatedString = truncatedString.Trim();
300
 
        truncatedString += suffix;
301
 
 
302
 
        return truncatedString;
303
 
      }
304
 
      else
305
 
      {
306
 
        return s;
307
 
      }
308
 
    }
309
 
 
310
 
    public static StringWriter CreateStringWriter(int capacity)
311
 
    {
312
 
      StringBuilder sb = new StringBuilder(capacity);
313
 
      StringWriter sw = new StringWriter(sb, CultureInfo.InvariantCulture);
314
 
 
315
 
      return sw;
316
 
    }
317
 
 
318
 
    public static int? GetLength(string value)
319
 
    {
320
 
      if (value == null)
321
 
        return null;
322
 
      else
323
 
        return value.Length;
324
 
    }
325
 
 
326
 
    public static string ToCharAsUnicode(char c)
327
 
    {
328
 
      char h1 = MathUtils.IntToHex((c >> 12) & '\x000f');
329
 
      char h2 = MathUtils.IntToHex((c >> 8) & '\x000f');
330
 
      char h3 = MathUtils.IntToHex((c >> 4) & '\x000f');
331
 
      char h4 = MathUtils.IntToHex(c & '\x000f');
332
 
 
333
 
      return new string(new[] { '\\', 'u', h1, h2, h3, h4 });
334
 
    }
335
 
 
336
 
    public static void WriteCharAsUnicode(TextWriter writer, char c)
337
 
    {
338
 
      ValidationUtils.ArgumentNotNull(writer, "writer");
339
 
 
340
 
      char h1 = MathUtils.IntToHex((c >> 12) & '\x000f');
341
 
      char h2 = MathUtils.IntToHex((c >> 8) & '\x000f');
342
 
      char h3 = MathUtils.IntToHex((c >> 4) & '\x000f');
343
 
      char h4 = MathUtils.IntToHex(c & '\x000f');
344
 
 
345
 
      writer.Write('\\');
346
 
      writer.Write('u');
347
 
      writer.Write(h1);
348
 
      writer.Write(h2);
349
 
      writer.Write(h3);
350
 
      writer.Write(h4);
351
 
    }
352
 
 
353
 
    public static TSource ForgivingCaseSensitiveFind<TSource>(this IEnumerable<TSource> source, Func<TSource, string> valueSelector, string testValue)
354
 
    {
355
 
      if (source == null)
356
 
        throw new ArgumentNullException("source");
357
 
      if (valueSelector == null)
358
 
        throw new ArgumentNullException("valueSelector");
359
 
 
360
 
      var caseInsensitiveResults = source.Where(s => string.Compare(valueSelector(s), testValue, StringComparison.OrdinalIgnoreCase) == 0);
361
 
      if (caseInsensitiveResults.Count() <= 1)
362
 
      {
363
 
        return caseInsensitiveResults.SingleOrDefault();
364
 
      }
365
 
      else
366
 
      {
367
 
        // multiple results returned. now filter using case sensitivity
368
 
        var caseSensitiveResults = source.Where(s => string.Compare(valueSelector(s), testValue, StringComparison.Ordinal) == 0);
369
 
        return caseSensitiveResults.SingleOrDefault();
370
 
      }
371
 
    }
372
 
  }
 
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.Generic;
 
28
using System.IO;
 
29
using System.Text;
 
30
using System.Globalization;
 
31
#if NET20
 
32
using Newtonsoft.Json.Utilities.LinqBridge;
 
33
#else
 
34
using System.Linq;
 
35
#endif
 
36
using Newtonsoft.Json.Serialization;
 
37
 
 
38
namespace Newtonsoft.Json.Utilities
 
39
{
 
40
  internal static class StringUtils
 
41
  {
 
42
    public const string CarriageReturnLineFeed = "\r\n";
 
43
    public const string Empty = "";
 
44
    public const char CarriageReturn = '\r';
 
45
    public const char LineFeed = '\n';
 
46
    public const char Tab = '\t';
 
47
 
 
48
    public static string FormatWith(this string format, IFormatProvider provider, object arg0)
 
49
    {
 
50
      return format.FormatWith(provider, new[] { arg0 });
 
51
    }
 
52
 
 
53
    public static string FormatWith(this string format, IFormatProvider provider, object arg0, object arg1)
 
54
    {
 
55
      return format.FormatWith(provider, new[] { arg0, arg1 });
 
56
    }
 
57
 
 
58
    public static string FormatWith(this string format, IFormatProvider provider, object arg0, object arg1, object arg2)
 
59
    {
 
60
      return format.FormatWith(provider, new[] { arg0, arg1, arg2 });
 
61
    }
 
62
 
 
63
    public static string FormatWith(this string format, IFormatProvider provider, params object[] args)
 
64
    {
 
65
      ValidationUtils.ArgumentNotNull(format, "format");
 
66
 
 
67
      return string.Format(provider, format, args);
 
68
    }
 
69
 
 
70
    /// <summary>
 
71
    /// Determines whether the string is all white space. Empty string will return false.
 
72
    /// </summary>
 
73
    /// <param name="s">The string to test whether it is all white space.</param>
 
74
    /// <returns>
 
75
    ///         <c>true</c> if the string is all white space; otherwise, <c>false</c>.
 
76
    /// </returns>
 
77
    public static bool IsWhiteSpace(string s)
 
78
    {
 
79
      if (s == null)
 
80
        throw new ArgumentNullException("s");
 
81
 
 
82
      if (s.Length == 0)
 
83
        return false;
 
84
 
 
85
      for (int i = 0; i < s.Length; i++)
 
86
      {
 
87
        if (!char.IsWhiteSpace(s[i]))
 
88
          return false;
 
89
      }
 
90
 
 
91
      return true;
 
92
    }
 
93
 
 
94
    /// <summary>
 
95
    /// Nulls an empty string.
 
96
    /// </summary>
 
97
    /// <param name="s">The string.</param>
 
98
    /// <returns>Null if the string was null, otherwise the string unchanged.</returns>
 
99
    public static string NullEmptyString(string s)
 
100
    {
 
101
      return (string.IsNullOrEmpty(s)) ? null : s;
 
102
    }
 
103
 
 
104
    public static StringWriter CreateStringWriter(int capacity)
 
105
    {
 
106
      StringBuilder sb = new StringBuilder(capacity);
 
107
      StringWriter sw = new StringWriter(sb, CultureInfo.InvariantCulture);
 
108
 
 
109
      return sw;
 
110
    }
 
111
 
 
112
    public static int? GetLength(string value)
 
113
    {
 
114
      if (value == null)
 
115
        return null;
 
116
      else
 
117
        return value.Length;
 
118
    }
 
119
 
 
120
    public static void ToCharAsUnicode(char c, char[] buffer)
 
121
    {
 
122
      buffer[0] = '\\';
 
123
      buffer[1] = 'u';
 
124
      buffer[2] = MathUtils.IntToHex((c >> 12) & '\x000f');
 
125
      buffer[3] = MathUtils.IntToHex((c >> 8) & '\x000f');
 
126
      buffer[4] = MathUtils.IntToHex((c >> 4) & '\x000f');
 
127
      buffer[5] = MathUtils.IntToHex(c & '\x000f');
 
128
    }
 
129
 
 
130
    public static TSource ForgivingCaseSensitiveFind<TSource>(this IEnumerable<TSource> source, Func<TSource, string> valueSelector, string testValue)
 
131
    {
 
132
      if (source == null)
 
133
        throw new ArgumentNullException("source");
 
134
      if (valueSelector == null)
 
135
        throw new ArgumentNullException("valueSelector");
 
136
 
 
137
      var caseInsensitiveResults = source.Where(s => string.Equals(valueSelector(s), testValue, StringComparison.OrdinalIgnoreCase));
 
138
      if (caseInsensitiveResults.Count() <= 1)
 
139
      {
 
140
        return caseInsensitiveResults.SingleOrDefault();
 
141
      }
 
142
      else
 
143
      {
 
144
        // multiple results returned. now filter using case sensitivity
 
145
        var caseSensitiveResults = source.Where(s => string.Equals(valueSelector(s), testValue, StringComparison.Ordinal));
 
146
        return caseSensitiveResults.SingleOrDefault();
 
147
      }
 
148
    }
 
149
 
 
150
    public static string ToCamelCase(string s)
 
151
    {
 
152
      if (string.IsNullOrEmpty(s))
 
153
        return s;
 
154
 
 
155
      if (!char.IsUpper(s[0]))
 
156
        return s;
 
157
 
 
158
      string camelCase = null;
 
159
#if !(NETFX_CORE || PORTABLE)
 
160
      camelCase = char.ToLower(s[0], CultureInfo.InvariantCulture).ToString(CultureInfo.InvariantCulture);
 
161
#else
 
162
      camelCase = char.ToLower(s[0]).ToString();
 
163
#endif
 
164
 
 
165
      if (s.Length > 1)
 
166
        camelCase += s.Substring(1);
 
167
 
 
168
      return camelCase;
 
169
    }
 
170
 
 
171
    public static bool IsHighSurrogate(char c)
 
172
    {
 
173
#if !(SILVERLIGHT || PORTABLE)
 
174
      return char.IsHighSurrogate(c);
 
175
#else
 
176
      return (c >= 55296 && c <= 56319);
 
177
#endif
 
178
    }
 
179
 
 
180
    public static bool IsLowSurrogate(char c)
 
181
    {
 
182
#if !(SILVERLIGHT || PORTABLE)
 
183
      return char.IsLowSurrogate(c);
 
184
#else
 
185
      return (c >= 56320 && c <= 57343);
 
186
#endif
 
187
    }
 
188
  }
373
189
}
 
 
b'\\ No newline at end of file'