~john-koepi/ubuntu/trusty/golang/default

« back to all changes in this revision

Viewing changes to src/pkg/big/int.go

  • Committer: Bazaar Package Importer
  • Author(s): Ondřej Surý
  • Date: 2011-08-03 17:04:59 UTC
  • mfrom: (14.1.2 sid)
  • Revision ID: james.westby@ubuntu.com-20110803170459-wzd99m3567y80ila
Tags: 1:59-1
* Imported Upstream version 59
* Refresh patches to a new release
* Fix FTBFS on ARM (Closes: #634270)
* Update version.bash to work with Debian packaging and not hg
  repository

Show diffs side-by-side

added added

removed removed

Lines of Context:
368
368
                        format = "0X%s"
369
369
                }
370
370
        }
371
 
        if x.neg {
372
 
                format = "-" + format
373
 
        }
374
 
 
375
 
        fmt.Fprintf(s, format, x.abs.string(cs))
 
371
        t := fmt.Sprintf(format, x.abs.string(cs))
 
372
 
 
373
        // insert spaces in hexadecimal formats if needed
 
374
        if len(t) > 0 && s.Flag(' ') && (ch == 'x' || ch == 'X') {
 
375
                spaces := (len(t)+1)/2 - 1
 
376
                spaced := make([]byte, len(t)+spaces)
 
377
                var i, j int
 
378
                spaced[i] = t[j]
 
379
                i++
 
380
                j++
 
381
                if len(t)&1 == 0 {
 
382
                        spaced[i] = t[j]
 
383
                        i++
 
384
                        j++
 
385
                }
 
386
                for j < len(t) {
 
387
                        spaced[i] = ' '
 
388
                        i++
 
389
                        spaced[i] = t[j]
 
390
                        i++
 
391
                        j++
 
392
                        spaced[i] = t[j]
 
393
                        i++
 
394
                        j++
 
395
                }
 
396
                t = string(spaced)
 
397
        }
 
398
 
 
399
        // determine sign prefix
 
400
        prefix := ""
 
401
        switch {
 
402
        case x.neg:
 
403
                prefix = "-"
 
404
        case s.Flag('+'):
 
405
                prefix = "+"
 
406
        case s.Flag(' ') && ch != 'x' && ch != 'X':
 
407
                prefix = " "
 
408
        }
 
409
 
 
410
        // fill to minimum width and prepend sign prefix
 
411
        if width, ok := s.Width(); ok && len(t)+len(prefix) < width {
 
412
                if s.Flag('0') {
 
413
                        t = fmt.Sprintf("%s%0*d%s", prefix, width-len(t)-len(prefix), 0, t)
 
414
                } else {
 
415
                        if s.Flag('-') {
 
416
                                width = -width
 
417
                        }
 
418
                        t = fmt.Sprintf("%*s", width, prefix+t)
 
419
                }
 
420
        } else if prefix != "" {
 
421
                t = prefix + t
 
422
        }
 
423
 
 
424
        fmt.Fprint(s, t)
376
425
}
377
426
 
378
427
 
417
466
// the scanned number. It accepts the formats 'b' (binary), 'o' (octal),
418
467
// 'd' (decimal), 'x' (lowercase hexadecimal), and 'X' (uppercase hexadecimal).
419
468
func (z *Int) Scan(s fmt.ScanState, ch int) os.Error {
 
469
        s.SkipSpace() // skip leading space characters
420
470
        base := 0
421
471
        switch ch {
422
472
        case 'b':
430
480
        case 's', 'v':
431
481
                // let scan determine the base
432
482
        default:
433
 
                return os.ErrorString("Int.Scan: invalid verb")
 
483
                return os.NewError("Int.Scan: invalid verb")
434
484
        }
435
485
        _, _, err := z.scan(s, base)
436
486
        return err
585
635
}
586
636
 
587
637
 
588
 
// Rand sets z to a pseudo-random number in [0, n) and returns z. 
 
638
// Rand sets z to a pseudo-random number in [0, n) and returns z.
589
639
func (z *Int) Rand(rnd *rand.Rand, n *Int) *Int {
590
640
        z.neg = false
591
641
        if n.neg == true || len(n.abs) == 0 {
834
884
// GobDecode implements the gob.GobDecoder interface.
835
885
func (z *Int) GobDecode(buf []byte) os.Error {
836
886
        if len(buf) == 0 {
837
 
                return os.ErrorString("Int.GobDecode: no data")
 
887
                return os.NewError("Int.GobDecode: no data")
838
888
        }
839
889
        b := buf[0]
840
890
        if b>>1 != intGobVersion {
841
 
                return os.ErrorString(fmt.Sprintf("Int.GobDecode: encoding version %d not supported", b>>1))
 
891
                return os.NewError(fmt.Sprintf("Int.GobDecode: encoding version %d not supported", b>>1))
842
892
        }
843
893
        z.neg = b&1 != 0
844
894
        z.abs = z.abs.setBytes(buf[1:])