ExtensionMethods.cs 898 B

12345678910111213141516171819202122232425262728293031
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. namespace Utils
  5. {
  6. public static class ExtensionMethods
  7. {
  8. public static TR MapValue<TK, TV, TR>(this Dictionary<TK, TV> dic, TK key, Func<TV, TR> found)
  9. {
  10. return dic.TryGetValue(key, out var value)
  11. ? found(value)
  12. : default;
  13. }
  14. public static TR MapValue<TK, TV, TR>(this Dictionary<TK, TV> dic, TK key, Func<TV, TR> found, Func<TR> notFound)
  15. {
  16. return dic.TryGetValue(key, out var value)
  17. ? found(value)
  18. : notFound();
  19. }
  20. public static void FilterChars(this StringBuilder sb, HashSet<char> chars, char replace = '_')
  21. {
  22. for (var i = 0; i < sb.Length; i++)
  23. {
  24. if (chars.Contains(sb[i])) sb[i] = replace;
  25. }
  26. }
  27. }
  28. }