Extensions.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. public static bool Split(this MemoryStream input, out List<byte[]>? output, int packLength = 4096)
  83. {
  84. output = default;
  85. if (packLength < 1)
  86. return false;
  87. Span<byte> t = input.ToArray();
  88. int count = t.Length / packLength;
  89. Span<byte> ts;
  90. output = [];
  91. for (int i = 0; i <= count; i++)
  92. {
  93. if (i == count)
  94. {
  95. ts = t.Slice(i * packLength);
  96. output.Add(ts.ToArray());
  97. break;
  98. }
  99. ts = t.Slice(i * packLength, packLength);
  100. output.Add(ts.ToArray());
  101. }
  102. return true;
  103. }
  104. }
  105. #endif
  106. //Require C# 14+
  107. #if NET10_0_OR_GREATER
  108. public static class ExtensionTest
  109. {
  110. extension<TSource>(TSource source) where TSource : INumber<TSource>
  111. {
  112. public bool InRange(TSource leftBorder, TSource rightBorder, bool startInclude = true, bool endInclude = true)
  113. {
  114. if (startInclude)
  115. {
  116. if (source < leftBorder)
  117. return false;
  118. }
  119. else
  120. {
  121. if (source <= leftBorder)
  122. return false;
  123. }
  124. if (endInclude)
  125. {
  126. if (source > rightBorder)
  127. return false;
  128. }
  129. else
  130. {
  131. if (source >= rightBorder)
  132. return false;
  133. }
  134. return true;
  135. }
  136. }
  137. extension(string input)
  138. {
  139. public string ToBase64()
  140. {
  141. if (string.IsNullOrEmpty(input))
  142. return string.Empty;
  143. byte[] bytes = Encoding.Default.GetBytes(input);
  144. return Convert.ToBase64String(bytes);
  145. }
  146. public string FromBase64()
  147. {
  148. if (string.IsNullOrEmpty(input))
  149. return string.Empty;
  150. byte[] bytes = Convert.FromBase64String(input);
  151. return Encoding.Default.GetString(bytes);
  152. }
  153. }
  154. extension<TSource>(TSource source) where TSource : struct
  155. {
  156. public bool ValueIn(params TSource[] values) => values.Contains(source);
  157. }
  158. extension<TSource>(TSource source) where TSource : class
  159. {
  160. public bool ReferenceIn(params TSource[] values) => values.Contains(source);
  161. }
  162. extension<TSource>(IEnumerable<TSource> sources)
  163. {
  164. public bool IsEmpty => !sources.Any();
  165. public void For(uint startIndex, uint count, Action<TSource> action)
  166. {
  167. TSource[] values = [.. sources];
  168. uint endIndex = startIndex + count;
  169. for (uint i = startIndex; i < endIndex; i++)
  170. action?.Invoke(values[i]);
  171. }
  172. public void Foreach(Action<TSource> action)
  173. {
  174. foreach (TSource source in sources)
  175. action?.Invoke(source);
  176. }
  177. public int ConditionForeach(Action<TSource> action, Func<TSource, bool> condition)
  178. {
  179. int count = 0;
  180. foreach (TSource source in sources)
  181. {
  182. if (!condition.Invoke(source))
  183. continue;
  184. action.Invoke(source);
  185. count++;
  186. }
  187. return count;
  188. }
  189. public IEnumerable<IEnumerable<TSource>> Splits(int size)
  190. {
  191. for (var i = 0; i < sources.Count() / size + 1; i++)
  192. yield return sources.Skip(i * size).Take(size);
  193. }
  194. }
  195. }
  196. #endif