github.com/phuslu/fastdns@v0.8.3-0.20240310041952-69506fc67dd1/util.go (about)

     1  package fastdns
     2  
     3  import (
     4  	"unsafe"
     5  )
     6  
     7  // EncodeDomain encodes domain to dst.
     8  func EncodeDomain(dst []byte, domain string) []byte {
     9  	i := len(dst)
    10  	j := i + len(domain)
    11  
    12  	dst = append(dst, '.')
    13  	dst = append(dst, domain...)
    14  
    15  	var n byte = 0
    16  	for k := j; k >= i; k-- {
    17  		if dst[k] == '.' {
    18  			dst[k] = n
    19  			n = 0
    20  		} else {
    21  			n++
    22  		}
    23  	}
    24  
    25  	dst = append(dst, 0)
    26  
    27  	return dst
    28  }
    29  
    30  // nolint
    31  func b2s(b []byte) string { return *(*string)(unsafe.Pointer(&b)) }
    32  
    33  // cheaprandn returns a pseudorandom uint32 in [0,n).
    34  //
    35  //go:noescape
    36  //go:linkname cheaprandn runtime.cheaprandn
    37  func cheaprandn(x uint32) uint32