// ShiftLeft shifts the binary string left by one bit, causing the
// most-signficant bit to disappear and a zero to be introduced at the right.
// This corresponds to the `x << 1` notation of RFC 4493.
7431
func ShiftLeft(b []byte) []byte {
	l := len(b)
	if l == 0 {
		panic("shiftLeft requires a non-empty buffer.")
	}

	output := make([]byte, l)

	overflow := byte(0)
	for i := int(l - 1); i >= 0; i-- {
		output[i] = b[i] << 1
		output[i] |= overflow
		overflow = (b[i] & 0x80) >> 7
	}

	return output
}
609523
func (s *Sessions) ShiftExpirationFasthttp(ctx *fasthttp.RequestCtx) {
	s.UpdateExpirationFasthttp(ctx, s.config.Expires)
}
283101
func leftShift(a *decimal, k uint) {
	delta := leftcheats[k].delta
	if prefixIsLessThan(a.d[0:a.nd], leftcheats[k].cutoff) {
		delta--
	}

	r := a.nd         // read index
	w := a.nd + delta // write index
	n := 0

	// Pick up a digit, put down a digit.
	for r--; r >= 0; r-- {
		n += (int(a.d[r]) - '0') << k
		quo := n / 10
		rem := n - 10*quo
		w--
		if w < len(a.d) {
			a.d[w] = byte(rem + '0')
		} else if rem != 0 {
			a.trunc = true
		}
		n = quo
	}

	// Put down extra digits.
	for n > 0 {
		quo := n / 10
		rem := n - 10*quo
		w--
		if w < len(a.d) {
			a.d[w] = byte(rem + '0')
		} else if rem != 0 {
			a.trunc = true
		}
		n = quo
	}

	a.nd += delta
	if a.nd >= len(a.d) {
		a.nd = len(a.d)
	}
	a.dp += delta
	trim(a)
}
213209
func (s *state) addBinaryShiftChar(index int) *state {
	tokens := s.tokens
	mode := s.mode
	bitCnt := s.bitCount
	if s.mode == mode_punct || s.mode == mode_digit {
		latch := latchTable[s.mode][mode_upper]
		tokens = newSimpleToken(tokens, latch&0xFFFF, byte(latch>>16))
		bitCnt += latch >> 16
		mode = mode_upper
	}
	deltaBitCount := 8
	if s.bShiftByteCount == 0 || s.bShiftByteCount == 31 {
		deltaBitCount = 18
	} else if s.bShiftByteCount == 62 {
		deltaBitCount = 9
	}
	result := &state{
		mode:            mode,
		tokens:          tokens,
		bShiftByteCount: s.bShiftByteCount + 1,
		bitCount:        bitCnt + deltaBitCount,
	}
	if result.bShiftByteCount == 2047+31 {
		// The string is as long as it's allowed to be.  We should end it.
		result = result.endBinaryShift(index + 1)
	}

	return result
}
393359
func SignExtend(back uint64, x *big.Int) *big.Int {
	// we assume x contains a signed integer of back + 1 bytes width
	// most significant bit of the back'th byte,
	signBit := back*8 + 7
	// single bit set at sign bit position
	mask := new(big.Int).Lsh(big1, uint(signBit))
	// all bits below sign bit set to 1 all above (including sign bit) set to 0
	mask.Sub(mask, big1)
	if x.Bit(int(signBit)) == 1 {
		// Number represented is negative - set all bits above sign bit (including sign bit)
		return x.Or(x, mask.Not(mask))
	} else {
		// Number represented is positive - clear all bits above sign bit (including sign bit)
		return x.And(x, mask)
	}
}
915733
func shiftNRuneBytes(rb [4]byte, n int) [4]byte {
	switch n {
	case zero:
		return rb
	case one:
		return [4]byte{rb[one], rb[2], rb[3], zero}
	case 2:
		return [4]byte{rb[2], rb[3]}
	case 3:
		return [4]byte{rb[3]}
	default:
		return [4]byte{}
	}
}
90085
func PadLeft(str string, padStr string, padLen int) string {
	return buildPadStr(str, padStr, padLen, true, false)
}
586274
func alignmentShift(block []byte) int {
	if len(block) == 0 {
		return 0
	}
	return int(uintptr(unsafe.Pointer(&block[0])) & uintptr(alignment-1))
}
672509
func rightShift(slice []byte, num int) {
	for i := len(slice) - num - 1; i >= 0; i-- {
		slice[num+i] = slice[i]
	}

	for i := 0; i < num; i++ {
		slice[i] = 0
	}
}
155773
func (b *builtinLeftSig) evalString(row chunk.Row) (string, bool, error) {
	str, isNull, err := b.args[0].EvalString(b.ctx, row)
	if isNull || err != nil {
		return "", true, err
	}
	left, isNull, err := b.args[1].EvalInt(b.ctx, row)
	if isNull || err != nil {
		return "", true, err
	}
	runes, leftLength := []rune(str), int(left)
	if runeLength := len(runes); leftLength > runeLength {
		leftLength = runeLength
	} else if leftLength < 0 {
		leftLength = 0
	}
	return string(runes[:leftLength]), false, nil
}
60620
func (s *state) shiftAndAppend(mode encodingMode, value int) *state {
	tokens := s.tokens

	// Shifts exist only to UPPER and PUNCT, both with tokens size 5.
	tokens = newSimpleToken(tokens, shiftTable[s.mode][mode], s.mode.BitCount())
	tokens = newSimpleToken(tokens, value, 5)

	return &state{
		mode:            s.mode,
		tokens:          tokens,
		bShiftByteCount: 0,
		bitCount:        s.bitCount + int(s.mode.BitCount()) + 5,
	}
}
903699
func FillLeft(s string, w int) string {
	return DefaultCondition.FillLeft(s, w)
}
1160696
func (s *rowSegment) Shift() (*rowSegment, error) {
	//TODO deal with overflow
	data, err := s.data.Shift(1)
	if err != nil {
		return nil, errors.Wrap(err, "shifting roaring data")
	}

	return &rowSegment{
		data:  *data,
		shard: s.shard,
		n:     data.Count(),
	}, nil
}
796796
func lexLeftDelim(l *lexer) stateFn {
	l.pos += Pos(len(l.leftDelim))
	if strings.HasPrefix(l.input[l.pos:], leftComment) {
		return lexComment
	}
	l.emit(itemLeftDelim)
	l.parenDepth = 0
	return lexInsideAction
}
619432
func (a *decimal) Shift(k int) {
	switch {
	case a.nd == 0:
		// nothing to do: a == 0
	case k > 0:
		for k > maxShift {
			leftShift(a, maxShift)
			k -= maxShift
		}
		leftShift(a, uint(k))
	case k < 0:
		for k < -maxShift {
			rightShift(a, maxShift)
			k += maxShift
		}
		rightShift(a, uint(-k))
	}
}
580535
func (b BinaryLiteral) ToBitLiteralString(trimLeadingZero bool) string {
	if len(b) == 0 {
		return "b''"
	}
	var buf bytes.Buffer
	for _, data := range b {
		fmt.Fprintf(&buf, "%08b", data)
	}
	ret := buf.Bytes()
	if trimLeadingZero {
		ret = bytes.TrimLeft(ret, "0")
		if len(ret) == 0 {
			ret = []byte{'0'}
		}
	}
	return fmt.Sprintf("b'%s'", string(ret))
}
92047
func fmtF(dst []byte, neg bool, d decimalSlice, prec int) []byte {
	// sign
	if neg {
		dst = append(dst, '-')
	}

	// integer, padded with zeros as needed.
	if d.dp > 0 {
		var i int
		for i = 0; i < d.dp && i < d.nd; i++ {
			dst = append(dst, d.d[i])
		}
		for ; i < d.dp; i++ {
			dst = append(dst, '0')
		}
	} else {
		dst = append(dst, '0')
	}

	// fraction
	if prec > 0 {
		dst = append(dst, '.')
		for i := 0; i < prec; i++ {
			ch := byte('0')
			if j := d.dp + i; 0 <= j && j < d.nd {
				ch = d.d[j]
			}
			dst = append(dst, ch)
		}
	}

	return dst
}