Commit 12dbd06f authored by Robert Griesemer's avatar Robert Griesemer

gofmt-ify encoding

R=rsc
http://go/go-review/1017048
parent 9df7d6ef
...@@ -65,14 +65,14 @@ func Encode(dst, src []byte) int { ...@@ -65,14 +65,14 @@ func Encode(dst, src []byte) int {
// Otherwise, 5 base 85 digits starting at !. // Otherwise, 5 base 85 digits starting at !.
for i := 4; i >= 0; i-- { for i := 4; i >= 0; i-- {
dst[i] = '!' + byte(v%85); dst[i] = '!'+byte(v%85);
v /= 85; v /= 85;
} }
// If src was short, discard the low destination bytes. // If src was short, discard the low destination bytes.
m := 5; m := 5;
if len(src) < 4 { if len(src) < 4 {
m -= 4 - len(src); m -= 4-len(src);
src = nil; src = nil;
} else { } else {
src = src[4:len(src)]; src = src[4:len(src)];
...@@ -160,7 +160,7 @@ func (e *encoder) Write(p []byte) (n int, err os.Error) { ...@@ -160,7 +160,7 @@ func (e *encoder) Write(p []byte) (n int, err os.Error) {
func (e *encoder) Close() os.Error { func (e *encoder) Close() os.Error {
// If there's anything left in the buffer, flush it out // If there's anything left in the buffer, flush it out
if e.err == nil && e.nbuf > 0 { if e.err == nil && e.nbuf > 0 {
nout := Encode(&e.out, e.buf[0:e.nbuf]); nout := Encode(&e.out, e.buf[0 : e.nbuf]);
e.nbuf = 0; e.nbuf = 0;
_, e.err = e.w.Write(e.out[0:nout]); _, e.err = e.w.Write(e.out[0:nout]);
} }
...@@ -195,7 +195,7 @@ func Decode(dst, src []byte, flush bool) (ndst, nsrc int, err os.Error) { ...@@ -195,7 +195,7 @@ func Decode(dst, src []byte, flush bool) (ndst, nsrc int, err os.Error) {
var v uint32; var v uint32;
var nb int; var nb int;
for i, b := range src { for i, b := range src {
if len(dst) - ndst < 4 { if len(dst)-ndst < 4 {
return; return;
} }
switch { switch {
...@@ -205,7 +205,7 @@ func Decode(dst, src []byte, flush bool) (ndst, nsrc int, err os.Error) { ...@@ -205,7 +205,7 @@ func Decode(dst, src []byte, flush bool) (ndst, nsrc int, err os.Error) {
nb = 5; nb = 5;
v = 0; v = 0;
case '!' <= b && b <= 'u': case '!' <= b && b <= 'u':
v = v*85 + uint32(b - '!'); v = v*85 + uint32(b-'!');
nb++; nb++;
default: default:
return 0, 0, CorruptInputError(i); return 0, 0, CorruptInputError(i);
...@@ -282,10 +282,10 @@ func (d *decoder) Read(p []byte) (n int, err os.Error) { ...@@ -282,10 +282,10 @@ func (d *decoder) Read(p []byte) (n int, err os.Error) {
// Decode leftover input from last read. // Decode leftover input from last read.
var nn, nsrc, ndst int; var nn, nsrc, ndst int;
if d.nbuf > 0 { if d.nbuf > 0 {
ndst, nsrc, d.err = Decode(&d.outbuf, d.buf[0:d.nbuf], d.readErr != nil); ndst, nsrc, d.err = Decode(&d.outbuf, d.buf[0 : d.nbuf], d.readErr != nil);
if ndst > 0 { if ndst > 0 {
d.out = d.outbuf[0:ndst]; d.out = d.outbuf[0:ndst];
d.nbuf = bytes.Copy(&d.buf, d.buf[nsrc:d.nbuf]); d.nbuf = bytes.Copy(&d.buf, d.buf[nsrc : d.nbuf]);
continue; // copy out and return continue; // copy out and return
} }
} }
...@@ -300,9 +300,8 @@ func (d *decoder) Read(p []byte) (n int, err os.Error) { ...@@ -300,9 +300,8 @@ func (d *decoder) Read(p []byte) (n int, err os.Error) {
} }
// Read more data. // Read more data.
nn, d.readErr = d.r.Read(d.buf[d.nbuf:len(d.buf)]); nn, d.readErr = d.r.Read(d.buf[d.nbuf : len(d.buf)]);
d.nbuf += nn; d.nbuf += nn;
} }
panic("unreachable"); panic("unreachable");
} }
...@@ -28,11 +28,11 @@ var pairs = []testpair{ ...@@ -28,11 +28,11 @@ var pairs = []testpair{
"O<DJ+*.@<*K0@<6L(Df-\\0Ec5e;DffZ(EZee.Bl.9pF\"AGXBPCsi+DGm>@3BB/F*&OCAfu2/AKY\n" "O<DJ+*.@<*K0@<6L(Df-\\0Ec5e;DffZ(EZee.Bl.9pF\"AGXBPCsi+DGm>@3BB/F*&OCAfu2/AKY\n"
"i(DIb:@FD,*)+C]U=@3BN#EcYf8ATD3s@q?d$AftVqCh[NqF<G:8+EV:.+Cf>-FD5W8ARlolDIa\n" "i(DIb:@FD,*)+C]U=@3BN#EcYf8ATD3s@q?d$AftVqCh[NqF<G:8+EV:.+Cf>-FD5W8ARlolDIa\n"
"l(DId<j@<?3r@:F%a+D58'ATD4$Bl@l3De:,-DJs`8ARoFb/0JMK@qB4^F!,R<AKZ&-DfTqBG%G\n" "l(DId<j@<?3r@:F%a+D58'ATD4$Bl@l3De:,-DJs`8ARoFb/0JMK@qB4^F!,R<AKZ&-DfTqBG%G\n"
">uD.RTpAKYo'+CT/5+Cei#DII?(E,9)oF*2M7/c\n" ">uD.RTpAKYo'+CT/5+Cei#DII?(E,9)oF*2M7/c\n",
} },
} }
var bigtest = pairs[len(pairs)-1]; var bigtest = pairs[len(pairs)-1]
func testEqual(t *testing.T, msg string, args ...) bool { func testEqual(t *testing.T, msg string, args ...) bool {
v := reflect.NewValue(args).(*reflect.StructValue); v := reflect.NewValue(args).(*reflect.StructValue);
......
...@@ -24,20 +24,20 @@ const encode = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!# ...@@ -24,20 +24,20 @@ const encode = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!#
// The decodings are 1+ the actual value, so that the // The decodings are 1+ the actual value, so that the
// default zero value can be used to mean "not valid". // default zero value can be used to mean "not valid".
var decode = [256]uint8{ var decode = [256]uint8{
'0': 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, '0': 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
'A': 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 'A': 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23,
24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36,
'a': 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 'a': 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49,
50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62,
'!': 63, '!': 63,
'#': 64, 65, 66, 67, '#': 64, 65, 66, 67,
'(': 68, 69, 70, 71, '(': 68, 69, 70, 71,
'-': 72, '-': 72,
';': 73, ';': 73,
'<': 74, 75, 76, 77, '<': 74, 75, 76, 77,
'@': 78, '@': 78,
'^': 79, 80, 81, '^': 79, 80, 81,
'{': 82, 83, 84, 85 '{': 82, 83, 84, 85,
} }
// Encode encodes src into EncodedLen(len(src)) // Encode encodes src into EncodedLen(len(src))
...@@ -56,15 +56,15 @@ func Encode(dst, src []byte) int { ...@@ -56,15 +56,15 @@ func Encode(dst, src []byte) int {
n = 52; n = 52;
} }
if n <= 27 { if n <= 27 {
dst[ndst] = byte('A' + n - 1); dst[ndst] = byte('A'+n-1);
} else { } else {
dst[ndst] = byte('a' + n - 26 - 1); dst[ndst] = byte('a'+n-26-1);
} }
ndst++; ndst++;
for i := 0; i < n; i += 4 { for i := 0; i < n; i += 4 {
var v uint32; var v uint32;
for j := 0; j < 4 && i+j < n; j++ { for j := 0; j < 4 && i+j < n; j++ {
v |= uint32(src[i+j]) << uint(24 - j*8); v |= uint32(src[i+j])<<uint(24 - j*8);
} }
for j := 4; j >= 0; j-- { for j := 4; j >= 0; j-- {
dst[ndst+j] = encode[v%85]; dst[ndst+j] = encode[v%85];
...@@ -103,9 +103,9 @@ func Decode(dst, src []byte) (n int, err os.Error) { ...@@ -103,9 +103,9 @@ func Decode(dst, src []byte) (n int, err os.Error) {
var l int; var l int;
switch ch := int(src[nsrc]); { switch ch := int(src[nsrc]); {
case 'A' <= ch && ch <= 'Z': case 'A' <= ch && ch <= 'Z':
l = ch - 'A' + 1; l = ch-'A'+1;
case 'a' <= ch && ch <= 'z': case 'a' <= ch && ch <= 'z':
l = ch - 'a' + 26 + 1; l = ch-'a'+26+1;
default: default:
return ndst, CorruptInputError(nsrc); return ndst, CorruptInputError(nsrc);
} }
...@@ -116,7 +116,7 @@ func Decode(dst, src []byte) (n int, err os.Error) { ...@@ -116,7 +116,7 @@ func Decode(dst, src []byte) (n int, err os.Error) {
if nsrc+1+el+1 > len(src) || src[nsrc+1+el] != '\n' { if nsrc+1+el+1 > len(src) || src[nsrc+1+el] != '\n' {
return ndst, CorruptInputError(nsrc); return ndst, CorruptInputError(nsrc);
} }
line := src[nsrc+1:nsrc+1+el]; line := src[nsrc+1 : nsrc+1+el];
for i := 0; i < el; i += 5 { for i := 0; i < el; i += 5 {
var v uint32; var v uint32;
for j := 0; j < 5; j++ { for j := 0; j < 5; j++ {
...@@ -156,12 +156,12 @@ func NewEncoder(w io.Writer) io.WriteCloser { ...@@ -156,12 +156,12 @@ func NewEncoder(w io.Writer) io.WriteCloser {
} }
type encoder struct { type encoder struct {
w io.Writer; w io.Writer;
err os.Error; err os.Error;
buf [52]byte; buf [52]byte;
nbuf int; nbuf int;
out [1024]byte; out [1024]byte;
nout int; nout int;
} }
func (e *encoder) Write(p []byte) (n int, err os.Error) { func (e *encoder) Write(p []byte) (n int, err os.Error) {
...@@ -190,9 +190,9 @@ func (e *encoder) Write(p []byte) (n int, err os.Error) { ...@@ -190,9 +190,9 @@ func (e *encoder) Write(p []byte) (n int, err os.Error) {
// Large interior chunks. // Large interior chunks.
for len(p) >= 52 { for len(p) >= 52 {
nn := len(e.out)/(1+52/4*5+1) * 52; nn := len(e.out)/(1 + 52/4*5 + 1)*52;
if nn > len(p) { if nn > len(p) {
nn = len(p)/52 * 52; nn = len(p)/52*52;
} }
if nn > 0 { if nn > 0 {
nout := Encode(&e.out, p[0:nn]); nout := Encode(&e.out, p[0:nn]);
...@@ -216,7 +216,7 @@ func (e *encoder) Write(p []byte) (n int, err os.Error) { ...@@ -216,7 +216,7 @@ func (e *encoder) Write(p []byte) (n int, err os.Error) {
func (e *encoder) Close() os.Error { func (e *encoder) Close() os.Error {
// If there's anything left in the buffer, flush it out // If there's anything left in the buffer, flush it out
if e.err == nil && e.nbuf > 0 { if e.err == nil && e.nbuf > 0 {
nout := Encode(&e.out, e.buf[0:e.nbuf]); nout := Encode(&e.out, e.buf[0 : e.nbuf]);
e.nbuf = 0; e.nbuf = 0;
_, e.err = e.w.Write(e.out[0:nout]); _, e.err = e.w.Write(e.out[0:nout]);
} }
...@@ -229,14 +229,14 @@ func NewDecoder(r io.Reader) io.Reader { ...@@ -229,14 +229,14 @@ func NewDecoder(r io.Reader) io.Reader {
} }
type decoder struct { type decoder struct {
r io.Reader; r io.Reader;
err os.Error; err os.Error;
readErr os.Error; readErr os.Error;
buf [1024]byte; buf [1024]byte;
nbuf int; nbuf int;
out []byte; out []byte;
outbuf [1024]byte; outbuf [1024]byte;
off int64; off int64;
} }
func (d *decoder) Read(p []byte) (n int, err os.Error) { func (d *decoder) Read(p []byte) (n int, err os.Error) {
...@@ -263,22 +263,21 @@ func (d *decoder) Read(p []byte) (n int, err os.Error) { ...@@ -263,22 +263,21 @@ func (d *decoder) Read(p []byte) (n int, err os.Error) {
// Read and decode more input. // Read and decode more input.
var nn int; var nn int;
nn, d.readErr = d.r.Read(d.buf[d.nbuf:len(d.buf)]); nn, d.readErr = d.r.Read(d.buf[d.nbuf : len(d.buf)]);
d.nbuf += nn; d.nbuf += nn;
// Send complete lines to Decode. // Send complete lines to Decode.
nl := bytes.LastIndex(d.buf[0:d.nbuf], newline); nl := bytes.LastIndex(d.buf[0 : d.nbuf], newline);
if nl < 0 { if nl < 0 {
continue; continue;
} }
nn, d.err = Decode(&d.outbuf, d.buf[0:nl+1]); nn, d.err = Decode(&d.outbuf, d.buf[0 : nl+1]);
if e, ok := d.err.(CorruptInputError); ok { if e, ok := d.err.(CorruptInputError); ok {
d.err = CorruptInputError(int64(e)+d.off); d.err = CorruptInputError(int64(e) + d.off);
} }
d.out = d.outbuf[0:nn]; d.out = d.outbuf[0:nn];
d.nbuf = bytes.Copy(&d.buf, d.buf[nl+1:d.nbuf]); d.nbuf = bytes.Copy(&d.buf, d.buf[nl+1 : d.nbuf]);
d.off += int64(nl+1); d.off += int64(nl+1);
} }
panic("unreacahable"); panic("unreacahable");
} }
...@@ -56,11 +56,11 @@ var gitPairs = []testpair{ ...@@ -56,11 +56,11 @@ var gitPairs = []testpair{
"zVIXXEb95kYW*~HEWgu;7Ze%PVbZB98AYyqSVIXj2a&u*NWpZI|V`U(3W*}r`Y-wj`\n" "zVIXXEb95kYW*~HEWgu;7Ze%PVbZB98AYyqSVIXj2a&u*NWpZI|V`U(3W*}r`Y-wj`\n"
"zbRcPNAarPDAY*TCbZKsNWn>^>Ze$>7Ze(R<VRUI{VPb4$AZKN6WpZJ3X>V>IZ)PBC\n" "zbRcPNAarPDAY*TCbZKsNWn>^>Ze$>7Ze(R<VRUI{VPb4$AZKN6WpZJ3X>V>IZ)PBC\n"
"zZf|#NWn^b%EFfigV`XJzb0BnRWgv5CZ*p`Xc4cT~ZDnp_Wgu^6AYpEKAY);2ZeeU7\n" "zZf|#NWn^b%EFfigV`XJzb0BnRWgv5CZ*p`Xc4cT~ZDnp_Wgu^6AYpEKAY);2ZeeU7\n"
"IaBO8^b9HiME&u=k\n" "IaBO8^b9HiME&u=k\n",
} },
} }
var gitBigtest = gitPairs[len(gitPairs)-1]; var gitBigtest = gitPairs[len(gitPairs)-1]
func TestEncode(t *testing.T) { func TestEncode(t *testing.T) {
for _, p := range gitPairs { for _, p := range gitPairs {
......
...@@ -50,7 +50,7 @@ func TestEncode(t *testing.T) { ...@@ -50,7 +50,7 @@ func TestEncode(t *testing.T) {
} }
type decodeTest struct { type decodeTest struct {
in, out []byte; in, out []byte;
ok bool; ok bool;
} }
...@@ -120,8 +120,8 @@ func TestEncodeToString(t *testing.T) { ...@@ -120,8 +120,8 @@ func TestEncodeToString(t *testing.T) {
} }
type decodeStringTest struct { type decodeStringTest struct {
in string; in string;
out []byte; out []byte;
ok bool; ok bool;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment