Extensions.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. namespace Universal;
  2. #if true
  3. public static class Extensions
  4. {
  5. public static bool In<T>(this T input, params T[] values)
  6. {
  7. return values.Contains(input);
  8. }
  9. public static void Foreach<T>(this IEnumerable<T> t, Action<T>? action = null)
  10. {
  11. foreach (var item in t)
  12. action?.Invoke(item);
  13. }
  14. public static void For<T>(this IEnumerable<T> t, uint startIndex, uint count, Action<T>? action = null)
  15. {
  16. T[] values = [.. t];
  17. uint endIndex = startIndex + count;
  18. for (uint i = startIndex; i < endIndex; i++)
  19. action?.Invoke(values[i]);
  20. }
  21. public static bool InRange<T>(this T t, T leftBorder, T rightBorder, bool startInclude = true, bool endInclude = true) where T : INumber<T>
  22. {
  23. if (startInclude)
  24. {
  25. if (t < leftBorder)
  26. return false;
  27. }
  28. else
  29. {
  30. if (t <= leftBorder)
  31. return false;
  32. }
  33. if (endInclude)
  34. {
  35. if (t > rightBorder)
  36. return false;
  37. }
  38. else
  39. {
  40. if (t >= rightBorder)
  41. return false;
  42. }
  43. return true;
  44. }
  45. public static void ForeachWhere<T>(this IEnumerable<T> t, Action<T> action, Func<T, bool> judger)
  46. {
  47. foreach (var item in t)
  48. {
  49. if (!judger.Invoke(item))
  50. continue;
  51. action.Invoke(item);
  52. }
  53. }
  54. public static bool TryForeach<T>(this IEnumerable<T> t, Func<T, bool> func)
  55. {
  56. foreach (var item in t)
  57. {
  58. if (!func.Invoke(item))
  59. return false;
  60. }
  61. return true;
  62. }
  63. public static IEnumerable<IEnumerable<T>> Split<T>(this IEnumerable<T> arr, int size)
  64. {
  65. for (var i = 0; i < arr.Count() / size + 1; i++)
  66. yield return arr.Skip(i * size).Take(size);
  67. }
  68. public static string ToBase64(this string input)
  69. {
  70. if (string.IsNullOrEmpty(input))
  71. return string.Empty;
  72. byte[] bytes = Encoding.ASCII.GetBytes(input);
  73. return Convert.ToBase64String(bytes);
  74. }
  75. public static string FromBase64(this string input)
  76. {
  77. if (string.IsNullOrEmpty(input))
  78. return string.Empty;
  79. byte[] bytes = Convert.FromBase64String(input);
  80. return Encoding.ASCII.GetString(bytes);
  81. }
  82. }
  83. #endif
  84. //Require C# 14+
  85. #if false
  86. public static class ExtensionTest
  87. {
  88. extension<TSource>(TSource source) where TSource : INumber<TSource>
  89. {
  90. public bool InRange(TSource leftBorder, TSource rightBorder, bool startInclude = true, bool endInclude = true)
  91. {
  92. if (startInclude)
  93. {
  94. if (source < leftBorder)
  95. return false;
  96. }
  97. else
  98. {
  99. if (source <= leftBorder)
  100. return false;
  101. }
  102. if (endInclude)
  103. {
  104. if (source > rightBorder)
  105. return false;
  106. }
  107. else
  108. {
  109. if (source >= rightBorder)
  110. return false;
  111. }
  112. return true;
  113. }
  114. }
  115. extension(string input)
  116. {
  117. public string ToBase64()
  118. {
  119. if (string.IsNullOrEmpty(input))
  120. return string.Empty;
  121. byte[] bytes = Encoding.Default.GetBytes(input);
  122. return Convert.ToBase64String(bytes);
  123. }
  124. public string FromBase64()
  125. {
  126. if (string.IsNullOrEmpty(input))
  127. return string.Empty;
  128. byte[] bytes = Convert.FromBase64String(input);
  129. return Encoding.Default.GetString(bytes);
  130. }
  131. }
  132. extension<TSource>(TSource source) where TSource : struct
  133. {
  134. public bool ValueIn(params TSource[] values) => values.Contains(source);
  135. }
  136. extension<TSource>(TSource source) where TSource : class
  137. {
  138. public bool ReferenceIn(params TSource[] values) => values.Contains(source);
  139. }
  140. extension<TSource>(IEnumerable<TSource> sources)
  141. {
  142. public bool IsEmpty => !sources.Any();
  143. public void For(uint startIndex, uint count, Action<TSource> action)
  144. {
  145. TSource[] values = [.. sources];
  146. uint endIndex = startIndex + count;
  147. for (uint i = startIndex; i < endIndex; i++)
  148. action?.Invoke(values[i]);
  149. }
  150. public void Foreach(Action<TSource> action)
  151. {
  152. foreach (TSource source in sources)
  153. action?.Invoke(source);
  154. }
  155. public int ConditionForeach(Action<TSource> action, Func<TSource, bool> condition)
  156. {
  157. int count = 0;
  158. foreach (TSource source in sources)
  159. {
  160. if (!condition.Invoke(source))
  161. continue;
  162. action.Invoke(source);
  163. count++;
  164. }
  165. return count;
  166. }
  167. public IEnumerable<IEnumerable<TSource>> Splits(int size)
  168. {
  169. for (var i = 0; i < sources.Count() / size + 1; i++)
  170. yield return sources.Skip(i * size).Take(size);
  171. }
  172. }
  173. }
  174. #endif