summaryrefslogtreecommitdiff
path: root/src/pkg/big/int.go
diff options
context:
space:
mode:
authorOndřej Surý <ondrej@sury.org>2011-08-03 16:54:30 +0200
committerOndřej Surý <ondrej@sury.org>2011-08-03 16:54:30 +0200
commit28592ee1ea1f5cdffcf85472f9de0285d928cf12 (patch)
tree32944e18b23f7fe4a0818a694aa2a6dfb1835463 /src/pkg/big/int.go
parente836bee4716dc0d4d913537ad3ad1925a7ac32d0 (diff)
downloadgolang-upstream/59.tar.gz
Imported Upstream version 59upstream/59
Diffstat (limited to 'src/pkg/big/int.go')
-rwxr-xr-xsrc/pkg/big/int.go64
1 files changed, 57 insertions, 7 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:])