vrobot3 is a chat bot for IRC and Discord.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

82 lines
2.0KB

  1. //-----------------------------------------------------------------------------
  2. //
  3. // Copyright © 2016 Project Golan
  4. //
  5. // See "LICENSE" for more information.
  6. //
  7. //-----------------------------------------------------------------------------
  8. //
  9. // Link (URI) utilities.
  10. //
  11. //-----------------------------------------------------------------------------
  12. using System;
  13. using System.Text.RegularExpressions;
  14. namespace ProjectGolan.Vrobot3
  15. {
  16. //
  17. // Utils
  18. //
  19. public static partial class Utils
  20. {
  21. //
  22. // URI
  23. //
  24. public struct URI
  25. {
  26. public String method, host, path, query, tag, uri;
  27. //
  28. // ToString
  29. //
  30. public override String ToString() => uri;
  31. //
  32. // Finder
  33. //
  34. public static Regex Finder = new Regex(
  35. @"((?<method>[^:/?# ]+)?:)" +
  36. @"(//(?<host>[^/?# ]*))" +
  37. @"(?<path>[^?# ]*)" +
  38. @"(?<query>\?([^# ]*))?" +
  39. @"(?<tag>#(.*))?");
  40. //
  41. // FromMatch
  42. //
  43. public static URI FromMatch(Match match)
  44. {
  45. return new URI{
  46. method = match.Groups["method"]?.Value ?? String.Empty,
  47. host = match.Groups["host"]?.Value,
  48. path = match.Groups["path"]?.Value,
  49. query = match.Groups["query"]?.Value ?? String.Empty,
  50. tag = match.Groups["tag"]?.Value ?? String.Empty,
  51. uri = match.Value
  52. };
  53. }
  54. //
  55. // Match
  56. //
  57. public static URI Match(String str) => FromMatch(Finder.Match(str));
  58. //
  59. // Matches
  60. //
  61. public static URI[] Matches(String str)
  62. {
  63. var matchbox = Finder.Matches(str);
  64. if(matchbox.Count == 0) return null;
  65. var matches = new URI[matchbox.Count];
  66. for(var i = 0; i < matchbox.Count; i++)
  67. matches[i] = FromMatch(matchbox[i]);
  68. return matches;
  69. }
  70. }
  71. }
  72. }
  73. // EOF