2
// Copyright (c) 2007 James Newton-King
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
13
// The above copyright notice and this permission notice shall be
14
// included in all copies or substantial portions of the Software.
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.
27
using System.Collections.Generic;
30
using System.Text.RegularExpressions;
32
using System.Globalization;
34
namespace Newtonsoft.Json.Utilities
36
internal static class StringUtils
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';
44
//public static string FormatWith(this string format, params object[] args)
46
// return FormatWith(format, null, args);
49
public static string FormatWith(this string format, IFormatProvider provider, params object[] args)
51
ValidationUtils.ArgumentNotNull(format, "format");
53
return string.Format(provider, format, args);
57
/// Determines whether the string contains white space.
59
/// <param name="s">The string to test for white space.</param>
61
/// <c>true</c> if the string contains white space; otherwise, <c>false</c>.
63
public static bool ContainsWhiteSpace(string s)
66
throw new ArgumentNullException("s");
68
for (int i = 0; i < s.Length; i++)
70
if (char.IsWhiteSpace(s[i]))
77
/// Determines whether the string is all white space. Empty string will return false.
79
/// <param name="s">The string to test whether it is all white space.</param>
81
/// <c>true</c> if the string is all white space; otherwise, <c>false</c>.
83
public static bool IsWhiteSpace(string s)
86
throw new ArgumentNullException("s");
91
for (int i = 0; i < s.Length; i++)
93
if (!char.IsWhiteSpace(s[i]))
101
/// Ensures the target string ends with the specified string.
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)
109
throw new ArgumentNullException("target");
112
throw new ArgumentNullException("value");
114
if (target.Length >= value.Length)
116
if (string.Compare(target, target.Length - value.Length, value, 0, value.Length, StringComparison.OrdinalIgnoreCase) ==
120
string trimmedString = target.TrimEnd(null);
122
if (string.Compare(trimmedString, trimmedString.Length - value.Length, value, 0, value.Length,
123
StringComparison.OrdinalIgnoreCase) == 0)
127
return target + value;
130
public static bool IsNullOrEmptyOrWhiteSpace(string s)
132
if (string.IsNullOrEmpty(s))
134
else if (IsWhiteSpace(s))
141
/// Perform an action if the string is not null or empty.
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)
147
IfNotNullOrEmpty(value, action, null);
150
private static void IfNotNullOrEmpty(string value, Action<string> trueAction, Action<string> falseAction)
152
if (!string.IsNullOrEmpty(value))
154
if (trueAction != null)
159
if (falseAction != null)
165
/// Indents the specified string.
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)
172
return Indent(s, indentation, ' ');
176
/// Indents the specified string.
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)
185
throw new ArgumentNullException("s");
187
if (indentation <= 0)
188
throw new ArgumentException("Must be greater than zero.", "indentation");
190
StringReader sr = new StringReader(s);
191
StringWriter sw = new StringWriter(CultureInfo.InvariantCulture);
193
ActionTextReaderLine(sr, sw, delegate(TextWriter tw, string line)
195
tw.Write(new string(indentChar, indentation));
199
return sw.ToString();
202
private delegate void ActionLine(TextWriter textWriter, string line);
204
private static void ActionTextReaderLine(TextReader textReader, TextWriter textWriter, ActionLine lineAction)
207
bool firstLine = true;
208
while ((line = textReader.ReadLine()) != null)
211
textWriter.WriteLine();
215
lineAction(textWriter, line);
220
/// Numbers the lines.
222
/// <param name="s">The string to number.</param>
223
/// <returns></returns>
224
public static string NumberLines(string s)
227
throw new ArgumentNullException("s");
229
StringReader sr = new StringReader(s);
230
StringWriter sw = new StringWriter(CultureInfo.InvariantCulture);
234
ActionTextReaderLine(sr, sw, delegate(TextWriter tw, string line)
236
tw.Write(lineNumber.ToString(CultureInfo.InvariantCulture).PadLeft(4));
243
return sw.ToString();
247
/// Nulls an empty string.
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)
253
return (string.IsNullOrEmpty(s)) ? null : s;
256
public static string ReplaceNewLines(string s, string replacement)
258
StringReader sr = new StringReader(s);
259
StringBuilder sb = new StringBuilder();
264
while ((line = sr.ReadLine()) != null)
269
sb.Append(replacement);
274
return sb.ToString();
277
public static string Truncate(string s, int maximumLength)
279
return Truncate(s, maximumLength, "...");
282
public static string Truncate(string s, int maximumLength, string suffix)
285
throw new ArgumentNullException("suffix");
287
if (maximumLength <= 0)
288
throw new ArgumentException("Maximum length must be greater than zero.", "maximumLength");
290
int subStringLength = maximumLength - suffix.Length;
292
if (subStringLength <= 0)
293
throw new ArgumentException("Length of suffix string is greater or equal to maximumLength");
295
if (s != null && s.Length > maximumLength)
297
string truncatedString = s.Substring(0, subStringLength);
298
// incase the last character is a space
299
truncatedString = truncatedString.Trim();
300
truncatedString += suffix;
302
return truncatedString;
310
public static StringWriter CreateStringWriter(int capacity)
312
StringBuilder sb = new StringBuilder(capacity);
313
StringWriter sw = new StringWriter(sb, CultureInfo.InvariantCulture);
318
public static int? GetLength(string value)
326
public static string ToCharAsUnicode(char c)
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');
333
return new string(new[] { '\\', 'u', h1, h2, h3, h4 });
336
public static void WriteCharAsUnicode(TextWriter writer, char c)
338
ValidationUtils.ArgumentNotNull(writer, "writer");
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');
353
public static TSource ForgivingCaseSensitiveFind<TSource>(this IEnumerable<TSource> source, Func<TSource, string> valueSelector, string testValue)
356
throw new ArgumentNullException("source");
357
if (valueSelector == null)
358
throw new ArgumentNullException("valueSelector");
360
var caseInsensitiveResults = source.Where(s => string.Compare(valueSelector(s), testValue, StringComparison.OrdinalIgnoreCase) == 0);
361
if (caseInsensitiveResults.Count() <= 1)
363
return caseInsensitiveResults.SingleOrDefault();
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();
2
// Copyright (c) 2007 James Newton-King
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
13
// The above copyright notice and this permission notice shall be
14
// included in all copies or substantial portions of the Software.
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.
27
using System.Collections.Generic;
30
using System.Globalization;
32
using Newtonsoft.Json.Utilities.LinqBridge;
36
using Newtonsoft.Json.Serialization;
38
namespace Newtonsoft.Json.Utilities
40
internal static class StringUtils
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';
48
public static string FormatWith(this string format, IFormatProvider provider, object arg0)
50
return format.FormatWith(provider, new[] { arg0 });
53
public static string FormatWith(this string format, IFormatProvider provider, object arg0, object arg1)
55
return format.FormatWith(provider, new[] { arg0, arg1 });
58
public static string FormatWith(this string format, IFormatProvider provider, object arg0, object arg1, object arg2)
60
return format.FormatWith(provider, new[] { arg0, arg1, arg2 });
63
public static string FormatWith(this string format, IFormatProvider provider, params object[] args)
65
ValidationUtils.ArgumentNotNull(format, "format");
67
return string.Format(provider, format, args);
71
/// Determines whether the string is all white space. Empty string will return false.
73
/// <param name="s">The string to test whether it is all white space.</param>
75
/// <c>true</c> if the string is all white space; otherwise, <c>false</c>.
77
public static bool IsWhiteSpace(string s)
80
throw new ArgumentNullException("s");
85
for (int i = 0; i < s.Length; i++)
87
if (!char.IsWhiteSpace(s[i]))
95
/// Nulls an empty string.
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)
101
return (string.IsNullOrEmpty(s)) ? null : s;
104
public static StringWriter CreateStringWriter(int capacity)
106
StringBuilder sb = new StringBuilder(capacity);
107
StringWriter sw = new StringWriter(sb, CultureInfo.InvariantCulture);
112
public static int? GetLength(string value)
120
public static void ToCharAsUnicode(char c, char[] buffer)
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');
130
public static TSource ForgivingCaseSensitiveFind<TSource>(this IEnumerable<TSource> source, Func<TSource, string> valueSelector, string testValue)
133
throw new ArgumentNullException("source");
134
if (valueSelector == null)
135
throw new ArgumentNullException("valueSelector");
137
var caseInsensitiveResults = source.Where(s => string.Equals(valueSelector(s), testValue, StringComparison.OrdinalIgnoreCase));
138
if (caseInsensitiveResults.Count() <= 1)
140
return caseInsensitiveResults.SingleOrDefault();
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();
150
public static string ToCamelCase(string s)
152
if (string.IsNullOrEmpty(s))
155
if (!char.IsUpper(s[0]))
158
string camelCase = null;
159
#if !(NETFX_CORE || PORTABLE)
160
camelCase = char.ToLower(s[0], CultureInfo.InvariantCulture).ToString(CultureInfo.InvariantCulture);
162
camelCase = char.ToLower(s[0]).ToString();
166
camelCase += s.Substring(1);
171
public static bool IsHighSurrogate(char c)
173
#if !(SILVERLIGHT || PORTABLE)
174
return char.IsHighSurrogate(c);
176
return (c >= 55296 && c <= 56319);
180
public static bool IsLowSurrogate(char c)
182
#if !(SILVERLIGHT || PORTABLE)
183
return char.IsLowSurrogate(c);
185
return (c >= 56320 && c <= 57343);
b'\\ No newline at end of file'