X-Git-Url: https://mattmccutchen.net/bigint/bigint.git/blobdiff_plain/b3fe29df9a21e6ade45c470b9b2632e9f75a7aaa..918d66f23e6b2528010db6f121e229f488fe3410:/BigUnsignedInABase.cc diff --git a/BigUnsignedInABase.cc b/BigUnsignedInABase.cc index a450cf0..762f14e 100644 --- a/BigUnsignedInABase.cc +++ b/BigUnsignedInABase.cc @@ -1,6 +1,6 @@ /* * Matt McCutchen's Big Integer Library -* http://mysite.verizon.net/mccutchen/bigint/ +* http://hashproduct.metaesthetics.net/bigint/ */ /* @@ -29,6 +29,7 @@ namespace { } BigUnsignedInABase::BigUnsignedInABase(const BigUnsigned &x, Base base) { + // Check the base if (base < 2) throw "BigUnsignedInABase(BigUnsigned, Base): The base must be at least 2"; @@ -37,10 +38,11 @@ BigUnsignedInABase::BigUnsignedInABase(const BigUnsigned &x, Base base) { this->base = base; // Get an upper bound on how much space we need - int maxBitLenOfX = x.getLength() * 8 * sizeof(BigUnsigned::Blk); + int maxBitLenOfX = x.getLength() * BigUnsigned::N; int minBitsPerDigit = bitLen(base) - 1; int maxDigitLenOfX = ceilingDiv(maxBitLenOfX, minBitsPerDigit); - allocate(maxDigitLenOfX); // Get the space + len = maxDigitLenOfX; // Another change to comply with `staying in bounds'; see `BigUnsigned::divideWithRemainder'. + allocate(len); // Get the space BigUnsigned x2(x), buBase(base); Index digitNum = 0; @@ -55,7 +57,7 @@ BigUnsignedInABase::BigUnsignedInABase(const BigUnsigned &x, Base base) { digitNum++; } - // Save the eventual length. + // Save the actual length. len = digitNum; } @@ -78,7 +80,9 @@ BigUnsignedInABase::BigUnsignedInABase(const std::string &s, Base base) { // This pattern is seldom seen in C++, but the analogous ``this.'' is common in Java. this->base = base; - len = s.length(); + // `s.length()' is a `size_t', while `len' is a `NumberlikeArray::Index', + // also known as an `unsigned int'. Some compilers warn without this cast. + len = Index(s.length()); allocate(len); Index digitNum, symbolNumInString; @@ -113,5 +117,7 @@ BigUnsignedInABase::operator std::string() const { else s[symbolNumInString] = char('A' + theDigit - 10); } - return std::string(s); + std::string s2(s); + delete s; + return s2; }