1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- package misc
- import (
- "bytes"
- "regexp"
- "strconv"
- "strings"
- )
- var (
- keyValue = regexp.MustCompile(`^.+(=|:) *(.+)?$`)
- )
- // TwoSpotValue returns value from "key:value" or "key: value" line. The value will be not contains spaces
- //
- // Deprecated: use RexKeyValue instead
- func TwoSpotValue(line string) string {
- return strings.TrimSpace(strings.SplitN(line, ":", 2)[1])
- }
- // RexKeyValue returns value from "key:value" or "key: value" or "key=value" or "key = value" line. The value will be not contains spaces
- func RexKeyValue(line string) string {
- res := keyValue.FindStringSubmatch(strings.TrimSpace(line))
- return res[len(res)-1]
- }
- // RexSubStrSingleVal returns value in last position of out
- func RexSubStrSingleVal(out []string) string {
- if len(out) != 2 {
- return ""
- } else {
- return out[1]
- }
- }
- // RemoveQuoteMark removes quote marks
- func RemoveQuoteMark(line string) string {
- return strings.ReplaceAll(strings.ReplaceAll(line, "\"", ""), "'", "")
- }
- // IntExtractor returns int value from string represents of int
- func IntExtractor(val string) int {
- if res, err := strconv.Atoi(val); err != nil {
- return -1
- } else {
- return res
- }
- }
- // RemoveBraces remove braces from line
- func RemoveBraces(line string) string {
- line = strings.ReplaceAll(strings.ReplaceAll(line, "(", ""), ")", "")
- line = strings.ReplaceAll(strings.ReplaceAll(line, "{", ""), "}", "")
- line = strings.ReplaceAll(strings.ReplaceAll(line, "[", ""), "]", "")
- return line
- }
- func TrimOut(out []byte, err error) ([]byte, error) {
- out = bytes.TrimSpace(out)
- return out, err
- }
|