func parseInt64()

in cassandra-bigtable-migration-tools/cassandra-bigtable-proxy/translator/orderedcode.go [433:482]


func parseInt64(dst *int64, s string, dir byte) (string, error) {
	if len(s) == 0 {
		return "", errCorrupt
	}
	// Fast-path any single-byte encoding.
	c := s[0] ^ dir
	if c >= 0x40 && c < 0xc0 {
		*dst = int64(int8(c ^ 0x80))
		return s[1:], nil
	}
	// Invert everything if the encoded value is negative.
	neg := c&0x80 == 0
	if neg {
		c, dir = ^c, ^dir
	}
	// Consume the leading 0xff full of 1 bits, if present.
	n := 0
	if c == 0xff {
		if len(s) == 1 {
			return "", errCorrupt
		}
		s = s[1:]
		c = s[0] ^ dir
		// The encoding of the largest int64 (1<<63-1) starts with "\xff\xc0".
		if c > 0xc0 {
			return "", errCorrupt
		}
		// The 7 (being 8 - 1) is for the same reason as in appendInt64.
		n = 7
	}
	// Count and mask off any remaining 1 bits.
	for mask := byte(0x80); c&mask != 0; mask >>= 1 {
		c &= ^mask
		n++
	}
	if len(s) < n {
		return "", errCorrupt
	}
	// Decode the big-endian, invert if necessary, and return.
	x := int64(c)
	for i := 1; i < n; i++ {
		c = s[i] ^ dir
		x = x<<8 | int64(c)
	}
	if neg {
		x = ^x
	}
	*dst = x
	return s[n:], nil
}