Source File
natconv.go
Belonging Package
math/big
package big
import (
)
const digits = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
const MaxBase = 10 + ('z' - 'a' + 1) + ('Z' - 'A' + 1)
const maxBaseSmall = 10 + ('z' - 'a' + 1)
*=
++
return
}
= 1
for > 0 {
if &1 != 0 {
*=
}
*=
>>= 1
}
return
}
var (
errNoDigits = errors.New("number has no digits")
errInvalSep = errors.New("'_' must separate successive digits")
)
:= '.'
:= false
, := .ReadByte()
, := , 0
switch {
case 'b', 'B':
, = 2, 'b'
case 'o', 'O':
, = 8, 'o'
case 'x', 'X':
, = 16, 'x'
default:
if ! {
, = 8, '0'
}
}
if != 0 {
= 0 // prefix is not counted
if != '0' {
, = .ReadByte()
}
}
}
}
}
var Word
switch {
case '0' <= && <= '9':
= Word( - '0')
case 'a' <= && <= 'z':
= Word( - 'a' + 10)
case 'A' <= && <= 'Z':
if <= maxBaseSmall {
= Word( - 'A' + 10)
} else {
= Word( - 'A' + maxBaseSmall)
}
default:
= MaxBase + 1
}
if >= {
.UnreadByte() // ch does not belong to number anymore
break
}
= '0'
++
= * +
++
if == nil && ( || == '_') {
= errInvalSep
}
return [:0], 10, 1,
}
= errNoDigits // fall through; result will be 0
}
= -
}
return
}
for >= {
--
[] = digits[&]
>>=
-=
}
= []
= _W
|= [] <<
--
[] = digits[&]
= [] >> ( - )
= _W - ( - )
}
}
.convertWords(, , , , )
= 0
for [] == '0' {
++
}
}
if {
--
[] = '-'
}
return [:]
}
, = .divW(, )
for := 0; < && > 0; ++ {
:= / 10
[] = '0' + byte(-*10)
=
}
}
} else {
for > 0 { // while need more leading zeros
--
[] = '0'
}
}
var leafSize int = 8 // number of Word-size binary values treat as a monolithic block
type divisor struct {
bbb nat // divisor
nbits int // bit length of divisor (discounting leading zeros) ~= log2(bbb)
ndigits int // digit length of divisor in terms of output base digits
}
var cacheBase10 struct {
sync.Mutex
table [64]divisor // cached divisors for base 10
}
:= 1
for := leafSize; < >>1 && < len(cacheBase10.table); <<= 1 {
++
}
var []divisor // for b == 10, table overlaps with cacheBase10.table
if == 10 {
cacheBase10.Lock()
= cacheBase10.table[0:] // reuse old table for this conversion
} else {
= make([]divisor, ) // create new table for this conversion
}
![]() |
The pages are generated with Golds v0.3.2-preview. (GOOS=darwin GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds. |