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.
 
 
 

55 lines
1.3 KiB

  1. // Copyright 2013 The Go Authors. All rights reserved.
  2. //
  3. // Use of this source code is governed by a BSD-style
  4. // license that can be found in the LICENSE file or at
  5. // https://developers.google.com/open-source/licenses/bsd.
  6. //go:generate go run gen.go -output data.go
  7. package gosrc
  8. import (
  9. "path"
  10. "regexp"
  11. "strings"
  12. )
  13. var validHost = regexp.MustCompile(`^[-a-z0-9]+(?:\.[-a-z0-9]+)+$`)
  14. var validPathElement = regexp.MustCompile(`^[-A-Za-z0-9~+_][-A-Za-z0-9_.]*$`)
  15. func isValidPathElement(s string) bool {
  16. return validPathElement.MatchString(s)
  17. }
  18. // IsValidRemotePath returns true if importPath is structurally valid for "go get".
  19. func IsValidRemotePath(importPath string) bool {
  20. parts := strings.Split(importPath, "/")
  21. if !validTLDs[path.Ext(parts[0])] {
  22. return false
  23. }
  24. if !validHost.MatchString(parts[0]) {
  25. return false
  26. }
  27. for _, part := range parts[1:] {
  28. if !isValidPathElement(part) {
  29. return false
  30. }
  31. }
  32. return true
  33. }
  34. // IsGoRepoPath returns true if path is in $GOROOT/src.
  35. func IsGoRepoPath(path string) bool {
  36. return pathFlags[path]&goRepoPath != 0
  37. }
  38. // IsValidPath returns true if importPath is structurally valid.
  39. func IsValidPath(importPath string) bool {
  40. return pathFlags[importPath]&packagePath != 0 || IsValidRemotePath(importPath)
  41. }