forked from exercism/go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimpl3.go
More file actions
36 lines (31 loc) · 668 Bytes
/
impl3.go
File metadata and controls
36 lines (31 loc) · 668 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package counter
import "unicode"
// Incorrect implementation: assumes ASCII.
type Impl3 struct {
newlines, characters, letters int
lastChar rune
}
func (c *Impl3) AddString(s string) {
for i := 0; i < len(s); i++ {
char := rune(s[i])
c.lastChar = char
if char == '\n' {
c.newlines++
} else if unicode.IsLetter(char) {
c.letters++
}
c.characters++
}
}
func (c Impl3) Lines() int {
switch {
case c.characters == 0:
return 0
case c.lastChar == '\n':
return c.newlines
default:
return c.newlines + 1
}
}
func (c Impl3) Letters() int { return c.letters }
func (c Impl3) Characters() int { return c.characters }