util.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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. package namespace
  15. func prefixInterval(pfx string, key, end []byte) (pfxKey []byte, pfxEnd []byte) {
  16. pfxKey = make([]byte, len(pfx)+len(key))
  17. copy(pfxKey[copy(pfxKey, pfx):], key)
  18. if len(end) == 1 && end[0] == 0 {
  19. // the edge of the keyspace
  20. pfxEnd = make([]byte, len(pfx))
  21. copy(pfxEnd, pfx)
  22. ok := false
  23. for i := len(pfxEnd) - 1; i >= 0; i-- {
  24. if pfxEnd[i]++; pfxEnd[i] != 0 {
  25. ok = true
  26. break
  27. }
  28. }
  29. if !ok {
  30. // 0xff..ff => 0x00
  31. pfxEnd = []byte{0}
  32. }
  33. } else if len(end) >= 1 {
  34. pfxEnd = make([]byte, len(pfx)+len(end))
  35. copy(pfxEnd[copy(pfxEnd, pfx):], end)
  36. }
  37. return pfxKey, pfxEnd
  38. }