OSDN Git Service

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