PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/Signum.Utilities/Extensions.cs

http://signum.codeplex.com
C# | 388 lines | 317 code | 70 blank | 1 comment | 57 complexity | 1447caa49055d35e3b7ba8c5a55a01a7 MD5 | raw file
Possible License(s): LGPL-3.0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Reflection;
  6. using System.IO;
  7. using System.Runtime.Serialization.Formatters.Binary;
  8. using System.Diagnostics;
  9. using System.Globalization;
  10. namespace Signum.Utilities
  11. {
  12. [DebuggerStepThrough]
  13. public static class Extensions
  14. {
  15. #region Parse Number
  16. public static int? ToInt(this string str)
  17. {
  18. int result;
  19. if (int.TryParse(str, out result))
  20. return result;
  21. else
  22. return null;
  23. }
  24. public static long? ToLong(this string str)
  25. {
  26. long result;
  27. if (long.TryParse(str, out result))
  28. return result;
  29. else
  30. return null;
  31. }
  32. public static short? ToShort(this string str)
  33. {
  34. short result;
  35. if (short.TryParse(str, out result))
  36. return result;
  37. else
  38. return null;
  39. }
  40. public static float? ToFloat(this string str)
  41. {
  42. float result;
  43. if (float.TryParse(str, out result))
  44. return result;
  45. else
  46. return null;
  47. }
  48. public static double? ToDouble(this string str)
  49. {
  50. double result;
  51. if (double.TryParse(str, out result))
  52. return result;
  53. else
  54. return null;
  55. }
  56. public static decimal? ToDecimal(this string str)
  57. {
  58. decimal result;
  59. if (decimal.TryParse(str, out result))
  60. return result;
  61. else
  62. return null;
  63. }
  64. public static int ToInt(this string str, string error)
  65. {
  66. int result;
  67. if (int.TryParse(str, out result))
  68. return result;
  69. throw new FormatException(error);
  70. }
  71. public static long ToLong(this string str, string error)
  72. {
  73. long result;
  74. if (long.TryParse(str, out result))
  75. return result;
  76. throw new FormatException(error);
  77. }
  78. public static short ToShort(this string str, string error)
  79. {
  80. short result;
  81. if (short.TryParse(str, out result))
  82. return result;
  83. throw new FormatException(error);
  84. }
  85. public static float? ToFloat(this string str, string error)
  86. {
  87. float result;
  88. if (float.TryParse(str, out result))
  89. return result;
  90. throw new FormatException(error);
  91. }
  92. public static double ToDouble(this string str, string error)
  93. {
  94. double result;
  95. if (double.TryParse(str, out result))
  96. return result;
  97. throw new FormatException(error);
  98. }
  99. public static decimal ToDecimal(this string str, string error)
  100. {
  101. decimal result;
  102. if (decimal.TryParse(str, out result))
  103. return result;
  104. throw new FormatException(error);
  105. }
  106. #endregion
  107. #region Math
  108. //http://en.wikipedia.org/wiki/Modulo_operation
  109. public static int Mod(this int a, int b)
  110. {
  111. int result = a % b;
  112. if (a < 0)
  113. result += b;
  114. if (b < 0)
  115. result -= b;
  116. return result;
  117. }
  118. public static long Mod(this long a, long b)
  119. {
  120. long mod = a % b;
  121. if (a < 0)
  122. mod += b;
  123. if (b < 0)
  124. mod -= b;
  125. return mod;
  126. }
  127. public static int DivMod(this int a, int b, out int mod)
  128. {
  129. int result = Math.DivRem(a, b, out mod);
  130. if (a < 0)
  131. {
  132. result--;
  133. mod += b;
  134. }
  135. if (b < 0)
  136. {
  137. result++;
  138. mod -= b;
  139. }
  140. return result;
  141. }
  142. public static long DivMod(this long a, long b, out long mod)
  143. {
  144. long result = Math.DivRem(a, b, out mod);
  145. if (a < 0)
  146. {
  147. result--;
  148. mod += b;
  149. }
  150. if (b < 0)
  151. {
  152. result++;
  153. mod -= b;
  154. }
  155. return result;
  156. }
  157. #endregion
  158. public static T? DefaultToNull<T>(this T value)
  159. where T : struct
  160. {
  161. return EqualityComparer<T>.Default.Equals(default(T), value) ? (T?)null : value;
  162. }
  163. public static T? DefaultToNull<T>(this T value, T defaultValue)
  164. where T : struct
  165. {
  166. return EqualityComparer<T>.Default.Equals(defaultValue, value) ? (T?)null : value;
  167. }
  168. public static int? NotFoundToNull(this int value)
  169. {
  170. return value == -1 ? null : (int?)value;
  171. }
  172. public static int NotFound(this int value, int defaultValue)
  173. {
  174. return value == -1 ? defaultValue : value;
  175. }
  176. public static T ThrowIfNullS<T>(this T? t, string mensaje)
  177. where T : struct
  178. {
  179. if (t == null)
  180. throw new NullReferenceException(mensaje);
  181. return t.Value;
  182. }
  183. public static T ThrowIfNullC<T>(this T t, string mensaje)
  184. where T : class
  185. {
  186. if (t == null)
  187. throw new NullReferenceException(mensaje);
  188. return t;
  189. }
  190. public static string TryToString(this object obj)
  191. {
  192. if (obj == null)
  193. return null;
  194. return obj.ToString();
  195. }
  196. public static string TryToString(this IFormattable obj, string format)
  197. {
  198. if (obj == null)
  199. return null;
  200. return obj.ToString(format, CultureInfo.CurrentCulture);
  201. }
  202. #region Map Try Do TryDo
  203. public static R Map<T, R>(this T t, Func<T, R> func)
  204. {
  205. return func(t);
  206. }
  207. public static R TryCC<T, R>(this T t, Func<T, R> func)
  208. where T : class
  209. where R : class
  210. {
  211. if (t == null) return null;
  212. return func(t);
  213. }
  214. public static R? TryCS<T, R>(this T t, Func<T, R> func)
  215. where T : class
  216. where R : struct
  217. {
  218. if (t == null) return null;
  219. return func(t);
  220. }
  221. public static R? TryCS<T, R>(this T t, Func<T, R?> func)
  222. where T : class
  223. where R : struct
  224. {
  225. if (t == null) return null;
  226. return func(t);
  227. }
  228. public static R TrySC<T, R>(this T? t, Func<T, R> func)
  229. where T : struct
  230. where R : class
  231. {
  232. if (t == null) return null;
  233. return func(t.Value);
  234. }
  235. public static R? TrySS<T, R>(this T? t, Func<T, R> func)
  236. where T : struct
  237. where R : struct
  238. {
  239. if (t == null) return null;
  240. return func(t.Value);
  241. }
  242. public static R? TrySS<T, R>(this T? t, Func<T, R?> func)
  243. where T : struct
  244. where R : struct
  245. {
  246. if (t == null) return null;
  247. return func(t.Value);
  248. }
  249. public static T Do<T>(this T t, Action<T> action)
  250. {
  251. action(t);
  252. return t;
  253. }
  254. public static T TryDoC<T>(this T t, Action<T> action) where T : class
  255. {
  256. if (t != null)
  257. action(t);
  258. return t;
  259. }
  260. public static T? TryDoS<T>(this T? t, Action<T> action) where T : struct
  261. {
  262. if (t != null)
  263. action(t.Value);
  264. return t;
  265. }
  266. #endregion
  267. public static IEnumerable<int> To(this int start, int endNotIncluded)
  268. {
  269. for (int i = start; i < endNotIncluded; i++)
  270. yield return i;
  271. }
  272. public static IEnumerable<int> To(this int start, int endNotIncluded, int step)
  273. {
  274. for (int i = start; i < endNotIncluded; i += step)
  275. yield return i;
  276. }
  277. public static IEnumerable<DateTime> To(this DateTime start, DateTime endNotIncluded)
  278. {
  279. for (DateTime i = start; i < endNotIncluded; i = i.AddDays(1))
  280. yield return i;
  281. }
  282. public static IEnumerable<DateTime> To(this DateTime start, DateTime endNotIncluded, TimeSpan span)
  283. {
  284. for (DateTime i = start; i < endNotIncluded; i = i.Add(span))
  285. yield return i;
  286. }
  287. public static IEnumerable<int> DownTo(this int startNotIncluded, int end)
  288. {
  289. for (int i = startNotIncluded - 1; i >= end; i--)
  290. yield return i;
  291. }
  292. public static IEnumerable<int> DownTo(this int startNotIncluded, int end, int step)
  293. {
  294. for (int i = startNotIncluded - 1; i >= end; i-= step)
  295. yield return i;
  296. }
  297. public static IEnumerable<T> For<T>(this T start, Func<T, bool> condition, Func<T, T> increment)
  298. {
  299. for (T i = start; condition(i); i = increment(i))
  300. yield return i;
  301. }
  302. public static IEnumerable<T> FollowC<T>(this T start, Func<T, T> next) where T : class
  303. {
  304. for (T i = start; i != null; i = next(i))
  305. yield return i;
  306. }
  307. public static IEnumerable<T> FollowS<T>(this T start, Func<T, T?> next) where T : struct
  308. {
  309. for (T? i = start; i.HasValue; i = next(i.Value))
  310. yield return i.Value;
  311. }
  312. public static IEnumerable<T> FollowS<T>(this T? start, Func<T, T?> next) where T : struct
  313. {
  314. for (T? i = start; i.HasValue; i = next(i.Value))
  315. yield return i.Value;
  316. }
  317. }
  318. }