diff --git a/go.mod b/go.mod index 8b02fed..6804feb 100644 --- a/go.mod +++ b/go.mod @@ -1,5 +1,5 @@ module github.com/s0rg/fantasyname -go 1.20 +go 1.21.4 -require golang.org/x/text v0.11.0 +require golang.org/x/text v0.14.0 diff --git a/go.sum b/go.sum index 5f53cd0..c9c7c64 100644 --- a/go.sum +++ b/go.sum @@ -1,2 +1,2 @@ -golang.org/x/text v0.11.0 h1:LAntKIrcmeSKERyiOh0XMV39LXS8IE9UL2yP7+f5ij4= -golang.org/x/text v0.11.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE= +golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= +golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= diff --git a/parser.go b/parser.go index 0b47376..6083f3c 100644 --- a/parser.go +++ b/parser.go @@ -33,13 +33,20 @@ func newParser(opts ...Option) (p *parser) { p = &parser{conf: c} - p.stack.Push(state{rndfn: p.conf.RandIntN}) // "root" state + // "root" state + p.stack.Push(&state{ + rand: p.conf.RandIntN, + }) return p } func (p *parser) OnGroupStart(isLiteral bool) { - p.stack.Push(state{IsGroup: true, IsLiteral: isLiteral, rndfn: p.conf.RandIntN}) + p.stack.Push(&state{ + IsGroup: true, + IsLiteral: isLiteral, + rand: p.conf.RandIntN, + }) } func (p *parser) OnGroupEnd(isLiteral bool) (err error) { diff --git a/state.go b/state.go index 8a92d88..c43655c 100644 --- a/state.go +++ b/state.go @@ -9,7 +9,7 @@ import ( type wrapper func(fmt.Stringer) fmt.Stringer type state struct { - rndfn func(int) int + rand func(int) int items []fmt.Stringer wrappers []wrapper splitpos int @@ -19,7 +19,7 @@ type state struct { func (s *state) Add(v fmt.Stringer) { if !s.IsGroup { - v = s.withWrappers(v) + v = s.applyWrappers(v) } s.items = append(s.items, v) @@ -60,15 +60,15 @@ func (s *state) Stringer() (rv fmt.Stringer) { case len(s.items) == 1: rv = s.items[0] case s.IsGroup: - rv = stringers.MakeRandom(s.items, s.rndfn) + rv = stringers.MakeRandom(s.items, s.rand) default: rv = stringers.Sequence(s.items) } - return s.withWrappers(rv) + return s.applyWrappers(rv) } -func (s *state) withWrappers(v fmt.Stringer) (rv fmt.Stringer) { +func (s *state) applyWrappers(v fmt.Stringer) (rv fmt.Stringer) { rv = v if len(s.wrappers) > 0 { diff --git a/state_test.go b/state_test.go index 4f972e1..254ab14 100644 --- a/state_test.go +++ b/state_test.go @@ -50,7 +50,7 @@ func TestStateLiteralWrap(t *testing.T) { func TestStateLiteralGroupSplitNotEmpty(t *testing.T) { t.Parallel() - s := state{IsGroup: true, IsLiteral: true, rndfn: rand.Intn} + s := state{IsGroup: true, IsLiteral: true, rand: rand.Intn} s.Add(stringers.Literal("a")) s.Add(stringers.Literal("c")) s.Split() @@ -70,7 +70,7 @@ func TestStateLiteralGroupSplitNotEmpty(t *testing.T) { func TestStateLiteralGroupSplitEmpty(t *testing.T) { t.Parallel() - s := state{IsGroup: true, IsLiteral: true, rndfn: rand.Intn} + s := state{IsGroup: true, IsLiteral: true, rand: rand.Intn} s.Split() s.Add(stringers.Literal("a")) s.Split() diff --git a/statestack.go b/statestack.go index 31d8922..9f26122 100644 --- a/statestack.go +++ b/statestack.go @@ -1,12 +1,12 @@ package fantasyname -type statestack []state +type statestack []*state -func (ss *statestack) Push(s state) { +func (ss *statestack) Push(s *state) { *ss = append(*ss, s) } -func (ss *statestack) Pop() (rv state, ok bool) { +func (ss *statestack) Pop() (rv *state, ok bool) { sp := *ss if n := len(sp); n > 0 { @@ -21,7 +21,7 @@ func (ss *statestack) Top() (rv *state, ok bool) { sp := *ss if n := len(sp); n > 0 { - rv, ok = &sp[n-1], true + rv, ok = sp[n-1], true } return diff --git a/statestack_test.go b/statestack_test.go index 8f10c52..908dfc8 100644 --- a/statestack_test.go +++ b/statestack_test.go @@ -25,7 +25,7 @@ func TestStateStackPushPop(t *testing.T) { stack := statestack{} - stack.Push(state{IsLiteral: true}) + stack.Push(&state{IsLiteral: true}) if len(stack) != 1 { t.Error("unexpected len")