func parseString()

in cassandra-bigtable-migration-tools/cassandra-bigtable-proxy/translator/orderedcode.go [368:418]


func parseString(dst *string, s string, dir byte) (string, error) {
	var (
		buf     []byte
		last, i int
	)
	for i < len(s) {
		switch v := s[i] ^ dir; v {
		case 0x00:
			if i+1 >= len(s) {
				return "", errCorrupt
			}
			switch s[i+1] ^ dir {
			case 0x01:
				// The terminator mark ends the string.
				if last == 0 && dir == increasing {
					// As an optimization, if no \x00 or \xff bytes were escaped,
					// and the result does not need inverting, then set *dst to a
					// sub-string of the original input.
					*dst = s[:i]
					return s[i+2:], nil
				}
				buf = append(buf, s[last:i]...)
				if dir != increasing {
					invert(buf)
				}
				*dst = string(buf)
				return s[i+2:], nil
			case 0xff:
				// Unescape the \x00.
				buf = append(buf, s[last:i]...)
				buf = append(buf, 0x00^dir)
				i += 2
				last = i
			default:
				return "", errCorrupt
			}
		case 0xff:
			if i+1 >= len(s) || s[i+1]^dir != 0x00 {
				return "", errCorrupt
			}
			// Unescape the \xff.
			buf = append(buf, s[last:i]...)
			buf = append(buf, 0xff^dir)
			i += 2
			last = i
		default:
			i++
		}
	}
	return "", errCorrupt
}