OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package memory |
| 6 |
| 7 import ( |
| 8 "fmt" |
| 9 ) |
| 10 |
| 11 func bjoin(itms ...[]byte) []byte { |
| 12 total := 0 |
| 13 for _, i := range itms { |
| 14 total += len(i) |
| 15 } |
| 16 ret := make([]byte, 0, total) |
| 17 for _, i := range itms { |
| 18 ret = append(ret, i...) |
| 19 } |
| 20 return ret |
| 21 } |
| 22 |
| 23 // invert simply inverts all the bytes in bs. |
| 24 func invert(bs []byte) []byte { |
| 25 if bs == nil { |
| 26 return nil |
| 27 } |
| 28 ret := make([]byte, len(bs)) |
| 29 for i, b := range bs { |
| 30 ret[i] = 0xFF ^ b |
| 31 } |
| 32 return ret |
| 33 } |
| 34 |
| 35 func increment(bstr []byte) []byte { |
| 36 if bstr == nil { |
| 37 return nil |
| 38 } |
| 39 |
| 40 // Copy bstr |
| 41 ret := bjoin(bstr) |
| 42 for i := len(ret) - 1; i >= 0; i-- { |
| 43 if ret[i] == 0xFF { |
| 44 ret[i] = 0 |
| 45 } else { |
| 46 ret[i]++ |
| 47 return ret |
| 48 } |
| 49 } |
| 50 |
| 51 // This byte string was ALL FF's. The only safe incrementation to do her
e |
| 52 // would be to add a new byte to the beginning of bstr with the value 0x
01, |
| 53 // and a byte to the beginning OF ALL OTHER []byte's which bstr may be |
| 54 // compared with. This is obviously impossible to do here, so panic. If
we |
| 55 // hit this, then we would need to add a spare 0 byte before every index |
| 56 // column. |
| 57 // |
| 58 // Another way to think about this is that we just accumulated a 'carry'
bit, |
| 59 // and the new value has overflowed this representation. |
| 60 // |
| 61 // Fortunately, the first byte of a serialized index column entry is a |
| 62 // PropertyType byte, and the only valid values that we'll be incrementi
ng |
| 63 // are never equal to 0xFF, since they have the higbit set (so either th
ey're |
| 64 // 0x8*, or 0x7*, depending on if it's inverted). |
| 65 impossible(fmt.Errorf("incrementing %v would require more sigfigs", bstr
)) |
| 66 return nil |
| 67 } |
OLD | NEW |