fix: use loop variable instead of global ticker in GenerateFreshName (#40)
## Description `GenerateFreshName` used a global `ticker` variable but never incremented it inside the loop. This caused an infinite loop if the first generated name (`_0`) was already in the used set. - Remove global `ticker` variable. - Use loop variable `i` directly to generate candidate names. ## Benefits - Fixes infinite loop bug when generated name collides with used set. - Removes unnecessary global state. - Simpler and more predictable behavior. ## Checklist - [x] Code follows conventional commit format. - [x] Branch follows naming convention (`<type>/<description>`). Always use underscores. - [x] Tests pass (if applicable). - [x] Documentation updated (if applicable). Reviewed-on: #40 Co-authored-by: M.V. Hutz <git@maximhutz.me> Co-committed-by: M.V. Hutz <git@maximhutz.me>
This commit was merged in pull request #40.
This commit is contained in:
@@ -6,13 +6,11 @@ import (
|
|||||||
"git.maximhutz.com/max/lambda/pkg/set"
|
"git.maximhutz.com/max/lambda/pkg/set"
|
||||||
)
|
)
|
||||||
|
|
||||||
var ticker uint64 = 0
|
|
||||||
|
|
||||||
// GenerateFreshName generates a variable name that is not in the used set.
|
// GenerateFreshName generates a variable name that is not in the used set.
|
||||||
// This function does not mutate the used set.
|
// This function does not mutate the used set.
|
||||||
func GenerateFreshName(used set.Set[string]) string {
|
func GenerateFreshName(used set.Set[string]) string {
|
||||||
for i := uint64(0); ; i++ {
|
for i := uint64(0); ; i++ {
|
||||||
attempt := "_" + string(strconv.AppendUint(nil, ticker, 10))
|
attempt := "_" + string(strconv.AppendUint(nil, i, 10))
|
||||||
|
|
||||||
if !used.Has(attempt) {
|
if !used.Has(attempt) {
|
||||||
return attempt
|
return attempt
|
||||||
|
|||||||
Reference in New Issue
Block a user