package wsimport ()// SelectFromSlice creates accept function that could be used as Protocol/Extension// select during upgrade.func ( []string) func(string) bool {iflen() > 16 { := make(map[string]struct{}, len())for , := range { [] = struct{}{} }returnfunc( string) bool { , := []return } }returnfunc( string) bool {for , := range {if == {returntrue } }returnfalse }}// SelectEqual creates accept function that could be used as Protocol/Extension// select during upgrade.func ( string) func(string) bool {returnfunc( string) bool {return == }}// asciiToInt converts bytes to int.func asciiToInt( []byte) ( int, error) {// ASCII numbers all start with the high-order bits 0011. // If you see that, and the next bits are 0-9 (0000 - 1001) you can grab those // bits and interpret them directly as an integer.varintif = len(); < 1 {return0, fmt.Errorf("converting empty bytes to int") }for := 0; < ; ++ {if []&0xf0 != 0x30 {return0, fmt.Errorf("%s is not a numeric character", string([])) } += int([]&0xf) * pow(10, --1) }return , nil}// pow for integers implementation.// See Donald Knuth, The Art of Computer Programming, Volume 2, Section 4.6.3.func pow(, int) int { := 1for > 0 {if &1 != 0 { *= } >>= 1 *= }return}func bsplit3( []byte, byte) (, , []byte) { := bytes.IndexByte(, ) := bytes.IndexByte([+1:], )if == -1 || == -1 {return , nil, nil } += + 1return [:], [+1 : ], [+1:]}func btrim( []byte) []byte {var , intfor = 0; < len() && ([] == ' ' || [] == '\t'); { ++ }for = len(); > && ([-1] == ' ' || [-1] == '\t'); { -- }return [:]}func strHasToken(, string) ( bool) {returnbtsHasToken(strToBytes(), strToBytes())}func btsHasToken(, []byte) ( bool) {httphead.ScanTokens(, func( []byte) bool { = bytes.EqualFold(, )return ! })return}const ( toLower = 'a' - 'A'// for use with OR. toUpper = ^byte(toLower) // for use with AND. toLower8 = uint64(toLower) |uint64(toLower)<<8 |uint64(toLower)<<16 |uint64(toLower)<<24 |uint64(toLower)<<32 |uint64(toLower)<<40 |uint64(toLower)<<48 |uint64(toLower)<<56)// Algorithm below is like standard textproto/CanonicalMIMEHeaderKey, except// that it operates with slice of bytes and modifies it inplace without copying.func canonicalizeHeaderKey( []byte) { := truefor , := range {if && 'a' <= && <= 'z' { [] &= toUpper } elseif ! && 'A' <= && <= 'Z' { [] |= toLower } = == '-' }}// readLine reads line from br. It reads until '\n' and returns bytes without// '\n' or '\r\n' at the end.// It returns err if and only if line does not end in '\n'. Note that read// bytes returned in any case of error.//// It is much like the textproto/Reader.ReadLine() except the thing that it// returns raw bytes, instead of string. That is, it avoids copying bytes read// from br.//// textproto/Reader.ReadLineBytes() is also makes copy of resulting bytes to be// safe with future I/O operations on br.//// We could control I/O operations on br and do not need to make additional// copy for safety.//// NOTE: it may return copied flag to notify that returned buffer is safe to// use.func readLine( *bufio.Reader) ([]byte, error) {var []bytefor { , := .ReadSlice('\n')if == bufio.ErrBufferFull {// Copy bytes because next read will discard them. = append(, ...)continue }// Avoid copy of single read.if == nil { = } else { = append(, ...) }if != nil {return , }// Size of line is at least 1. // In other case bufio.ReadSlice() returns error. := len()// Cut '\n' or '\r\n'.if > 1 && [-2] == '\r' { = [:-2] } else { = [:-1] }return , nil }}func min(, int) int {if < {return }return}func nonZero(, int) int {if != 0 {return }return}
The pages are generated with Goldsv0.8.7. (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.