X-Git-Url: https://mattmccutchen.net/bigint/bigint.git/blobdiff_plain/05780f4b578d6ae054be0b19b8498d32a4f16c60..c17afa550d0a00498301454195699120e2bf58c5:/BigUnsignedInABase.cc diff --git a/BigUnsignedInABase.cc b/BigUnsignedInABase.cc index b668c58..999faaf 100644 --- a/BigUnsignedInABase.cc +++ b/BigUnsignedInABase.cc @@ -1,10 +1,20 @@ -/* -* Matt McCutchen's Big Integer Library -* http://mysite.verizon.net/mccutchen/bigint/ -*/ - #include "BigUnsignedInABase.hh" +BigUnsignedInABase::BigUnsignedInABase(const Digit *d, Index l, Base base) + : NumberlikeArray(d, l), base(base) { + // Check the base + if (base < 2) + throw "BigUnsignedInABase::BigUnsignedInABase(const Digit *, Index, Base): The base must be at least 2"; + + // Validate the digits. + for (Index i = 0; i < l; i++) + if (blk[i] >= base) + throw "BigUnsignedInABase::BigUnsignedInABase(const Digit *, Index, Base): A digit is too large for the specified base"; + + // Eliminate any leading zeros we may have been passed. + zapLeadingZeros(); +} + namespace { unsigned int bitLen(unsigned int x) { unsigned int len = 0; @@ -23,30 +33,29 @@ BigUnsignedInABase::BigUnsignedInABase(const BigUnsigned &x, Base base) { // Check the base if (base < 2) throw "BigUnsignedInABase(BigUnsigned, Base): The base must be at least 2"; - // Save the base. - // This pattern is seldom seen in C++, but the analogous ``this.'' is common in Java. 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'. + allocate(len); // Get the space + BigUnsigned x2(x), buBase(base); Index digitNum = 0; - + while (!x2.isZero()) { // Get last digit. This is like `lastDigit = x2 % buBase, x2 /= buBase'. BigUnsigned lastDigit(x2); lastDigit.divideWithRemainder(buBase, x2); // Save the digit. - blk[digitNum] = Digit(lastDigit); // invokes `BigUnsigned ==> unsigned short' converter + blk[digitNum] = lastDigit.toUnsignedShort(); // Move on. We can't run out of room: we figured it out above. digitNum++; } - - // Save the eventual length. + + // Save the actual length. len = digitNum; } @@ -68,10 +77,12 @@ BigUnsignedInABase::BigUnsignedInABase(const std::string &s, Base base) { // Save the 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; for (digitNum = 0; digitNum < len; digitNum++) { symbolNumInString = len - 1 - digitNum; @@ -84,6 +95,9 @@ BigUnsignedInABase::BigUnsignedInABase(const std::string &s, Base base) { blk[digitNum] = theSymbol - 'a' + 10; else throw "BigUnsignedInABase(std::string, Base): Bad symbol in input. Only 0-9, A-Z, a-z are accepted."; + + if (blk[digitNum] >= base) + throw "BigUnsignedInABase::BigUnsignedInABase(const Digit *, Index, Base): A digit is too large for the specified base"; } zapLeadingZeros(); } @@ -93,16 +107,19 @@ BigUnsignedInABase::operator std::string() const { throw "BigUnsignedInABase ==> std::string: The default string conversion routines use the symbol set 0-9, A-Z and therefore support only up to base 36. You tried a conversion with a base over 36; write your own string conversion routine."; if (len == 0) return std::string("0"); - std::string s; - s.reserve(len); + // Some compilers don't have push_back, so use a char * buffer instead. + char *s = new char[len + 1]; + s[len] = '\0'; Index digitNum, symbolNumInString; for (symbolNumInString = 0; symbolNumInString < len; symbolNumInString++) { digitNum = len - 1 - symbolNumInString; Digit theDigit = blk[digitNum]; if (theDigit < 10) - s.push_back(char('0' + theDigit)); + s[symbolNumInString] = char('0' + theDigit); else - s.push_back(char('A' + theDigit - 10)); + s[symbolNumInString] = char('A' + theDigit - 10); } - return s; + std::string s2(s); + delete [] s; + return s2; }