Skip to content

Commit

Permalink
replace regexp with strings comparing
Browse files Browse the repository at this point in the history
  • Loading branch information
apokrovskiyn committed Oct 31, 2024
1 parent 69b0d26 commit 379788d
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions internal/types/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"go/ast"
"go/printer"
"go/token"
"regexp"
"strings"

"github.com/hexdigest/gowrap/pkg"
Expand Down Expand Up @@ -157,10 +156,19 @@ func findSourcePackage(ident *ast.Ident, imports []*ast.ImportSpec) string {
continue
}

matched, err := regexp.MatchString(`^(.*\/)?`+ident.Name+`(\/.[^\/]*)?$`, cleanPath)
if err == nil && matched {
// try last segment, like in "github.com/my/package/identName"
lastSlash := strings.LastIndex(cleanPath, "/")
if ident.Name == cleanPath[lastSlash+1:] {
return cleanPath
}

// try prev segment, like in "github.com/my/package/identName/v5"
if cleanPath[lastSlash+1] == 'v' {
prevSlash := strings.LastIndex(cleanPath[:lastSlash], "/")
if ident.Name == cleanPath[prevSlash+1:lastSlash] {
return cleanPath
}
}
}

// todo: should not reach here?
Expand Down

0 comments on commit 379788d

Please sign in to comment.