blob: ecf04046c32daada17c9c02e15d88a58e301bdfa [file] [log] [blame]
khenaidooffe076b2019-01-15 16:08:08 -05001// 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
15package namespace
16
17func 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}