Chromium Code Reviews| Index: service/datastore/serialize/binary_tools.go |
| diff --git a/service/datastore/serialize/binary_tools.go b/service/datastore/serialize/binary_tools.go |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..a0b9e7ee5268e923a6e510fd5ba31a27a8c6067a |
| --- /dev/null |
| +++ b/service/datastore/serialize/binary_tools.go |
| @@ -0,0 +1,53 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +package serialize |
| + |
| +// Join will allocate a new []byte large enough to contain all itms and return |
| +// it. |
| +// |
| +// If itms only has one element, this effectively just copies that one element. |
| +func Join(itms ...[]byte) []byte { |
|
Vadim Sh.
2015/09/24 19:06:14
consider using https://golang.org/pkg/bytes/#Join
iannucci
2015/09/24 19:43:41
LOL! TIL....
|
| + total := 0 |
| + for _, i := range itms { |
| + total += len(i) |
| + } |
| + if total == 0 { |
| + return nil |
| + } |
| + ret := make([]byte, 0, total) |
| + for _, i := range itms { |
| + ret = append(ret, i...) |
| + } |
| + return ret |
| +} |
| + |
| +// Invert simply inverts all the bytes in bs. |
| +func Invert(bs []byte) []byte { |
| + if len(bs) == 0 { |
| + return nil |
| + } |
| + ret := make([]byte, len(bs)) |
| + for i, b := range bs { |
| + ret[i] = 0xFF ^ b |
| + } |
| + return ret |
| +} |
| + |
| +// Increment attempts to increment a copy of bstr as if adding 1 to an integer. |
| +// |
| +// If it overflows, the returned []byte will be all 0's, and the overflow bool |
| +// will be true. |
| +func Increment(bstr []byte) ([]byte, bool) { |
| + ret := Join(bstr) |
|
Vadim Sh.
2015/09/24 19:06:14
append(nil, bstr...) is a common pattern for copyi
iannucci
2015/09/24 19:43:41
Hm... Is that efficient? Probably...
It's more ve
|
| + for i := len(ret) - 1; i >= 0; i-- { |
| + if ret[i] == 0xFF { |
| + ret[i] = 0 |
| + } else { |
| + ret[i]++ |
| + return ret, false |
| + } |
| + } |
| + return ret, true |
| +} |