github.com/yunabe/lgo@v0.0.0-20190709125917-42c42d410fdf/converter/namepicker.go (about)

     1  package converter
     2  
     3  import (
     4  	"fmt"
     5  	"go/ast"
     6  	"go/types"
     7  )
     8  
     9  type namePicker struct {
    10  	m map[string]bool
    11  }
    12  
    13  func newNamePicker(defs map[*ast.Ident]types.Object) *namePicker {
    14  	m := make(map[string]bool)
    15  	for id := range defs {
    16  		m[id.Name] = true
    17  	}
    18  	return &namePicker{m}
    19  }
    20  
    21  func (p *namePicker) NewName(base string) string {
    22  	if !p.m[base] {
    23  		p.m[base] = true
    24  		return base
    25  	}
    26  	for i := 0; ; i++ {
    27  		name := fmt.Sprintf("%s%d", base, i)
    28  		if !p.m[name] {
    29  			p.m[name] = true
    30  			return name
    31  		}
    32  	}
    33  }