Skip to content

Commit

Permalink
A little bit of refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
gordon-matt committed Dec 8, 2021
1 parent 4b3101f commit 4f9161c
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 40 deletions.
30 changes: 10 additions & 20 deletions Extenso.Core/Collections/EnumerableExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,31 +136,25 @@ public static bool HasMoreThan<T>(this IEnumerable<T> 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<T>;

if (collection != null)
if (source is ICollection<T> 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;
}

/// <summary>
Expand Down Expand Up @@ -684,16 +678,12 @@ internal static bool FastAny<TSource>(this IEnumerable<TSource> 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<TSource>;

if (collection != null)
if (source is ICollection<TSource> collection)
{
return collection.Count > 0;
}
Expand Down
2 changes: 1 addition & 1 deletion Extenso.Core/ObjectExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ public static bool In<T>(this T source, params T[] values)
/// <returns>true if source is equal to the default value of its type; otherwise false.</returns>
public static bool IsDefault<T>(this T source)
{
return source.GenericEquals(default(T));
return source.GenericEquals(default);
}

/// <summary>
Expand Down
38 changes: 19 additions & 19 deletions Extenso.Core/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -96,9 +96,9 @@ public static T Base64Deserialize<T>(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();
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -530,7 +530,7 @@ public static string Left(this string source, int length)
return source;
}

return source.Substring(0, length);
return source[..length];
}

/// <summary>
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand Down Expand Up @@ -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);
}

Expand All @@ -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);
}

Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand Down Expand Up @@ -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..]);
}

/// <summary>
Expand Down Expand Up @@ -1124,7 +1124,7 @@ public static T XmlDeserialize<T>(this string source)
{
if (string.IsNullOrEmpty(source))
{
return default(T);
return default;
}

var locker = new object();
Expand Down
8 changes: 8 additions & 0 deletions Extenso.Sandbox/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text.RegularExpressions;
using Extenso.Collections;
using Extenso.Data;
Expand All @@ -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)
Expand Down

0 comments on commit 4f9161c

Please sign in to comment.