Chromium Code Reviews| 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 // Join will allocate a new []byte large enough to contain all itms and return | |
| 8 // it. | |
| 9 // | |
| 10 // If itms only has one element, this effectively just copies that one element. | |
| 11 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....
| |
| 12 total := 0 | |
| 13 for _, i := range itms { | |
| 14 total += len(i) | |
| 15 } | |
| 16 if total == 0 { | |
| 17 return nil | |
| 18 } | |
| 19 ret := make([]byte, 0, total) | |
| 20 for _, i := range itms { | |
| 21 ret = append(ret, i...) | |
| 22 } | |
| 23 return ret | |
| 24 } | |
| 25 | |
| 26 // Invert simply inverts all the bytes in bs. | |
| 27 func Invert(bs []byte) []byte { | |
| 28 if len(bs) == 0 { | |
| 29 return nil | |
| 30 } | |
| 31 ret := make([]byte, len(bs)) | |
| 32 for i, b := range bs { | |
| 33 ret[i] = 0xFF ^ b | |
| 34 } | |
| 35 return ret | |
| 36 } | |
| 37 | |
| 38 // Increment attempts to increment a copy of bstr as if adding 1 to an integer. | |
| 39 // | |
| 40 // If it overflows, the returned []byte will be all 0's, and the overflow bool | |
| 41 // will be true. | |
| 42 func Increment(bstr []byte) ([]byte, bool) { | |
| 43 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
| |
| 44 for i := len(ret) - 1; i >= 0; i-- { | |
| 45 if ret[i] == 0xFF { | |
| 46 ret[i] = 0 | |
| 47 } else { | |
| 48 ret[i]++ | |
| 49 return ret, false | |
| 50 } | |
| 51 } | |
| 52 return ret, true | |
| 53 } | |
| OLD | NEW |