package nstdimport ()// A ByteSeq is either a string or a byte slice.typeByteSeqinterface{ ~string | ~[]byte }// ByteSeqCommonPrefixes returns the common prefixes of two [ByteSeq] values.func [, ByteSeq]( , ) ( , ) {// Tried several coding mamners and found // the current one is inline-able, with cost 80. := minOfTwo(len(), len()) , = [:], [:]iflen() == len() { // BCE hintfor := 0; < len(); ++ {if [] != [] {return [:], [:] } } }return}var ( _, _ = ByteSeqCommonPrefix("", []byte{}) _, _ = ByteSeqCommonPrefix("", "") _, _ = ByteSeqCommonPrefix([]byte{}, "") _, _ = ByteSeqCommonPrefix([]byte{}, []byte{}))// ReverseBytes inverts the bytes in a byte slice.// The argument is returned so that calls to ReverseBytes can be used as expressions.func [ ~[]byte]( ) {iflen() == 0 {return [:0] }var , = len() - 1, 0for > { [], [] = [], [] ++ -- }return}// ReverseByteSeq returnes a copy (in the form of byte slice) of// a byte sequence (in the form of either string or byte slice) but reversed.func [ ByteSeq]( ) []byte {iflen() == 0 {return []byte([:0]) }var = make([]byte, len())var = 0for := len() - 1; >= 0; -- { [] = [] ++ }return}// ReverseRuneSeq returnes a copy (in the form of byte slice) of// a rune sequence (in the form of either string or byte slice) but reversed.//// See://// * https://github.com/golang/go/issues/14777// * https://github.com/golang/go/issues/68348func [ ByteSeq]( ) []byte {iflen() == 0 {return []byte([:0]) }var = make([]byte, len())var = []byte() // doesn't allocate since Go toolchain 1.22var = len()for { , := utf8.DecodeRune()if == 0 {if != 0 {Panicf("i (%v) != 0", ) }break }if < {Panicf("i (%v) < size (%v)", , ) } -= var , = , 0for < { [] = [] ++ ++ } = [:] }return}
The pages are generated with Goldsv0.7.0-preview. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds.