| 1 |
package vcfgo |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"fmt" |
| 6 |
"strconv" |
| 7 |
"strings" |
| 8 |
) |
| 9 |
|
| 10 |
type InfoByte struct { |
| 11 |
Info []byte |
| 12 |
|
| 13 |
header *Header |
| 14 |
} |
| 15 |
|
| 16 |
func NewInfoByte(info []byte, h *Header) *InfoByte { |
| 17 |
if len(info) == 1 && info[0] == '.' { |
| 18 |
return &InfoByte{Info: []byte(""), header: h} |
| 19 |
} |
| 20 |
return &InfoByte{Info: info, header: h} |
| 21 |
} |
| 22 |
|
| 23 |
/* |
| 24 |
func bytesToString(b []byte) string { |
| 25 |
bh := (*reflect.SliceHeader)(unsafe.Pointer(&b)) |
| 26 |
sh := reflect.StringHeader{bh.Data, bh.Len} |
| 27 |
return *(*string)(unsafe.Pointer(&sh)) |
| 28 |
} |
| 29 |
*/ |
| 30 |
|
| 31 |
// return the start and end positions of the value. |
| 32 |
// for flag the value is the flag. |
| 33 |
func getpositions(info []byte, key string) (start, end int) { |
| 34 |
//if ipos := strings.LastIndex(bytesToString(info), ";"+key+"="); ipos != -1 { |
| 35 |
if ipos := bytes.Index(info, []byte(";"+key+"=")); ipos != -1 { |
| 36 |
start = ipos + 2 + len(key) |
| 37 |
for end := start + 1; end < len(info)-1; end++ { |
| 38 |
if info[end] == ';' { |
| 39 |
return start, end - 1 |
| 40 |
} |
| 41 |
} |
| 42 |
return start, len(info) - 1 |
| 43 |
} |
| 44 |
|
| 45 |
bkey := []byte(key) |
| 46 |
ipos, pos := 0, 0 |
| 47 |
|
| 48 |
for { |
| 49 |
if pos >= len(info) { |
| 50 |
return -1, -1 |
| 51 |
} |
| 52 |
if pos == 0 { |
| 53 |
ipos = bytes.Index(info, bkey) |
| 54 |
} else { |
| 55 |
ipos = bytes.Index(info[pos:], bkey) |
| 56 |
} |
| 57 |
|
| 58 |
if ipos == -1 { |
| 59 |
return -1, -1 |
| 60 |
} |
| 61 |
pos += ipos |
| 62 |
if pos != 0 && info[pos-1] != ';' { |
| 63 |
pos += 1 |
| 64 |
continue |
| 65 |
} |
| 66 |
eq := pos + bytes.IndexByte(info[pos:], '=') |
| 67 |
// at end of field and we found an Flag |
| 68 |
var semi int |
| 69 |
if eq == -1 { |
| 70 |
return pos, len(info) |
| 71 |
} else if eq-pos != len(bkey) { |
| 72 |
// found a longer key with same prefix. |
| 73 |
semi = bytes.IndexByte(info[pos:], ';') |
| 74 |
// flag field |
| 75 |
if semi == -1 { |
| 76 |
semi = len(info) |
| 77 |
} else { |
| 78 |
semi += pos |
| 79 |
} |
| 80 |
if semi-pos == len(bkey) { |
| 81 |
return pos, semi - 1 |
| 82 |
} |
| 83 |
pos = semi + 1 |
| 84 |
continue |
| 85 |
} else { |
| 86 |
semi = bytes.IndexByte(info[pos+1:], byte(';')) |
| 87 |
} |
| 88 |
if semi > -1 && eq > pos+semi { |
| 89 |
// should be a flag. |
| 90 |
return pos, pos + semi |
| 91 |
} |
| 92 |
|
| 93 |
// not at end of info field |
| 94 |
if semi != -1 { |
| 95 |
semi += pos |
| 96 |
} |
| 97 |
return eq + 1, semi |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
func (i InfoByte) Contains(key string) bool { |
| 102 |
// short-circuit common case. |
| 103 |
if !bytes.Contains(i.Info, []byte(key+"=")) { |
| 104 |
return false |
| 105 |
} |
| 106 |
s, _ := getpositions(i.Info, key) |
| 107 |
return s != -1 |
| 108 |
} |
| 109 |
|
| 110 |
func (i InfoByte) Keys() []string { |
| 111 |
sp := bytes.Split(i.Info, []byte{';'}) |
| 112 |
keys := make([]string, 0, len(sp)) |
| 113 |
for _, pair := range sp { |
| 114 |
key := bytes.SplitN(pair, []byte{'='}, 2)[0] |
| 115 |
keys = append(keys, string(key)) |
| 116 |
} |
| 117 |
return keys |
| 118 |
|
| 119 |
} |
| 120 |
|
| 121 |
func (i *InfoByte) Delete(key string) { |
| 122 |
s, e := getpositions(i.Info, key) |
| 123 |
if s == -1 { |
| 124 |
return |
| 125 |
} |
| 126 |
// check if it's a flag |
| 127 |
if s != 0 && i.Info[s-1] != ';' { |
| 128 |
s -= (len(key) + 1) |
| 129 |
} |
| 130 |
if s < 0 { |
| 131 |
s = 0 |
| 132 |
} |
| 133 |
if e == -1 { |
| 134 |
e = len(i.Info) |
| 135 |
} else { |
| 136 |
e += 2 |
| 137 |
} |
| 138 |
if s == 0 && e == len(i.Info) { |
| 139 |
i.Info = i.Info[:0] |
| 140 |
} else if e < len(i.Info) { |
| 141 |
i.Info = append(i.Info[:s], i.Info[e:]...) |
| 142 |
} else { |
| 143 |
i.Info = i.Info[:s-1] |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
func ItoS(k string, v interface{}) string { |
| 148 |
if _, ok := v.(bool); ok { |
| 149 |
return k |
| 150 |
} else if v == nil { |
| 151 |
return "." |
| 152 |
} else { |
| 153 |
switch v.(type) { |
| 154 |
case []interface{}: |
| 155 |
vs := v.([]interface{}) |
| 156 |
out := make([]string, len(vs)) |
| 157 |
for i, vv := range vs { |
| 158 |
out[i] = ItoS(k, vv) |
| 159 |
} |
| 160 |
return strings.Join(out, ",") |
| 161 |
case float32: |
| 162 |
return fmtFloat32(v.(float32)) |
| 163 |
case float64: |
| 164 |
return fmtFloat64(v.(float64)) |
| 165 |
case int: |
| 166 |
return fmt.Sprintf("%d", v.(int)) |
| 167 |
case uint32: |
| 168 |
return fmt.Sprintf("%d", v.(uint32)) |
| 169 |
case []float64: |
| 170 |
vals := v.([]float64) |
| 171 |
svals := make([]string, len(vals)) |
| 172 |
for i, val := range vals { |
| 173 |
svals[i] = fmtFloat64(float64(val)) |
| 174 |
} |
| 175 |
return strings.Join(svals, ",") |
| 176 |
case []float32: |
| 177 |
vals := v.([]float32) |
| 178 |
svals := make([]string, len(vals)) |
| 179 |
for i, val := range vals { |
| 180 |
svals[i] = fmtFloat32(float32(val)) |
| 181 |
} |
| 182 |
return strings.Join(svals, ",") |
| 183 |
case []int: |
| 184 |
vals := v.([]int) |
| 185 |
svals := make([]string, len(vals)) |
| 186 |
for i, val := range vals { |
| 187 |
svals[i] = strconv.Itoa(int(val)) |
| 188 |
} |
| 189 |
return strings.Join(svals, ",") |
| 190 |
case []int32: |
| 191 |
vals := v.([]int32) |
| 192 |
svals := make([]string, len(vals)) |
| 193 |
for i, val := range vals { |
| 194 |
svals[i] = strconv.Itoa(int(int32(val))) |
| 195 |
} |
| 196 |
return strings.Join(svals, ",") |
| 197 |
case []string: |
| 198 |
return strings.Join(v.([]string), ",") |
| 199 |
|
| 200 |
default: |
| 201 |
return v.(string) |
| 202 |
} |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
func (i InfoByte) SGet(key string) []byte { |
| 207 |
var sub []byte |
| 208 |
if key == "" || len(i.Info) == 1 { |
| 209 |
return sub |
| 210 |
} |
| 211 |
start, end := getpositions(i.Info, key) |
| 212 |
if start == -1 { |
| 213 |
return sub |
| 214 |
} |
| 215 |
if end == -1 { |
| 216 |
end = len(i.Info) - 1 |
| 217 |
} |
| 218 |
val := i.Info[start : end+1] |
| 219 |
return val |
| 220 |
} |
| 221 |
|
| 222 |
// Get a value from the bytes typed according to the header. |
| 223 |
func (i InfoByte) Get(key string) (interface{}, error) { |
| 224 |
v := string(i.SGet(key)) |
| 225 |
skey := string(key) |
| 226 |
var ok bool |
| 227 |
var hi *Info |
| 228 |
if i.header == nil || i.header.Infos == nil { |
| 229 |
ok = false |
| 230 |
} else { |
| 231 |
i.header.RLock() |
| 232 |
hi, ok = i.header.Infos[key] |
| 233 |
i.header.RUnlock() |
| 234 |
} |
| 235 |
if !ok { |
| 236 |
err := fmt.Errorf("Info Error: %s not found in header", skey) |
| 237 |
// flag |
| 238 |
if skey == v { |
| 239 |
return true, err |
| 240 |
} |
| 241 |
if v == "" { |
| 242 |
return nil, err |
| 243 |
} |
| 244 |
return v, err |
| 245 |
} |
| 246 |
|
| 247 |
if v == "" { |
| 248 |
var err error |
| 249 |
var val interface{} = nil |
| 250 |
if hi.Type != "Flag" { |
| 251 |
err = fmt.Errorf("Info Error: %s not found in INFO", skey) |
| 252 |
} else { |
| 253 |
val = false |
| 254 |
} |
| 255 |
return val, err |
| 256 |
} |
| 257 |
if v == key { |
| 258 |
var err error |
| 259 |
if hi.Type != "Flag" { |
| 260 |
err = fmt.Errorf("Info Error: flag field (%s) should be specified as such in the header", skey) |
| 261 |
} |
| 262 |
return true, err |
| 263 |
} |
| 264 |
|
| 265 |
var err error |
| 266 |
var iv interface{} |
| 267 |
if hi.Number == "1" || (hi.Number == "." && strings.IndexByte(v, ',') == -1) { |
| 268 |
return parseOne(skey, v, hi.Type) |
| 269 |
} |
| 270 |
|
| 271 |
switch hi.Number { |
| 272 |
|
| 273 |
case "0": |
| 274 |
if hi.Type != "Flag" { |
| 275 |
err = fmt.Errorf("Info Error: flag field (%s) should have Number=0", skey) |
| 276 |
} |
| 277 |
return true, err |
| 278 |
|
| 279 |
case "R", "A", "G", "2", "3", ".": |
| 280 |
vals := strings.Split(v, ",") |
| 281 |
var vi interface{} |
| 282 |
switch hi.Type { |
| 283 |
case "Integer": |
| 284 |
vi = make([]int, len(vals)) |
| 285 |
for j, val := range vals { |
| 286 |
iv, err = parseOne(skey, val, hi.Type) |
| 287 |
vi.([]int)[j] = iv.(int) |
| 288 |
} |
| 289 |
case "Float": |
| 290 |
vi = make([]float32, len(vals)) |
| 291 |
for j, val := range vals { |
| 292 |
iv, err = parseOne(skey, val, hi.Type) |
| 293 |
if o, ok := iv.(float32); ok { |
| 294 |
vi.([]float32)[j] = o |
| 295 |
} else { |
| 296 |
vi.([]float32)[j] = float32(iv.(float64)) |
| 297 |
} |
| 298 |
|
| 299 |
} |
| 300 |
case "String": |
| 301 |
vi = make([]string, len(vals)) |
| 302 |
for j, val := range vals { |
| 303 |
iv, err = parseOne(skey, val, hi.Type) |
| 304 |
vi.([]string)[j] = iv.(string) |
| 305 |
} |
| 306 |
default: |
| 307 |
vi = make([]interface{}, len(vals)) |
| 308 |
for j, val := range vals { |
| 309 |
iv, err = parseOne(skey, val, hi.Type) |
| 310 |
vi.([]interface{})[j] = iv |
| 311 |
} |
| 312 |
} |
| 313 |
return vi, nil |
| 314 |
|
| 315 |
default: |
| 316 |
vals := strings.Split(v, ",") |
| 317 |
var vi interface{} = make([]interface{}, len(vals)) |
| 318 |
if _, err := strconv.Atoi(hi.Number); err == nil { |
| 319 |
for j, val := range vals { |
| 320 |
iv, err = parseOne(skey, val, hi.Type) |
| 321 |
vi.([]interface{})[j] = iv |
| 322 |
} |
| 323 |
} |
| 324 |
return vi, err |
| 325 |
|
| 326 |
} |
| 327 |
} |
| 328 |
|
| 329 |
func (i InfoByte) String() string { |
| 330 |
if len(i.Info) == 0 { |
| 331 |
return "." |
| 332 |
} |
| 333 |
return string(i.Info) |
| 334 |
} |
| 335 |
|
| 336 |
func (i InfoByte) Bytes() []byte { |
| 337 |
return i.Info |
| 338 |
} |
| 339 |
|
| 340 |
func (i *InfoByte) UpdateHeader(key string, value interface{}) { |
| 341 |
i.header.Lock() |
| 342 |
defer i.header.Unlock() |
| 343 |
if i.header != nil { |
| 344 |
switch value.(type) { |
| 345 |
case bool: |
| 346 |
i.header.Infos[key] = &Info{Id: key, Description: key, Number: "0", Type: "Flag"} |
| 347 |
case string: |
| 348 |
i.header.Infos[key] = &Info{Id: key, Description: key, Number: "1", Type: "Character"} |
| 349 |
case int, int32, int64, uint32, uint64: |
| 350 |
i.header.Infos[key] = &Info{Id: key, Description: key, Number: "1", Type: "Integer"} |
| 351 |
case float32, float64: |
| 352 |
i.header.Infos[key] = &Info{Id: key, Description: key, Number: "1", Type: "Float"} |
| 353 |
case []interface{}: |
| 354 |
v := value.([]interface{})[0] |
| 355 |
i.UpdateHeader(key, v) |
| 356 |
} |
| 357 |
} |
| 358 |
} |
| 359 |
|
| 360 |
func (i *InfoByte) Set(key string, value interface{}) error { |
| 361 |
if len(i.Info) == 0 { |
| 362 |
if v, ok := value.(bool); ok { |
| 363 |
if v { |
| 364 |
i.Info = []byte(key) |
| 365 |
} |
| 366 |
} else { |
| 367 |
i.Info = []byte(fmt.Sprintf("%s=%s", key, ItoS(key, value))) |
| 368 |
} |
| 369 |
return nil |
| 370 |
} |
| 371 |
s, e := getpositions(i.Info, key) |
| 372 |
if s == -1 || s == len(i.Info) { |
| 373 |
if b, ok := value.(bool); ok { |
| 374 |
if b { |
| 375 |
i.Info = append(i.Info, ';') |
| 376 |
i.Info = append(i.Info, key...) |
| 377 |
} |
| 378 |
return nil |
| 379 |
} |
| 380 |
slug := fmt.Sprintf(";%s=%s", key, ItoS(key, value)) |
| 381 |
i.Info = append(i.Info, slug...) |
| 382 |
//i.UpdateHeader(key, value) |
| 383 |
return nil |
| 384 |
} |
| 385 |
if b, ok := value.(bool); ok { |
| 386 |
if !b { |
| 387 |
i.Delete(key) |
| 388 |
} |
| 389 |
return nil |
| 390 |
} |
| 391 |
slug := []byte(ItoS(key, value)) |
| 392 |
if e == -1 { |
| 393 |
i.Info = append(i.Info[:s], slug...) |
| 394 |
} else { |
| 395 |
i.Info = append(i.Info[:s], append(slug, i.Info[e+1:]...)...) |
| 396 |
} |
| 397 |
return nil |
| 398 |
} |
| 399 |
|
| 400 |
func (i *InfoByte) Add(key string, value interface{}) { |
| 401 |
i.Set(key, value) |
| 402 |
} |