/// <summary> /// Count how many occurences of the specified <see cref="string"/> exist within a given <see cref="char"/>[]. /// </summary> /// <param name="self">The string to look through.</param> /// <param name="value">The value to look for.</param> /// <returns>The number of occurrences of the given value in the <see cref="string"/>.</returns> public static int CountOccurrences(this char[] self, string value) { GenericExtensions.ThrowIfNull(self, nameof(self)); GenericExtensions.ThrowIfNull(value, nameof(value)); char[] checkChars = value.ToCharArray(); int length = self.Length; int checkLength = checkChars.Length; int count = 0; int checkCount = 0; for (int i = length - 1; i >= 0; i--) { if (checkCount == checkLength) { count++; checkCount = 0; } if (self[i] == checkChars[checkCount]) { checkCount++; } else { checkCount = 0; } } return(count); }
public static async Task <T> InvokeAndRetryAsync <T>(this Func <Task <T> > action, int attempts, TimeSpan retryInterval, IEnumerable <Exception> fatalExceptions = null) { GenericExtensions.ThrowIfNull(action, nameof(action)); if (attempts < 1) { throw new ArgumentOutOfRangeException(nameof(attempts), "Must make at least one attempt."); } int maxAttempts = attempts; ICollection <Exception> exceptions = new List <Exception>(); while (attempts > 0) { try { if (attempts < maxAttempts) { await Task.Delay(retryInterval); } return(await action()); } catch (Exception ex) when(!fatalExceptions?.Contains(ex) ?? true) { exceptions.Add(ex); } attempts--; } throw new AggregateException(exceptions); }
public static T InvokeAndRetryIf <T, TExc>(this Func <T> action, int attempts, TimeSpan retryInterval) where TExc : Exception { GenericExtensions.ThrowIfNull(action, nameof(action)); if (attempts < 1) { throw new ArgumentOutOfRangeException(nameof(attempts), "Must make at least one attempt."); } Exception firstExc = null; while (attempts-- > 0) { try { return(action()); } catch (TExc ex) { firstExc ??= ex; Thread.Sleep(retryInterval); } } throw firstExc; }
/// <summary> /// Checks if a given <see cref="string"/> is a numeric value. /// </summary> /// <param name="value">The <see cref="string"/> to parse.</param> /// <param name="style">The number style to use for parsing.</param> /// <param name="culture">The culture info to use for parsing.</param> /// <returns></returns> /// <remarks>Uses <see cref="double.TryParse(string, NumberStyles, IFormatProvider, out double)"/>.</remarks> public static bool IsNumeric(this string value, NumberStyles style = NumberStyles.Number, CultureInfo culture = null) { GenericExtensions.ThrowIfNull(value, nameof(value)); if (culture is null) { culture = CultureInfo.InvariantCulture; } return(!string.IsNullOrWhiteSpace(value) && double.TryParse(value, style, culture, out _)); }
public static IEnumerable <T> ReverseEnumerable <T>(this LinkedList <T> list) { GenericExtensions.ThrowIfNull(list, nameof(list)); LinkedListNode <T> node = list.Last; while (node != null) { yield return(node.Value); node = node.Previous; } }
/// <summary> /// Count how many occurences of the specified <see cref="char"/> exist within a given <see cref="char"/>[]. /// </summary> /// <param name="self">The string to look through.</param> /// <param name="value">The value to look for.</param> /// <returns>The number of occurrences of the given value in the <see cref="char"/>[].</returns> public static int CountOccurrences(this char[] self, char value) { GenericExtensions.ThrowIfNull(self, nameof(self)); int length = self.Length; int count = 0; for (int i = length - 1; i >= 0; i--) { if (self[i] == value) { count++; } } return(count); }
/// <summary> /// Count how many occurences of the specified <see cref="char"/>s exist within a given <see cref="string"/>. /// </summary> /// <param name="self">The string to look through.</param> /// <param name="value">The value to look for.</param> /// <returns>The number of occurrences of the given values in the <see cref="string"/>.</returns> public static int CountOccurrences(this string self, params char[] values) { GenericExtensions.ThrowIfNull(self, nameof(self)); return(self.ToCharArray().CountOccurrences(values)); }