SYS-REQ-115
(config Config, data []byte, keys ...string)
| 392 | |
| 393 | // SYS-REQ-115 |
| 394 | func searchKeysConfig(config Config, data []byte, keys ...string) int { |
| 395 | keyLevel := 0 |
| 396 | level := 0 |
| 397 | i := 0 |
| 398 | ln := len(data) |
| 399 | lk := len(keys) |
| 400 | lastMatched := true |
| 401 | |
| 402 | if lk == 0 { |
| 403 | return 0 |
| 404 | } |
| 405 | |
| 406 | var stackbuf [unescapeStackBufSize]byte // stack-allocated array for allocation-free unescaping of small strings |
| 407 | |
| 408 | for i < ln { |
| 409 | switch data[i] { |
| 410 | case '"', '\'': |
| 411 | quote := data[i] |
| 412 | if quote == '\'' && !config.AllowSingleQuotes { |
| 413 | break |
| 414 | } |
| 415 | i++ |
| 416 | keyBegin := i |
| 417 | |
| 418 | strEnd, keyEscaped := stringEndConfig(config, data[i:], quote) |
| 419 | if strEnd == -1 { |
| 420 | return -1 |
| 421 | } |
| 422 | i += strEnd |
| 423 | keyEnd := i - 1 |
| 424 | |
| 425 | valueOffset := nextTokenConfig(config, data[i:]) |
| 426 | if valueOffset == -1 { |
| 427 | return -1 |
| 428 | } |
| 429 | |
| 430 | i += valueOffset |
| 431 | |
| 432 | // if string is a key |
| 433 | if data[i] == ':' { |
| 434 | if level < 1 { |
| 435 | return -1 |
| 436 | } |
| 437 | |
| 438 | key := data[keyBegin:keyEnd] |
| 439 | |
| 440 | // for unescape: if there are no escape sequences, this is cheap; if there are, it is a |
| 441 | // bit more expensive, but causes no allocations unless len(key) > unescapeStackBufSize |
| 442 | var keyUnesc []byte |
| 443 | if !keyEscaped { |
| 444 | keyUnesc = key |
| 445 | } else if ku, err := unescapeConfig(config, key, stackbuf[:]); err != nil { |
| 446 | return -1 |
| 447 | } else { |
| 448 | keyUnesc = ku |
| 449 | } |
| 450 | |
| 451 | if level <= len(keys) { |
no test coverage detected