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

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