-
Notifications
You must be signed in to change notification settings - Fork 13
/
location.go
79 lines (67 loc) · 1.36 KB
/
location.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// gosax: example of using gosax.
//
// Eli Bendersky [https://eli.thegreenplace.net]
// This code is in the public domain.
package main
import (
"flag"
"fmt"
"log"
"os"
"runtime/pprof"
"strings"
"unsafe"
"github.com/eliben/gosax"
)
var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file")
func main() {
flag.Parse()
if *cpuprofile != "" {
f, err := os.Create(*cpuprofile)
if err != nil {
log.Fatal(err)
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}
counter := 0
inLocation := false
scb := gosax.SaxCallbacks{
StartElement: func(name string, attrs []string) {
if name == "location" {
inLocation = true
} else {
inLocation = false
}
},
// this overrides StartElement
StartElementNoAttr: func(name string) {
if name == "location" {
inLocation = true
} else {
inLocation = false
}
},
EndElement: func(name string) {
inLocation = false
},
Characters: func(contents string) {
if inLocation && strings.Contains(contents, "Africa") {
counter++
}
},
// this overrides Characters
CharactersRaw: func(ch unsafe.Pointer, chlen int) {
if inLocation {
if strings.Contains(gosax.UnpackString(ch, chlen), "Africa") {
counter++
}
}
},
}
err := gosax.ParseFile(flag.Args()[0], scb)
if err != nil {
panic(err)
}
fmt.Println("counter =", counter)
}