Skip to content

Commit

Permalink
Ensure greedy alternation
Browse files Browse the repository at this point in the history
  • Loading branch information
jdkato committed Mar 6, 2024
1 parent 5ad9282 commit bb6b286
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
6 changes: 6 additions & 0 deletions strcase/opt.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package strcase

import "sort"

// A CaseConverter converts a string to a specific case.
type CaseConverter interface {
Convert(string) string
Expand Down Expand Up @@ -31,6 +33,10 @@ type CaseOpts struct {
// UsingVocab sets the vocab for the CaseConverter.
func UsingVocab(vocab []string) CaseOptFunc {
return func(opts *CaseOpts) {
// NOTE: This is required to ensure that we have greedy alternation.
sort.Slice(vocab, func(p, q int) bool {
return len(vocab[p]) > len(vocab[q])
})
opts.vocab = vocab
}
}
Expand Down
3 changes: 3 additions & 0 deletions strcase/sentence_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ var vocabCases = []testCase{
{"Configuration", "Configuration"},
{"Six NASA Instruments Will Fly to Moon on Intuitive Machines Lander", "Six NASA instruments will fly to Moon on intuitive machines lander"},
{"b. Next title text", "b. Next title text"},
{"Axon Server connection", "Axon Server connection"},
}

func TestSentence(t *testing.T) {
Expand All @@ -43,6 +44,8 @@ func TestVocab(t *testing.T) {
"[Cc]onfig",
"NASA",
"Moon",
"Axon",
"Axon Server",
}),
strcase.UsingPrefix(`^[a-z]\.\s`))

Expand Down

0 comments on commit bb6b286

Please sign in to comment.