Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(1905)

Side by Side Diff: src/pkg/fmt/print.go

Issue 201058: code review 201058: handle nils safely in Printf. (Closed)
Patch Set: code review 201058: handle nils safely in Printf. Created 14 years, 1 month ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/pkg/fmt/fmt_test.go ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 /* 5 /*
6 Package fmt implements formatted I/O with functions analogous 6 Package fmt implements formatted I/O with functions analogous
7 to C's printf. The format 'verbs' are derived from C's but 7 to C's printf. The format 'verbs' are derived from C's but
8 are simpler. 8 are simpler.
9 9
10 The verbs: 10 The verbs:
(...skipping 360 matching lines...) Expand 10 before | Expand all | Expand 10 after
371 return int64(i.Get()), false, true 371 return int64(i.Get()), false, true
372 case *reflect.Uint64Value: 372 case *reflect.Uint64Value:
373 return int64(i.Get()), false, true 373 return int64(i.Get()), false, true
374 case *reflect.UintptrValue: 374 case *reflect.UintptrValue:
375 return int64(i.Get()), false, true 375 return int64(i.Get()), false, true
376 } 376 }
377 return 377 return
378 } 378 }
379 379
380 func getString(a interface{}) (val string, ok bool) { 380 func getString(a interface{}) (val string, ok bool) {
381 if a == nil {
382 return "<nil>", ok
383 }
381 // Is it a regular string or []byte type? 384 // Is it a regular string or []byte type?
382 switch s := a.(type) { 385 switch s := a.(type) {
383 case string: 386 case string:
384 return s, true 387 return s, true
385 case []byte: 388 case []byte:
386 return string(s), true 389 return string(s), true
387 } 390 }
388 // Must be a renamed string or []byte type. 391 // Must be a renamed string or []byte type.
389 v := reflect.NewValue(a) 392 v := reflect.NewValue(a)
390 if s, ok := v.(*reflect.StringValue); ok { 393 if s, ok := v.(*reflect.StringValue); ok {
(...skipping 543 matching lines...) Expand 10 before | Expand all | Expand 10 after
934 937
935 // the value's type 938 // the value's type
936 case 'T': 939 case 'T':
937 p.buf.WriteString(reflect.Typeof(field).String()) 940 p.buf.WriteString(reflect.Typeof(field).String())
938 941
939 default: 942 default:
940 badtype: 943 badtype:
941 p.buf.WriteByte('%') 944 p.buf.WriteByte('%')
942 p.add(c) 945 p.add(c)
943 p.buf.WriteByte('(') 946 p.buf.WriteByte('(')
944 » » » p.buf.WriteString(reflect.Typeof(field).String()) 947 » » » if field != nil {
945 » » » p.buf.WriteByte('=') 948 » » » » p.buf.WriteString(reflect.Typeof(field).String() )
949 » » » » p.buf.WriteByte('=')
950 » » » }
946 p.printField(field, false, false, 0) 951 p.printField(field, false, false, 0)
947 p.buf.WriteByte(')') 952 p.buf.WriteByte(')')
948 } 953 }
949 } 954 }
950 if fieldnum < len(a) { 955 if fieldnum < len(a) {
951 p.buf.Write(extraBytes) 956 p.buf.Write(extraBytes)
952 for ; fieldnum < len(a); fieldnum++ { 957 for ; fieldnum < len(a); fieldnum++ {
953 field := a[fieldnum] 958 field := a[fieldnum]
954 p.buf.WriteString(reflect.Typeof(field).String()) 959 p.buf.WriteString(reflect.Typeof(field).String())
955 p.buf.WriteByte('=') 960 p.buf.WriteByte('=')
(...skipping 16 matching lines...) Expand all
972 if addspace || !is_string && !prev_string { 977 if addspace || !is_string && !prev_string {
973 p.buf.WriteByte(' ') 978 p.buf.WriteByte(' ')
974 } 979 }
975 } 980 }
976 prev_string = p.printField(field, false, false, 0) 981 prev_string = p.printField(field, false, false, 0)
977 } 982 }
978 if addnewline { 983 if addnewline {
979 p.buf.WriteByte('\n') 984 p.buf.WriteByte('\n')
980 } 985 }
981 } 986 }
OLDNEW
« no previous file with comments | « src/pkg/fmt/fmt_test.go ('k') | no next file » | no next file with comments »

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b