Version 2007.06.14:
[bigint/bigint.git] / BigUnsignedInABase.cc
index b668c58..4692a0a 100644 (file)
@@ -1,6 +1,14 @@
 /*
 * Matt McCutchen's Big Integer Library
-* http://mysite.verizon.net/mccutchen/bigint/
+*/
+
+/*
+* Milan Tomic had trouble compiling this file on Microsoft
+* Visual C++ 6 because, in the libraries that come with
+* Visual C++ 6, the `std::string::push_back' method apparently
+* does not exist.  To get around the problem, I rewrote
+* `BigUnsignedInABase::operator std::string' (at the bottom
+* of this file) so it doesn't use `push_back'.
 */
 
 #include "BigUnsignedInABase.hh"
@@ -20,6 +28,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";
@@ -28,10 +37,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;
@@ -46,7 +56,7 @@ BigUnsignedInABase::BigUnsignedInABase(const BigUnsigned &x, Base base) {
                digitNum++;
        }
        
-       // Save the eventual length.
+       // Save the actual length.
        len = digitNum;
 }
 
@@ -69,7 +79,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;
@@ -93,16 +105,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);
+       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);
+       // 2006.05.03: This needs to be [] to match the allocation
+       delete [] s;
+       return s2;
 }