github.com/pubgo/xprocess@v0.1.11/xutil/strings.go (about)

     1  package xutil
     2  
     3  // ToLower is the equivalent of strings.ToLower
     4  func ToLower(b string) string {
     5  	var res = make([]byte, len(b))
     6  	copy(res, b)
     7  	for i := 0; i < len(res); i++ {
     8  		res[i] = toLowerTable[res[i]]
     9  	}
    10  
    11  	return UnsafeString(res)
    12  }
    13  
    14  // ToUpper is the equivalent of strings.ToUpper
    15  func ToUpper(b string) string {
    16  	var res = make([]byte, len(b))
    17  	copy(res, b)
    18  	for i := 0; i < len(res); i++ {
    19  		res[i] = toUpperTable[res[i]]
    20  	}
    21  
    22  	return UnsafeString(res)
    23  }
    24  
    25  // TrimLeft is the equivalent of strings.TrimLeft
    26  func TrimLeft(s string, cutset byte) string {
    27  	lenStr, start := len(s), 0
    28  	for start < lenStr && s[start] == cutset {
    29  		start++
    30  	}
    31  	return s[start:]
    32  }
    33  
    34  // Trim is the equivalent of strings.Trim
    35  func Trim(s string, cutset byte) string {
    36  	i, j := 0, len(s)-1
    37  	for ; i < j; i++ {
    38  		if s[i] != cutset {
    39  			break
    40  		}
    41  	}
    42  	for ; i < j; j-- {
    43  		if s[j] != cutset {
    44  			break
    45  		}
    46  	}
    47  
    48  	return s[i : j+1]
    49  }
    50  
    51  // TrimRight is the equivalent of strings.TrimRight
    52  func TrimRight(s string, cutset byte) string {
    53  	lenStr := len(s)
    54  	for lenStr > 0 && s[lenStr-1] == cutset {
    55  		lenStr--
    56  	}
    57  	return s[:lenStr]
    58  }
    59  
    60  // EqualFold the equivalent of strings.EqualFold
    61  func EqualFold(b, s string) (equals bool) {
    62  	n := len(b)
    63  	equals = n == len(s)
    64  	if equals {
    65  		for i := 0; i < n; i++ {
    66  			if equals = b[i]|0x20 == s[i]|0x20; !equals {
    67  				break
    68  			}
    69  		}
    70  	}
    71  	return
    72  }