Add words even if empty, to prevent undersized lists

This commit is contained in:
zikaeroh 2020-05-23 16:47:28 -07:00
parent 8192cf3315
commit 93610858d0
1 changed files with 3 additions and 8 deletions

View File

@ -22,26 +22,21 @@ func NewList(words []string) List {
for _, w := range words { for _, w := range words {
w = strings.TrimSpace(w) w = strings.TrimSpace(w)
w = strings.ToUpper(w) w = strings.ToUpper(w)
if w != "" { cleaned = append(cleaned, w)
cleaned = append(cleaned, w)
}
} }
return newList(cleaned) return newList(cleaned)
} }
func NewListFromLines(s string) List { func NewListFromLines(s string) List {
s = strings.TrimSpace(s)
words := make([]string, 0, strings.Count(s, "\n")) words := make([]string, 0, strings.Count(s, "\n"))
scanner := bufio.NewScanner(strings.NewReader(s)) scanner := bufio.NewScanner(strings.NewReader(s))
for scanner.Scan() { for scanner.Scan() {
word := scanner.Text() word := scanner.Text()
word = strings.TrimSpace(word) word = strings.TrimSpace(word)
word = strings.ToUpper(word) word = strings.ToUpper(word)
words = append(words, word)
if word != "" {
words = append(words, word)
}
} }
return newList(words) return newList(words)