-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathxml_shared_strings.go
66 lines (62 loc) · 1.21 KB
/
xml_shared_strings.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
package excel
import (
"encoding/xml"
"io"
"github.com/szyhf/go-convert"
)
func readSharedStringsXML(rc io.ReadCloser) []string {
decoder := xml.NewDecoder(rc)
tStart, rStart := false, false
var slc []string
siIndex := 0
for t, err := decoder.Token(); err == nil; t, err = decoder.Token() {
switch token := t.(type) {
case xml.StartElement:
switch token.Name.Local {
case _SI:
// don't enter default ...
case _T:
tStart = true
case _R:
rStart = true
case _SST:
count := 0
unqCount := 0
for _, attr := range token.Attr {
switch attr.Name.Local {
case _Count:
count = convert.MustInt(attr.Value)
case _UniqueCount:
unqCount = convert.MustInt(attr.Value)
}
}
if unqCount != 0 {
slc = make([]string, unqCount)
} else {
slc = make([]string, count)
}
default:
_ = decoder.Skip()
}
case xml.EndElement:
switch token.Name.Local {
case _SI:
siIndex++
case _T:
tStart = false
case _R:
rStart = false
}
case xml.CharData:
if tStart {
if rStart {
str := slc[siIndex]
slc[siIndex] = str + string(token)
} else {
slc[siIndex] = string(token)
}
}
}
}
return slc
}