OLD | NEW |
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 "bytes" | 8 "bytes" |
9 "io" | 9 "io" |
10 "os" | 10 "os" |
(...skipping 679 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
690 if goSyntax { | 690 if goSyntax { |
691 p.buf.WriteString(reflect.Typeof(field).String()
) | 691 p.buf.WriteString(reflect.Typeof(field).String()
) |
692 p.buf.Write(nilParenBytes) | 692 p.buf.Write(nilParenBytes) |
693 } else { | 693 } else { |
694 p.buf.Write(nilAngleBytes) | 694 p.buf.Write(nilAngleBytes) |
695 } | 695 } |
696 } else { | 696 } else { |
697 return p.printField(value.Interface(), verb, plus, goSyn
tax, depth+1) | 697 return p.printField(value.Interface(), verb, plus, goSyn
tax, depth+1) |
698 } | 698 } |
699 case reflect.ArrayOrSliceValue: | 699 case reflect.ArrayOrSliceValue: |
| 700 // Byte slices are special. |
| 701 if f.Type().(reflect.ArrayOrSliceType).Elem().Kind() == reflect.
Uint8 { |
| 702 // We know it's a slice of bytes, but we also know it do
es not have static type |
| 703 // []byte, or it would have been caught above. Therefor
e we cannot convert |
| 704 // it directly in the (slightly) obvious way: f.Interfac
e().([]byte); it doesn't have |
| 705 // that type, and we can't write an expression of the ri
ght type and do a |
| 706 // conversion because we don't have a static way to writ
e the right type. |
| 707 // So we build a slice by hand. This is a rare case but
it would be nice |
| 708 // if reflection could help a little more. |
| 709 bytes := make([]byte, f.Len()) |
| 710 for i := range bytes { |
| 711 bytes[i] = byte(f.Elem(i).(*reflect.UintValue).G
et()) |
| 712 } |
| 713 p.fmtBytes(bytes, verb, goSyntax, depth, field) |
| 714 return verb == 's' |
| 715 } |
700 if goSyntax { | 716 if goSyntax { |
701 p.buf.WriteString(reflect.Typeof(field).String()) | 717 p.buf.WriteString(reflect.Typeof(field).String()) |
702 p.buf.WriteByte('{') | 718 p.buf.WriteByte('{') |
703 } else { | 719 } else { |
704 p.buf.WriteByte('[') | 720 p.buf.WriteByte('[') |
705 } | 721 } |
706 for i := 0; i < f.Len(); i++ { | 722 for i := 0; i < f.Len(); i++ { |
707 if i > 0 { | 723 if i > 0 { |
708 if goSyntax { | 724 if goSyntax { |
709 p.buf.Write(commaSpaceBytes) | 725 p.buf.Write(commaSpaceBytes) |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
797 // do we have 20 (width)? | 813 // do we have 20 (width)? |
798 p.fmt.wid, p.fmt.widPresent, i = parsenum(format, i, end) | 814 p.fmt.wid, p.fmt.widPresent, i = parsenum(format, i, end) |
799 // do we have .20 (precision)? | 815 // do we have .20 (precision)? |
800 if i < end && format[i] == '.' { | 816 if i < end && format[i] == '.' { |
801 p.fmt.prec, p.fmt.precPresent, i = parsenum(format, i+1,
end) | 817 p.fmt.prec, p.fmt.precPresent, i = parsenum(format, i+1,
end) |
802 } | 818 } |
803 c, w = utf8.DecodeRuneInString(format[i:]) | 819 c, w = utf8.DecodeRuneInString(format[i:]) |
804 i += w | 820 i += w |
805 // percent is special - absorbs no operand | 821 // percent is special - absorbs no operand |
806 if c == '%' { | 822 if c == '%' { |
807 » » » p.buf.WriteByte('%') // TODO: should we bother with widt
h & prec? | 823 » » » p.buf.WriteByte('%') // We ignore width and prec. |
808 continue | 824 continue |
809 } | 825 } |
810 if fieldnum >= len(a) { // out of operands | 826 if fieldnum >= len(a) { // out of operands |
811 p.buf.WriteByte('%') | 827 p.buf.WriteByte('%') |
812 p.add(c) | 828 p.add(c) |
813 p.buf.Write(missingBytes) | 829 p.buf.Write(missingBytes) |
814 continue | 830 continue |
815 } | 831 } |
816 field := a[fieldnum] | 832 field := a[fieldnum] |
817 fieldnum++ | 833 fieldnum++ |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
849 if addspace || !isString && !prevString { | 865 if addspace || !isString && !prevString { |
850 p.buf.WriteByte(' ') | 866 p.buf.WriteByte(' ') |
851 } | 867 } |
852 } | 868 } |
853 prevString = p.printField(field, 'v', false, false, 0) | 869 prevString = p.printField(field, 'v', false, false, 0) |
854 } | 870 } |
855 if addnewline { | 871 if addnewline { |
856 p.buf.WriteByte('\n') | 872 p.buf.WriteByte('\n') |
857 } | 873 } |
858 } | 874 } |
OLD | NEW |