github.com/gofiber/fiber/v2@v2.47.0/utils/bytes.go (about)

     1  // ⚡️ Fiber is an Express inspired web framework written in Go with ☕️
     2  // 🤖 Github Repository: https://github.com/gofiber/fiber
     3  // 📌 API Documentation: https://docs.gofiber.io
     4  
     5  package utils
     6  
     7  // ToLowerBytes converts ascii slice to lower-case in-place.
     8  func ToLowerBytes(b []byte) []byte {
     9  	for i := 0; i < len(b); i++ {
    10  		b[i] = toLowerTable[b[i]]
    11  	}
    12  	return b
    13  }
    14  
    15  // ToUpperBytes converts ascii slice to upper-case in-place.
    16  func ToUpperBytes(b []byte) []byte {
    17  	for i := 0; i < len(b); i++ {
    18  		b[i] = toUpperTable[b[i]]
    19  	}
    20  	return b
    21  }
    22  
    23  // TrimRightBytes is the equivalent of bytes.TrimRight
    24  func TrimRightBytes(b []byte, cutset byte) []byte {
    25  	lenStr := len(b)
    26  	for lenStr > 0 && b[lenStr-1] == cutset {
    27  		lenStr--
    28  	}
    29  	return b[:lenStr]
    30  }
    31  
    32  // TrimLeftBytes is the equivalent of bytes.TrimLeft
    33  func TrimLeftBytes(b []byte, cutset byte) []byte {
    34  	lenStr, start := len(b), 0
    35  	for start < lenStr && b[start] == cutset {
    36  		start++
    37  	}
    38  	return b[start:]
    39  }
    40  
    41  // TrimBytes is the equivalent of bytes.Trim
    42  func TrimBytes(b []byte, cutset byte) []byte {
    43  	i, j := 0, len(b)-1
    44  	for ; i <= j; i++ {
    45  		if b[i] != cutset {
    46  			break
    47  		}
    48  	}
    49  	for ; i < j; j-- {
    50  		if b[j] != cutset {
    51  			break
    52  		}
    53  	}
    54  
    55  	return b[i : j+1]
    56  }
    57  
    58  // EqualFoldBytes tests ascii slices for equality case-insensitively
    59  func EqualFoldBytes(b, s []byte) bool {
    60  	if len(b) != len(s) {
    61  		return false
    62  	}
    63  	for i := len(b) - 1; i >= 0; i-- {
    64  		if toUpperTable[b[i]] != toUpperTable[s[i]] {
    65  			return false
    66  		}
    67  	}
    68  	return true
    69  }