package ws

import (
	
	
	

	
)

// SelectFromSlice creates accept function that could be used as Protocol/Extension
// select during upgrade.
func ( []string) func(string) bool {
	if len() > 16 {
		 := make(map[string]struct{}, len())
		for ,  := range  {
			[] = struct{}{}
		}
		return func( string) bool {
			,  := []
			return 
		}
	}
	return func( string) bool {
		for ,  := range  {
			if  ==  {
				return true
			}
		}
		return false
	}
}

// SelectEqual creates accept function that could be used as Protocol/Extension
// select during upgrade.
func ( string) func(string) bool {
	return func( 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.
	var  int
	if  = len();  < 1 {
		return 0, fmt.Errorf("converting empty bytes to int")
	}
	for  := 0;  < ; ++ {
		if []&0xf0 != 0x30 {
			return 0, 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 {
	 := 1
	for  > 0 {
		if &1 != 0 {
			 *= 
		}
		 >>= 1
		 *= 
	}
	return 
}

func bsplit3( []byte,  byte) (, ,  []byte) {
	 := bytes.IndexByte(, )
	 := bytes.IndexByte([+1:], )
	if  == -1 ||  == -1 {
		return , nil, nil
	}
	 +=  + 1
	return [:], [+1 : ], [+1:]
}

func btrim( []byte) []byte {
	var ,  int
	for  = 0;  < len() && ([] == ' ' || [] == '\t'); {
		++
	}
	for  = len();  >  && ([-1] == ' ' || [-1] == '\t'); {
		--
	}
	return [:]
}

func strHasToken(,  string) ( bool) {
	return btsHasToken(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) {
	 := true
	for ,  := range  {
		if  && 'a' <=  &&  <= 'z' {
			[] &= toUpper
		} else if ! && '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  []byte
	for {
		,  := .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 
}