public static T[] GetSpan <T> ( [NotNull] this T[] array, int offset ) { Sure.NotNull(array, nameof(array)); Sure.NonNegative(offset, nameof(offset)); if (offset >= array.Length) { return(new T[0]); } int count = array.Length - offset; T[] result = array.GetSpan(offset, count); return(result); }
/// <summary> /// Try to execute specified function. /// </summary> public void Try ( [NotNull] Action action ) { Sure.NotNull(action, "action"); for (int i = 0; i <= RetryLimit; i++) { try { action(); break; } catch (Exception ex) { _Resolve(i, ex); } } }
/// <summary> /// Get max or default value for the sequence. /// </summary> public static TOutput MaxOrDefault <TInput, TOutput> ( [NotNull] this IEnumerable <TInput> sequence, [NotNull] Func <TInput, TOutput> selector, TOutput defaultValue ) { Sure.NotNull(sequence, nameof(sequence)); Sure.NotNull(selector, nameof(selector)); TInput[] array = sequence.ToArray(); if (array.Length == 0) { return(defaultValue); } TOutput result = array.Max(selector); return(result); }
public static string ConvertFloatToInvariant ( [NotNull] string text ) { Sure.NotNull(text, nameof(text)); if (text.Contains(',')) { if (text.Contains('.')) { text = text.Replace(",", string.Empty); } else { text = text.Replace(',', '.'); } } return(text); }
public static IEnumerable <T> Replace <T> ( [NotNull] this IEnumerable <T> list, [CanBeNull] T replaceFrom, [CanBeNull] T replaceTo ) where T : IEquatable <T> { Sure.NotNull(list, nameof(list)); foreach (T item in list) { if (item.Equals(replaceFrom)) { yield return(replaceTo); } else { yield return(item); } } }
public static T ThrowIfNull <T> ( [CanBeNull] this T value, [NotNull] string message ) where T : class { Sure.NotNull(message, nameof(message)); if (ReferenceEquals(value, null)) { Log.Error ( nameof(Utility) + "::" + nameof(ThrowIfNull) + ": " + message ); throw new ArgumentException(message); } return(value); }
/// <summary> /// Separate the sequence with given separator. /// </summary> public static IEnumerable Separate ( [NotNull] this IEnumerable sequence, [CanBeNull] object separator ) { Sure.NotNull(sequence, nameof(sequence)); bool first = true; foreach (object obj in sequence) { if (first) { first = false; } else { yield return(separator); } yield return(obj); } }
/// <summary> /// Reports the index of the first occurrence in this instance /// of any string in a specified array. /// </summary> public static int IndexOfAny ( [NotNull] this string text, out int which, params string[] anyOf ) { Sure.NotNull(text, nameof(text)); int result = -1; which = -1; for (int i = 0; i < anyOf.Length; i++) { string value = anyOf[i]; int index = text.IndexOf(value, StringComparison.Ordinal); if (index >= 0) { if (result >= 0) { if (index < result) { result = index; which = i; } } else { result = index; which = i; } } } return(result); }
public static T[] Merge <T> ( [NotNull] params T[][] arrays ) { Sure.NotNull(arrays, nameof(arrays)); int resultLength = 0; for (int i = 0; i < arrays.Length; i++) { if (ReferenceEquals(arrays[i], null)) { Log.Error ( nameof(ArrayUtility) + "::" + nameof(Merge) + ": array[" + i + "] is null" ); throw new ArgumentNullException(nameof(arrays)); } resultLength += arrays[i].Length; } T[] result = new T[resultLength]; int offset = 0; for (int i = 0; i < arrays.Length; i++) { arrays[i].CopyTo(result, offset); offset += arrays[i].Length; } return(result); }
public static IEnumerable <T[]> Slice <T> ( [NotNull] this IEnumerable <T> sequence, int pieceSize ) { Sure.NotNull(sequence, nameof(sequence)); if (pieceSize <= 0) { Log.Error ( nameof(Sequence) + "::" + nameof(Slice) + "pieceSize=" + pieceSize ); throw new ArgumentOutOfRangeException(nameof(pieceSize)); } List <T> piece = new List <T>(pieceSize); foreach (T item in sequence) { piece.Add(item); if (piece.Count >= pieceSize) { yield return(piece.ToArray()); piece = new List <T>(pieceSize); } } if (piece.Count != 0) { yield return(piece.ToArray()); } }
/// <summary> /// Converts given object to boolean value. /// </summary> /// <param name="value">Object to be converted.</param> /// <returns>Converted value.</returns> /// <exception cref="FormatException"> /// Value can't be converted. /// </exception> public static bool ToBoolean ( [NotNull] object value ) { Sure.NotNull(value, "value"); if (value is bool) { return((bool)value); } try { bool result = bool.Parse(value as string); return(result); } // ReSharper disable once EmptyGeneralCatchClause catch { // Pass through } string svalue = value as string; if (!ReferenceEquals(svalue, null)) { svalue = svalue.ToLowerInvariant(); if (svalue == "false" || svalue == "0" || svalue == "no" || svalue == "n" || svalue == "off" || svalue == "negative" || svalue == "neg" || svalue == "disabled" || svalue == "incorrect" || svalue == "wrong" || svalue == "нет" ) { return(false); } if (svalue == "true" || svalue == "1" || svalue == "yes" || svalue == "y" || svalue == "on" || svalue == "positiva" || svalue == "pos" || svalue == "enabled" || svalue == "correct" || svalue == "right" || svalue == "да" ) { return(true); } } if (value is IConvertible) { return(Convert.ToBoolean(value)); } TypeConverter converterFrom = TypeDescriptor.GetConverter(value); if (!ReferenceEquals(converterFrom, null) && converterFrom.CanConvertTo(typeof(bool))) { return((bool)converterFrom.ConvertTo ( value, typeof(bool) )); } Log.Error ( nameof(ConversionUtility) + "::" + nameof(ToBoolean) + "bad value=" + value ); throw new FormatException ( "Bad value " + value ); }