forked from uia-worker/pprof
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwordcount03.go
50 lines (41 loc) · 1.24 KB
/
wordcount03.go
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package main
import (
"fmt"
"io"
"log"
"os"
"unicode"
"github.com/pkg/profile"
"bufio" // endring A fra wordcount01
)
var buf [1]byte // endring C fra wordcount02
func readbyte(r io.Reader) (rune, error) {
//var buf [1]byte endring C fra wordcount02
_, err := r.Read(buf[:])
return rune(buf[0]), err
}
func main() {
// For å generee profiler; kun en aktivert av gangen
//defer profile.Start(profile.CPUProfile, profile.ProfilePath(".")).Stop()
//defer profile.Start(profile.MemProfile, profile.ProfilePath(".")).Stop()
defer profile.Start(profile.MemProfile, profile.MemProfileRate(1), profile.ProfilePath(".")).Stop()
f, err := os.Open(os.Args[1])
if err != nil {
log.Fatalf("Kunne ikke åpne filen %q: %v", os.Args[1], err)
}
words := 0
inword := false // tilstandsmaskin at vi er inn et ord (obs! unicode)
b := bufio.NewReader(f) // endring B fra wordcount01
for {
r, err := readbyte(b)
if err == io.EOF {
break
}
if unicode.IsSpace(r) && inword {
words++
inword = false
}
inword = unicode.IsLetter(r)
}
fmt.Printf("%q: %d words\n", os.Args[1], words)
}