LEFT | RIGHT |
(no file at all) | |
1 // Copyright 2009 The Go Authors. All rights reserved. | 1 // Copyright 2009 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 package fmt | 5 package fmt |
6 | 6 |
7 import ( | 7 import ( |
8 "errors" | 8 "errors" |
9 "io" | 9 "io" |
10 "os" | 10 "os" |
(...skipping 814 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
825 if value.CanInterface() { | 825 if value.CanInterface() { |
826 p.arg = value.Interface() | 826 p.arg = value.Interface() |
827 } | 827 } |
828 if isString, handled := p.handleMethods(verb, plus, goSyntax, depth); ha
ndled { | 828 if isString, handled := p.handleMethods(verb, plus, goSyntax, depth); ha
ndled { |
829 return isString | 829 return isString |
830 } | 830 } |
831 | 831 |
832 return p.printReflectValue(value, verb, plus, goSyntax, depth) | 832 return p.printReflectValue(value, verb, plus, goSyntax, depth) |
833 } | 833 } |
834 | 834 |
| 835 var byteType = reflect.TypeOf(byte(0)) |
| 836 |
835 // printReflectValue is the fallback for both printArg and printValue. | 837 // printReflectValue is the fallback for both printArg and printValue. |
836 // It uses reflect to print the value. | 838 // It uses reflect to print the value. |
837 func (p *pp) printReflectValue(value reflect.Value, verb rune, plus, goSyntax bo
ol, depth int) (wasString bool) { | 839 func (p *pp) printReflectValue(value reflect.Value, verb rune, plus, goSyntax bo
ol, depth int) (wasString bool) { |
838 oldValue := p.value | 840 oldValue := p.value |
839 p.value = value | 841 p.value = value |
840 BigSwitch: | 842 BigSwitch: |
841 switch f := value; f.Kind() { | 843 switch f := value; f.Kind() { |
842 case reflect.Bool: | 844 case reflect.Bool: |
843 p.fmtBool(f.Bool(), verb) | 845 p.fmtBool(f.Bool(), verb) |
844 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.In
t64: | 846 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.In
t64: |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
918 if goSyntax { | 920 if goSyntax { |
919 p.buf.WriteString(f.Type().String()) | 921 p.buf.WriteString(f.Type().String()) |
920 p.buf.Write(nilParenBytes) | 922 p.buf.Write(nilParenBytes) |
921 } else { | 923 } else { |
922 p.buf.Write(nilAngleBytes) | 924 p.buf.Write(nilAngleBytes) |
923 } | 925 } |
924 } else { | 926 } else { |
925 wasString = p.printValue(value, verb, plus, goSyntax, de
pth+1) | 927 wasString = p.printValue(value, verb, plus, goSyntax, de
pth+1) |
926 } | 928 } |
927 case reflect.Array, reflect.Slice: | 929 case reflect.Array, reflect.Slice: |
928 » » // Byte slices are special. | 930 » » // Byte slices are special: |
929 » » if typ := f.Type(); typ.Elem().Kind() == reflect.Uint8 { | 931 » » // - Handle []byte (== []uint8) with fmtBytes. |
| 932 » » // - Handle []T, where T is a named byte type, with fmtBytes onl
y |
| 933 » » // for the s, q, an x verbs. For other verbs, T might be a |
| 934 » » // Stringer, so we use printValue to print each element. |
| 935 » » if typ := f.Type(); typ.Elem().Kind() == reflect.Uint8 && (typ.E
lem() == byteType || verb == 's' || verb == 'q' || verb == 'x') { |
930 var bytes []byte | 936 var bytes []byte |
931 if f.Kind() == reflect.Slice { | 937 if f.Kind() == reflect.Slice { |
932 bytes = f.Bytes() | 938 bytes = f.Bytes() |
933 } else if f.CanAddr() { | 939 } else if f.CanAddr() { |
934 bytes = f.Slice(0, f.Len()).Bytes() | 940 bytes = f.Slice(0, f.Len()).Bytes() |
935 } else { | 941 } else { |
936 // We have an array, but we cannot Slice() a non
-addressable array, | 942 // We have an array, but we cannot Slice() a non
-addressable array, |
937 // so we build a slice by hand. This is a rare c
ase but it would be nice | 943 // so we build a slice by hand. This is a rare c
ase but it would be nice |
938 // if reflection could help a little more. | 944 // if reflection could help a little more. |
939 bytes = make([]byte, f.Len()) | 945 bytes = make([]byte, f.Len()) |
(...skipping 250 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1190 if addspace || !isString && !prevString { | 1196 if addspace || !isString && !prevString { |
1191 p.buf.WriteByte(' ') | 1197 p.buf.WriteByte(' ') |
1192 } | 1198 } |
1193 } | 1199 } |
1194 prevString = p.printArg(arg, 'v', false, false, 0) | 1200 prevString = p.printArg(arg, 'v', false, false, 0) |
1195 } | 1201 } |
1196 if addnewline { | 1202 if addnewline { |
1197 p.buf.WriteByte('\n') | 1203 p.buf.WriteByte('\n') |
1198 } | 1204 } |
1199 } | 1205 } |
LEFT | RIGHT |