github.com/supragya/TendermintConnector@v0.0.0-20210619045051-113e32b84fb1/_deprecated_chains/cosmos/libs/common/string.go (about)

     1  package common
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  )
     7  
     8  // StringInSlice returns true if a is found the list.
     9  func StringInSlice(a string, list []string) bool {
    10  	for _, b := range list {
    11  		if b == a {
    12  			return true
    13  		}
    14  	}
    15  	return false
    16  }
    17  
    18  // SplitAndTrim slices s into all subslices separated by sep and returns a
    19  // slice of the string s with all leading and trailing Unicode code points
    20  // contained in cutset removed. If sep is empty, SplitAndTrim splits after each
    21  // UTF-8 sequence. First part is equivalent to strings.SplitN with a count of
    22  // -1.
    23  func SplitAndTrim(s, sep, cutset string) []string {
    24  	if s == "" {
    25  		return []string{}
    26  	}
    27  
    28  	spl := strings.Split(s, sep)
    29  	for i := 0; i < len(spl); i++ {
    30  		spl[i] = strings.Trim(spl[i], cutset)
    31  	}
    32  	return spl
    33  }
    34  
    35  // Returns true if s is a non-empty printable non-tab ascii character.
    36  func IsASCIIText(s string) bool {
    37  	if len(s) == 0 {
    38  		return false
    39  	}
    40  	for _, b := range []byte(s) {
    41  		if 32 <= b && b <= 126 {
    42  			// good
    43  		} else {
    44  			return false
    45  		}
    46  	}
    47  	return true
    48  }
    49  
    50  // NOTE: Assumes that s is ASCII as per IsASCIIText(), otherwise panics.
    51  func ASCIITrim(s string) string {
    52  	r := make([]byte, 0, len(s))
    53  	for _, b := range []byte(s) {
    54  		if b == 32 {
    55  			continue // skip space
    56  		} else if 32 < b && b <= 126 {
    57  			r = append(r, b)
    58  		} else {
    59  			panic(fmt.Sprintf("non-ASCII (non-tab) char 0x%X", b))
    60  		}
    61  	}
    62  	return string(r)
    63  }
    64  
    65  // StringSliceEqual checks if string slices a and b are equal
    66  func StringSliceEqual(a, b []string) bool {
    67  	if len(a) != len(b) {
    68  		return false
    69  	}
    70  	for i := 0; i < len(a); i++ {
    71  		if a[i] != b[i] {
    72  			return false
    73  		}
    74  	}
    75  	return true
    76  }