Simplify maps to boolean value

pull/203/head
lu4p 4 years ago committed by Daniel Martí
parent c0731921c2
commit 2e2bd09b5e

@ -418,13 +418,13 @@ func newPrivateName(name, path string) (privateName, bool) {
} }
func dedupStrings(paths []string) []string { func dedupStrings(paths []string) []string {
seen := make(map[string]struct{}, len(paths)) seen := make(map[string]bool, len(paths))
j := 0 j := 0
for _, v := range paths { for _, v := range paths {
if _, ok := seen[v]; ok { if seen[v] {
continue continue
} }
seen[v] = struct{}{} seen[v] = true
paths[j] = v paths[j] = v
j++ j++
} }
@ -432,14 +432,14 @@ func dedupStrings(paths []string) []string {
} }
func dedupPrivateNames(names []privateName) []privateName { func dedupPrivateNames(names []privateName) []privateName {
seen := make(map[string]struct{}, len(names)) seen := make(map[string]bool, len(names))
j := 0 j := 0
for _, v := range names { for _, v := range names {
combined := v.name + string(v.seed) combined := v.name + string(v.seed)
if _, ok := seen[combined]; ok { if seen[combined] {
continue continue
} }
seen[combined] = struct{}{} seen[combined] = true
names[j] = v names[j] = v
j++ j++
} }

@ -21,7 +21,7 @@ func randObfuscator() obfuscator {
} }
// Obfuscate replace literals with obfuscated lambda functions // Obfuscate replace literals with obfuscated lambda functions
func Obfuscate(files []*ast.File, info *types.Info, fset *token.FileSet, blacklist map[types.Object]struct{}) []*ast.File { func Obfuscate(files []*ast.File, info *types.Info, fset *token.FileSet, blacklist map[types.Object]bool) []*ast.File {
pre := func(cursor *astutil.Cursor) bool { pre := func(cursor *astutil.Cursor) bool {
switch x := cursor.Node().(type) { switch x := cursor.Node().(type) {
case *ast.GenDecl: case *ast.GenDecl:
@ -49,7 +49,7 @@ func Obfuscate(files []*ast.File, info *types.Info, fset *token.FileSet, blackli
} }
// The object itself is blacklisted, e.g. a value that needs to be constant // The object itself is blacklisted, e.g. a value that needs to be constant
if _, ok := blacklist[obj]; ok { if blacklist[obj] {
return false return false
} }
} }
@ -209,7 +209,7 @@ func obfuscateByteArray(data []byte, length int64) *ast.CallExpr {
} }
// ConstBlacklist blacklist identifieres used in constant expressions // ConstBlacklist blacklist identifieres used in constant expressions
func ConstBlacklist(node ast.Node, info *types.Info, blacklist map[types.Object]struct{}) { func ConstBlacklist(node ast.Node, info *types.Info, blacklist map[types.Object]bool) {
blacklistObjects := func(node ast.Node) bool { blacklistObjects := func(node ast.Node) bool {
ident, ok := node.(*ast.Ident) ident, ok := node.(*ast.Ident)
if !ok { if !ok {
@ -217,7 +217,7 @@ func ConstBlacklist(node ast.Node, info *types.Info, blacklist map[types.Object]
} }
obj := info.ObjectOf(ident) obj := info.ObjectOf(ident)
blacklist[obj] = struct{}{} blacklist[obj] = true
return true return true
} }

@ -456,7 +456,7 @@ func transformCompile(args []string) ([]string, error) {
// The local name must not be obfuscated. // The local name must not be obfuscated.
obj := tf.pkg.Scope().Lookup(localName) obj := tf.pkg.Scope().Lookup(localName)
if obj != nil { if obj != nil {
tf.blacklist[obj] = struct{}{} tf.blacklist[obj] = true
} }
// If the new name is of the form "pkgpath.Name", and // If the new name is of the form "pkgpath.Name", and
@ -762,7 +762,7 @@ func fillBuildInfo(flags []string) error {
// //
// The blacklist mainly contains named types and their field declarations. // The blacklist mainly contains named types and their field declarations.
func (tf *transformer) buildBlacklist(files []*ast.File) { func (tf *transformer) buildBlacklist(files []*ast.File) {
tf.blacklist = make(map[types.Object]struct{}) tf.blacklist = make(map[types.Object]bool)
reflectBlacklist := func(node ast.Node) bool { reflectBlacklist := func(node ast.Node) bool {
expr, _ := node.(ast.Expr) // info.TypeOf(nil) will just return nil expr, _ := node.(ast.Expr) // info.TypeOf(nil) will just return nil
@ -814,11 +814,11 @@ func (tf *transformer) buildBlacklist(files []*ast.File) {
// collectNames collects all names, including the names of local variables, // collectNames collects all names, including the names of local variables,
// functions, global fields, etc. // functions, global fields, etc.
func collectNames(files []*ast.File) map[string]struct{} { func collectNames(files []*ast.File) map[string]bool {
blacklist := make(map[string]struct{}) blacklist := make(map[string]bool)
visit := func(node ast.Node) bool { visit := func(node ast.Node) bool {
if ident, ok := node.(*ast.Ident); ok { if ident, ok := node.(*ast.Ident); ok {
blacklist[ident.Name] = struct{}{} blacklist[ident.Name] = true
} }
return true return true
} }
@ -838,9 +838,9 @@ type transformer struct {
// Maps to keep track of how, or whether not, we should obfuscate // Maps to keep track of how, or whether not, we should obfuscate
// certain parts of the package. // certain parts of the package.
// TODO: document better and use better field names; see issue #169. // TODO: document better and use better field names; see issue #169.
blacklist map[types.Object]struct{} blacklist map[types.Object]bool
privateNameMap map[string]string privateNameMap map[string]string
existingNames map[string]struct{} existingNames map[string]bool
// nameCounter keeps track of how many unique identifier names we've // nameCounter keeps track of how many unique identifier names we've
// obfuscated, so that the obfuscated names get assigned incrementing // obfuscated, so that the obfuscated names get assigned incrementing
@ -909,7 +909,7 @@ func (tf *transformer) transformGo(file *ast.File) *ast.File {
} }
// The object itself is blacklisted, e.g. a type definition. // The object itself is blacklisted, e.g. a type definition.
if _, ok := tf.blacklist[obj]; ok { if tf.blacklist[obj] {
return true return true
} }
@ -1024,7 +1024,7 @@ func (tf *transformer) transformGo(file *ast.File) *ast.File {
for { for {
tf.nameCounter++ tf.nameCounter++
name = encodeIntToName(tf.nameCounter) name = encodeIntToName(tf.nameCounter)
if _, ok := tf.existingNames[name]; !ok { if !tf.existingNames[name] {
break break
} }
} }
@ -1038,14 +1038,14 @@ func (tf *transformer) transformGo(file *ast.File) *ast.File {
return astutil.Apply(file, pre, nil).(*ast.File) return astutil.Apply(file, pre, nil).(*ast.File)
} }
func blacklistStruct(named *types.Named, blacklist map[types.Object]struct{}) { func blacklistStruct(named *types.Named, blacklist map[types.Object]bool) {
blacklist[named.Obj()] = struct{}{} blacklist[named.Obj()] = true
strct, ok := named.Underlying().(*types.Struct) strct, ok := named.Underlying().(*types.Struct)
if !ok { if !ok {
return return
} }
for i := 0; i < strct.NumFields(); i++ { for i := 0; i < strct.NumFields(); i++ {
blacklist[strct.Field(i)] = struct{}{} blacklist[strct.Field(i)] = true
} }
} }

Loading…
Cancel
Save