StringUtils
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
namespace Newtonsoft.Json.Utilities
{
internal static class StringUtils
{
public const string CarriageReturnLineFeed = "\r\n";
public const string Empty = "";
public const char CarriageReturn = '\r';
public const char LineFeed = '\n';
public const char Tab = '\t';
public static string FormatWith(this string format, IFormatProvider provider, object arg0)
{
return format.FormatWith(provider, new object[1] {
arg0
});
}
public static string FormatWith(this string format, IFormatProvider provider, object arg0, object arg1)
{
return format.FormatWith(provider, new object[2] {
arg0,
arg1
});
}
public static string FormatWith(this string format, IFormatProvider provider, object arg0, object arg1, object arg2)
{
return format.FormatWith(provider, new object[3] {
arg0,
arg1,
arg2
});
}
public static string FormatWith(this string format, IFormatProvider provider, object arg0, object arg1, object arg2, object arg3)
{
return format.FormatWith(provider, new object[4] {
arg0,
arg1,
arg2,
arg3
});
}
private static string FormatWith(this string format, IFormatProvider provider, params object[] args)
{
ValidationUtils.ArgumentNotNull(format, "format");
return string.Format(provider, format, args);
}
public static bool IsWhiteSpace(string s)
{
if (s == null)
throw new ArgumentNullException("s");
if (s.Length == 0)
return false;
for (int i = 0; i < s.Length; i++) {
if (!char.IsWhiteSpace(s[i]))
return false;
}
return true;
}
public static string NullEmptyString(string s)
{
if (!string.IsNullOrEmpty(s))
return s;
return null;
}
public static StringWriter CreateStringWriter(int capacity)
{
StringBuilder sb = new StringBuilder(capacity);
return new StringWriter(sb, CultureInfo.InvariantCulture);
}
public static int? GetLength(string value)
{
return value?.Length;
}
public static void ToCharAsUnicode(char c, char[] buffer)
{
buffer[0] = '\\';
buffer[1] = 'u';
buffer[2] = MathUtils.IntToHex(((int)c >> 12) & 15);
buffer[3] = MathUtils.IntToHex(((int)c >> 8) & 15);
buffer[4] = MathUtils.IntToHex(((int)c >> 4) & 15);
buffer[5] = MathUtils.IntToHex(c & 15);
}
public static TSource ForgivingCaseSensitiveFind<TSource>(this IEnumerable<TSource> source, Func<TSource, string> valueSelector, string testValue)
{
if (source == null)
throw new ArgumentNullException("source");
if (valueSelector == null)
throw new ArgumentNullException("valueSelector");
IEnumerable<TSource> source2 = from s in source
where string.Equals(valueSelector(s), testValue, StringComparison.OrdinalIgnoreCase)
select s;
if (source2.Count() <= 1)
return source2.SingleOrDefault();
IEnumerable<TSource> source3 = from s in source
where string.Equals(valueSelector(s), testValue, StringComparison.Ordinal)
select s;
return source3.SingleOrDefault();
}
public static string ToCamelCase(string s)
{
if (string.IsNullOrEmpty(s))
return s;
if (!char.IsUpper(s[0]))
return s;
char[] array = s.ToCharArray();
for (int i = 0; i < array.Length; i++) {
bool flag = i + 1 < array.Length;
if (i > 0 && flag && !char.IsUpper(array[i + 1]))
break;
array[i] = char.ToLower(array[i], CultureInfo.InvariantCulture);
}
return new string(array);
}
public static bool IsHighSurrogate(char c)
{
return char.IsHighSurrogate(c);
}
public static bool IsLowSurrogate(char c)
{
return char.IsLowSurrogate(c);
}
public static bool StartsWith(this string source, char value)
{
if (source.Length > 0)
return source[0] == value;
return false;
}
public static bool EndsWith(this string source, char value)
{
if (source.Length > 0)
return source[source.Length - 1] == value;
return false;
}
}
}