From 0b8708a0c3d082193c7db796c35f85bf844fa7d3 Mon Sep 17 00:00:00 2001 From: Johan Vester Date: Wed, 21 Oct 2020 05:56:25 +0200 Subject: [PATCH] Refactor text package --- normalizers.go | 24 ++++++++++++------------ text/mutation.go | 8 ++++---- text/mutation_test.go | 12 ++++++------ text/query.go | 26 +++++++++++++------------- text/query_test.go | 40 ++++++++++++++++++++-------------------- 5 files changed, 55 insertions(+), 55 deletions(-) diff --git a/normalizers.go b/normalizers.go index cf631a0..d161dcc 100644 --- a/normalizers.go +++ b/normalizers.go @@ -20,14 +20,14 @@ func NormalizeAccidentals(s string) string { } for search, replace := range replacements { - q := &text.SubQuery{ - Search: text.CaseInsensitiveWordQuery{ - WordQuery: text.WordQuery{ + q := &text.ReplaceQuery{ + Search: text.CaseInsensitiveWord{ + Word: text.Word{ W: search, Delimiter: text.FilenameComponentDelimiter, }, }, - Sub: replace, + Replacement: replace, } s = q.Apply(s) @@ -38,14 +38,14 @@ func NormalizeAccidentals(s string) string { func ExtendMajor(s string) string { for _, n := range keys() { - q := &text.SubQuery{ - Search: text.CaseInsensitiveWordQuery{ - WordQuery: text.WordQuery{ + q := &text.ReplaceQuery{ + Search: text.CaseInsensitiveWord{ + Word: text.Word{ W: n, Delimiter: text.FilenameComponentDelimiter, }, }, - Sub: major(n), + Replacement: major(n), } s = q.Apply(s) @@ -56,14 +56,14 @@ func ExtendMajor(s string) string { func ExtendMinor(s string) string { for _, n := range keys() { - q := &text.SubQuery{ - Search: text.CaseInsensitiveWordQuery{ - WordQuery: text.WordQuery{ + q := &text.ReplaceQuery{ + Search: text.CaseInsensitiveWord{ + Word: text.Word{ W: fmt.Sprintf("%sm", n), Delimiter: text.FilenameComponentDelimiter, }, }, - Sub: minor(n), + Replacement: minor(n), } s = q.Apply(s) diff --git a/text/mutation.go b/text/mutation.go index 6c1801b..387ccac 100644 --- a/text/mutation.go +++ b/text/mutation.go @@ -8,12 +8,12 @@ type Mutation interface { Apply(text string) string } -type SubQuery struct { +type ReplaceQuery struct { Search Query - Sub string + Replacement string } -func (s *SubQuery) Apply(text string) string { +func (s *ReplaceQuery) Apply(text string) string { for { i, ln := s.Search.Match(text) if i < 0 { @@ -28,7 +28,7 @@ func (s *SubQuery) Apply(text string) string { var buf bytes.Buffer buf.WriteString(string(beginning)) - buf.WriteString(s.Sub) + buf.WriteString(s.Replacement) buf.WriteString(string(end)) text = buf.String() diff --git a/text/mutation_test.go b/text/mutation_test.go index 46afe8f..1d39aa5 100644 --- a/text/mutation_test.go +++ b/text/mutation_test.go @@ -5,27 +5,27 @@ import "testing" func TestSubWord_Apply(t *testing.T) { cases := []struct { str string - sw SubQuery + sw ReplaceQuery exp string }{ { str: "foo bar", - sw: SubQuery{Word("bar"), "boo"}, + sw: ReplaceQuery{NewWord("bar"), "boo"}, exp: "foo boo", }, { str: "foo åäö", - sw: SubQuery{Word("foo"), "bar"}, + sw: ReplaceQuery{NewWord("foo"), "bar"}, exp: "bar åäö", }, { str: "foo åäö", - sw: SubQuery{Word("åäö"), "bar"}, + sw: ReplaceQuery{NewWord("åäö"), "bar"}, exp: "foo bar", }, { str: "foo barbaz", - sw: SubQuery{Word("foo"), "long replacement"}, + sw: ReplaceQuery{NewWord("foo"), "long replacement"}, exp: "long replacement barbaz", }, } @@ -34,7 +34,7 @@ func TestSubWord_Apply(t *testing.T) { act := c.sw.Apply(c.str) if act != c.exp { - t.Errorf("Expected SubQuery{%#v, %q}.Apply(%q) to return %q, got %q", c.sw.Search, c.sw.Sub, c.str, c.exp, act) + t.Errorf("Expected SubQuery{%#v, %q}.Apply(%q) to return %q, got %q", c.sw.Search, c.sw.Replacement, c.str, c.exp, act) } } } diff --git a/text/query.go b/text/query.go index f815c43..b662464 100644 --- a/text/query.go +++ b/text/query.go @@ -10,22 +10,22 @@ type Query interface { Match(string) (i int, l int) } -// WordQuery matches the first occurrence of W in a search text where W is -// surrounded by delimiters as defined by Delimiter. -type WordQuery struct { +// Word matches the first occurrence of W in a search text where W is +// surrounded by delimiters that satisfy Delimiter. +type Word struct { W string Delimiter func(rune) bool } -func Word(w string) WordQuery { +func NewWord(w string) Word { if len(w) == 0 { panic("Cannot create WordQuery of length 0") } - return WordQuery{w, WordDelimiter} + return Word{w, WordDelimiter} } -func (q WordQuery) Match(s string) (int, int) { +func (q Word) Match(s string) (int, int) { if len(s) == 0 { return -1, 0 } @@ -63,23 +63,23 @@ func (q WordQuery) Match(s string) (int, int) { } -func (q WordQuery) Length() int { +func (q Word) Length() int { return utf8.RuneCountInString(q.W) } -type CaseInsensitiveWordQuery struct { - WordQuery +type CaseInsensitiveWord struct { + Word } -func IWord(w string) CaseInsensitiveWordQuery { - return CaseInsensitiveWordQuery{Word(w)} +func IWord(w string) CaseInsensitiveWord { + return CaseInsensitiveWord{NewWord(w)} } -func (q CaseInsensitiveWordQuery) Match(text string) (int, int) { +func (q CaseInsensitiveWord) Match(text string) (int, int) { sl := strings.ToLower(text) q.W = strings.ToLower(q.W) - return q.WordQuery.Match(sl) + return q.Word.Match(sl) } // FilenameComponentDelimiter returns a bool indicating if diff --git a/text/query_test.go b/text/query_test.go index afc2ea2..6905516 100644 --- a/text/query_test.go +++ b/text/query_test.go @@ -4,82 +4,82 @@ import ( "testing" ) -func TestWordQuery_Match(t *testing.T) { +func TestWord_Match(t *testing.T) { cases := []struct { s string - q WordQuery + q Word i int ln int }{ { s: "foo", - q: Word("foo"), + q: NewWord("foo"), i: 0, ln: 3, }, { s: "foo bar", - q: Word("bar"), + q: NewWord("bar"), i: 4, ln: 3, }, { s: "foo barbaz", - q: Word("bar"), + q: NewWord("bar"), i: -1, ln: 0, }, { s: "foobar baz", - q: Word("bar"), + q: NewWord("bar"), i: -1, ln: 0, }, { s: "foo bar", - q: Word("FOO"), + q: NewWord("FOO"), i: -1, ln: 0, }, { s: "FOO BAR", - q: Word("foo"), + q: NewWord("foo"), i: -1, ln: 0, }, { s: "foo\tbar", - q: Word("bar"), + q: NewWord("bar"), i: 4, ln: 3, }, { s: "foo\nbar", - q: Word("bar"), + q: NewWord("bar"), i: 4, ln: 3, }, { s: "foo\vbar", - q: Word("bar"), + q: NewWord("bar"), i: 4, ln: 3, }, { s: "foo\fbar", - q: Word("bar"), + q: NewWord("bar"), i: 4, ln: 3, }, { s: "foo\rbar", - q: Word("bar"), + q: NewWord("bar"), i: 4, ln: 3, }, { s: "åäö bar", - q: Word("bar"), + q: NewWord("bar"), i: 4, ln: 3, }, @@ -89,15 +89,15 @@ func TestWordQuery_Match(t *testing.T) { i, ln := c.q.Match(c.s) if i != c.i || ln != c.ln { - t.Errorf("Expected Word(%q).Match(%q) to return (%v, %v) got (%v, %v)", c.q.W, c.s, c.i, c.ln, i, ln) + t.Errorf("Expected NewWord(%q).Match(%q) to return (%v, %v) got (%v, %v)", c.q.W, c.s, c.i, c.ln, i, ln) } } } -func TestCaseInsensitiveWordQuery_Match(t *testing.T) { +func TestCaseInsensitiveWord_Match(t *testing.T) { cases := []struct { s string - q CaseInsensitiveWordQuery + q CaseInsensitiveWord i int ln int }{ @@ -131,7 +131,7 @@ func TestCaseInsensitiveWordQuery_Match(t *testing.T) { // This benchmark gives a good indication of the average performance of an // unsuccessful search. func BenchmarkWord_MatchNotExist_6_587(b *testing.B) { - w := Word("foobar") + w := NewWord("foobar") txt := `Lorem ipsum dolor sit amet, an cum vero soleat concludaturque, te purto vero reprimique vis. Ignota mediocritatem ut sea. Cetero deserunt pericula te vel. Omnis legendos no per. @@ -153,7 +153,7 @@ func BenchmarkWord_MatchNotExist_6_587(b *testing.B) { // This benchmark gives a good indication of the worst case performance // of an unsuccessful search. func BenchmarkWord_MatchPartials_6_587(b *testing.B) { - w := Word("foobar") + w := NewWord("foobar") txt := `fooba fooba fooba fooba fooba fooba fooba fooba fooba fooba fooba fooba fooba fooba fooba fooba fooba fooba fooba fooba @@ -177,7 +177,7 @@ func BenchmarkWord_MatchPartials_6_587(b *testing.B) { // 6 against a search text of length 587, where the sought string is at the // very end of the search text. func BenchmarkWord_MatchExist_6_587(b *testing.B) { - w := Word("foobar") + w := NewWord("foobar") txt := `Lorem ipsum dolor sit amet, an cum vero soleat concludaturque, te purto vero reprimique vis. Ignota mediocritatem ut sea. Cetero deserunt pericula te vel. Omnis legendos no per.