1
0
mirror of https://github.com/golang/go synced 2024-11-26 02:27:56 -07:00

use the new bytes package

R=rsc
DELTA=61  (8 added, 31 deleted, 22 changed)
OCL=29897
CL=29899
This commit is contained in:
Rob Pike 2009-06-04 15:28:09 -07:00
parent 9a9ffb2b0e
commit 424f4f0ff5
6 changed files with 23 additions and 34 deletions

View File

@ -22,7 +22,7 @@ hash.install: io.install
hash/adler32.install: hash.install os.install hash/adler32.install: hash.install os.install
hash/crc32.install: hash.install os.install hash/crc32.install: hash.install os.install
http.install: bufio.install fmt.install io.install log.install net.install os.install path.install strconv.install strings.install utf8.install http.install: bufio.install fmt.install io.install log.install net.install os.install path.install strconv.install strings.install utf8.install
io.install: os.install sync.install io.install: bytes.install os.install sync.install
json.install: container/vector.install fmt.install io.install math.install reflect.install strconv.install strings.install utf8.install json.install: container/vector.install fmt.install io.install math.install reflect.install strconv.install strings.install utf8.install
log.install: fmt.install io.install os.install runtime.install time.install log.install: fmt.install io.install os.install runtime.install time.install
malloc.install: malloc.install:
@ -36,11 +36,11 @@ reflect.install: strconv.install sync.install utf8.install
regexp.install: container/vector.install os.install runtime.install utf8.install regexp.install: container/vector.install os.install runtime.install utf8.install
runtime.install: runtime.install:
sort.install: sort.install:
strconv.install: math.install os.install utf8.install strconv.install: bytes.install math.install os.install utf8.install
strings.install: utf8.install strings.install: utf8.install
sync.install: sync.install:
syscall.install: sync.install syscall.install: sync.install
tabwriter.install: container/vector.install io.install os.install utf8.install tabwriter.install: bytes.install container/vector.install io.install os.install utf8.install
template.install: container/vector.install fmt.install io.install os.install reflect.install runtime.install strings.install template.install: container/vector.install fmt.install io.install os.install reflect.install runtime.install strings.install
testing.install: flag.install fmt.install os.install runtime.install testing.install: flag.install fmt.install os.install runtime.install
testing/iotest.install: io.install log.install os.install testing/iotest.install: io.install log.install os.install

View File

@ -43,11 +43,12 @@ func Equal(a, b []byte) bool {
// Copy copies the source to the destination, stopping when the source // Copy copies the source to the destination, stopping when the source
// is all transferred. The caller must guarantee that there is enough // is all transferred. The caller must guarantee that there is enough
// room in the destination. // room in the destination. It returns the number of bytes copied
func Copy(dst, src []byte) { func Copy(dst, src []byte) int {
for i, x := range src { for i, x := range src {
dst[i] = x dst[i] = x
} }
return len(src)
} }
// Explode splits s into an array of UTF-8 sequences, one per Unicode character (still arrays of bytes). // Explode splits s into an array of UTF-8 sequences, one per Unicode character (still arrays of bytes).

View File

@ -10,6 +10,7 @@
package io package io
import ( import (
"bytes";
"os"; "os";
) )
@ -210,9 +211,7 @@ func (r ByteReader) Read(p []byte) (int, os.Error) {
if n > len(b) { if n > len(b) {
n = len(b); n = len(b);
} }
for i := 0; i < n; i++ { bytes.Copy(p, b[0:n]);
p[i] = b[i];
}
*b = b[n:len(b)]; *b = b[n:len(b)];
return n, nil; return n, nil;
} }

View File

@ -11,6 +11,8 @@
package strconv package strconv
import "bytes"
type decimal struct { type decimal struct {
// TODO(rsc): Can make d[] a bit smaller and add // TODO(rsc): Can make d[] a bit smaller and add
// truncated bool; // truncated bool;
@ -27,7 +29,6 @@ func (a *decimal) RoundDown(nd int) *decimal;
func (a *decimal) RoundedInteger() uint64; func (a *decimal) RoundedInteger() uint64;
func copy(dst []byte, src []byte) int;
func digitZero(dst []byte) int; func digitZero(dst []byte) int;
func (a *decimal) String() string { func (a *decimal) String() string {
@ -52,18 +53,18 @@ func (a *decimal) String() string {
buf[w] = '.'; buf[w] = '.';
w++; w++;
w += digitZero(buf[w:w+-a.dp]); w += digitZero(buf[w:w+-a.dp]);
w += copy(buf[w:w+a.nd], a.d[0:a.nd]); w += bytes.Copy(buf[w:w+a.nd], a.d[0:a.nd]);
case a.dp < a.nd: case a.dp < a.nd:
// decimal point in middle of digits // decimal point in middle of digits
w += copy(buf[w:w+a.dp], a.d[0:a.dp]); w += bytes.Copy(buf[w:w+a.dp], a.d[0:a.dp]);
buf[w] = '.'; buf[w] = '.';
w++; w++;
w += copy(buf[w:w+a.nd-a.dp], a.d[a.dp:a.nd]); w += bytes.Copy(buf[w:w+a.nd-a.dp], a.d[a.dp:a.nd]);
default: default:
// zeros fill space between digits and decimal point // zeros fill space between digits and decimal point
w += copy(buf[w:w+a.nd], a.d[0:a.nd]); w += bytes.Copy(buf[w:w+a.nd], a.d[0:a.nd]);
w += digitZero(buf[w:w+a.dp-a.nd]); w += digitZero(buf[w:w+a.dp-a.nd]);
} }
return string(buf[0:w]); return string(buf[0:w]);

View File

@ -10,6 +10,7 @@
package tabwriter package tabwriter
import ( import (
"bytes";
"container/vector"; "container/vector";
"io"; "io";
"os"; "os";
@ -56,9 +57,7 @@ func (b *byteArray) append(s []byte) {
n2 = m; n2 = m;
} }
b := make([]byte, n2); b := make([]byte, n2);
for i := 0; i < n; i++ { bytes.Copy(b, a);
b[i] = a[i];
}
a = b; a = b;
} }

View File

@ -5,6 +5,7 @@
package utf8 package utf8
import ( import (
"bytes";
"fmt"; "fmt";
"io"; "io";
"testing"; "testing";
@ -45,7 +46,7 @@ var utf8map = []Utf8Map {
} }
// io.StringBytes with one extra byte at end // io.StringBytes with one extra byte at end
func bytes(s string) []byte { func makeBytes(s string) []byte {
s += "\x00"; s += "\x00";
b := io.StringBytes(s); b := io.StringBytes(s);
return b[0:len(s)-1]; return b[0:len(s)-1];
@ -54,7 +55,7 @@ func bytes(s string) []byte {
func TestFullRune(t *testing.T) { func TestFullRune(t *testing.T) {
for i := 0; i < len(utf8map); i++ { for i := 0; i < len(utf8map); i++ {
m := utf8map[i]; m := utf8map[i];
b := bytes(m.str); b := makeBytes(m.str);
if !utf8.FullRune(b) { if !utf8.FullRune(b) {
t.Errorf("FullRune(%q) (rune %04x) = false, want true", b, m.rune); t.Errorf("FullRune(%q) (rune %04x) = false, want true", b, m.rune);
} }
@ -73,26 +74,14 @@ func TestFullRune(t *testing.T) {
} }
} }
func equalBytes(a, b []byte) bool {
if len(a) != len(b) {
return false;
}
for i := 0; i < len(a); i++ {
if a[i] != b[i] {
return false;
}
}
return true;
}
func TestEncodeRune(t *testing.T) { func TestEncodeRune(t *testing.T) {
for i := 0; i < len(utf8map); i++ { for i := 0; i < len(utf8map); i++ {
m := utf8map[i]; m := utf8map[i];
b := bytes(m.str); b := makeBytes(m.str);
var buf [10]byte; var buf [10]byte;
n := utf8.EncodeRune(m.rune, &buf); n := utf8.EncodeRune(m.rune, &buf);
b1 := buf[0:n]; b1 := buf[0:n];
if !equalBytes(b, b1) { if !bytes.Equal(b, b1) {
t.Errorf("EncodeRune(0x%04x) = %q want %q", m.rune, b1, b); t.Errorf("EncodeRune(0x%04x) = %q want %q", m.rune, b1, b);
} }
} }
@ -101,7 +90,7 @@ func TestEncodeRune(t *testing.T) {
func TestDecodeRune(t *testing.T) { func TestDecodeRune(t *testing.T) {
for i := 0; i < len(utf8map); i++ { for i := 0; i < len(utf8map); i++ {
m := utf8map[i]; m := utf8map[i];
b := bytes(m.str); b := makeBytes(m.str);
rune, size := utf8.DecodeRune(b); rune, size := utf8.DecodeRune(b);
if rune != m.rune || size != len(b) { if rune != m.rune || size != len(b) {
t.Errorf("DecodeRune(%q) = 0x%04x, %d want 0x%04x, %d", b, rune, size, m.rune, len(b)); t.Errorf("DecodeRune(%q) = 0x%04x, %d want 0x%04x, %d", b, rune, size, m.rune, len(b));
@ -172,7 +161,7 @@ func TestRuneCount(t *testing.T) {
if out := utf8.RuneCountInString(tt.in); out != tt.out { if out := utf8.RuneCountInString(tt.in); out != tt.out {
t.Errorf("RuneCountInString(%q) = %d, want %d", tt.in, out, tt.out); t.Errorf("RuneCountInString(%q) = %d, want %d", tt.in, out, tt.out);
} }
if out := utf8.RuneCount(bytes(tt.in)); out != tt.out { if out := utf8.RuneCount(makeBytes(tt.in)); out != tt.out {
t.Errorf("RuneCount(%q) = %d, want %d", tt.in, out, tt.out); t.Errorf("RuneCount(%q) = %d, want %d", tt.in, out, tt.out);
} }
} }