1
0
mirror of https://github.com/golang/go synced 2024-11-21 18:24:46 -07:00

code changes for array conversion.

as a reminder, the old conversion
was that you could write

	var arr [10]byte;
	var slice []byte;
	slice = arr;

but now you have to write

	slice = &arr;

the change eliminates an implicit &, so that
the only implicit &s left are in the . operator
and in string(arr).

also, removed utf8.EncodeRuneToString
in favor of string(rune).

R=r
DELTA=83  (1 added, 23 deleted, 59 changed)
OCL=27531
CL=27534
This commit is contained in:
Russ Cox 2009-04-15 20:27:45 -07:00
parent 65d397f747
commit 60ce95d7a1
25 changed files with 44 additions and 66 deletions

View File

@ -15,7 +15,7 @@ func cat(f *file.File) {
const NBUF = 512;
var buf [NBUF]byte;
for {
switch nr, er := f.Read(buf); true {
switch nr, er := f.Read(&buf); true {
case nr < 0:
fmt.Fprintf(os.Stderr, "error reading from %s: %s\n", f.String(), er.String());
sys.Exit(1);

View File

@ -57,7 +57,7 @@ func cat(r reader) {
r = newRotate13(r)
}
for {
switch nr, er := r.Read(buf); {
switch nr, er := r.Read(&buf); {
case nr < 0:
fmt.Fprintf(os.Stderr, "error reading from %s: %s\n", r.String(), er.String());
sys.Exit(1);

View File

@ -16,6 +16,6 @@ func sum(a []int) int { // returns an int
func main() {
s := sum([3]int{1,2,3}); // a slice of the array is passed to sum
s := sum(&[3]int{1,2,3}); // a slice of the array is passed to sum
fmt.Print(s, "\n");
}

View File

@ -96,7 +96,7 @@ func Run(argv0 string, argv, envv []string, stdin, stdout, stderr int) (p *Cmd,
}
// Run command.
p.Pid, err = os.ForkExec(argv0, argv, envv, fd);
p.Pid, err = os.ForkExec(argv0, argv, envv, &fd);
if err != nil {
goto Error;
}

View File

@ -19,7 +19,7 @@ func TestRunCat(t *testing.T) {
io.WriteString(cmd.Stdin, "hello, world\n");
cmd.Stdin.Close();
var buf [64]byte;
n, err1 := io.Readn(cmd.Stdout, buf);
n, err1 := io.Readn(cmd.Stdout, &buf);
if err1 != nil && err1 != io.ErrEOF {
t.Fatalf("reading from /bin/cat: %v", err1);
}
@ -38,7 +38,7 @@ func TestRunEcho(t *testing.T) {
t.Fatalf("opencmd /bin/echo: %v", err);
}
var buf [64]byte;
n, err1 := io.Readn(cmd.Stdout, buf);
n, err1 := io.Readn(cmd.Stdout, &buf);
if err1 != nil && err1 != io.ErrEOF {
t.Fatalf("reading from /bin/echo: %v", err1);
}

View File

@ -104,7 +104,7 @@ func charString(ch int) string {
case '\v': s = `\v`;
case '\\': s = `\\`;
case '\'': s = `\'`;
default : s = utf8.EncodeRuneToString(ch);
default : s = string(ch);
}
return "'" + s + "' (U+" + strconv.Itob(ch, 16) + ")";
}

View File

@ -51,7 +51,7 @@ func (d *Digest) Write(p []byte) (nn int, err *os.Error) {
}
d.nx += n;
if d.nx == _Chunk {
_Block(d, d.x);
_Block(d, &d.x);
d.nx = 0;
}
p = p[n:len(p)];

View File

@ -53,7 +53,7 @@ func (d *Digest) Write(p []byte) (nn int, err *os.Error) {
}
d.nx += n;
if d.nx == _Chunk {
_Block(d, d.x);
_Block(d, &d.x);
d.nx = 0;
}
p = p[n:len(p)];

View File

@ -142,7 +142,7 @@ func serveFileInternal(c *Conn, r *Request, name string, redirect bool) {
} else {
// read first chunk to decide between utf-8 text and binary
var buf [1024]byte;
n, err := io.Readn(f, buf);
n, err := io.Readn(f, &buf);
b := buf[0:n];
if isText(b) {
c.SetHeader("Content-Type", "text-plain; charset=utf-8");

View File

@ -26,7 +26,7 @@ func checkWrite(t *testing.T, w io.Write, data []byte, c chan int) {
func TestPipe1(t *testing.T) {
c := make(chan int);
r, w := io.Pipe();
var buf [64]byte;
var buf = make([]byte, 64);
go checkWrite(t, w, io.StringBytes("hello, world"), c);
n, err := r.Read(buf);
if err != nil {
@ -41,7 +41,7 @@ func TestPipe1(t *testing.T) {
}
func reader(t *testing.T, r io.Read, c chan int) {
var buf [64]byte;
var buf = make([]byte, 64);
for {
n, err := r.Read(buf);
if err != nil {
@ -59,7 +59,7 @@ func TestPipe2(t *testing.T) {
c := make(chan int);
r, w := io.Pipe();
go reader(t, r, c);
var buf [64]byte;
var buf = make([]byte, 64);
for i := 0; i < 5; i++ {
p := buf[0:5+i*10];
n, err := w.Write(p);
@ -91,12 +91,12 @@ func writer(w io.WriteClose, buf []byte, c chan pipeReturn) {
func TestPipe3(t *testing.T) {
c := make(chan pipeReturn);
r, w := io.Pipe();
var wdat [128]byte;
var wdat = make([]byte, 128);
for i := 0; i < len(wdat); i++ {
wdat[i] = byte(i);
}
go writer(w, wdat, c);
var rdat [1024]byte;
var rdat = make([]byte, 1024);
tot := 0;
for n := 1; n <= 256; n *= 2 {
nn, err := r.Read(rdat[tot:tot+n]);
@ -148,7 +148,7 @@ func testPipeReadClose(t *testing.T, async bool) {
} else {
delayClose(t, w, c);
}
var buf [64]byte;
var buf = make([]byte, 64);
n, err := r.Read(buf);
<-c;
if err != nil {

View File

@ -265,8 +265,8 @@ func (s *pollServer) Run() {
}
if fd == s.pr.Fd() {
// Drain our wakeup pipe.
for nn, e := s.pr.Read(scratch); nn > 0; {
nn, e = s.pr.Read(scratch)
for nn, e := s.pr.Read(&scratch); nn > 0; {
nn, e = s.pr.Read(&scratch)
}
// Read from channels
@ -287,9 +287,9 @@ func (s *pollServer) Run() {
}
}
var wakeupbuf [1]byte;
func (s *pollServer) Wakeup() {
var b [1]byte;
s.pw.Write(b)
s.pw.Write(&wakeupbuf)
}
func (s *pollServer) WaitRead(fd *netFD) {

View File

@ -49,7 +49,7 @@ func (p *pollster) AddFD(fd int64, mode int, repeat bool) *os.Error {
ev.Flags |= syscall.EV_ONESHOT
}
n, e := syscall.Kevent(p.kq, events, events, nil);
n, e := syscall.Kevent(p.kq, &events, &events, nil);
if e != 0 {
return os.ErrnoToError(e)
}
@ -78,7 +78,7 @@ func (p *pollster) DelFD(fd int64, mode int) {
// EV_RECEIPT - generate fake EV_ERROR as result of add,
// rather than waiting for real event
ev.Flags = syscall.EV_DELETE | syscall.EV_RECEIPT;
syscall.Kevent(p.kq, events, events, nil);
syscall.Kevent(p.kq, &events, &events, nil);
}
func (p *pollster) WaitFD(nsec int64) (fd int64, mode int, err *os.Error) {
@ -91,7 +91,7 @@ func (p *pollster) WaitFD(nsec int64) (fd int64, mode int, err *os.Error) {
t.Sec = nsec / 1e9;
t.Nsec = uint64(nsec % 1e9);
}
nn, e := syscall.Kevent(p.kq, nil, p.eventbuf, t);
nn, e := syscall.Kevent(p.kq, nil, &p.eventbuf, t);
if e != 0 {
if e == syscall.EINTR {
continue

View File

@ -15,7 +15,7 @@ func runEcho(fd io.ReadWrite, done chan<- int) {
var buf [1024]byte;
for {
n, err := fd.Read(buf);
n, err := fd.Read(&buf);
if err != nil || n == 0 {
break;
}
@ -58,7 +58,7 @@ func connect(t *testing.T, network, addr string) {
t.Fatalf("fd.Write(%q) = %d, %v", b, n, errno);
}
n, errno = fd.Read(b1);
n, errno = fd.Read(&b1);
if n != len(b) {
t.Fatalf("fd.Read() = %d, %v", n, errno);
}

View File

@ -20,7 +20,7 @@ func testTimeout(t *testing.T, network, addr string) {
t0 := time.Nanoseconds();
fd.SetReadTimeout(1e8); // 100ms
var b [100]byte;
n, err1 := fd.Read(b);
n, err1 := fd.Read(&b);
t1 := time.Nanoseconds();
if n != 0 || err1 != os.EAGAIN {
t.Errorf("fd.Read on %s %s did not return 0, EAGAIN: %v, %v", network, addr, n, err1);

View File

@ -38,7 +38,7 @@ func size(name string, t *testing.T) uint64 {
var buf [100]byte;
len := 0;
for {
n, e := file.Read(buf);
n, e := file.Read(&buf);
if n < 0 || e != nil {
t.Fatal("read failed:", err);
}

View File

@ -48,8 +48,8 @@ func TestSortStringArray(t *testing.T) {
func TestSortInts(t *testing.T) {
data := ints;
sort.SortInts(data);
if !sort.IntsAreSorted(data) {
sort.SortInts(&data);
if !sort.IntsAreSorted(&data) {
t.Errorf("sorted %v", ints);
t.Errorf(" got %v", data);
}
@ -57,8 +57,8 @@ func TestSortInts(t *testing.T) {
func TestSortFloats(t *testing.T) {
data := floats;
sort.SortFloats(data);
if !sort.FloatsAreSorted(data) {
sort.SortFloats(&data);
if !sort.FloatsAreSorted(&data) {
t.Errorf("sorted %v", floats);
t.Errorf(" got %v", data);
}
@ -66,8 +66,8 @@ func TestSortFloats(t *testing.T) {
func TestSortStrings(t *testing.T) {
data := strings;
sort.SortStrings(data);
if !sort.StringsAreSorted(data) {
sort.SortStrings(&data);
if !sort.StringsAreSorted(&data) {
t.Errorf("sorted %v", strings);
t.Errorf(" got %v", data);
}

View File

@ -249,7 +249,7 @@ func (b *Writer) writePadding(textw, cellw int) (err *os.Error) {
}
for n > len(b.padbytes) {
err = b.write0(b.padbytes);
err = b.write0(&b.padbytes);
if err != nil {
goto exit;
}

View File

@ -256,17 +256,6 @@ func EncodeRune(rune int, p []byte) int {
return 4;
}
// EncodeRuneToString returns the UTF-8 encoding of the rune.
func EncodeRuneToString(rune int) string {
if rune < _Rune1Max {
return string([1]byte{byte(rune)})
}
var buf [UTFMax]byte;
size := EncodeRune(rune, buf);
return string(buf[0:size]);
}
// RuneCount returns the number of runes in p. Erroneous and short
// encodings are treated as single runes of width 1 byte.
func RuneCount(p []byte) int {

View File

@ -90,7 +90,7 @@ func TestEncodeRune(t *testing.T) {
m := utf8map[i];
b := bytes(m.str);
var buf [10]byte;
n := utf8.EncodeRune(m.rune, buf);
n := utf8.EncodeRune(m.rune, &buf);
b1 := buf[0:n];
if !equalBytes(b, b1) {
t.Errorf("EncodeRune(0x%04x) = %q want %q", m.rune, b1, b);
@ -98,17 +98,6 @@ func TestEncodeRune(t *testing.T) {
}
}
func TestEncodeRuneToString(t *testing.T) {
for i := 0; i < len(utf8map); i++ {
m := utf8map[i];
s := m.str;
s1 := utf8.EncodeRuneToString(m.rune);
if s != s1 {
t.Errorf("EncodeRuneToString(0x%04x) = %s want %s", m.rune, s1, s);
}
}
}
func TestDecodeRune(t *testing.T) {
for i := 0; i < len(utf8map); i++ {
m := utf8map[i];

View File

@ -13,7 +13,7 @@ type T struct {
func main() {
var ta []*T;
ta = *new([1]*T); // TODO: the first * shouldn't be necessary
ta = new([1]*T);
ta[0] = nil;
}
/*

View File

@ -23,7 +23,7 @@ func main() {
as := new([2]string);
as[0] = "0";
as[1] = "1";
m["0"] = *as;
m["0"] = as;
a := m["0"];
a[0] = "x";

View File

@ -96,8 +96,8 @@ func
testpdpf1()
{
a := new([40]int);
setpd(*a);
res(sumpd(*a), 0, 40);
setpd(a);
res(sumpd(a), 0, 40);
b := (*a)[5:30];
res(sumpd(b), 5, 30);
@ -109,8 +109,8 @@ testpdpf2()
{
var a [80]int;
setpd(a);
res(sumpd(a), 0, 80);
setpd(&a);
res(sumpd(&a), 0, 80);
}
// generate bounds error with ptr dynamic

View File

@ -241,7 +241,7 @@ func atom(i int) *Slist // BUG: uses tokenbuf; should take argument
slist.atom.integer = i;
slist.isstring = false;
} else {
slist.atom.str = string(tokenbuf)[0:tokenlen];
slist.atom.str = string(tokenbuf[0:tokenlen]);
slist.isstring = true;
}
slist.isatom = true;

View File

@ -43,7 +43,7 @@ func readfile(filename string) ([]byte, *OS.Error) {
return []byte{}, err;
}
var buf [1<<20]byte;
n, err1 := IO.Readn(f, buf);
n, err1 := IO.Readn(f, &buf);
f.Close();
if err1 == IO.ErrEOF {
err1 = nil;

View File

@ -87,5 +87,5 @@ func IntToString(x, base int) string {
buf[i] = '-';
}
return string(buf)[i : len(buf)];
return string(buf[i : len(buf)]);
}