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.

55 lines
1.1 KiB

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  1. package common
  2. import (
  3. "encoding/hex"
  4. "fmt"
  5. "strings"
  6. )
  7. // Fmt shorthand, XXX DEPRECATED
  8. var Fmt = fmt.Sprintf
  9. // RightPadString adds spaces to the right of a string to make it length totalLength
  10. func RightPadString(s string, totalLength int) string {
  11. remaining := totalLength - len(s)
  12. if remaining > 0 {
  13. s = s + strings.Repeat(" ", remaining)
  14. }
  15. return s
  16. }
  17. // LeftPadString adds spaces to the left of a string to make it length totalLength
  18. func LeftPadString(s string, totalLength int) string {
  19. remaining := totalLength - len(s)
  20. if remaining > 0 {
  21. s = strings.Repeat(" ", remaining) + s
  22. }
  23. return s
  24. }
  25. // IsHex returns true for non-empty hex-string prefixed with "0x"
  26. func IsHex(s string) bool {
  27. if len(s) > 2 && strings.EqualFold(s[:2], "0x") {
  28. _, err := hex.DecodeString(s[2:])
  29. return err == nil
  30. }
  31. return false
  32. }
  33. // StripHex returns hex string without leading "0x"
  34. func StripHex(s string) string {
  35. if IsHex(s) {
  36. return s[2:]
  37. }
  38. return s
  39. }
  40. // StringInSlice returns true if a is found the list.
  41. func StringInSlice(a string, list []string) bool {
  42. for _, b := range list {
  43. if b == a {
  44. return true
  45. }
  46. }
  47. return false
  48. }