Left: | ||
Right: |
OLD | NEW |
---|---|
1 // Copyright 2010 The Go Authors. All rights reserved. | 1 // Copyright 2010 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 441 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
452 } | 452 } |
453 | 453 |
454 // Numerical elements | 454 // Numerical elements |
455 const ( | 455 const ( |
456 binaryDigits = "01" | 456 binaryDigits = "01" |
457 octalDigits = "01234567" | 457 octalDigits = "01234567" |
458 decimalDigits = "0123456789" | 458 decimalDigits = "0123456789" |
459 hexadecimalDigits = "0123456789aAbBcCdDeEfF" | 459 hexadecimalDigits = "0123456789aAbBcCdDeEfF" |
460 sign = "+-" | 460 sign = "+-" |
461 period = "." | 461 period = "." |
462 » exponent = "eE" | 462 » exponent = "eEp" |
463 ) | 463 ) |
464 | 464 |
465 // getBase returns the numeric base represented by the verb and its digit string . | 465 // getBase returns the numeric base represented by the verb and its digit string . |
466 func (s *ss) getBase(verb int) (base int, digits string) { | 466 func (s *ss) getBase(verb int) (base int, digits string) { |
467 s.okVerb(verb, "bdoUxXv", "integer") // sets s.err | 467 s.okVerb(verb, "bdoUxXv", "integer") // sets s.err |
468 base = 10 | 468 base = 10 |
469 digits = decimalDigits | 469 digits = decimalDigits |
470 switch verb { | 470 switch verb { |
471 case 'b': | 471 case 'b': |
472 base = 2 | 472 base = 2 |
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
610 s.error(complexError) | 610 s.error(complexError) |
611 } | 611 } |
612 if parens && !s.accept(")") { | 612 if parens && !s.accept(")") { |
613 s.error(complexError) | 613 s.error(complexError) |
614 } | 614 } |
615 return real, imagSign + imag | 615 return real, imagSign + imag |
616 } | 616 } |
617 | 617 |
618 // convertFloat converts the string to a float64value. | 618 // convertFloat converts the string to a float64value. |
619 func (s *ss) convertFloat(str string, n int) float64 { | 619 func (s *ss) convertFloat(str string, n int) float64 { |
620 if p := strings.Index(str, "p"); p >= 0 { | |
621 // Atof doesn't handle power-of-2 exponents, | |
622 // but they're easy to evaluate. | |
623 f, err := strconv.AtofN(str[:p], n) | |
624 if err != nil { | |
625 s.error(err) | |
rsc
2011/02/02 20:20:51
In this error and the next one, fill in the
whole
| |
626 } | |
627 n, err := strconv.Atoi64(str[p+1:]) | |
rsc
2011/02/02 20:20:51
Use Atoi and then below call math.Ldexp.
| |
628 if err != nil { | |
629 s.error(err) | |
630 } | |
631 for n >= 32 { | |
632 f *= 1 << 32 | |
633 n -= 32 | |
634 } | |
635 for n > 0 { | |
636 f *= 2 | |
637 n-- | |
638 } | |
639 for n <= -32 { | |
640 f /= 1 << 32 | |
641 n += 32 | |
642 } | |
643 for n < 0 { | |
644 f /= 2 | |
645 n++ | |
646 } | |
647 return f | |
648 } | |
620 f, err := strconv.AtofN(str, n) | 649 f, err := strconv.AtofN(str, n) |
621 if err != nil { | 650 if err != nil { |
622 s.error(err) | 651 s.error(err) |
623 } | 652 } |
624 return f | 653 return f |
625 } | 654 } |
626 | 655 |
627 // convertComplex converts the next token to a complex128 value. | 656 // convertComplex converts the next token to a complex128 value. |
628 // The atof argument is a type-specific reader for the underlying type. | 657 // The atof argument is a type-specific reader for the underlying type. |
629 // If we're reading complex64, atof will parse float32s and convert them | 658 // If we're reading complex64, atof will parse float32s and convert them |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
740 } | 769 } |
741 s.buf.WriteByte(b) | 770 s.buf.WriteByte(b) |
742 } | 771 } |
743 if s.buf.Len() == 0 { | 772 if s.buf.Len() == 0 { |
744 s.errorString("Scan: no hex data for %x string") | 773 s.errorString("Scan: no hex data for %x string") |
745 return "" | 774 return "" |
746 } | 775 } |
747 return s.buf.String() | 776 return s.buf.String() |
748 } | 777 } |
749 | 778 |
750 const floatVerbs = "eEfFgGv" | 779 const floatVerbs = "beEfFgGv" |
751 | 780 |
752 // scanOne scans a single value, deriving the scanner from the type of the argum ent. | 781 // scanOne scans a single value, deriving the scanner from the type of the argum ent. |
753 func (s *ss) scanOne(verb int, field interface{}) { | 782 func (s *ss) scanOne(verb int, field interface{}) { |
754 s.buf.Reset() | 783 s.buf.Reset() |
755 var err os.Error | 784 var err os.Error |
756 // If the parameter has its own Scan method, use that. | 785 // If the parameter has its own Scan method, use that. |
757 if v, ok := field.(Scanner); ok { | 786 if v, ok := field.(Scanner); ok { |
758 err = v.Scan(s, verb) | 787 err = v.Scan(s, verb) |
759 if err != nil { | 788 if err != nil { |
760 s.error(err) | 789 s.error(err) |
(...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
972 field := a[numProcessed] | 1001 field := a[numProcessed] |
973 | 1002 |
974 s.scanOne(c, field) | 1003 s.scanOne(c, field) |
975 numProcessed++ | 1004 numProcessed++ |
976 } | 1005 } |
977 if numProcessed < len(a) { | 1006 if numProcessed < len(a) { |
978 s.errorString("too many operands") | 1007 s.errorString("too many operands") |
979 } | 1008 } |
980 return | 1009 return |
981 } | 1010 } |
OLD | NEW |