Skip to content

Commit

Permalink
Update CoreExtension.cs
Browse files Browse the repository at this point in the history
  • Loading branch information
WeihanLi authored Jan 9, 2024
1 parent 6558997 commit 8d4b360
Showing 1 changed file with 19 additions and 19 deletions.
38 changes: 19 additions & 19 deletions src/WeihanLi.Common/Extensions/CoreExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2212,7 +2212,7 @@ public static string Concatenate<T>(this IEnumerable<T> source, Func<T, string>
/// <param name="this">The @this to act on.</param>
/// <param name="value">The value.</param>
/// <returns>true if the value is in the string, false if not.</returns>
public static bool Contains(this string @this, string value) => Guard.NotNull(@this, nameof(@this)).IndexOf(value, StringComparison.Ordinal) != -1;
public static bool Contains(this string @this, string value) => Guard.NotNull(@this).IndexOf(value, StringComparison.Ordinal) != -1;

/// <summary>
/// A string extension method that query if this object contains the given value.
Expand All @@ -2221,23 +2221,23 @@ public static string Concatenate<T>(this IEnumerable<T> source, Func<T, string>
/// <param name="value">The value.</param>
/// <param name="comparisonType">Type of the comparison.</param>
/// <returns>true if the value is in the string, false if not.</returns>
public static bool Contains(this string @this, string value, StringComparison comparisonType) => Guard.NotNull(@this, nameof(@this)).IndexOf(value, comparisonType) != -1;
public static bool Contains(this string @this, string value, StringComparison comparisonType) => Guard.NotNull(@this).IndexOf(value, comparisonType) != -1;

/// <summary>
/// A string extension method that extracts this object.
/// </summary>
/// <param name="this">The @this to act on.</param>
/// <param name="predicate">The predicate.</param>
/// <returns>A string.</returns>
public static string Extract(this string @this, Func<char, bool> predicate) => new(Guard.NotNull(@this, nameof(@this)).ToCharArray().Where(predicate).ToArray());
public static string Extract(this string @this, Func<char, bool> predicate) => new(Guard.NotNull(@this).ToCharArray().Where(predicate).ToArray());

/// <summary>
/// A string extension method that removes the letter.
/// </summary>
/// <param name="this">The @this to act on.</param>
/// <param name="predicate">The predicate.</param>
/// <returns>A string.</returns>
public static string RemoveWhere(this string @this, Func<char, bool> predicate) => new(Guard.NotNull(@this, nameof(@this)).ToCharArray().Where(x => !predicate(x)).ToArray());
public static string RemoveWhere(this string @this, Func<char, bool> predicate) => new(Guard.NotNull(@this).ToCharArray().Where(x => !predicate(x)).ToArray());

/// <summary>
/// Replaces the format item in a specified String with the text equivalent of the value of a corresponding
Expand All @@ -2249,17 +2249,17 @@ public static string Concatenate<T>(this IEnumerable<T> source, Func<T, string>
/// A copy of format in which the format items have been replaced by the String equivalent of the corresponding
/// instances of Object in args.
/// </returns>
public static string FormatWith(this string @this, params object[] values) => string.Format(Guard.NotNull(@this, nameof(@this)), values);
public static string FormatWith(this string @this, params object?[] values) => string.Format(Guard.NotNull(@this), values);

/// <summary>
/// A string extension method that query if '@this' satisfy the specified pattern.
/// A string extension method that query if '@this' satisfies the specified pattern.
/// </summary>
/// <param name="this">The @this to act on.</param>
/// <param name="pattern">The pattern to use. Use '*' as wildcard string.</param>
/// <returns>true if '@this' satisfy the specified pattern, false if not.</returns>
/// <param name="pattern">The pattern to use. Use '*' as a wildcard string.</param>
/// <returns>true if '@this' satisfies the specified pattern, false if not.</returns>
public static bool IsLike(this string @this, string pattern)
{
// Turn the pattern into regex pattern, and match the whole string with ^$
// Turn the pattern into a regex pattern, and match the whole string with ^$
var regexPattern = "^" + Regex.Escape(pattern) + "$";

// Escape special character ?, #, *, [], and [!]
Expand All @@ -2270,7 +2270,7 @@ public static bool IsLike(this string @this, string pattern)
.Replace(@"\*", ".*")
.Replace(@"\#", @"\d");

return Regex.IsMatch(Guard.NotNull(@this, nameof(@this)), regexPattern);
return Regex.IsMatch(Guard.NotNull(@this), regexPattern);
}

/// <summary>
Expand Down Expand Up @@ -2313,7 +2313,7 @@ public static string SafeSubstring(this string str, int startIndex, int length)
/// <returns>substring</returns>
public static string Sub(this string @this, int startIndex)
{
Guard.NotNull(@this, nameof(@this));
Guard.NotNull(@this);
if (startIndex >= 0)
{
return @this.SafeSubstring(startIndex);
Expand All @@ -2333,7 +2333,7 @@ public static string Sub(this string @this, int startIndex)
/// <returns>The repeated string.</returns>
public static string Repeat(this string @this, int repeatCount)
{
Guard.NotNull(@this, nameof(@this));
Guard.NotNull(@this);
if (@this.Length == 1)
{
return new string(@this[0], repeatCount);
Expand Down Expand Up @@ -2378,22 +2378,22 @@ public static string Reverse(this string? @this)
/// <returns>
/// An array whose elements contain the substrings in this string that are delimited by the separator.
/// </returns>
public static string[] Split(this string @this, string separator, StringSplitOptions option = StringSplitOptions.None) => Guard.NotNull(@this, nameof(@this)).Split(new[] { separator }, option);
public static string[] Split(this string @this, string separator, StringSplitOptions option = StringSplitOptions.None) => Guard.NotNull(@this).Split(new[] { separator }, option);

/// <summary>
/// A string extension method that converts the @this to a byte array.
/// </summary>
/// <param name="this">The @this to act on.</param>
/// <returns>@this as a byte[].</returns>
public static byte[] ToByteArray(this string @this) => Encoding.UTF8.GetBytes(Guard.NotNull(@this, nameof(@this)));
public static byte[] ToByteArray(this string @this) => Encoding.UTF8.GetBytes(Guard.NotNull(@this));

/// <summary>
/// A string extension method that converts the @this to a byte array.
/// </summary>
/// <param name="this">The @this to act on.</param>
/// <param name="encoding">encoding</param>
/// <returns>@this as a byte[].</returns>
public static byte[] ToByteArray(this string @this, Encoding encoding) => encoding.GetBytes(Guard.NotNull(@this, nameof(@this)));
public static byte[] ToByteArray(this string @this, Encoding encoding) => encoding.GetBytes(Guard.NotNull(@this));

public static byte[] GetBytes(this string str) => Guard.NotNull(str, nameof(str)).GetBytes(null);

Expand All @@ -2405,30 +2405,30 @@ public static string Reverse(this string? @this)
/// <typeparam name="T">Generic type parameter.</typeparam>
/// <param name="this">The @this to act on.</param>
/// <returns>@this as a T.</returns>
public static T ToEnum<T>(this string @this) => (T)Enum.Parse(typeof(T), Guard.NotNull(@this, nameof(@this)));
public static T ToEnum<T>(this string @this) => (T)Enum.Parse(typeof(T), Guard.NotNull(@this));

/// <summary>
/// A string extension method that converts the @this to a title case.
/// </summary>
/// <param name="this">The @this to act on.</param>
/// <returns>@this as a string.</returns>
public static string ToTitleCase(this string @this) => new CultureInfo("en-US").TextInfo.ToTitleCase(Guard.NotNull(@this, nameof(@this)));
public static string ToTitleCase(this string @this) => new CultureInfo("en-US").TextInfo.ToTitleCase(Guard.NotNull(@this));

/// <summary>
/// A string extension method that converts the @this to a title case.
/// </summary>
/// <param name="this">The @this to act on.</param>
/// <param name="cultureInfo">Information describing the culture.</param>
/// <returns>@this as a string.</returns>
public static string ToTitleCase(this string @this, CultureInfo cultureInfo) => cultureInfo.TextInfo.ToTitleCase(Guard.NotNull(@this, nameof(@this)));
public static string ToTitleCase(this string @this, CultureInfo cultureInfo) => cultureInfo.TextInfo.ToTitleCase(Guard.NotNull(@this));

/// <summary>
/// A string extension method that truncates.
/// </summary>
/// <param name="this">The @this to act on.</param>
/// <param name="maxLength">The maximum length.</param>
/// <returns>A string.</returns>
public static string Truncate(this string @this, int maxLength) => Guard.NotNull(@this, nameof(@this)).Truncate(maxLength, "...");
public static string Truncate(this string @this, int maxLength) => Guard.NotNull(@this).Truncate(maxLength, "...");

/// <summary>
/// A string extension method that truncates.
Expand Down

0 comments on commit 8d4b360

Please sign in to comment.