forked from golang/gddo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
path.go
56 lines (43 loc) · 1.31 KB
/
path.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Copyright 2013 The Go Authors. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd.
//go:generate go run gen.go -output data.go
package gosrc
import (
"path"
"regexp"
"strings"
)
var validHost = regexp.MustCompile(`^[-a-z0-9]+(?:\.[-a-z0-9]+)+$`)
var validPathElement = regexp.MustCompile(`^[-\p{L}0-9~+_][-\p{L}0-9_.]*$`)
func isValidPathElement(s string) bool {
return validPathElement.MatchString(s)
}
// IsValidRemotePath returns true if importPath is structurally valid for "go get".
func IsValidRemotePath(importPath string) bool {
parts := strings.Split(importPath, "/")
if !validTLDs[path.Ext(parts[0])] {
return false
}
if !validHost.MatchString(parts[0]) {
return false
}
for _, part := range parts[1:] {
if !isValidPathElement(part) {
return false
}
}
return true
}
// IsGoRepoPath returns true if path is in $GOROOT/src.
func IsGoRepoPath(path string) bool {
return pathFlags[path]&goRepoPath != 0
}
// IsValidPath returns true if importPath is structurally valid.
func IsValidPath(importPath string) bool {
return pathFlags[importPath]&packagePath != 0 ||
pathFlags["vendor/"+importPath]&packagePath != 0 ||
IsValidRemotePath(importPath)
}