You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

43 lines
800 B

9 years ago
9 years ago
9 years ago
  1. package common
  2. import (
  3. "encoding/hex"
  4. "fmt"
  5. "strings"
  6. )
  7. // Like fmt.Sprintf, but skips formatting if args are empty.
  8. var Fmt = func(format string, a ...interface{}) string {
  9. if len(a) == 0 {
  10. return format
  11. } else {
  12. return fmt.Sprintf(format, a...)
  13. }
  14. }
  15. // IsHex returns true for non-empty hex-string prefixed with "0x"
  16. func IsHex(s string) bool {
  17. if len(s) > 2 && strings.EqualFold(s[:2], "0x") {
  18. _, err := hex.DecodeString(s[2:])
  19. return err == nil
  20. }
  21. return false
  22. }
  23. // StripHex returns hex string without leading "0x"
  24. func StripHex(s string) string {
  25. if IsHex(s) {
  26. return s[2:]
  27. }
  28. return s
  29. }
  30. // StringInSlice returns true if a is found the list.
  31. func StringInSlice(a string, list []string) bool {
  32. for _, b := range list {
  33. if b == a {
  34. return true
  35. }
  36. }
  37. return false
  38. }