utils.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package misc
  2. import (
  3. "bytes"
  4. "regexp"
  5. "strconv"
  6. "strings"
  7. )
  8. var (
  9. keySpotVal = regexp.MustCompile(`^.+: ?(.+)$`)
  10. keyEqualSignVal = regexp.MustCompile(`^.+ ?= ?(.+)$`)
  11. )
  12. // TwoSpotValue returns value from "key:value" or "key: value" line. The value will be not contains spaces
  13. //
  14. // Deprecated: use TwoSpotRexValue instead
  15. func TwoSpotValue(line string) string {
  16. return strings.TrimSpace(strings.SplitN(line, ":", 2)[1])
  17. }
  18. // TwoSpotRexValue returns value from "key:value" or "key: value" line. The value will be not contains spaces
  19. func TwoSpotRexValue(line string) string {
  20. res := keySpotVal.FindStringSubmatch(strings.TrimSpace(line))
  21. return res[len(res)-1]
  22. }
  23. // EqualSignRexValue returns value from "key=value" or "key = value" line. The value will be not contains spaces
  24. func EqualSignRexValue(line string) string {
  25. res := keyEqualSignVal.FindStringSubmatch(strings.TrimSpace(line))
  26. return res[len(res)-1]
  27. }
  28. // RexSubStrSingleVal returns value in last position of out
  29. func RexSubStrSingleVal(out []string) string {
  30. if len(out) != 2 {
  31. return ""
  32. } else {
  33. return out[1]
  34. }
  35. }
  36. // RemoveQuoteMark removes quote marks
  37. func RemoveQuoteMark(line string) string {
  38. return strings.ReplaceAll(strings.ReplaceAll(line, "\"", ""), "'", "")
  39. }
  40. // IntExtractor returns int value from string represents of int
  41. func IntExtractor(val string) int {
  42. if res, err := strconv.Atoi(val); err != nil {
  43. return -1
  44. } else {
  45. return res
  46. }
  47. }
  48. // RemoveBraces remove braces from line
  49. func RemoveBraces(line string) string {
  50. line = strings.ReplaceAll(strings.ReplaceAll(line, "(", ""), ")", "")
  51. line = strings.ReplaceAll(strings.ReplaceAll(line, "{", ""), "}", "")
  52. line = strings.ReplaceAll(strings.ReplaceAll(line, "[", ""), "]", "")
  53. return line
  54. }
  55. func TrimOut(out []byte, err error) ([]byte, error) {
  56. out = bytes.TrimSpace(out)
  57. return out, err
  58. }