Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit f258d57

Browse files
committed
Add IsTuple + use primitive type name
1 parent 043ddb2 commit f258d57

File tree

1 file changed

+44
-43
lines changed

1 file changed

+44
-43
lines changed

src/ServiceStack.Text/StringExtensions.cs

Lines changed: 44 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
using ServiceStack.Text;
2121
using ServiceStack.Text.Common;
2222
using ServiceStack.Text.Support;
23+
using static System.String;
2324

2425
namespace ServiceStack
2526
{
@@ -57,19 +58,19 @@ public static string BaseConvert(this string source, int from, int to)
5758
var chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
5859
var len = source.Length;
5960
if (len == 0)
60-
throw new Exception(string.Format("Parameter: '{0}' is not valid integer (in base {1}).", source, from));
61+
throw new Exception(Format("Parameter: '{0}' is not valid integer (in base {1}).", source, from));
6162
var minus = source[0] == '-' ? "-" : "";
6263
var src = minus == "" ? source : source.Substring(1);
6364
len = src.Length;
6465
if (len == 0)
65-
throw new Exception(string.Format("Parameter: '{0}' is not valid integer (in base {1}).", source, from));
66+
throw new Exception(Format("Parameter: '{0}' is not valid integer (in base {1}).", source, from));
6667

6768
var d = 0;
6869
for (int i = 0; i < len; i++) // Convert to decimal
6970
{
7071
int c = chars.IndexOf(src[i]);
7172
if (c >= from)
72-
throw new Exception(string.Format("Parameter: '{0}' is not valid integer (in base {1}).", source, from));
73+
throw new Exception(Format("Parameter: '{0}' is not valid integer (in base {1}).", source, from));
7374
d = d * from + c;
7475
}
7576
if (to == 10 || d == 0)
@@ -91,7 +92,7 @@ public static string EncodeXml(this string value)
9192

9293
public static string EncodeJson(this string value)
9394
{
94-
return String.Concat
95+
return Concat
9596
("\"",
9697
value.Replace("\\", "\\\\").Replace("\"", "\\\"").Replace("\r", "").Replace("\n", "\\n"),
9798
"\""
@@ -106,7 +107,7 @@ public static string EncodeJsv(this string value)
106107
}
107108
return String.IsNullOrEmpty(value) || !JsWriter.HasAnyEscapeChars(value)
108109
? value
109-
: String.Concat
110+
: Concat
110111
(
111112
JsWriter.QuoteString,
112113
value.Replace(JsWriter.QuoteString, TypeSerializer.DoubleQuoteString),
@@ -126,7 +127,7 @@ public static string DecodeJsv(this string value)
126127

127128
public static string UrlEncode(this string text, bool upperCase=false)
128129
{
129-
if (string.IsNullOrEmpty(text)) return text;
130+
if (String.IsNullOrEmpty(text)) return text;
130131

131132
var sb = StringBuilderThreadStatic.Allocate();
132133
var fmt = upperCase ? "X2" : "x2";
@@ -188,7 +189,7 @@ public static string UrlDecode(this string text)
188189

189190
public static string HexUnescape(this string text, params char[] anyCharOf)
190191
{
191-
if (string.IsNullOrEmpty(text)) return null;
192+
if (String.IsNullOrEmpty(text)) return null;
192193
if (anyCharOf == null || anyCharOf.Length == 0) return text;
193194

194195
var sb = StringBuilderThreadStatic.Allocate();
@@ -221,7 +222,7 @@ public static string UrlFormat(this string url, params string[] urlComponents)
221222
encodedUrlComponents[i] = x.UrlEncode();
222223
}
223224

224-
return String.Format(url, encodedUrlComponents);
225+
return Format(url, encodedUrlComponents);
225226
}
226227

227228
public static string ToRot13(this string value)
@@ -472,7 +473,7 @@ public static string[] SplitOnLast(this string strVal, string needle)
472473

473474
public static string WithoutExtension(this string filePath)
474475
{
475-
if (string.IsNullOrEmpty(filePath))
476+
if (String.IsNullOrEmpty(filePath))
476477
return null;
477478

478479
var extPos = filePath.LastIndexOf('.');
@@ -484,11 +485,11 @@ public static string WithoutExtension(this string filePath)
484485

485486
public static string GetExtension(this string filePath)
486487
{
487-
if (string.IsNullOrEmpty(filePath))
488+
if (String.IsNullOrEmpty(filePath))
488489
return null;
489490

490491
var extPos = filePath.LastIndexOf('.');
491-
return extPos == -1 ? string.Empty : filePath.Substring(extPos);
492+
return extPos == -1 ? Empty : filePath.Substring(extPos);
492493
}
493494

494495
static readonly char[] DirSeps = new[] { '\\', '/' };
@@ -554,27 +555,27 @@ public static T FromCsv<T>(this string csv)
554555

555556
public static string FormatWith(this string text, params object[] args)
556557
{
557-
return String.Format(text, args);
558+
return Format(text, args);
558559
}
559560

560561
public static string Fmt(this string text, params object[] args)
561562
{
562-
return String.Format(text, args);
563+
return Format(text, args);
563564
}
564565

565566
public static string Fmt(this string text, object arg1)
566567
{
567-
return String.Format(text, arg1);
568+
return Format(text, arg1);
568569
}
569570

570571
public static string Fmt(this string text, object arg1, object arg2)
571572
{
572-
return String.Format(text, arg1, arg2);
573+
return Format(text, arg1, arg2);
573574
}
574575

575576
public static string Fmt(this string text, object arg1, object arg2, object arg3)
576577
{
577-
return String.Format(text, arg1, arg2, arg3);
578+
return Format(text, arg1, arg2, arg3);
578579
}
579580

580581
public static bool StartsWithIgnoreCase(this string text, string startsWith)
@@ -663,7 +664,7 @@ public static string ExtractContents(this string fromText, string uniqueMarker,
663664

664665
public static string StripHtml(this string html)
665666
{
666-
return string.IsNullOrEmpty(html) ? null : StripHtmlRegEx.Replace(html, "");
667+
return String.IsNullOrEmpty(html) ? null : StripHtmlRegEx.Replace(html, "");
667668
}
668669

669670
public static string Quoted(this string text)
@@ -675,7 +676,7 @@ public static string Quoted(this string text)
675676

676677
public static string StripQuotes(this string text)
677678
{
678-
return string.IsNullOrEmpty(text) || text.Length < 2
679+
return String.IsNullOrEmpty(text) || text.Length < 2
679680
? text
680681
: text[0] == '"' && text[text.Length - 1] == '"'
681682
? text.Substring(1, text.Length - 2)
@@ -703,7 +704,7 @@ public static string StripMarkdownMarkup(this string markdown)
703704
private const int LowerCaseOffset = 'a' - 'A';
704705
public static string ToCamelCase(this string value)
705706
{
706-
if (string.IsNullOrEmpty(value)) return value;
707+
if (String.IsNullOrEmpty(value)) return value;
707708

708709
var len = value.Length;
709710
var newValue = new char[len];
@@ -729,7 +730,7 @@ public static string ToCamelCase(this string value)
729730

730731
public static string ToPascalCase(this string value)
731732
{
732-
if (string.IsNullOrEmpty(value)) return value;
733+
if (String.IsNullOrEmpty(value)) return value;
733734

734735
if (value.IndexOf('_') >= 0)
735736
{
@@ -754,7 +755,7 @@ public static string ToTitleCase(this string value)
754755

755756
public static string ToLowercaseUnderscore(this string value)
756757
{
757-
if (string.IsNullOrEmpty(value)) return value;
758+
if (String.IsNullOrEmpty(value)) return value;
758759
value = value.ToCamelCase();
759760

760761
var sb = StringBuilderThreadStatic.Allocate();
@@ -790,11 +791,11 @@ public static string SafeSubstring(this string value, int startIndex)
790791

791792
public static string SafeSubstring(this string value, int startIndex, int length)
792793
{
793-
if (string.IsNullOrEmpty(value)) return string.Empty;
794+
if (String.IsNullOrEmpty(value)) return Empty;
794795
if (value.Length >= (startIndex + length))
795796
return value.Substring(startIndex, length);
796797

797-
return value.Length > startIndex ? value.Substring(startIndex) : String.Empty;
798+
return value.Length > startIndex ? value.Substring(startIndex) : Empty;
798799
}
799800

800801
public static bool IsAnonymousType(this Type type)
@@ -807,7 +808,7 @@ public static bool IsAnonymousType(this Type type)
807808

808809
public static int CompareIgnoreCase(this string strA, string strB)
809810
{
810-
return string.Compare(strA, strB, PclExport.Instance.InvariantComparisonIgnoreCase);
811+
return Compare(strA, strB, PclExport.Instance.InvariantComparisonIgnoreCase);
811812
}
812813

813814
public static bool EndsWithInvariant(this string str, string endsWith)
@@ -827,7 +828,7 @@ public static T ToEnum<T>(this string value)
827828

828829
public static T ToEnumOrDefault<T>(this string value, T defaultValue)
829830
{
830-
if (string.IsNullOrEmpty(value)) return defaultValue;
831+
if (String.IsNullOrEmpty(value)) return defaultValue;
831832
return (T)Enum.Parse(typeof(T), value, true);
832833
}
833834

@@ -858,17 +859,17 @@ public static string ToHttps(this string url)
858859

859860
public static bool IsEmpty(this string value)
860861
{
861-
return string.IsNullOrEmpty(value);
862+
return String.IsNullOrEmpty(value);
862863
}
863864

864865
public static bool IsNullOrEmpty(this string value)
865866
{
866-
return string.IsNullOrEmpty(value);
867+
return String.IsNullOrEmpty(value);
867868
}
868869

869870
public static bool EqualsIgnoreCase(this string value, string other)
870871
{
871-
return string.Equals(value, other, StringComparison.CurrentCultureIgnoreCase);
872+
return String.Equals(value, other, StringComparison.CurrentCultureIgnoreCase);
872873
}
873874

874875
public static string ReplaceFirst(this string haystack, string needle, string replacement)
@@ -904,18 +905,18 @@ public static bool ContainsAny(this string text, params string[] testMatches)
904905

905906
public static string SafeVarName(this string text)
906907
{
907-
if (String.IsNullOrEmpty(text)) return null;
908+
if (string.IsNullOrEmpty(text)) return null;
908909
return InvalidVarCharsRegex.Replace(text, "_");
909910
}
910911

911912
public static string Join(this List<string> items)
912913
{
913-
return String.Join(JsWriter.ItemSeperatorString, items.ToArray());
914+
return string.Join(JsWriter.ItemSeperatorString, items.ToArray());
914915
}
915916

916917
public static string Join(this List<string> items, string delimeter)
917918
{
918-
return String.Join(delimeter, items.ToArray());
919+
return string.Join(delimeter, items.ToArray());
919920
}
920921

921922
public static string ToParentPath(this string path)
@@ -941,15 +942,15 @@ public static string RemoveCharFlags(this string text, bool[] charFlags)
941942
copy[nonWsPos++] = @char;
942943
}
943944

944-
return new String(copy, 0, nonWsPos);
945+
return new string(copy, 0, nonWsPos);
945946
}
946947

947948
public static string ToNullIfEmpty(this string text)
948949
{
949-
return String.IsNullOrEmpty(text) ? null : text;
950+
return string.IsNullOrEmpty(text) ? null : text;
950951
}
951952

952-
private static char[] SystemTypeChars = new[] { '<', '>', '+' };
953+
private static readonly char[] SystemTypeChars = { '<', '>', '+' };
953954

954955
public static bool IsUserType(this Type type)
955956
{
@@ -970,11 +971,13 @@ public static bool IsSystemType(this Type type)
970971
|| type.Name.IndexOfAny(SystemTypeChars) >= 0;
971972
}
972973

974+
public static bool IsTuple(this Type type) => type.Name.StartsWith("Tuple`");
975+
973976
public static bool IsInt(this string text)
974977
{
975-
if (String.IsNullOrEmpty(text)) return false;
978+
if (string.IsNullOrEmpty(text)) return false;
976979
int ret;
977-
return Int32.TryParse(text, out ret);
980+
return int.TryParse(text, out ret);
978981
}
979982

980983
public static int ToInt(this string text)
@@ -985,18 +988,18 @@ public static int ToInt(this string text)
985988
public static int ToInt(this string text, int defaultValue)
986989
{
987990
int ret;
988-
return Int32.TryParse(text, out ret) ? ret : defaultValue;
991+
return int.TryParse(text, out ret) ? ret : defaultValue;
989992
}
990993

991994
public static long ToInt64(this string text)
992995
{
993-
return Int64.Parse(text);
996+
return long.Parse(text);
994997
}
995998

996999
public static long ToInt64(this string text, long defaultValue)
9971000
{
9981001
long ret;
999-
return Int64.TryParse(text, out ret) ? ret : defaultValue;
1002+
return long.TryParse(text, out ret) ? ret : defaultValue;
10001003
}
10011004

10021005
public static float ToFloat(this string text)
@@ -1056,7 +1059,7 @@ public static bool Glob(this string value, string pattern)
10561059
return false;
10571060

10581061
default:
1059-
if (value.Length == pos || Char.ToUpper(pattern[pos]) != Char.ToUpper(value[pos]))
1062+
if (value.Length == pos || char.ToUpper(pattern[pos]) != char.ToUpper(value[pos]))
10601063
{
10611064
return false;
10621065
}
@@ -1132,9 +1135,7 @@ public static int CountOccurrencesOf(this string text, char needle)
11321135

11331136
public static string NormalizeNewLines(this string text)
11341137
{
1135-
return text != null
1136-
? text.Replace("\r\n", "\n")
1137-
: null;
1138+
return text?.Replace("\r\n", "\n");
11381139
}
11391140

11401141
#if !LITE

0 commit comments

Comments
 (0)