1
0
mirror of https://github.com/golang/go synced 2024-11-18 12:34:42 -07:00

changes required if we disallow the implicit *

in cap, len, [], and range on maps, strings, and slices.

R=r
DELTA=57  (2 added, 12 deleted, 43 changed)
OCL=30549
CL=30590
This commit is contained in:
Russ Cox 2009-06-22 13:34:21 -07:00
parent 64684cc2a2
commit da5abb9fb3
5 changed files with 42 additions and 52 deletions

View File

@ -178,8 +178,9 @@ func (tr *Reader) verifyChecksum(header []byte) bool {
} }
type slicer []byte type slicer []byte
func (s *slicer) next(n int) (b []byte) { func (sp *slicer) next(n int) (b []byte) {
b, *s = s[0:n], s[n:len(s)]; s := *sp;
b, *sp = s[0:n], s[n:len(s)];
return return
} }

View File

@ -154,7 +154,8 @@ func (f *Fmt) pad(s string) {
// never mind.) val is known to be unsigned. we could make things maybe // never mind.) val is known to be unsigned. we could make things maybe
// marginally faster by splitting the 32-bit case out into a separate function // marginally faster by splitting the 32-bit case out into a separate function
// but it's not worth the duplication, so val has 64 bits. // but it's not worth the duplication, so val has 64 bits.
func putint(buf *[nByte]byte, i int, base, val uint64, digits *string) int { func putint(buf []byte, base, val uint64, digits string) int {
i := len(buf) - 1;
for val >= base { for val >= base {
buf[i] = digits[val%base]; buf[i] = digits[val%base];
i--; i--;
@ -176,7 +177,7 @@ func (f *Fmt) Fmt_boolean(v bool) *Fmt {
} }
// integer; interprets prec but not wid. // integer; interprets prec but not wid.
func (f *Fmt) integer(a int64, base uint, is_signed bool, digits *string) string { func (f *Fmt) integer(a int64, base uint, is_signed bool, digits string) string {
var buf [nByte]byte; var buf [nByte]byte;
negative := is_signed && a < 0; negative := is_signed && a < 0;
if negative { if negative {
@ -196,7 +197,7 @@ func (f *Fmt) integer(a int64, base uint, is_signed bool, digits *string) string
} }
} }
i := putint(&buf, nByte-1, uint64(base), uint64(a), digits); i := putint(&buf, uint64(base), uint64(a), digits);
for i > 0 && prec > (nByte-1-i) { for i > 0 && prec > (nByte-1-i) {
buf[i] = '0'; buf[i] = '0';
i--; i--;
@ -232,7 +233,7 @@ func (f *Fmt) integer(a int64, base uint, is_signed bool, digits *string) string
// Fmt_d64 formats an int64 in decimal. // Fmt_d64 formats an int64 in decimal.
func (f *Fmt) Fmt_d64(v int64) *Fmt { func (f *Fmt) Fmt_d64(v int64) *Fmt {
f.pad(f.integer(v, 10, true, &ldigits)); f.pad(f.integer(v, 10, true, ldigits));
f.clearflags(); f.clearflags();
return f; return f;
} }
@ -249,7 +250,7 @@ func (f *Fmt) Fmt_d(v int) *Fmt {
// Fmt_ud64 formats a uint64 in decimal. // Fmt_ud64 formats a uint64 in decimal.
func (f *Fmt) Fmt_ud64(v uint64) *Fmt { func (f *Fmt) Fmt_ud64(v uint64) *Fmt {
f.pad(f.integer(int64(v), 10, false, &ldigits)); f.pad(f.integer(int64(v), 10, false, ldigits));
f.clearflags(); f.clearflags();
return f; return f;
} }
@ -266,7 +267,7 @@ func (f *Fmt) Fmt_ud(v uint) *Fmt {
// Fmt_x64 formats an int64 in hexadecimal. // Fmt_x64 formats an int64 in hexadecimal.
func (f *Fmt) Fmt_x64(v int64) *Fmt { func (f *Fmt) Fmt_x64(v int64) *Fmt {
f.pad(f.integer(v, 16, true, &ldigits)); f.pad(f.integer(v, 16, true, ldigits));
f.clearflags(); f.clearflags();
return f; return f;
} }
@ -283,7 +284,7 @@ func (f *Fmt) Fmt_x(v int) *Fmt {
// Fmt_ux64 formats a uint64 in hexadecimal. // Fmt_ux64 formats a uint64 in hexadecimal.
func (f *Fmt) Fmt_ux64(v uint64) *Fmt { func (f *Fmt) Fmt_ux64(v uint64) *Fmt {
f.pad(f.integer(int64(v), 16, false, &ldigits)); f.pad(f.integer(int64(v), 16, false, ldigits));
f.clearflags(); f.clearflags();
return f; return f;
} }
@ -300,7 +301,7 @@ func (f *Fmt) Fmt_ux(v uint) *Fmt {
// Fmt_X64 formats an int64 in upper case hexadecimal. // Fmt_X64 formats an int64 in upper case hexadecimal.
func (f *Fmt) Fmt_X64(v int64) *Fmt { func (f *Fmt) Fmt_X64(v int64) *Fmt {
f.pad(f.integer(v, 16, true, &udigits)); f.pad(f.integer(v, 16, true, udigits));
f.clearflags(); f.clearflags();
return f; return f;
} }
@ -317,7 +318,7 @@ func (f *Fmt) Fmt_X(v int) *Fmt {
// Fmt_uX64 formats a uint64 in upper case hexadecimal. // Fmt_uX64 formats a uint64 in upper case hexadecimal.
func (f *Fmt) Fmt_uX64(v uint64) *Fmt { func (f *Fmt) Fmt_uX64(v uint64) *Fmt {
f.pad(f.integer(int64(v), 16, false, &udigits)); f.pad(f.integer(int64(v), 16, false, udigits));
f.clearflags(); f.clearflags();
return f; return f;
} }
@ -334,7 +335,7 @@ func (f *Fmt) Fmt_uX(v uint) *Fmt {
// Fmt_o64 formats an int64 in octal. // Fmt_o64 formats an int64 in octal.
func (f *Fmt) Fmt_o64(v int64) *Fmt { func (f *Fmt) Fmt_o64(v int64) *Fmt {
f.pad(f.integer(v, 8, true, &ldigits)); f.pad(f.integer(v, 8, true, ldigits));
f.clearflags(); f.clearflags();
return f; return f;
} }
@ -351,7 +352,7 @@ func (f *Fmt) Fmt_o(v int) *Fmt {
// Fmt_uo64 formats a uint64 in octal. // Fmt_uo64 formats a uint64 in octal.
func (f *Fmt) Fmt_uo64(v uint64) *Fmt { func (f *Fmt) Fmt_uo64(v uint64) *Fmt {
f.pad(f.integer(int64(v), 8, false, &ldigits)); f.pad(f.integer(int64(v), 8, false, ldigits));
f.clearflags(); f.clearflags();
return f; return f;
} }
@ -368,7 +369,7 @@ func (f *Fmt) Fmt_uo(v uint) *Fmt {
// Fmt_b64 formats a uint64 in binary. // Fmt_b64 formats a uint64 in binary.
func (f *Fmt) Fmt_b64(v uint64) *Fmt { func (f *Fmt) Fmt_b64(v uint64) *Fmt {
f.pad(f.integer(int64(v), 2, false, &ldigits)); f.pad(f.integer(int64(v), 2, false, ldigits));
f.clearflags(); f.clearflags();
return f; return f;
} }

View File

@ -197,7 +197,7 @@ type ByteReader struct {
func (r ByteReader) Read(p []byte) (int, os.Error) { func (r ByteReader) Read(p []byte) (int, os.Error) {
n := len(p); n := len(p);
b := r.Data; b := *r.Data;
if len(b) == 0 { if len(b) == 0 {
return 0, os.EOF; return 0, os.EOF;
} }
@ -205,7 +205,7 @@ func (r ByteReader) Read(p []byte) (int, os.Error) {
n = len(b); n = len(b);
} }
bytes.Copy(p, b[0:n]); bytes.Copy(p, b[0:n]);
*b = b[n:len(b)]; *r.Data = b[n:len(b)];
return n, nil; return n, nil;
} }

View File

@ -118,12 +118,10 @@ func (b *_StructBuilder) Array() {
if b == nil { if b == nil {
return return
} }
if v := b.val; v.Kind() == reflect.PtrKind { if v := b.val; v.Kind() == reflect.ArrayKind {
pv := v.(reflect.PtrValue); av := v.(reflect.ArrayValue);
psubtype := pv.Type().(reflect.PtrType).Sub(); if av.IsSlice() && av.IsNil() {
if pv.Get() == nil && psubtype.Kind() == reflect.ArrayKind { av.Set(reflect.NewSliceValue(av.Type().(reflect.ArrayType), 0, 8));
av := reflect.NewSliceValue(psubtype.(reflect.ArrayType), 0, 8);
pv.SetSub(av);
} }
} }
} }
@ -133,38 +131,28 @@ func (b *_StructBuilder) Elem(i int) Builder {
return nobuilder return nobuilder
} }
v := b.val; v := b.val;
if v.Kind() == reflect.PtrKind { if v.Kind() != reflect.ArrayKind {
// If we have a pointer to an array, allocate or grow return nobuilder
// the array as necessary. Then set v to the array itself.
pv := v.(reflect.PtrValue);
psub := pv.Sub();
if psub.Kind() == reflect.ArrayKind {
av := psub.(reflect.ArrayValue);
if i > av.Cap() {
n := av.Cap();
if n < 8 {
n = 8
}
for n <= i {
n *= 2
}
av1 := reflect.NewSliceValue(av.Type().(reflect.ArrayType), av.Len(), n);
av1.CopyFrom(av, av.Len());
pv.SetSub(av1);
av = av1;
}
}
v = psub;
} }
if v.Kind() == reflect.ArrayKind { av := v.(reflect.ArrayValue);
// Array was grown above, or is fixed size. if av.IsSlice() && i > av.Cap() {
av := v.(reflect.ArrayValue); n := av.Cap();
if av.Len() <= i && i < av.Cap() { if n < 8 {
av.SetLen(i+1); n = 8
} }
if i < av.Len() { for n <= i {
return &_StructBuilder{ av.Elem(i) } n *= 2
} }
av1 := reflect.NewSliceValue(av.Type().(reflect.ArrayType), av.Len(), n);
av1.CopyFrom(av, av.Len());
av.Set(av1);
}
// Array was grown above, or is fixed size.
if av.Len() <= i && i < av.Cap() {
av.SetLen(i+1);
}
if i < av.Len() {
return &_StructBuilder{ av.Elem(i) }
} }
return nobuilder return nobuilder
} }

View File

@ -26,7 +26,7 @@ type _MyStruct struct {
fl float; fl float;
fl32 float32; fl32 float32;
fl64 float64; fl64 float64;
a *[]string; // TODO(rsc): Should be able to use []string. a []string;
my *_MyStruct; my *_MyStruct;
}; };