Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1075)

Unified Diff: service/datastore/serialize/binary_tools.go

Issue 1365743002: Refactor serialization helpers from impl/memory -> service/datastore/serialize (Closed) Base URL: https://github.com/luci/gae.git@estimate_size
Patch Set: fix comments Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « impl/memory/gkvlite_iter_test.go ('k') | service/datastore/serialize/serialize.go » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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..38c899e10645e5d2dbeb67592f8c88f09708a2a9
--- /dev/null
+++ b/service/datastore/serialize/binary_tools.go
@@ -0,0 +1,43 @@
+// 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
+
+import (
+ "bytes"
+)
+
+// Join is a convenience invocation of bytes.Join(itms, nil)
+func Join(itms ...[]byte) []byte {
+ return bytes.Join(itms, nil)
+}
+
+// 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)
+ for i := len(ret) - 1; i >= 0; i-- {
+ if ret[i] == 0xFF {
+ ret[i] = 0
+ } else {
+ ret[i]++
+ return ret, false
+ }
+ }
+ return ret, true
+}
« no previous file with comments | « impl/memory/gkvlite_iter_test.go ('k') | service/datastore/serialize/serialize.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698