StringUtils
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
namespace Newtonsoft.Json.Utilities
{
[System.Runtime.CompilerServices.NullableContext(1)]
[System.Runtime.CompilerServices.Nullable(0)]
internal static class StringUtils
{
[System.Runtime.CompilerServices.NullableContext(0)]
private enum SeparatedCaseState
{
Start,
Lower,
Upper,
NewWord
}
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';
[System.Runtime.CompilerServices.NullableContext(2)]
public static bool IsNullOrEmpty([System.Diagnostics.CodeAnalysis.NotNullWhen(false)] string value)
{
return string.IsNullOrEmpty(value);
}
public static string FormatWith(this string format, IFormatProvider provider, [System.Runtime.CompilerServices.Nullable(2)] object arg0)
{
return format.FormatWith(provider, new object[1] {
arg0
});
}
public static string FormatWith(this string format, IFormatProvider provider, [System.Runtime.CompilerServices.Nullable(2)] object arg0, [System.Runtime.CompilerServices.Nullable(2)] object arg1)
{
return format.FormatWith(provider, new object[2] {
arg0,
arg1
});
}
public static string FormatWith(this string format, IFormatProvider provider, [System.Runtime.CompilerServices.Nullable(2)] object arg0, [System.Runtime.CompilerServices.Nullable(2)] object arg1, [System.Runtime.CompilerServices.Nullable(2)] object arg2)
{
return format.FormatWith(provider, new object[3] {
arg0,
arg1,
arg2
});
}
[System.Runtime.CompilerServices.NullableContext(2)]
[return: System.Runtime.CompilerServices.Nullable(1)]
public static string FormatWith([System.Runtime.CompilerServices.Nullable(1)] this string format, [System.Runtime.CompilerServices.Nullable(1)] 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, [System.Runtime.CompilerServices.Nullable(new byte[] {
1,
2
})] 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 StringWriter CreateStringWriter(int capacity)
{
return new StringWriter(new StringBuilder(capacity), CultureInfo.InvariantCulture);
}
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);
}
[return: System.Runtime.CompilerServices.Nullable(2)]
public static TSource ForgivingCaseSensitiveFind<[System.Runtime.CompilerServices.Nullable(2)] 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();
return (from s in source
where string.Equals(valueSelector(s), testValue, StringComparison.Ordinal)
select s).SingleOrDefault();
}
public static string ToCamelCase(string s)
{
if (IsNullOrEmpty(s) || !char.IsUpper(s[0]))
return s;
char[] array = s.ToCharArray();
for (int i = 0; i < array.Length; i++) {
if (i == 1 && !char.IsUpper(array[i]))
break;
bool flag = i + 1 < array.Length;
if (((i > 0) & flag) && !char.IsUpper(array[i + 1])) {
if (char.IsSeparator(array[i + 1]))
array[i] = ToLower(array[i]);
break;
}
array[i] = ToLower(array[i]);
}
return new string(array);
}
private static char ToLower(char c)
{
c = char.ToLowerInvariant(c);
return c;
}
public static string ToSnakeCase(string s)
{
return ToSeparatedCase(s, '_');
}
public static string ToKebabCase(string s)
{
return ToSeparatedCase(s, '-');
}
private static string ToSeparatedCase(string s, char separator)
{
if (IsNullOrEmpty(s))
return s;
StringBuilder stringBuilder = new StringBuilder();
SeparatedCaseState separatedCaseState = SeparatedCaseState.Start;
for (int i = 0; i < s.Length; i++) {
if (s[i] == ' ') {
if (separatedCaseState != 0)
separatedCaseState = SeparatedCaseState.NewWord;
} else if (char.IsUpper(s[i])) {
switch (separatedCaseState) {
case SeparatedCaseState.Upper: {
bool flag = i + 1 < s.Length;
if ((i > 0) & flag) {
char c = s[i + 1];
if (!char.IsUpper(c) && c != separator)
stringBuilder.Append(separator);
}
break;
}
case SeparatedCaseState.Lower:
case SeparatedCaseState.NewWord:
stringBuilder.Append(separator);
break;
}
char value = char.ToLowerInvariant(s[i]);
stringBuilder.Append(value);
separatedCaseState = SeparatedCaseState.Upper;
} else if (s[i] == separator) {
stringBuilder.Append(separator);
separatedCaseState = SeparatedCaseState.Start;
} else {
if (separatedCaseState == SeparatedCaseState.NewWord)
stringBuilder.Append(separator);
stringBuilder.Append(s[i]);
separatedCaseState = SeparatedCaseState.Lower;
}
}
return stringBuilder.ToString();
}
public static bool IsHighSurrogate(char c)
{
return char.IsHighSurrogate(c);
}
public static bool IsLowSurrogate(char c)
{
return char.IsLowSurrogate(c);
}
public static int IndexOf(string s, char c)
{
return s.IndexOf(c);
}
public static string Replace(string s, string oldValue, string newValue)
{
return s.Replace(oldValue, newValue);
}
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;
}
public static string Trim(this string s, int start, int length)
{
if (s == null)
throw new ArgumentNullException();
if (start < 0)
throw new ArgumentOutOfRangeException("start");
if (length < 0)
throw new ArgumentOutOfRangeException("length");
int num = start + length - 1;
if (num >= s.Length)
throw new ArgumentOutOfRangeException("length");
while (start < num && char.IsWhiteSpace(s[start])) {
start++;
}
while (num >= start && char.IsWhiteSpace(s[num])) {
num--;
}
return s.Substring(start, num - start + 1);
}
}
}