Skip to content

Commit

Permalink
dependencies update + code grouming (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
s0rg authored Nov 21, 2023
1 parent e61d86c commit 7d4f822
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 18 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
11 changes: 9 additions & 2 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
10 changes: 5 additions & 5 deletions state.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand Down
8 changes: 4 additions & 4 deletions statestack.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion statestack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 7d4f822

Please sign in to comment.