From bb6b28688597876dd44cf66641583007eca422af Mon Sep 17 00:00:00 2001 From: Joseph Kato Date: Wed, 6 Mar 2024 14:55:22 -0800 Subject: [PATCH] Ensure greedy alternation --- strcase/opt.go | 6 ++++++ strcase/sentence_test.go | 3 +++ 2 files changed, 9 insertions(+) diff --git a/strcase/opt.go b/strcase/opt.go index 31e5366..5165fe0 100644 --- a/strcase/opt.go +++ b/strcase/opt.go @@ -1,5 +1,7 @@ package strcase +import "sort" + // A CaseConverter converts a string to a specific case. type CaseConverter interface { Convert(string) string @@ -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 } } diff --git a/strcase/sentence_test.go b/strcase/sentence_test.go index 525dd75..3e4e86d 100644 --- a/strcase/sentence_test.go +++ b/strcase/sentence_test.go @@ -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) { @@ -43,6 +44,8 @@ func TestVocab(t *testing.T) { "[Cc]onfig", "NASA", "Moon", + "Axon", + "Axon Server", }), strcase.UsingPrefix(`^[a-z]\.\s`))