Extensions.cs 6.7 KB

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