X-Git-Url: https://mattmccutchen.net/bigint/bigint.git/blobdiff_plain/3e1327901d299a537a8d932c49dd330f87ac3bda..cb2f0c288d4b7acfa37d7a9c8bc1024c3f332b5f:/BigIntegerUtils.cc diff --git a/BigIntegerUtils.cc b/BigIntegerUtils.cc index 3ac75dc..44073af 100644 --- a/BigIntegerUtils.cc +++ b/BigIntegerUtils.cc @@ -1,33 +1,27 @@ #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) { +std::string bigIntegerToString(const BigInteger &x) { return (x.getSign() == BigInteger::negative) - ? (std::string("-") + easyBUtoString(x.getMagnitude())) - : (easyBUtoString(x.getMagnitude())); + ? (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(); @@ -47,8 +41,7 @@ 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 << '-';