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.

48 lines
987 B

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 && s[:2] == "0x" {
  28. _, err := hex.DecodeString(s[2:])
  29. if err != nil {
  30. return false
  31. }
  32. return true
  33. }
  34. return false
  35. }
  36. // StripHex returns hex string without leading "0x"
  37. func StripHex(s string) string {
  38. if IsHex(s) {
  39. return s[2:]
  40. }
  41. return s
  42. }