本站源代码
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.

38 lines
1.1KB

  1. package url
  2. import (
  3. "regexp"
  4. )
  5. var (
  6. isSchemeRegExp = regexp.MustCompile(`^[^:]+://`)
  7. scpLikeUrlRegExp = regexp.MustCompile(`^(?:(?P<user>[^@]+)@)?(?P<host>[^:\s]+):(?:(?P<port>[0-9]{1,5})/)?(?P<path>[^\\].*)$`)
  8. )
  9. // MatchesScheme returns true if the given string matches a URL-like
  10. // format scheme.
  11. func MatchesScheme(url string) bool {
  12. return isSchemeRegExp.MatchString(url)
  13. }
  14. // MatchesScpLike returns true if the given string matches an SCP-like
  15. // format scheme.
  16. func MatchesScpLike(url string) bool {
  17. return scpLikeUrlRegExp.MatchString(url)
  18. }
  19. // FindScpLikeComponents returns the user, host, port and path of the
  20. // given SCP-like URL.
  21. func FindScpLikeComponents(url string) (user, host, port, path string) {
  22. m := scpLikeUrlRegExp.FindStringSubmatch(url)
  23. return m[1], m[2], m[3], m[4]
  24. }
  25. // IsLocalEndpoint returns true if the given URL string specifies a
  26. // local file endpoint. For example, on a Linux machine,
  27. // `/home/user/src/go-git` would match as a local endpoint, but
  28. // `https://github.com/src-d/go-git` would not.
  29. func IsLocalEndpoint(url string) bool {
  30. return !MatchesScheme(url) && !MatchesScpLike(url)
  31. }
上海开阖软件有限公司 沪ICP备12045867号-1