aboutsummaryrefslogtreecommitdiff
path: root/varint.go
diff options
context:
space:
mode:
Diffstat (limited to 'varint.go')
-rw-r--r--varint.go38
1 files changed, 38 insertions, 0 deletions
diff --git a/varint.go b/varint.go
new file mode 100644
index 0000000..923f490
--- /dev/null
+++ b/varint.go
@@ -0,0 +1,38 @@
+package binproto
+
+import "math"
+
+var (
+ n1 = uint64(math.Pow(2, 7))
+ n2 = uint64(math.Pow(2, 14))
+ n3 = uint64(math.Pow(2, 21))
+ n4 = uint64(math.Pow(2, 28))
+ n5 = uint64(math.Pow(2, 35))
+ n6 = uint64(math.Pow(2, 42))
+ n7 = uint64(math.Pow(2, 49))
+ n8 = uint64(math.Pow(2, 56))
+ n9 = uint64(math.Pow(2, 63))
+)
+
+func encodingLength(i uint64) int {
+ if i < n1 {
+ return 1
+ } else if i < n2 {
+ return 2
+ } else if i < n3 {
+ return 3
+ } else if i < n4 {
+ return 4
+ } else if i < n5 {
+ return 5
+ } else if i < n6 {
+ return 6
+ } else if i < n7 {
+ return 7
+ } else if i < n8 {
+ return 8
+ } else if i < n9 {
+ return 9
+ }
+ return 10
+}