- Fix a sign-checking bug in the BigInteger(..., Sign) constructors.
[bigint/bigint.git] / BigIntegerUtils.cc
index 636a4d7..44073af 100644 (file)
@@ -1,37 +1,27 @@
-/*
-* Matt McCutchen's Big Integer Library
-* http://hashproduct.metaesthetics.net/bigint/
-*/
-
 #include "BigIntegerUtils.hh"
 #include "BigUnsignedInABase.hh"
 
-/*
-* This file includes:
-* (1) `std::string <=> BigUnsigned/BigInteger' conversion routines easier than `BigUnsignedInABase'
-* (2) << and >> operators for BigUnsigned/BigInteger, std::istream/std::ostream
-*/
-
-std::string easyBUtoString(const BigUnsigned &x) {
+std::string bigUnsignedToString(const BigUnsigned &x) {
        return std::string(BigUnsignedInABase(x, 10));
 }
 
-std::string easyBItoString(const BigInteger &x) {
-       return (x.getSign() == BigInteger::negative) ?
-               (std::string("-") + easyBUtoString(x)) : (easyBUtoString(x));
+std::string bigIntegerToString(const BigInteger &x) {
+       return (x.getSign() == BigInteger::negative)
+               ? (std::string("-") + bigUnsignedToString(x.getMagnitude()))
+               : (bigUnsignedToString(x.getMagnitude()));
 }
 
-BigUnsigned easyStringToBU(const std::string &s) {
+BigUnsigned stringToBigUnsigned(const std::string &s) {
        return BigUnsigned(BigUnsignedInABase(s, 10));
 }
 
-BigInteger easyStringToBI(const std::string &s) {
-       return (s[0] == '-') ?
-               BigInteger(easyStringToBU(s.substr(1, s.length() - 1)), BigInteger::negative) :
-               BigInteger(easyStringToBU(s));
+BigInteger stringToBigInteger(const std::string &s) {
+       // Recognize a sign followed by a BigUnsigned.
+       return (s[0] == '-') ? BigInteger(stringToBigUnsigned(s.substr(1, s.length() - 1)), BigInteger::negative)
+               : (s[0] == '+') ? BigInteger(stringToBigUnsigned(s.substr(1, s.length() - 1)))
+               : BigInteger(stringToBigUnsigned(s));
 }
 
-// Outputs x to os, obeying the flags `dec', `hex', `bin', and `showbase'.
 std::ostream &operator <<(std::ostream &os, const BigUnsigned &x) {
        BigUnsignedInABase::Base base;
        long osFlags = os.flags();
@@ -51,11 +41,10 @@ std::ostream &operator <<(std::ostream &os, const BigUnsigned &x) {
        os << s;
        return os;
 }
-// Outputs x to os, obeying the flags `dec', `hex', `bin', and `showbase'.
-// My somewhat arbitrary policy: a negative sign comes before a base indicator (like -0xFF).
+
 std::ostream &operator <<(std::ostream &os, const BigInteger &x) {
        if (x.getSign() == BigInteger::negative)
                os << '-';
-       os << (const BigUnsigned &)(x);
+       os << x.getMagnitude();
        return os;
 }