Unique returns a unique name for the given name. A suffix is appended to the name if given name is not unique. If suffixed name is still not unique, a counter value is added to the suffixed name until unique.
(name string, suffix ...string)
| 54 | // name if given name is not unique. If suffixed name is still not unique, a |
| 55 | // counter value is added to the suffixed name until unique. |
| 56 | func (s *NameScope) Unique(name string, suffix ...string) string { |
| 57 | c, ok := s.counts[name] |
| 58 | if !ok { |
| 59 | s.counts[name]++ |
| 60 | return name |
| 61 | } |
| 62 | if len(suffix) > 0 { |
| 63 | name += suffix[0] |
| 64 | c, ok = s.counts[name] |
| 65 | if !ok { |
| 66 | s.counts[name]++ |
| 67 | return name |
| 68 | } |
| 69 | } |
| 70 | for i := c; ; i++ { |
| 71 | ret := name + strconv.Itoa(i+1) |
| 72 | if _, ok := s.counts[ret]; !ok { |
| 73 | s.counts[ret]++ |
| 74 | return ret |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | // PeekUnique returns the name that Unique would return for the same inputs, |
| 80 | // without mutating the scope. |
no outgoing calls