/*
** Zabbix
** Copyright (C) 2001-2023 Zabbix SIA
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
**/
package itemutil
import (
"bytes"
"errors"
"fmt"
)
func isKeyChar(c byte, wildcard bool) bool {
if c >= 'a' && c <= 'z' {
return true
}
if c == '.' || c == '-' || c == '_' {
return true
}
if c >= '0' && c <= '9' {
return true
}
if c >= 'A' && c <= 'Z' {
return true
}
if wildcard && c == '*' {
return true
}
return false
}
// parseQuotedParam parses item key quoted parameter "..." and returns
// the parsed parameter (including quotes, but without whitespace outside quotes)
// and the data after the parameter (skipping also whitespace after closing quotes).
func parseQuotedParam(data []byte) (param []byte, left []byte, err error) {
var last byte
for i, c := range data[1:] {
if c == '"' && last != '\\' {
i += 2
param = data[:i]
for ; i < len(data) && data[i] == ' '; i++ {
}
left = data[i:]
return
}
last = c
}
err = errors.New("unterminated quoted string")
return
}
// parseUnquotedParam parses item key normal parameter (any combination of any characters except ',' and ']',
// including trailing whitespace) and returns the parsed parameter and the data after the parameter.
func parseUnquotedParam(data []byte) (param []byte, left []byte, err error) {
for i, c := range data {
if c == ',' || c == ']' {
param = data[:i]
left = data[i:]
return
}
}
err = errors.New("unterminated parameter")
return
}
// parseArrayParam parses item key array parameter [...] and returns.
func parseArrayParam(data []byte) (param []byte, left []byte, err error) {
var pos int
b := data[1:]
for len(b) > 0 {
loop:
for i, c := range b {
switch c {
case ' ':
continue