go.etcd.io/etcd@v3.3.27+incompatible/clientv3/namespace/util.go (about)

     1  // Copyright 2017 The etcd Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package namespace
    16  
    17  func prefixInterval(pfx string, key, end []byte) (pfxKey []byte, pfxEnd []byte) {
    18  	pfxKey = make([]byte, len(pfx)+len(key))
    19  	copy(pfxKey[copy(pfxKey, pfx):], key)
    20  
    21  	if len(end) == 1 && end[0] == 0 {
    22  		// the edge of the keyspace
    23  		pfxEnd = make([]byte, len(pfx))
    24  		copy(pfxEnd, pfx)
    25  		ok := false
    26  		for i := len(pfxEnd) - 1; i >= 0; i-- {
    27  			if pfxEnd[i]++; pfxEnd[i] != 0 {
    28  				ok = true
    29  				break
    30  			}
    31  		}
    32  		if !ok {
    33  			// 0xff..ff => 0x00
    34  			pfxEnd = []byte{0}
    35  		}
    36  	} else if len(end) >= 1 {
    37  		pfxEnd = make([]byte, len(pfx)+len(end))
    38  		copy(pfxEnd[copy(pfxEnd, pfx):], end)
    39  	}
    40  
    41  	return pfxKey, pfxEnd
    42  }