diff options
author | Christopher Speller <crspeller@gmail.com> | 2017-04-24 20:11:36 -0400 |
---|---|---|
committer | Joram Wilander <jwawilander@gmail.com> | 2017-04-24 20:11:36 -0400 |
commit | f5437632f486b7d0a0a181c58f113c86d032b02c (patch) | |
tree | 407388e3003a210a89f4b2128d7ad656f8b79d26 /vendor/golang.org/x/text/internal/catmsg | |
parent | 7f68a60f8c228d5604e0566bf84cabb145d16c37 (diff) | |
download | chat-f5437632f486b7d0a0a181c58f113c86d032b02c.tar.gz chat-f5437632f486b7d0a0a181c58f113c86d032b02c.tar.bz2 chat-f5437632f486b7d0a0a181c58f113c86d032b02c.zip |
Upgrading server dependancies (#6215)
Diffstat (limited to 'vendor/golang.org/x/text/internal/catmsg')
-rw-r--r-- | vendor/golang.org/x/text/internal/catmsg/varint.go | 62 | ||||
-rw-r--r-- | vendor/golang.org/x/text/internal/catmsg/varint_test.go | 123 |
2 files changed, 185 insertions, 0 deletions
diff --git a/vendor/golang.org/x/text/internal/catmsg/varint.go b/vendor/golang.org/x/text/internal/catmsg/varint.go new file mode 100644 index 000000000..a2cee2cf5 --- /dev/null +++ b/vendor/golang.org/x/text/internal/catmsg/varint.go @@ -0,0 +1,62 @@ +// Copyright 2017 The Go 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 catmsg + +// This file implements varint encoding analogous to the one in encoding/binary. +// We need a string version of this function, so we add that here and then add +// the rest for consistency. + +import "errors" + +var ( + errIllegalVarint = errors.New("catmsg: illegal varint") + errVarintTooLarge = errors.New("catmsg: varint too large for uint64") +) + +const maxVarintBytes = 10 // maximum length of a varint + +// encodeUint encodes x as a variable-sized integer into buf and returns the +// number of bytes written. buf must be at least maxVarintBytes long +func encodeUint(buf []byte, x uint64) (n int) { + for ; x > 127; n++ { + buf[n] = 0x80 | uint8(x&0x7F) + x >>= 7 + } + buf[n] = uint8(x) + n++ + return n +} + +func decodeUintString(s string) (x uint64, size int, err error) { + i := 0 + for shift := uint(0); shift < 64; shift += 7 { + if i >= len(s) { + return 0, i, errIllegalVarint + } + b := uint64(s[i]) + i++ + x |= (b & 0x7F) << shift + if b&0x80 == 0 { + return x, i, nil + } + } + return 0, i, errVarintTooLarge +} + +func decodeUint(b []byte) (x uint64, size int, err error) { + i := 0 + for shift := uint(0); shift < 64; shift += 7 { + if i >= len(b) { + return 0, i, errIllegalVarint + } + c := uint64(b[i]) + i++ + x |= (c & 0x7F) << shift + if c&0x80 == 0 { + return x, i, nil + } + } + return 0, i, errVarintTooLarge +} diff --git a/vendor/golang.org/x/text/internal/catmsg/varint_test.go b/vendor/golang.org/x/text/internal/catmsg/varint_test.go new file mode 100644 index 000000000..a079c775d --- /dev/null +++ b/vendor/golang.org/x/text/internal/catmsg/varint_test.go @@ -0,0 +1,123 @@ +// Copyright 2017 The Go 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 catmsg + +import ( + "fmt" + "testing" +) + +func TestEncodeUint(t *testing.T) { + testCases := []struct { + x uint64 + enc string + }{ + {0, "\x00"}, + {1, "\x01"}, + {2, "\x02"}, + {0x7f, "\x7f"}, + {0x80, "\x80\x01"}, + {1 << 14, "\x80\x80\x01"}, + {0xffffffff, "\xff\xff\xff\xff\x0f"}, + {0xffffffffffffffff, "\xff\xff\xff\xff\xff\xff\xff\xff\xff\x01"}, + } + for _, tc := range testCases { + buf := [maxVarintBytes]byte{} + got := string(buf[:encodeUint(buf[:], tc.x)]) + if got != tc.enc { + t.Errorf("EncodeUint(%#x) = %q; want %q", tc.x, got, tc.enc) + } + } +} + +func TestDecodeUint(t *testing.T) { + testCases := []struct { + x uint64 + size int + enc string + err error + }{{ + x: 0, + size: 0, + enc: "", + err: errIllegalVarint, + }, { + x: 0, + size: 1, + enc: "\x80", + err: errIllegalVarint, + }, { + x: 0, + size: 3, + enc: "\x80\x80\x80", + err: errIllegalVarint, + }, { + x: 0, + size: 1, + enc: "\x00", + }, { + x: 1, + size: 1, + enc: "\x01", + }, { + x: 2, + size: 1, + enc: "\x02", + }, { + x: 0x7f, + size: 1, + enc: "\x7f", + }, { + x: 0x80, + size: 2, + enc: "\x80\x01", + }, { + x: 1 << 14, + size: 3, + enc: "\x80\x80\x01", + }, { + x: 0xffffffff, + size: 5, + enc: "\xff\xff\xff\xff\x0f", + }, { + x: 0xffffffffffffffff, + size: 10, + enc: "\xff\xff\xff\xff\xff\xff\xff\xff\xff\x01", + }, { + x: 0xffffffffffffffff, + size: 10, + enc: "\xff\xff\xff\xff\xff\xff\xff\xff\xff\x01\x00", + }, { + x: 0, + size: 10, + enc: "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\x01", + err: errVarintTooLarge, + }} + forms := []struct { + name string + decode func(s string) (x uint64, size int, err error) + }{ + {"decode", func(s string) (x uint64, size int, err error) { + return decodeUint([]byte(s)) + }}, + {"decodeString", decodeUintString}, + } + for _, f := range forms { + for _, tc := range testCases { + t.Run(fmt.Sprintf("%s:%q", f.name, tc.enc), func(t *testing.T) { + x, size, err := f.decode(tc.enc) + if err != tc.err { + t.Error("err = %q; want %q", err, tc.err) + } + if size != tc.size { + t.Errorf("size = %d; want %d", size, tc.size) + } + if x != tc.x { + t.Errorf("decode = %#x; want %#x", x, tc.x) + } + }) + } + } +} |