github.com/ari-anchor/sei-tendermint@v0.0.0-20230519144642-dc826b7b56bb/libs/strings/string.go (about)

     1  package strings
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  )
     7  
     8  // SplitAndTrimEmpty slices s into all subslices separated by sep and returns a
     9  // slice of the string s with all leading and trailing Unicode code points
    10  // contained in cutset removed. If sep is empty, SplitAndTrim splits after each
    11  // UTF-8 sequence. First part is equivalent to strings.SplitN with a count of
    12  // -1.  also filter out empty strings, only return non-empty strings.
    13  func SplitAndTrimEmpty(s, sep, cutset string) []string {
    14  	if s == "" {
    15  		return []string{}
    16  	}
    17  
    18  	spl := strings.Split(s, sep)
    19  	nonEmptyStrings := make([]string, 0, len(spl))
    20  
    21  	for i := 0; i < len(spl); i++ {
    22  		element := strings.Trim(spl[i], cutset)
    23  		if element != "" {
    24  			nonEmptyStrings = append(nonEmptyStrings, element)
    25  		}
    26  	}
    27  
    28  	return nonEmptyStrings
    29  }
    30  
    31  // ASCIITrim removes spaces from an a ASCII string, erroring if the
    32  // sequence is not an ASCII string.
    33  func ASCIITrim(s string) (string, error) {
    34  	if len(s) == 0 {
    35  		return "", nil
    36  	}
    37  	r := make([]byte, 0, len(s))
    38  	for _, b := range []byte(s) {
    39  		switch {
    40  		case b == 32:
    41  			continue // skip space
    42  		case 32 < b && b <= 126:
    43  			r = append(r, b)
    44  		default:
    45  			return "", fmt.Errorf("non-ASCII (non-tab) char 0x%X", b)
    46  		}
    47  	}
    48  	return string(r), nil
    49  }
    50  
    51  // StringSliceEqual checks if string slices a and b are equal
    52  func StringSliceEqual(a, b []string) bool {
    53  	if len(a) != len(b) {
    54  		return false
    55  	}
    56  	for i := 0; i < len(a); i++ {
    57  		if a[i] != b[i] {
    58  			return false
    59  		}
    60  	}
    61  	return true
    62  }