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 serialize |
| 6 |
| 7 import ( |
| 8 "bytes" |
| 9 ) |
| 10 |
| 11 // Join is a convenience invocation of bytes.Join(itms, nil) |
| 12 func Join(itms ...[]byte) []byte { |
| 13 return bytes.Join(itms, nil) |
| 14 } |
| 15 |
| 16 // Invert simply inverts all the bytes in bs. |
| 17 func Invert(bs []byte) []byte { |
| 18 if len(bs) == 0 { |
| 19 return nil |
| 20 } |
| 21 ret := make([]byte, len(bs)) |
| 22 for i, b := range bs { |
| 23 ret[i] = 0xFF ^ b |
| 24 } |
| 25 return ret |
| 26 } |
| 27 |
| 28 // Increment attempts to increment a copy of bstr as if adding 1 to an integer. |
| 29 // |
| 30 // If it overflows, the returned []byte will be all 0's, and the overflow bool |
| 31 // will be true. |
| 32 func Increment(bstr []byte) ([]byte, bool) { |
| 33 ret := Join(bstr) |
| 34 for i := len(ret) - 1; i >= 0; i-- { |
| 35 if ret[i] == 0xFF { |
| 36 ret[i] = 0 |
| 37 } else { |
| 38 ret[i]++ |
| 39 return ret, false |
| 40 } |
| 41 } |
| 42 return ret, true |
| 43 } |
OLD | NEW |