Debian Codegraph @

plist_types.go

raw symbols SCIP
1 package plist
2
3 import (
4 "hash/crc32"
5 "sort"
6 "time"
7 "strconv"
8 )
9
10 // magic value used in the non-binary encoding of UIDs
11 // (stored as a dictionary mapping CF$UID->integer)
12 const cfUIDMagic = "CF$UID"
13
14 type cfValue interface {
15 typeName() string
16 hash() interface{}
17 }
18
19 type cfDictionary struct {
20 keys sort.StringSlice
21 values []cfValue
22 }
23
24 func (*cfDictionary) typeName() string {
25 return "dictionary"
26 }
27
28 func (p *cfDictionary) hash() interface{} {
29 return p
30 }
31
32 func (p *cfDictionary) Len() int {
33 return len(p.keys)
34 }
35
36 func (p *cfDictionary) Less(i, j int) bool {
37 return p.keys.Less(i, j)
38 }
39
40 func (p *cfDictionary) Swap(i, j int) {
41 p.keys.Swap(i, j)
42 p.values[i], p.values[j] = p.values[j], p.values[i]
43 }
44
45 func (p *cfDictionary) sort() {
46 sort.Sort(p)
47 }
48
49 func (p *cfDictionary) maybeUID(lax bool) cfValue {
50 if len(p.keys) == 1 && p.keys[0] == "CF$UID" && len(p.values) == 1 {
51 pval := p.values[0]
52 if integer, ok := pval.(*cfNumber); ok {
53 return cfUID(integer.value)
54 }
55 // Openstep only has cfString. Act like the unmarshaller a bit.
56 if lax {
57 if str, ok := pval.(cfString); ok {
58 if i, err := strconv.ParseUint(string(str), 10, 64); err == nil {
59 return cfUID(i)
60 }
61 }
62 }
63 }
64 return p
65 }
66
67 type cfArray struct {
68 values []cfValue
69 }
70
71 func (*cfArray) typeName() string {
72 return "array"
73 }
74
75 func (p *cfArray) hash() interface{} {
76 return p
77 }
78
79 type cfString string
80
81 func (cfString) typeName() string {
82 return "string"
83 }
84
85 func (p cfString) hash() interface{} {
86 return string(p)
87 }
88
89 type cfNumber struct {
90 signed bool
91 value uint64
92 }
93
94 func (*cfNumber) typeName() string {
95 return "integer"
96 }
97
98 func (p *cfNumber) hash() interface{} {
99 if p.signed {
100 return int64(p.value)
101 }
102 return p.value
103 }
104
105 type cfReal struct {
106 wide bool
107 value float64
108 }
109
110 func (cfReal) typeName() string {
111 return "real"
112 }
113
114 func (p *cfReal) hash() interface{} {
115 if p.wide {
116 return p.value
117 }
118 return float32(p.value)
119 }
120
121 type cfBoolean bool
122
123 func (cfBoolean) typeName() string {
124 return "boolean"
125 }
126
127 func (p cfBoolean) hash() interface{} {
128 return bool(p)
129 }
130
131 type cfUID UID
132
133 func (cfUID) typeName() string {
134 return "UID"
135 }
136
137 func (p cfUID) hash() interface{} {
138 return p
139 }
140
141 func (p cfUID) toDict() *cfDictionary {
142 return &cfDictionary{
143 keys: []string{cfUIDMagic},
144 values: []cfValue{&cfNumber{
145 signed: false,
146 value: uint64(p),
147 }},
148 }
149 }
150
151 type cfData []byte
152
153 func (cfData) typeName() string {
154 return "data"
155 }
156
157 func (p cfData) hash() interface{} {
158 // Data are uniqued by their checksums.
159 // Todo: Look at calculating this only once and storing it somewhere;
160 // crc32 is fairly quick, however.
161 return crc32.ChecksumIEEE([]byte(p))
162 }
163
164 type cfDate time.Time
165
166 func (cfDate) typeName() string {
167 return "date"
168 }
169
170 func (p cfDate) hash() interface{} {
171 return time.Time(p)
172 }