Skip to content

Commit

Permalink
fix: split dictionary using scanner
Browse files Browse the repository at this point in the history
Don't split dictionary lines by line-ending characters. Instead, use
bufio scanner, avoiding inconsistencies between LF and CRLF
line-endings.
  • Loading branch information
Splode committed Oct 20, 2022
1 parent b1194a4 commit 1d41799
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions dictionary.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,26 @@
package fname

import (
"bufio"
_ "embed"
"strings"
)

//go:embed data/adjective
var _adjective string
var adjective = strings.Split(_adjective, "\n")
var adjective = split(_adjective)

//go:embed data/adverb
var _adverb string
var adverb = strings.Split(_adverb, "\n")
var adverb = split(_adverb)

//go:embed data/noun
var _noun string
var noun = strings.Split(_noun, "\n")
var noun = split(_noun)

//go:embed data/verb
var _verb string
var verb = strings.Split(_verb, "\n")
var verb = split(_verb)

// Dictionary is a collection of words.
type Dictionary struct {
Expand Down Expand Up @@ -60,3 +61,13 @@ func (d *Dictionary) LengthNoun() int {
func (d *Dictionary) LengthVerb() int {
return len(d.verbs)
}

func split(s string) []string {
scanner := bufio.NewScanner(strings.NewReader(s))
scanner.Split(bufio.ScanLines)
var lines []string
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
return lines
}

0 comments on commit 1d41799

Please sign in to comment.