github.com/cockroachdb/cockroachdb-parser@v0.23.3-0.20240213214944-911057d40c9a/pkg/keysbase/data.go (about)

     1  // Copyright 2022 The Cockroach Authors.
     2  //
     3  // Use of this software is governed by the Business Source License
     4  // included in the file licenses/BSL.txt.
     5  //
     6  // As of the Change Date specified in that file, in accordance with
     7  // the Business Source License, use of this software will be governed
     8  // by the Apache License, Version 2.0, included in the file
     9  // licenses/APL.txt.
    10  
    11  package keysbase
    12  
    13  // KeyMax is a maximum key value which sorts after all other keys.
    14  var KeyMax = []byte{0xff, 0xff}
    15  
    16  // PrefixEnd determines the end key given b as a prefix, that is the key that
    17  // sorts precisely behind all keys starting with prefix: "1" is added to the
    18  // final byte and the carry propagated. The special cases of nil and KeyMin
    19  // always returns KeyMax.
    20  func PrefixEnd(b []byte) []byte {
    21  	if len(b) == 0 {
    22  		return KeyMax
    23  	}
    24  	// Switched to "make and copy" pattern in #4963 for performance.
    25  	end := make([]byte, len(b))
    26  	copy(end, b)
    27  	for i := len(end) - 1; i >= 0; i-- {
    28  		end[i] = end[i] + 1
    29  		if end[i] != 0 {
    30  			return end[:i+1]
    31  		}
    32  	}
    33  	// This statement will only be reached if the key is already a maximal byte
    34  	// string (i.e. already \xff...).
    35  	return b
    36  }