Source File
scan.go
Belonging Package
github.com/google/licensecheck
package licensecheck
import (
)
func ( []License) (*Scanner, error) {
:= new(Scanner)
:= .init()
if != nil {
return nil,
}
return , nil
}
func ( *Scanner) ( []License) error {
:= new(match.Dict)
.Insert("copyright")
.Insert("http")
var []*match.LRE
.urls = make(map[string]License)
for , := range {
if .URL != "" {
.urls[.URL] =
}
if .LRE != "" {
.licenses = append(.licenses, )
, := match.ParseLRE(, .ID, .LRE)
if != nil {
return fmt.Errorf("parsing %v: %v", .ID, )
}
= append(, )
}
}
, := match.NewMultiLRE()
if != nil {
return
}
if == nil {
return errors.New("missing lre")
}
.re =
return nil
}
const maxCopyrightWords = 50
func ( []byte) Coverage {
return builtinScanner.Scan()
}
var urlScanRE = regexp.MustCompile(`^(?i)https?://[-a-z0-9_.]+\.(org|com)(/[-a-z0-9_.#?=]+)+/?`)
func ( *Scanner) ( []byte) Coverage {
if == builtinScanner {
builtinScannerOnce.Do(func() {
if := builtinScanner.init(BuiltinLicenses()); != nil {
panic("licensecheck: initializing Scan: " + .Error())
}
})
}
:= .re.Match(string()) // TODO remove conversion
var Coverage
:= .Words
:= 0
:= 0
:= .re.Dict().Lookup("copyright")
:= .re.Dict().Lookup("http")
for := ; < .Start; ++ {
:= &[]
if := urlScanRE.FindIndex([.Lo:]); != nil && (.Start == len() || int(.Lo)+[1] <= int([.Start].Lo)) {
, := int(.Lo)+[0], int(.Lo)+[1]
if , := .licenseURL(string([:])); {
.Match = append(.Match, Match{
ID: .ID,
Type: .Type,
Start: ,
End: ,
IsURL: true,
})
:=
for < .Start && int([].Hi) <= {
++
}
+= -
-- // counter loop i++
}
}
}
}
if .ID < 0 { // sentinel added above
break
}
:= int([.Start].Lo) // byte offset (unlike m.Start)
if .Start == 0 {
= 0
} else {
:= int([.Start-1].Hi)
if := bytes.LastIndexByte([:], '\n'); >= 0 {
= + + 1
}
}
:= int([.End-1].Hi) // byte offset (unlike m.End)
if .End == len() {
= len()
} else {
:= int([.End].Lo)
if := bytes.IndexByte([:], '\n'); >= 0 {
= + + 1
}
}
:= &.licenses[.ID]
.Match = append(.Match, Match{
ID: .ID,
Type: .Type,
Start: ,
End: ,
})
+= .End - .Start
= .End
}
if len() > 0 { // len(words)==0 should be impossible, but avoid NaN
.Percent = 100.0 * float64() / float64(len())
}
return
}
= strings.TrimPrefix(, "http://")
= strings.TrimPrefix(, "https://")
= strings.TrimSuffix(, "/")
= strings.TrimSuffix(, "/legalcode") // Common for CC licenses.
= strings.ToLower()
, := .urls[]
if {
return , true
}
![]() |
The pages are generated with Golds v0.3.2-preview. (GOOS=darwin GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds. |