diff --git a/Extenso.Core/Collections/EnumerableExtensions.cs b/Extenso.Core/Collections/EnumerableExtensions.cs index be901bb..7877fdc 100644 --- a/Extenso.Core/Collections/EnumerableExtensions.cs +++ b/Extenso.Core/Collections/EnumerableExtensions.cs @@ -136,31 +136,25 @@ public static bool HasMoreThan(this IEnumerable source, int n) //throw new ArgumentNullException("source"); } - var array = source as T[]; - - if (array != null) + if (source is T[] array) { return array.Length > n; } - var collection = source as ICollection; - - if (collection != null) + if (source is ICollection collection) { return collection.Count > n; } - using (var enumerator = source.GetEnumerator()) + using var enumerator = source.GetEnumerator(); + for (int i = 0; i < n + 1; i++) { - for (int i = 0; i < n + 1; i++) + if (!enumerator.MoveNext()) { - if (!enumerator.MoveNext()) - { - return false; - }; - } - return true; + return false; + }; } + return true; } /// @@ -684,16 +678,12 @@ internal static bool FastAny(this IEnumerable source) throw new ArgumentNullException(nameof(source)); } - var array = source as TSource[]; - - if (array != null) + if (source is TSource[] array) { return array.Length > 0; } - var collection = source as ICollection; - - if (collection != null) + if (source is ICollection collection) { return collection.Count > 0; } diff --git a/Extenso.Core/ObjectExtensions.cs b/Extenso.Core/ObjectExtensions.cs index 1cda7d2..89f5112 100644 --- a/Extenso.Core/ObjectExtensions.cs +++ b/Extenso.Core/ObjectExtensions.cs @@ -219,7 +219,7 @@ public static bool In(this T source, params T[] values) /// true if source is equal to the default value of its type; otherwise false. public static bool IsDefault(this T source) { - return source.GenericEquals(default(T)); + return source.GenericEquals(default); } /// diff --git a/Extenso.Core/StringExtensions.cs b/Extenso.Core/StringExtensions.cs index 7ef4a5e..020ed77 100644 --- a/Extenso.Core/StringExtensions.cs +++ b/Extenso.Core/StringExtensions.cs @@ -24,7 +24,7 @@ public static class StringExtensions #region Fields private const string RegexArabicAndHebrew = @"[\u0600-\u06FF,\u0590-\u05FF]+"; - private static readonly char[] validSegmentChars = "/?#[]@\"^{}|`<>\t\r\n\f ".ToCharArray(); + //private static readonly char[] validSegmentChars = "/?#[]@\"^{}|`<>\t\r\n\f ".ToCharArray(); #endregion Fields @@ -96,9 +96,9 @@ public static T Base64Deserialize(this string source) } else { - int length = int.Parse(source.Substring(0, lengthDelimiterPosition)); + int length = int.Parse(source[..lengthDelimiterPosition]); - var bytes = Convert.FromBase64String(source.Substring(lengthDelimiterPosition + 1)); + var bytes = Convert.FromBase64String(source[(lengthDelimiterPosition + 1)..]); using (var memoryStream = new MemoryStream(bytes, 0, length)) { var binaryFormatter = new BinaryFormatter(); @@ -129,7 +129,7 @@ public static string Between(this string source, char left, char right) int indexTo = source.IndexOf(right, indexFrom); if (indexTo != -1) { - return source.Substring(indexFrom, indexTo - indexFrom); + return source[indexFrom..indexTo]; } } return string.Empty; @@ -530,7 +530,7 @@ public static string Left(this string source, int length) return source; } - return source.Substring(0, length); + return source[..length]; } /// @@ -549,7 +549,7 @@ public static string LeftOf(this string source, char value) int index = source.IndexOf(value); if (index != -1) { - return source.Substring(0, index); + return source[..index]; } return source; } @@ -578,7 +578,7 @@ public static string LeftOf(this string source, char value, int n) } if (index != -1) { - return source.Substring(0, index); + return source[..index]; } return source; } @@ -599,7 +599,7 @@ public static string LeftOf(this string source, string value) int index = source.IndexOf(value); if (index != -1) { - return source.Substring(0, index); + return source[..index]; } return source; } @@ -621,7 +621,7 @@ public static string LeftOfLastIndexOf(this string source, char value) int index = source.LastIndexOf(value); if (index != -1) { - ret = source.Substring(0, index); + ret = source[..index]; } return ret; } @@ -643,7 +643,7 @@ public static string LeftOfLastIndexOf(this string source, string value) int index = source.LastIndexOf(value); if (index != -1) { - ret = source.Substring(0, index); + ret = source[..index]; } return ret; } @@ -673,7 +673,7 @@ public static string Prepend(this string source, params string[] values) { var items = new string[values.Length + 1]; values.CopyTo(items, 0); - items[items.Length - 1] = source; + items[^1] = source; return string.Concat(items); } @@ -687,7 +687,7 @@ public static string Prepend(this string source, params object[] values) { var items = new object[values.Length + 1]; values.CopyTo(items, 0); - items[items.Length - 1] = source; + items[^1] = source; return string.Concat(items); } @@ -795,7 +795,7 @@ public static string RightOf(this string source, char value) int index = source.IndexOf(value); if (index != -1) { - return source.Substring(index + 1); + return source[(index + 1)..]; } return source; } @@ -825,7 +825,7 @@ public static string RightOf(this string source, char value, int n) if (index != -1) { - return source.Substring(index + 1); + return source[(index + 1)..]; } return source; } @@ -846,7 +846,7 @@ public static string RightOf(this string source, string value) int index = source.IndexOf(value); if (index != -1) { - return source.Substring(index + 1); + return source[(index + 1)..]; } return source; } @@ -868,7 +868,7 @@ public static string RightOfLastIndexOf(this string source, char value) int index = source.LastIndexOf(value); if (index != -1) { - ret = source.Substring(index + 1); + ret = source[(index + 1)..]; } return ret; } @@ -890,7 +890,7 @@ public static string RightOfLastIndexOf(this string source, string value) int index = source.LastIndexOf(value); if (index != -1) { - ret = source.Substring(index + 1); + ret = source[(index + 1)..]; } return ret; } @@ -968,7 +968,7 @@ public static bool StartsWithAny(this string source, params string[] values) public static string ToCamelCase(this string source) { string pascal = source.ToPascalCase(); - return string.Concat(pascal[0].ToString().ToLower(), pascal.Substring(1)); + return string.Concat(pascal[0].ToString().ToLower(), pascal[1..]); } /// @@ -1124,7 +1124,7 @@ public static T XmlDeserialize(this string source) { if (string.IsNullOrEmpty(source)) { - return default(T); + return default; } var locker = new object(); diff --git a/Extenso.Sandbox/Program.cs b/Extenso.Sandbox/Program.cs index 909ee60..d3fb937 100644 --- a/Extenso.Sandbox/Program.cs +++ b/Extenso.Sandbox/Program.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Globalization; using System.Text.RegularExpressions; using Extenso.Collections; using Extenso.Data; @@ -19,6 +20,13 @@ private static void Main(string[] args) //DataTableExtensions(); + //string sample = "key:value"; + //string key = sample.LeftOf(':'); + //string value = sample.RightOf(':'); + + string desc = "the quick brown fox jumped over the lazy dog"; + string result = desc.AsSpan()[..19].ToString(); + Console.ReadLine(); } private static string SeparatorReplacement(string value, string separator, bool capitalizeFirstChar)