aboutsummaryrefslogtreecommitdiff
path: root/libgo/go/fmt
diff options
context:
space:
mode:
authorIan Lance Taylor <ian@gcc.gnu.org>2011-05-20 00:18:15 +0000
committerIan Lance Taylor <ian@gcc.gnu.org>2011-05-20 00:18:15 +0000
commit9ff56c9570642711d5b7ab29920ecf5dbff14a27 (patch)
treec891bdec1e6f073f73fedeef23718bc3ac30d499 /libgo/go/fmt
parent37cb25ed7acdb844b218231130e54b8b7a0ff6e6 (diff)
downloadgcc-9ff56c9570642711d5b7ab29920ecf5dbff14a27.zip
gcc-9ff56c9570642711d5b7ab29920ecf5dbff14a27.tar.gz
gcc-9ff56c9570642711d5b7ab29920ecf5dbff14a27.tar.bz2
Update to current version of Go library.
From-SVN: r173931
Diffstat (limited to 'libgo/go/fmt')
-rw-r--r--libgo/go/fmt/doc.go2
-rw-r--r--libgo/go/fmt/fmt_test.go1
-rw-r--r--libgo/go/fmt/print.go115
-rw-r--r--libgo/go/fmt/scan.go40
-rw-r--r--libgo/go/fmt/scan_test.go14
5 files changed, 85 insertions, 87 deletions
diff --git a/libgo/go/fmt/doc.go b/libgo/go/fmt/doc.go
index 77ee62b..e4d4f18 100644
--- a/libgo/go/fmt/doc.go
+++ b/libgo/go/fmt/doc.go
@@ -27,7 +27,7 @@
%o base 8
%x base 16, with lower-case letters for a-f
%X base 16, with upper-case letters for A-F
- %U Unicode format: U+1234; same as "U+%x" with 4 digits default
+ %U Unicode format: U+1234; same as "U+%0.4X"
Floating-point and complex constituents:
%b decimalless scientific notation with exponent a power
of two, in the manner of strconv.Ftoa32, e.g. -123456p-78
diff --git a/libgo/go/fmt/fmt_test.go b/libgo/go/fmt/fmt_test.go
index 3766c838..b3c0c5a 100644
--- a/libgo/go/fmt/fmt_test.go
+++ b/libgo/go/fmt/fmt_test.go
@@ -170,6 +170,7 @@ var fmttests = []struct {
// unicode format
{"%U", 0x1, "U+0001"},
+ {"%U", uint(0x1), "U+0001"},
{"%.8U", 0x2, "U+00000002"},
{"%U", 0x1234, "U+1234"},
{"%U", 0x12345, "U+12345"},
diff --git a/libgo/go/fmt/print.go b/libgo/go/fmt/print.go
index 4b68051..10e0fe7 100644
--- a/libgo/go/fmt/print.go
+++ b/libgo/go/fmt/print.go
@@ -256,11 +256,11 @@ func Sprintln(a ...interface{}) string {
// Get the i'th arg of the struct value.
// If the arg itself is an interface, return a value for
// the thing inside the interface, not the interface itself.
-func getField(v *reflect.StructValue, i int) reflect.Value {
+func getField(v reflect.Value, i int) reflect.Value {
val := v.Field(i)
- if i, ok := val.(*reflect.InterfaceValue); ok {
+ if i := val; i.Kind() == reflect.Interface {
if inter := i.Interface(); inter != nil {
- return reflect.NewValue(inter)
+ return reflect.ValueOf(inter)
}
}
return val
@@ -278,18 +278,13 @@ func parsenum(s string, start, end int) (num int, isnum bool, newi int) {
return
}
-// Reflection values like reflect.FuncValue implement this method. We use it for %p.
-type uintptrGetter interface {
- Get() uintptr
-}
-
func (p *pp) unknownType(v interface{}) {
if v == nil {
p.buf.Write(nilAngleBytes)
return
}
p.buf.WriteByte('?')
- p.buf.WriteString(reflect.Typeof(v).String())
+ p.buf.WriteString(reflect.TypeOf(v).String())
p.buf.WriteByte('?')
}
@@ -301,7 +296,7 @@ func (p *pp) badVerb(verb int, val interface{}) {
if val == nil {
p.buf.Write(nilAngleBytes)
} else {
- p.buf.WriteString(reflect.Typeof(val).String())
+ p.buf.WriteString(reflect.TypeOf(val).String())
p.add('=')
p.printField(val, 'v', false, false, 0)
}
@@ -394,6 +389,8 @@ func (p *pp) fmtUint64(v uint64, verb int, goSyntax bool, value interface{}) {
p.fmt.integer(int64(v), 16, unsigned, ldigits)
case 'X':
p.fmt.integer(int64(v), 16, unsigned, udigits)
+ case 'U':
+ p.fmtUnicode(int64(v))
default:
p.badVerb(verb, value)
}
@@ -521,16 +518,16 @@ func (p *pp) fmtBytes(v []byte, verb int, goSyntax bool, depth int, value interf
func (p *pp) fmtPointer(field interface{}, value reflect.Value, verb int, goSyntax bool) {
var u uintptr
- switch value.(type) {
- case *reflect.ChanValue, *reflect.FuncValue, *reflect.MapValue, *reflect.PtrValue, *reflect.SliceValue, *reflect.UnsafePointerValue:
- u = value.(uintptrGetter).Get()
+ switch value.Kind() {
+ case reflect.Chan, reflect.Func, reflect.Map, reflect.Ptr, reflect.Slice, reflect.UnsafePointer:
+ u = value.Pointer()
default:
p.badVerb(verb, field)
return
}
if goSyntax {
p.add('(')
- p.buf.WriteString(reflect.Typeof(field).String())
+ p.buf.WriteString(reflect.TypeOf(field).String())
p.add(')')
p.add('(')
if u == 0 {
@@ -545,10 +542,10 @@ func (p *pp) fmtPointer(field interface{}, value reflect.Value, verb int, goSynt
}
var (
- intBits = reflect.Typeof(0).Bits()
- floatBits = reflect.Typeof(0.0).Bits()
- complexBits = reflect.Typeof(1i).Bits()
- uintptrBits = reflect.Typeof(uintptr(0)).Bits()
+ intBits = reflect.TypeOf(0).Bits()
+ floatBits = reflect.TypeOf(0.0).Bits()
+ complexBits = reflect.TypeOf(1i).Bits()
+ uintptrBits = reflect.TypeOf(uintptr(0)).Bits()
)
func (p *pp) printField(field interface{}, verb int, plus, goSyntax bool, depth int) (wasString bool) {
@@ -565,10 +562,10 @@ func (p *pp) printField(field interface{}, verb int, plus, goSyntax bool, depth
// %T (the value's type) and %p (its address) are special; we always do them first.
switch verb {
case 'T':
- p.printField(reflect.Typeof(field).String(), 's', false, false, 0)
+ p.printField(reflect.TypeOf(field).String(), 's', false, false, 0)
return false
case 'p':
- p.fmtPointer(field, reflect.NewValue(field), verb, goSyntax)
+ p.fmtPointer(field, reflect.ValueOf(field), verb, goSyntax)
return false
}
// Is it a Formatter?
@@ -656,38 +653,38 @@ func (p *pp) printField(field interface{}, verb int, plus, goSyntax bool, depth
}
// Need to use reflection
- value := reflect.NewValue(field)
+ value := reflect.ValueOf(field)
BigSwitch:
- switch f := value.(type) {
- case *reflect.BoolValue:
- p.fmtBool(f.Get(), verb, field)
- case *reflect.IntValue:
- p.fmtInt64(f.Get(), verb, field)
- case *reflect.UintValue:
- p.fmtUint64(uint64(f.Get()), verb, goSyntax, field)
- case *reflect.FloatValue:
+ switch f := value; f.Kind() {
+ case reflect.Bool:
+ p.fmtBool(f.Bool(), verb, field)
+ case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
+ p.fmtInt64(f.Int(), verb, field)
+ case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
+ p.fmtUint64(uint64(f.Uint()), verb, goSyntax, field)
+ case reflect.Float32, reflect.Float64:
if f.Type().Size() == 4 {
- p.fmtFloat32(float32(f.Get()), verb, field)
+ p.fmtFloat32(float32(f.Float()), verb, field)
} else {
- p.fmtFloat64(float64(f.Get()), verb, field)
+ p.fmtFloat64(float64(f.Float()), verb, field)
}
- case *reflect.ComplexValue:
+ case reflect.Complex64, reflect.Complex128:
if f.Type().Size() == 8 {
- p.fmtComplex64(complex64(f.Get()), verb, field)
+ p.fmtComplex64(complex64(f.Complex()), verb, field)
} else {
- p.fmtComplex128(complex128(f.Get()), verb, field)
+ p.fmtComplex128(complex128(f.Complex()), verb, field)
}
- case *reflect.StringValue:
- p.fmtString(f.Get(), verb, goSyntax, field)
- case *reflect.MapValue:
+ case reflect.String:
+ p.fmtString(f.String(), verb, goSyntax, field)
+ case reflect.Map:
if goSyntax {
p.buf.WriteString(f.Type().String())
p.buf.WriteByte('{')
} else {
p.buf.Write(mapBytes)
}
- keys := f.Keys()
+ keys := f.MapKeys()
for i, key := range keys {
if i > 0 {
if goSyntax {
@@ -698,20 +695,20 @@ BigSwitch:
}
p.printField(key.Interface(), verb, plus, goSyntax, depth+1)
p.buf.WriteByte(':')
- p.printField(f.Elem(key).Interface(), verb, plus, goSyntax, depth+1)
+ p.printField(f.MapIndex(key).Interface(), verb, plus, goSyntax, depth+1)
}
if goSyntax {
p.buf.WriteByte('}')
} else {
p.buf.WriteByte(']')
}
- case *reflect.StructValue:
+ case reflect.Struct:
if goSyntax {
- p.buf.WriteString(reflect.Typeof(field).String())
+ p.buf.WriteString(reflect.TypeOf(field).String())
}
p.add('{')
v := f
- t := v.Type().(*reflect.StructType)
+ t := v.Type()
for i := 0; i < v.NumField(); i++ {
if i > 0 {
if goSyntax {
@@ -729,11 +726,11 @@ BigSwitch:
p.printField(getField(v, i).Interface(), verb, plus, goSyntax, depth+1)
}
p.buf.WriteByte('}')
- case *reflect.InterfaceValue:
+ case reflect.Interface:
value := f.Elem()
- if value == nil {
+ if !value.IsValid() {
if goSyntax {
- p.buf.WriteString(reflect.Typeof(field).String())
+ p.buf.WriteString(reflect.TypeOf(field).String())
p.buf.Write(nilParenBytes)
} else {
p.buf.Write(nilAngleBytes)
@@ -741,9 +738,9 @@ BigSwitch:
} else {
return p.printField(value.Interface(), verb, plus, goSyntax, depth+1)
}
- case reflect.ArrayOrSliceValue:
+ case reflect.Array, reflect.Slice:
// Byte slices are special.
- if f.Type().(reflect.ArrayOrSliceType).Elem().Kind() == reflect.Uint8 {
+ if f.Type().Elem().Kind() == reflect.Uint8 {
// We know it's a slice of bytes, but we also know it does not have static type
// []byte, or it would have been caught above. Therefore we cannot convert
// it directly in the (slightly) obvious way: f.Interface().([]byte); it doesn't have
@@ -753,13 +750,13 @@ BigSwitch:
// if reflection could help a little more.
bytes := make([]byte, f.Len())
for i := range bytes {
- bytes[i] = byte(f.Elem(i).(*reflect.UintValue).Get())
+ bytes[i] = byte(f.Index(i).Uint())
}
p.fmtBytes(bytes, verb, goSyntax, depth, field)
return verb == 's'
}
if goSyntax {
- p.buf.WriteString(reflect.Typeof(field).String())
+ p.buf.WriteString(reflect.TypeOf(field).String())
p.buf.WriteByte('{')
} else {
p.buf.WriteByte('[')
@@ -772,24 +769,24 @@ BigSwitch:
p.buf.WriteByte(' ')
}
}
- p.printField(f.Elem(i).Interface(), verb, plus, goSyntax, depth+1)
+ p.printField(f.Index(i).Interface(), verb, plus, goSyntax, depth+1)
}
if goSyntax {
p.buf.WriteByte('}')
} else {
p.buf.WriteByte(']')
}
- case *reflect.PtrValue:
- v := f.Get()
+ case reflect.Ptr:
+ v := f.Pointer()
// pointer to array or slice or struct? ok at top level
// but not embedded (avoid loops)
if v != 0 && depth == 0 {
- switch a := f.Elem().(type) {
- case reflect.ArrayOrSliceValue:
+ switch a := f.Elem(); a.Kind() {
+ case reflect.Array, reflect.Slice:
p.buf.WriteByte('&')
p.printField(a.Interface(), verb, plus, goSyntax, depth+1)
break BigSwitch
- case *reflect.StructValue:
+ case reflect.Struct:
p.buf.WriteByte('&')
p.printField(a.Interface(), verb, plus, goSyntax, depth+1)
break BigSwitch
@@ -797,7 +794,7 @@ BigSwitch:
}
if goSyntax {
p.buf.WriteByte('(')
- p.buf.WriteString(reflect.Typeof(field).String())
+ p.buf.WriteString(reflect.TypeOf(field).String())
p.buf.WriteByte(')')
p.buf.WriteByte('(')
if v == 0 {
@@ -813,7 +810,7 @@ BigSwitch:
break
}
p.fmt0x64(uint64(v), true)
- case *reflect.ChanValue, *reflect.FuncValue, *reflect.UnsafePointerValue:
+ case reflect.Chan, reflect.Func, reflect.UnsafePointer:
p.fmtPointer(field, value, verb, goSyntax)
default:
p.unknownType(f)
@@ -918,7 +915,7 @@ func (p *pp) doPrintf(format string, a []interface{}) {
for ; fieldnum < len(a); fieldnum++ {
field := a[fieldnum]
if field != nil {
- p.buf.WriteString(reflect.Typeof(field).String())
+ p.buf.WriteString(reflect.TypeOf(field).String())
p.buf.WriteByte('=')
}
p.printField(field, 'v', false, false, 0)
@@ -937,7 +934,7 @@ func (p *pp) doPrint(a []interface{}, addspace, addnewline bool) {
// always add spaces if we're doing println
field := a[fieldnum]
if fieldnum > 0 {
- isString := field != nil && reflect.Typeof(field).Kind() == reflect.String
+ isString := field != nil && reflect.TypeOf(field).Kind() == reflect.String
if addspace || !isString && !prevString {
p.buf.WriteByte(' ')
}
diff --git a/libgo/go/fmt/scan.go b/libgo/go/fmt/scan.go
index 36271a8..42bc52c 100644
--- a/libgo/go/fmt/scan.go
+++ b/libgo/go/fmt/scan.go
@@ -423,7 +423,7 @@ func (s *ss) token(skipSpace bool, f func(int) bool) []byte {
// typeError indicates that the type of the operand did not match the format
func (s *ss) typeError(field interface{}, expected string) {
- s.errorString("expected field of type pointer to " + expected + "; found " + reflect.Typeof(field).String())
+ s.errorString("expected field of type pointer to " + expected + "; found " + reflect.TypeOf(field).String())
}
var complexError = os.ErrorString("syntax error scanning complex number")
@@ -908,37 +908,37 @@ func (s *ss) scanOne(verb int, field interface{}) {
// If we scanned to bytes, the slice would point at the buffer.
*v = []byte(s.convertString(verb))
default:
- val := reflect.NewValue(v)
- ptr, ok := val.(*reflect.PtrValue)
- if !ok {
+ val := reflect.ValueOf(v)
+ ptr := val
+ if ptr.Kind() != reflect.Ptr {
s.errorString("Scan: type not a pointer: " + val.Type().String())
return
}
- switch v := ptr.Elem().(type) {
- case *reflect.BoolValue:
- v.Set(s.scanBool(verb))
- case *reflect.IntValue:
- v.Set(s.scanInt(verb, v.Type().Bits()))
- case *reflect.UintValue:
- v.Set(s.scanUint(verb, v.Type().Bits()))
- case *reflect.StringValue:
- v.Set(s.convertString(verb))
- case *reflect.SliceValue:
+ switch v := ptr.Elem(); v.Kind() {
+ case reflect.Bool:
+ v.SetBool(s.scanBool(verb))
+ case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
+ v.SetInt(s.scanInt(verb, v.Type().Bits()))
+ case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
+ v.SetUint(s.scanUint(verb, v.Type().Bits()))
+ case reflect.String:
+ v.SetString(s.convertString(verb))
+ case reflect.Slice:
// For now, can only handle (renamed) []byte.
- typ := v.Type().(*reflect.SliceType)
+ typ := v.Type()
if typ.Elem().Kind() != reflect.Uint8 {
goto CantHandle
}
str := s.convertString(verb)
v.Set(reflect.MakeSlice(typ, len(str), len(str)))
for i := 0; i < len(str); i++ {
- v.Elem(i).(*reflect.UintValue).Set(uint64(str[i]))
+ v.Index(i).SetUint(uint64(str[i]))
}
- case *reflect.FloatValue:
+ case reflect.Float32, reflect.Float64:
s.skipSpace(false)
- v.Set(s.convertFloat(s.floatToken(), v.Type().Bits()))
- case *reflect.ComplexValue:
- v.Set(s.scanComplex(verb, v.Type().Bits()))
+ v.SetFloat(s.convertFloat(s.floatToken(), v.Type().Bits()))
+ case reflect.Complex64, reflect.Complex128:
+ v.SetComplex(s.scanComplex(verb, v.Type().Bits()))
default:
CantHandle:
s.errorString("Scan: can't handle type: " + val.Type().String())
diff --git a/libgo/go/fmt/scan_test.go b/libgo/go/fmt/scan_test.go
index 8eb3e5b..da13eb2 100644
--- a/libgo/go/fmt/scan_test.go
+++ b/libgo/go/fmt/scan_test.go
@@ -370,8 +370,8 @@ func testScan(name string, t *testing.T, scan func(r io.Reader, a ...interface{}
continue
}
// The incoming value may be a pointer
- v := reflect.NewValue(test.in)
- if p, ok := v.(*reflect.PtrValue); ok {
+ v := reflect.ValueOf(test.in)
+ if p := v; p.Kind() == reflect.Ptr {
v = p.Elem()
}
val := v.Interface()
@@ -409,8 +409,8 @@ func TestScanf(t *testing.T) {
continue
}
// The incoming value may be a pointer
- v := reflect.NewValue(test.in)
- if p, ok := v.(*reflect.PtrValue); ok {
+ v := reflect.ValueOf(test.in)
+ if p := v; p.Kind() == reflect.Ptr {
v = p.Elem()
}
val := v.Interface()
@@ -486,7 +486,7 @@ func TestInf(t *testing.T) {
}
func testScanfMulti(name string, t *testing.T) {
- sliceType := reflect.Typeof(make([]interface{}, 1)).(*reflect.SliceType)
+ sliceType := reflect.TypeOf(make([]interface{}, 1))
for _, test := range multiTests {
var r io.Reader
if name == "StringReader" {
@@ -513,8 +513,8 @@ func testScanfMulti(name string, t *testing.T) {
// Convert the slice of pointers into a slice of values
resultVal := reflect.MakeSlice(sliceType, n, n)
for i := 0; i < n; i++ {
- v := reflect.NewValue(test.in[i]).(*reflect.PtrValue).Elem()
- resultVal.Elem(i).(*reflect.InterfaceValue).Set(v)
+ v := reflect.ValueOf(test.in[i]).Elem()
+ resultVal.Index(i).Set(v)
}
result := resultVal.Interface()
if !reflect.DeepEqual(result, test.out) {