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

Delta Between Two Patch Sets: src/pkg/fmt/format.go

Issue 110040043: code review 110040043: [release-branch.go1.3] fmt: fix signs when zero padding. (Closed)
Left Patch Set: Created 10 years, 9 months ago
Right Patch Set: diff -r 7ec493c7f2bf https://code.google.com/p/go Created 10 years, 9 months 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:
Right: Side by side diff | Download
« no previous file with change/comment | « src/pkg/fmt/fmt_test.go ('k') | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
(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 "math" 8 "math"
9 "strconv" 9 "strconv"
10 "unicode/utf8" 10 "unicode/utf8"
(...skipping 350 matching lines...) Expand 10 before | Expand all | Expand 10 after
361 361
362 // formatFloat formats a float64; it is an efficient equivalent to f.pad(strcon v.FormatFloat()...). 362 // formatFloat formats a float64; it is an efficient equivalent to f.pad(strcon v.FormatFloat()...).
363 func (f *fmt) formatFloat(v float64, verb byte, prec, n int) { 363 func (f *fmt) formatFloat(v float64, verb byte, prec, n int) {
364 // Format number, reserving space for leading + sign if needed. 364 // Format number, reserving space for leading + sign if needed.
365 num := strconv.AppendFloat(f.intbuf[0:1], v, verb, prec, n) 365 num := strconv.AppendFloat(f.intbuf[0:1], v, verb, prec, n)
366 if num[1] == '-' || num[1] == '+' { 366 if num[1] == '-' || num[1] == '+' {
367 num = num[1:] 367 num = num[1:]
368 } else { 368 } else {
369 num[0] = '+' 369 num[0] = '+'
370 } 370 }
371 // Special handling for infinity, which doesn't look like a number so sh ouldn't be padded with zeros.
372 if math.IsInf(v, 0) {
373 if f.zero {
374 defer func() { f.zero = true }()
375 f.zero = false
376 }
377 }
371 // num is now a signed version of the number. 378 // num is now a signed version of the number.
372 // If we're zero padding, want the sign before the leading zeros. 379 // If we're zero padding, want the sign before the leading zeros.
373 // Achieve this by writing the sign out and then padding the unsigned nu mber. 380 // Achieve this by writing the sign out and then padding the unsigned nu mber.
374 if f.zero && f.widPresent && f.wid > len(num) { 381 if f.zero && f.widPresent && f.wid > len(num) {
375 » » f.buf.WriteByte(num[0]) 382 » » if f.space && v >= 0 {
376 » » f.wid-- 383 » » » f.buf.WriteByte(' ') // This is what C does: even with z ero, f.space means space.
384 » » » f.wid--
385 » » } else if f.plus || v < 0 {
386 » » » f.buf.WriteByte(num[0])
387 » » » f.wid--
388 » » }
377 f.pad(num[1:]) 389 f.pad(num[1:])
378 f.wid++ // Restore width; complex numbers will reuse this value for imaginary part.
379 return 390 return
380 } 391 }
381 // f.space says to replace a leading + with a space. 392 // f.space says to replace a leading + with a space.
382 if f.space && num[0] == '+' { 393 if f.space && num[0] == '+' {
383 num[0] = ' ' 394 num[0] = ' '
384 f.pad(num) 395 f.pad(num)
385 return 396 return
386 } 397 }
387 // Now we know the sign is attached directly to the number, if present a t all. 398 // Now we know the sign is attached directly to the number, if present a t all.
388 // We want a sign if asked for, if it's negative, or if it's infinity (+ Inf vs. -Inf). 399 // We want a sign if asked for, if it's negative, or if it's infinity (+ Inf vs. -Inf).
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
429 func (f *fmt) fmt_g32(v float32) { f.formatFloat(float64(v), 'g', doPrec(f, -1), 32) } 440 func (f *fmt) fmt_g32(v float32) { f.formatFloat(float64(v), 'g', doPrec(f, -1), 32) }
430 441
431 // fmt_G32 formats a float32 in the 'f' or 'E' form according to size. 442 // fmt_G32 formats a float32 in the 'f' or 'E' form according to size.
432 func (f *fmt) fmt_G32(v float32) { f.formatFloat(float64(v), 'G', doPrec(f, -1), 32) } 443 func (f *fmt) fmt_G32(v float32) { f.formatFloat(float64(v), 'G', doPrec(f, -1), 32) }
433 444
434 // fmt_fb32 formats a float32 in the form -123p3 (exponent is power of 2). 445 // fmt_fb32 formats a float32 in the form -123p3 (exponent is power of 2).
435 func (f *fmt) fmt_fb32(v float32) { f.formatFloat(float64(v), 'b', 0, 32) } 446 func (f *fmt) fmt_fb32(v float32) { f.formatFloat(float64(v), 'b', 0, 32) }
436 447
437 // fmt_c64 formats a complex64 according to the verb. 448 // fmt_c64 formats a complex64 according to the verb.
438 func (f *fmt) fmt_c64(v complex64, verb rune) { 449 func (f *fmt) fmt_c64(v complex64, verb rune) {
450 f.fmt_complex(float64(real(v)), float64(imag(v)), 32, verb)
451 }
452
453 // fmt_c128 formats a complex128 according to the verb.
454 func (f *fmt) fmt_c128(v complex128, verb rune) {
455 f.fmt_complex(real(v), imag(v), 64, verb)
456 }
457
458 // fmt_complex formats a complex number as (r+ji).
459 func (f *fmt) fmt_complex(r, j float64, size int, verb rune) {
439 f.buf.WriteByte('(') 460 f.buf.WriteByte('(')
440 r := real(v)
441 oldPlus := f.plus 461 oldPlus := f.plus
462 oldSpace := f.space
463 oldWid := f.wid
442 for i := 0; ; i++ { 464 for i := 0; ; i++ {
443 switch verb { 465 switch verb {
444 case 'b': 466 case 'b':
445 » » » f.fmt_fb32(r) 467 » » » f.formatFloat(r, 'b', 0, size)
446 case 'e': 468 case 'e':
447 » » » f.fmt_e32(r) 469 » » » f.formatFloat(r, 'e', doPrec(f, 6), size)
448 case 'E': 470 case 'E':
449 » » » f.fmt_E32(r) 471 » » » f.formatFloat(r, 'E', doPrec(f, 6), size)
450 case 'f', 'F': 472 case 'f', 'F':
451 » » » f.fmt_f32(r) 473 » » » f.formatFloat(r, 'f', doPrec(f, 6), size)
452 case 'g': 474 case 'g':
453 » » » f.fmt_g32(r) 475 » » » f.formatFloat(r, 'g', doPrec(f, -1), size)
454 case 'G': 476 case 'G':
455 » » » f.fmt_G32(r) 477 » » » f.formatFloat(r, 'G', doPrec(f, -1), size)
456 } 478 }
457 if i != 0 { 479 if i != 0 {
458 break 480 break
459 } 481 }
482 // Imaginary part always has a sign.
460 f.plus = true 483 f.plus = true
461 » » r = imag(v) 484 » » f.space = false
462 » } 485 » » f.wid = oldWid
486 » » r = j
487 » }
488 » f.space = oldSpace
463 f.plus = oldPlus 489 f.plus = oldPlus
490 f.wid = oldWid
464 f.buf.Write(irparenBytes) 491 f.buf.Write(irparenBytes)
465 } 492 }
466
467 // fmt_c128 formats a complex128 according to the verb.
468 func (f *fmt) fmt_c128(v complex128, verb rune) {
469 f.buf.WriteByte('(')
470 r := real(v)
471 oldPlus := f.plus
472 for i := 0; ; i++ {
473 switch verb {
474 case 'b':
475 f.fmt_fb64(r)
476 case 'e':
477 f.fmt_e64(r)
478 case 'E':
479 f.fmt_E64(r)
480 case 'f', 'F':
481 f.fmt_f64(r)
482 case 'g':
483 f.fmt_g64(r)
484 case 'G':
485 f.fmt_G64(r)
486 }
487 if i != 0 {
488 break
489 }
490 f.plus = true
491 r = imag(v)
492 }
493 f.plus = oldPlus
494 f.buf.Write(irparenBytes)
495 }
LEFTRIGHT

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