Skip to content

Commit

Permalink
Refactor text package
Browse files Browse the repository at this point in the history
  • Loading branch information
kayex committed Oct 21, 2020
1 parent baaf4b3 commit 0b8708a
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 55 deletions.
24 changes: 12 additions & 12 deletions normalizers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions text/mutation.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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()
Expand Down
12 changes: 6 additions & 6 deletions text/mutation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
}
Expand All @@ -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)
}
}
}
26 changes: 13 additions & 13 deletions text/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand Down
40 changes: 20 additions & 20 deletions text/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand All @@ -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
}{
Expand Down Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -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.
Expand Down

0 comments on commit 0b8708a

Please sign in to comment.