/
cgf_aux.go
223 lines (155 loc) · 4.18 KB
/
cgf_aux.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
//package main
package cgf
import "fmt"
import "strings"
import "strconv"
//import "./dlug"
import "github.com/abeconnelly/dlug"
//func fill_noc_seq(seq string, delpos_len []int) string {
func FillNocSeq(seq string, delpos_len []int) string {
s := []byte(seq)
cur_pos:=0
for i:=0; i<len(delpos_len); i+=2 {
loqpos := cur_pos + delpos_len[i]
loqlen := delpos_len[i+1]
for j:=0; j<loqlen; j++ {
if ((loqpos+j)>=len(s)) {
panic( fmt.Sprintf("!!!! %v, %s", delpos_len, seq))
}
s[loqpos+j] = 'n'
}
cur_pos += delpos_len[i]
}
return string(s)
}
//func parse_tilepos(s string) (path, ver, step int, err error) {
func ParseTilepos(s string) (path, ver, step int, err error) {
parts := strings.Split(s, ".")
if len(parts)<2 || len(parts)>3 {
err = fmt.Errorf("invalid string (must be 2 or 3 fields '.' separated)")
return
}
var u64 int64
p_pos:=0
u64,err = strconv.ParseInt(parts[p_pos], 16, 64)
if err!=nil { return }
p_pos++
path = int(u64)
if len(parts)==3 {
u64,err = strconv.ParseInt(parts[p_pos], 16, 64)
if err!=nil { return }
ver = int(u64)
p_pos++
}
u64,err = strconv.ParseInt(parts[p_pos], 16, 64)
if err!=nil { return }
step = int(u64)
p_pos++
return
}
func fill_slice_string(buf []byte, s string) ([]byte, int) {
var dn int
n:=0
tbuf := make([]byte, 8)
dn = dlug.FillSliceUint32(tbuf, uint32(len(s)))
buf = append(buf, tbuf[0:dn]...)
n += dn
buf = append(buf, []byte(s)...)
n+=len(s)
return buf, n
}
func create_tilemap_string_lookup2(step0,span0,step1,span1 []int) string {
b := make([]byte, 0, 1024)
for i:=0; i<len(step0); i++ {
if i>0 { b = append(b, ';') }
b = append(b, fmt.Sprintf("%x", step0[i])...)
if span0[i]>1 {
b = append(b, fmt.Sprintf("+%x", span0[i])...)
}
}
b = append(b, ':')
for i:=0; i<len(step1); i++ {
if i>0 { b = append(b, ';') }
b = append(b, fmt.Sprintf("%x", step1[i])...)
if span1[i]>1 {
b = append(b, fmt.Sprintf("+%x", span1[i])...)
}
}
return string(b)
}
func simple_text_field(line, field string) (string, int) {
sep := "\"" + field + "\""
p:=strings.Index(line, sep)
n:=len(line)
if p<0 { return "", p; }
p+=len(sep)
if p>=n { return "", -1 }
for ; p<n && line[p]==' '; p++ { }
if p==n { return "", -2; }
if line[p] != ':' { return "", -3; }
p++
if p==n { return "", -2; }
for ; p<n && line[p]==' '; p++ { }
if p==n { return "", -4; }
if line[p] != '"' { return "", -5; }
p++
if p==n { return "", -6; }
p_s := p
for ; p<n && line[p]!='"'; p++ { }
if p==n { return "", -7; }
p_e := p
return line[p_s:p_e], p_e
}
func simple_bool_field(line, field string) (bool, int) {
sep := "\"" + field + "\""
p:=strings.Index(line, sep)
n:=len(line)
if p<0 { return false, p; }
p+=len(sep)
if p>=n { return false, -1 }
for ; p<n && line[p]==' '; p++ { }
if p==n { return false, -2; }
if line[p] != ':' { return false, -3; }
p++
if p==n { return false,-2; }
for ; p<n && line[p]==' '; p++ { }
if p==n { return false, -4; }
val_start_pos := p
for ; p<n && line[p]!=',' && line[p]!='}' && line[p]!=' ' && line[p]!='\t' && line[p]!='\n' ; p++ { }
if p==n { return false, -5; }
val := line[val_start_pos:p]
if val=="true" { return true, p }
if val=="false" { return false, p }
return false, -6
}
func simple_int_field(line, field string) (int, int) {
sep := "\"" + field + "\""
p:=strings.Index(line, sep)
n:=len(line)
if p<0 { return 0, p; }
p+=len(sep)
if p>=n { return 0, -2 }
for ; p<n && line[p]==' '; p++ { }
if p==n { return 0, -3; }
if line[p] != ':' { return 0, -4; }
p++
if p==n { return 0, -5; }
for ; p<n && line[p]==' '; p++ { }
if p==n { return 0, -6; }
p_s := p
for ; p<n && (line[p]!='"') && (line[p]!=','); p++ { }
if p==n { return 0, -7; }
p_e := p
i,e := strconv.ParseInt(line[p_s:p_e], 10, 64)
if e!=nil { return 0, -8; }
return int(i), p_e
}
func Md5sum2str(md5sum [16]byte) string {
var str_md5sum [32]byte
for i:=0; i<16; i++ {
x := fmt.Sprintf("%02x", md5sum[i])
str_md5sum[2*i] = x[0]
str_md5sum[2*i+1] = x[1]
}
return string(str_md5sum[:])
}