Home | History | Annotate | Download | only in bigint
      1 // Copyright 2014 PDFium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 // Original code by Matt McCutchen, see the LICENSE file.
      6 
      7 #include "BigIntegerUtils.hh"
      8 #include "BigUnsignedInABase.hh"
      9 
     10 std::string bigUnsignedToString(const BigUnsigned &x) {
     11 	return std::string(BigUnsignedInABase(x, 10));
     12 }
     13 
     14 std::string bigIntegerToString(const BigInteger &x) {
     15 	return (x.getSign() == BigInteger::negative)
     16 		? (std::string("-") + bigUnsignedToString(x.getMagnitude()))
     17 		: (bigUnsignedToString(x.getMagnitude()));
     18 }
     19 
     20 BigUnsigned stringToBigUnsigned(const std::string &s) {
     21 	return BigUnsigned(BigUnsignedInABase(s, 10));
     22 }
     23 
     24 BigInteger stringToBigInteger(const std::string &s) {
     25 	// Recognize a sign followed by a BigUnsigned.
     26 	return (s[0] == '-') ? BigInteger(stringToBigUnsigned(s.substr(1, s.length() - 1)), BigInteger::negative)
     27 		: (s[0] == '+') ? BigInteger(stringToBigUnsigned(s.substr(1, s.length() - 1)))
     28 		: BigInteger(stringToBigUnsigned(s));
     29 }
     30 
     31 std::ostream &operator <<(std::ostream &os, const BigUnsigned &x) {
     32 	BigUnsignedInABase::Base base;
     33 	long osFlags = os.flags();
     34 	if (osFlags & os.dec)
     35 		base = 10;
     36 	else if (osFlags & os.hex) {
     37 		base = 16;
     38 		if (osFlags & os.showbase)
     39 			os << "0x";
     40 	} else if (osFlags & os.oct) {
     41 		base = 8;
     42 		if (osFlags & os.showbase)
     43 			os << '0';
     44 	} else
     45         abort();
     46 
     47 	std::string s = std::string(BigUnsignedInABase(x, base));
     48 	os << s;
     49 	return os;
     50 }
     51 
     52 std::ostream &operator <<(std::ostream &os, const BigInteger &x) {
     53 	if (x.getSign() == BigInteger::negative)
     54 		os << '-';
     55 	os << x.getMagnitude();
     56 	return os;
     57 }
     58