diff options
author | Ondřej Surý <ondrej@sury.org> | 2011-08-03 16:54:30 +0200 |
---|---|---|
committer | Ondřej Surý <ondrej@sury.org> | 2011-08-03 16:54:30 +0200 |
commit | 28592ee1ea1f5cdffcf85472f9de0285d928cf12 (patch) | |
tree | 32944e18b23f7fe4a0818a694aa2a6dfb1835463 /src/pkg/big | |
parent | e836bee4716dc0d4d913537ad3ad1925a7ac32d0 (diff) | |
download | golang-upstream/59.tar.gz |
Imported Upstream version 59upstream/59
Diffstat (limited to 'src/pkg/big')
-rwxr-xr-x | src/pkg/big/int.go | 64 | ||||
-rwxr-xr-x | src/pkg/big/int_test.go | 31 | ||||
-rwxr-xr-x | src/pkg/big/nat.go | 4 | ||||
-rw-r--r-- | src/pkg/big/rat.go | 10 |
4 files changed, 94 insertions, 15 deletions
diff --git a/src/pkg/big/int.go b/src/pkg/big/int.go index 22bdf8d2f..0948919cd 100755 --- a/src/pkg/big/int.go +++ b/src/pkg/big/int.go @@ -368,11 +368,60 @@ func (x *Int) Format(s fmt.State, ch int) { format = "0X%s" } } - if x.neg { - format = "-" + format + t := fmt.Sprintf(format, x.abs.string(cs)) + + // insert spaces in hexadecimal formats if needed + if len(t) > 0 && s.Flag(' ') && (ch == 'x' || ch == 'X') { + spaces := (len(t)+1)/2 - 1 + spaced := make([]byte, len(t)+spaces) + var i, j int + spaced[i] = t[j] + i++ + j++ + if len(t)&1 == 0 { + spaced[i] = t[j] + i++ + j++ + } + for j < len(t) { + spaced[i] = ' ' + i++ + spaced[i] = t[j] + i++ + j++ + spaced[i] = t[j] + i++ + j++ + } + t = string(spaced) + } + + // determine sign prefix + prefix := "" + switch { + case x.neg: + prefix = "-" + case s.Flag('+'): + prefix = "+" + case s.Flag(' ') && ch != 'x' && ch != 'X': + prefix = " " + } + + // fill to minimum width and prepend sign prefix + if width, ok := s.Width(); ok && len(t)+len(prefix) < width { + if s.Flag('0') { + t = fmt.Sprintf("%s%0*d%s", prefix, width-len(t)-len(prefix), 0, t) + } else { + if s.Flag('-') { + width = -width + } + t = fmt.Sprintf("%*s", width, prefix+t) + } + } else if prefix != "" { + t = prefix + t } - fmt.Fprintf(s, format, x.abs.string(cs)) + fmt.Fprint(s, t) } @@ -417,6 +466,7 @@ func (z *Int) scan(r io.RuneScanner, base int) (*Int, int, os.Error) { // the scanned number. It accepts the formats 'b' (binary), 'o' (octal), // 'd' (decimal), 'x' (lowercase hexadecimal), and 'X' (uppercase hexadecimal). func (z *Int) Scan(s fmt.ScanState, ch int) os.Error { + s.SkipSpace() // skip leading space characters base := 0 switch ch { case 'b': @@ -430,7 +480,7 @@ func (z *Int) Scan(s fmt.ScanState, ch int) os.Error { case 's', 'v': // let scan determine the base default: - return os.ErrorString("Int.Scan: invalid verb") + return os.NewError("Int.Scan: invalid verb") } _, _, err := z.scan(s, base) return err @@ -585,7 +635,7 @@ func ProbablyPrime(z *Int, n int) bool { } -// Rand sets z to a pseudo-random number in [0, n) and returns z. +// Rand sets z to a pseudo-random number in [0, n) and returns z. func (z *Int) Rand(rnd *rand.Rand, n *Int) *Int { z.neg = false if n.neg == true || len(n.abs) == 0 { @@ -834,11 +884,11 @@ func (z *Int) GobEncode() ([]byte, os.Error) { // GobDecode implements the gob.GobDecoder interface. func (z *Int) GobDecode(buf []byte) os.Error { if len(buf) == 0 { - return os.ErrorString("Int.GobDecode: no data") + return os.NewError("Int.GobDecode: no data") } b := buf[0] if b>>1 != intGobVersion { - return os.ErrorString(fmt.Sprintf("Int.GobDecode: encoding version %d not supported", b>>1)) + return os.NewError(fmt.Sprintf("Int.GobDecode: encoding version %d not supported", b>>1)) } z.neg = b&1 != 0 z.abs = z.abs.setBytes(buf[1:]) diff --git a/src/pkg/big/int_test.go b/src/pkg/big/int_test.go index 58a55030d..7f33c9522 100755 --- a/src/pkg/big/int_test.go +++ b/src/pkg/big/int_test.go @@ -376,6 +376,35 @@ var formatTests = []struct { {"-10", "%#X", "-0XA"}, {"10", "%#y", "%!y(big.Int=10)"}, {"-10", "%#y", "%!y(big.Int=-10)"}, + + {"1234", "%d", "1234"}, + {"1234", "%3d", "1234"}, + {"1234", "%4d", "1234"}, + {"-1234", "%d", "-1234"}, + {"1234", "% 5d", " 1234"}, + {"1234", "%+5d", "+1234"}, + {"1234", "%-5d", "1234 "}, + {"1234", "%x", "4d2"}, + {"1234", "%X", "4D2"}, + {"1234", "% x", "4 d2"}, + {"-1234", "%3x", "-4d2"}, + {"-1234", "%4x", "-4d2"}, + {"-1234", "%5x", " -4d2"}, + {"-1234", "%-5x", "-4d2 "}, + {"-1234", "% x", "-4 d2"}, + {"1234", "%03d", "1234"}, + {"1234", "%04d", "1234"}, + {"1234", "%05d", "01234"}, + {"1234", "%06d", "001234"}, + {"-1234", "%06d", "-01234"}, + {"1234", "%+06d", "+01234"}, + {"1234", "% 06d", " 01234"}, + {"1234", "%-6d", "1234 "}, + {"1234", "%-06d", "001234"}, + {"-1234", "%-06d", "-01234"}, + {"10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", // 10**100 + "% x", + "12 49 ad 25 94 c3 7c eb 0b 27 84 c4 ce 0b f3 8a ce 40 8e 21 1a 7c aa b2 43 08 a8 2e 8f 10 00 00 00 00 00 00 00 00 00 00 00 00"}, } @@ -391,7 +420,7 @@ func TestFormat(t *testing.T) { } output := fmt.Sprintf(test.format, x) if output != test.output { - t.Errorf("#%d got %s; want %s", i, output, test.output) + t.Errorf("#%d got %q; want %q", i, output, test.output) } } } diff --git a/src/pkg/big/nat.go b/src/pkg/big/nat.go index 734568e06..6755832be 100755 --- a/src/pkg/big/nat.go +++ b/src/pkg/big/nat.go @@ -644,7 +644,7 @@ func hexValue(ch int) Word { func (z nat) scan(r io.RuneScanner, base int) (nat, int, os.Error) { // reject illegal bases if base < 0 || base == 1 || MaxBase < base { - return z, 0, os.ErrorString("illegal number base") + return z, 0, os.NewError("illegal number base") } // one char look-ahead @@ -721,7 +721,7 @@ func (z nat) scan(r io.RuneScanner, base int) (nat, int, os.Error) { return z, 10, nil case base != 0 || b != 8: // there was neither a mantissa digit nor the octal prefix 0 - return z, int(b), os.ErrorString("syntax error scanning number") + return z, int(b), os.NewError("syntax error scanning number") } return z.norm(), int(b), nil diff --git a/src/pkg/big/rat.go b/src/pkg/big/rat.go index 1fbf8c459..b61cbb966 100644 --- a/src/pkg/big/rat.go +++ b/src/pkg/big/rat.go @@ -227,10 +227,10 @@ func (z *Rat) Scan(s fmt.ScanState, ch int) os.Error { return err } if strings.IndexRune("efgEFGv", ch) < 0 { - return os.ErrorString("Rat.Scan: invalid verb") + return os.NewError("Rat.Scan: invalid verb") } if _, ok := z.SetString(string(tok)); !ok { - return os.ErrorString("Rat.Scan: invalid syntax") + return os.NewError("Rat.Scan: invalid syntax") } return nil } @@ -368,7 +368,7 @@ func (z *Rat) GobEncode() ([]byte, os.Error) { n := i - j if int(uint32(n)) != n { // this should never happen - return nil, os.ErrorString("Rat.GobEncode: numerator too large") + return nil, os.NewError("Rat.GobEncode: numerator too large") } binary.BigEndian.PutUint32(buf[j-4:j], uint32(n)) j -= 1 + 4 @@ -384,11 +384,11 @@ func (z *Rat) GobEncode() ([]byte, os.Error) { // GobDecode implements the gob.GobDecoder interface. func (z *Rat) GobDecode(buf []byte) os.Error { if len(buf) == 0 { - return os.ErrorString("Rat.GobDecode: no data") + return os.NewError("Rat.GobDecode: no data") } b := buf[0] if b>>1 != ratGobVersion { - return os.ErrorString(fmt.Sprintf("Rat.GobDecode: encoding version %d not supported", b>>1)) + return os.NewError(fmt.Sprintf("Rat.GobDecode: encoding version %d not supported", b>>1)) } const j = 1 + 4 i := j + binary.BigEndian.Uint32(buf[j-4:j]) |