func B64ToBigInt(in string, b *big.Int) (err os.Error) { bsize := base64.StdEncoding.DecodedLen(len(in)) buff := make([]byte, bsize) n, err := base64.StdEncoding.Decode(buff, bytes.NewBufferString(in).Bytes()) neg := false if err == nil { buff = buff[0:n] if buff[0]&0x80 == 0x80 { neg = true buff[0] &= 0x7f } b.SetBytes(buff) // In case the passed in big was negative... b.Abs(b) if neg { b.Neg(b) } } return }
// a.convertTo(t) converts the value of the analyzed expression a, // which must be a constant, ideal number, to a new analyzed // expression with a constant value of type t. // // TODO(austin) Rename to resolveIdeal or something? func (a *expr) convertTo(t Type) *expr { if !a.t.isIdeal() { log.Panicf("attempted to convert from %v, expected ideal", a.t) } var rat *big.Rat // XXX(Spec) The spec says "It is erroneous". // // It is an error to assign a value with a non-zero fractional // part to an integer, or if the assignment would overflow or // underflow, or in general if the value cannot be represented // by the type of the variable. switch a.t { case IdealFloatType: rat = a.asIdealFloat()() if t.isInteger() && !rat.IsInt() { a.diag("constant %v truncated to integer", rat.FloatString(6)) return nil } case IdealIntType: i := a.asIdealInt()() rat = new(big.Rat).SetInt(i) default: log.Panicf("unexpected ideal type %v", a.t) } // Check bounds if t, ok := t.lit().(BoundedType); ok { if rat.Cmp(t.minVal()) < 0 { a.diag("constant %v underflows %v", rat.FloatString(6), t) return nil } if rat.Cmp(t.maxVal()) > 0 { a.diag("constant %v overflows %v", rat.FloatString(6), t) return nil } } // Convert rat to type t. res := a.newExpr(t, a.desc) switch t := t.lit().(type) { case *uintType: n, d := rat.Num(), rat.Denom() f := new(big.Int).Quo(n, d) f = f.Abs(f) v := uint64(f.Int64()) res.eval = func(*Thread) uint64 { return v } case *intType: n, d := rat.Num(), rat.Denom() f := new(big.Int).Quo(n, d) v := f.Int64() res.eval = func(*Thread) int64 { return v } case *idealIntType: n, d := rat.Num(), rat.Denom() f := new(big.Int).Quo(n, d) res.eval = func() *big.Int { return f } case *floatType: n, d := rat.Num(), rat.Denom() v := float64(n.Int64()) / float64(d.Int64()) res.eval = func(*Thread) float64 { return v } case *idealFloatType: res.eval = func() *big.Rat { return rat } default: log.Panicf("cannot convert to type %T", t) } return res }