| OLD | NEW |
| 1 // Copyright 2014 PDFium Authors. All rights reserved. | 1 // Copyright 2014 PDFium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // Original code by Matt McCutchen, see the LICENSE file. | 5 // Original code by Matt McCutchen, see the LICENSE file. |
| 6 | 6 |
| 7 #include "BigUnsignedInABase.hh" | 7 #include "BigUnsignedInABase.hh" |
| 8 | 8 |
| 9 BigUnsignedInABase::BigUnsignedInABase(const Digit *d, Index l, Base base) | 9 BigUnsignedInABase::BigUnsignedInABase(const Digit *d, Index l, Base base) |
| 10 : NumberlikeArray<Digit>(d, l), base(base) { | 10 : NumberlikeArray<Digit>(d, l), base(base) { |
| 11 // Check the base | 11 // Check the base |
| 12 if (base < 2) | 12 if (base < 2) |
| 13 #ifdef FOXIT_CHROME_BUILD | |
| 14 abort(); | 13 abort(); |
| 15 #else | |
| 16 throw "BigUnsignedInABase::BigUnsignedInABase(const Digit *, Ind
ex, Base): The base must be at least 2"; | |
| 17 #endif | |
| 18 | 14 |
| 19 // Validate the digits. | 15 // Validate the digits. |
| 20 for (Index i = 0; i < l; i++) | 16 for (Index i = 0; i < l; i++) |
| 21 if (blk[i] >= base) | 17 if (blk[i] >= base) |
| 22 #ifdef FOXIT_CHROME_BUILD | |
| 23 abort(); | 18 abort(); |
| 24 #else | |
| 25 throw "BigUnsignedInABase::BigUnsignedInABase(const Digi
t *, Index, Base): A digit is too large for the specified base"; | |
| 26 #endif | |
| 27 | 19 |
| 28 // Eliminate any leading zeros we may have been passed. | 20 // Eliminate any leading zeros we may have been passed. |
| 29 zapLeadingZeros(); | 21 zapLeadingZeros(); |
| 30 } | 22 } |
| 31 | 23 |
| 32 namespace { | 24 namespace { |
| 33 unsigned int bitLen(unsigned int x) { | 25 unsigned int bitLen(unsigned int x) { |
| 34 unsigned int len = 0; | 26 unsigned int len = 0; |
| 35 while (x > 0) { | 27 while (x > 0) { |
| 36 x >>= 1; | 28 x >>= 1; |
| 37 len++; | 29 len++; |
| 38 } | 30 } |
| 39 return len; | 31 return len; |
| 40 } | 32 } |
| 41 unsigned int ceilingDiv(unsigned int a, unsigned int b) { | 33 unsigned int ceilingDiv(unsigned int a, unsigned int b) { |
| 42 return (a + b - 1) / b; | 34 return (a + b - 1) / b; |
| 43 } | 35 } |
| 44 } | 36 } |
| 45 | 37 |
| 46 BigUnsignedInABase::BigUnsignedInABase(const BigUnsigned &x, Base base) { | 38 BigUnsignedInABase::BigUnsignedInABase(const BigUnsigned &x, Base base) { |
| 47 // Check the base | 39 // Check the base |
| 48 if (base < 2) | 40 if (base < 2) |
| 49 #ifdef FOXIT_CHROME_BUILD | |
| 50 abort(); | 41 abort(); |
| 51 #else | |
| 52 throw "BigUnsignedInABase(BigUnsigned, Base): The base must be a
t least 2"; | |
| 53 #endif | |
| 54 this->base = base; | 42 this->base = base; |
| 55 | 43 |
| 56 // Get an upper bound on how much space we need | 44 // Get an upper bound on how much space we need |
| 57 int maxBitLenOfX = x.getLength() * BigUnsigned::N; | 45 int maxBitLenOfX = x.getLength() * BigUnsigned::N; |
| 58 int minBitsPerDigit = bitLen(base) - 1; | 46 int minBitsPerDigit = bitLen(base) - 1; |
| 59 int maxDigitLenOfX = ceilingDiv(maxBitLenOfX, minBitsPerDigit); | 47 int maxDigitLenOfX = ceilingDiv(maxBitLenOfX, minBitsPerDigit); |
| 60 len = maxDigitLenOfX; // Another change to comply with `staying in bound
s'. | 48 len = maxDigitLenOfX; // Another change to comply with `staying in bound
s'. |
| 61 allocate(len); // Get the space | 49 allocate(len); // Get the space |
| 62 | 50 |
| 63 BigUnsigned x2(x), buBase(base); | 51 BigUnsigned x2(x), buBase(base); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 84 digitNum--; | 72 digitNum--; |
| 85 temp.multiply(ans, buBase); | 73 temp.multiply(ans, buBase); |
| 86 ans.add(temp, BigUnsigned(blk[digitNum])); | 74 ans.add(temp, BigUnsigned(blk[digitNum])); |
| 87 } | 75 } |
| 88 return ans; | 76 return ans; |
| 89 } | 77 } |
| 90 | 78 |
| 91 BigUnsignedInABase::BigUnsignedInABase(const std::string &s, Base base) { | 79 BigUnsignedInABase::BigUnsignedInABase(const std::string &s, Base base) { |
| 92 // Check the base. | 80 // Check the base. |
| 93 if (base > 36) | 81 if (base > 36) |
| 94 #ifdef FOXIT_CHROME_BUILD | |
| 95 abort(); | 82 abort(); |
| 96 #else | |
| 97 throw "BigUnsignedInABase(std::string, Base): The default string
conversion routines use the symbol set 0-9, A-Z and therefore support only up t
o base 36. You tried a conversion with a base over 36; write your own string co
nversion routine."; | |
| 98 #endif | |
| 99 // Save the base. | 83 // Save the base. |
| 100 // This pattern is seldom seen in C++, but the analogous ``this.'' is co
mmon in Java. | 84 // This pattern is seldom seen in C++, but the analogous ``this.'' is co
mmon in Java. |
| 101 this->base = base; | 85 this->base = base; |
| 102 | 86 |
| 103 // `s.length()' is a `size_t', while `len' is a `NumberlikeArray::Index'
, | 87 // `s.length()' is a `size_t', while `len' is a `NumberlikeArray::Index'
, |
| 104 // also known as an `unsigned int'. Some compilers warn without this ca
st. | 88 // also known as an `unsigned int'. Some compilers warn without this ca
st. |
| 105 len = Index(s.length()); | 89 len = Index(s.length()); |
| 106 allocate(len); | 90 allocate(len); |
| 107 | 91 |
| 108 Index digitNum, symbolNumInString; | 92 Index digitNum, symbolNumInString; |
| 109 for (digitNum = 0; digitNum < len; digitNum++) { | 93 for (digitNum = 0; digitNum < len; digitNum++) { |
| 110 symbolNumInString = len - 1 - digitNum; | 94 symbolNumInString = len - 1 - digitNum; |
| 111 char theSymbol = s[symbolNumInString]; | 95 char theSymbol = s[symbolNumInString]; |
| 112 if (theSymbol >= '0' && theSymbol <= '9') | 96 if (theSymbol >= '0' && theSymbol <= '9') |
| 113 blk[digitNum] = theSymbol - '0'; | 97 blk[digitNum] = theSymbol - '0'; |
| 114 else if (theSymbol >= 'A' && theSymbol <= 'Z') | 98 else if (theSymbol >= 'A' && theSymbol <= 'Z') |
| 115 blk[digitNum] = theSymbol - 'A' + 10; | 99 blk[digitNum] = theSymbol - 'A' + 10; |
| 116 else if (theSymbol >= 'a' && theSymbol <= 'z') | 100 else if (theSymbol >= 'a' && theSymbol <= 'z') |
| 117 blk[digitNum] = theSymbol - 'a' + 10; | 101 blk[digitNum] = theSymbol - 'a' + 10; |
| 118 else | 102 else |
| 119 #ifdef FOXIT_CHROME_BUILD | |
| 120 abort(); | 103 abort(); |
| 121 #else | |
| 122 throw "BigUnsignedInABase(std::string, Base): Bad symbol
in input. Only 0-9, A-Z, a-z are accepted."; | |
| 123 #endif | |
| 124 | 104 |
| 125 if (blk[digitNum] >= base) | 105 if (blk[digitNum] >= base) |
| 126 #ifdef FOXIT_CHROME_BUILD | |
| 127 abort(); | 106 abort(); |
| 128 #else | |
| 129 throw "BigUnsignedInABase::BigUnsignedInABase(const Digi
t *, Index, Base): A digit is too large for the specified base"; | |
| 130 #endif | |
| 131 } | 107 } |
| 132 zapLeadingZeros(); | 108 zapLeadingZeros(); |
| 133 } | 109 } |
| 134 | 110 |
| 135 BigUnsignedInABase::operator std::string() const { | 111 BigUnsignedInABase::operator std::string() const { |
| 136 if (base > 36) | 112 if (base > 36) |
| 137 #ifdef FOXIT_CHROME_BUILD | |
| 138 abort(); | 113 abort(); |
| 139 #else | |
| 140 throw "BigUnsignedInABase ==> std::string: The default string co
nversion routines use the symbol set 0-9, A-Z and therefore support only up to b
ase 36. You tried a conversion with a base over 36; write your own string conve
rsion routine."; | |
| 141 #endif | |
| 142 if (len == 0) | 114 if (len == 0) |
| 143 return std::string("0"); | 115 return std::string("0"); |
| 144 // Some compilers don't have push_back, so use a char * buffer instead. | 116 // Some compilers don't have push_back, so use a char * buffer instead. |
| 145 char *s = new char[len + 1]; | 117 char *s = new char[len + 1]; |
| 146 s[len] = '\0'; | 118 s[len] = '\0'; |
| 147 Index digitNum, symbolNumInString; | 119 Index digitNum, symbolNumInString; |
| 148 for (symbolNumInString = 0; symbolNumInString < len; symbolNumInString++
) { | 120 for (symbolNumInString = 0; symbolNumInString < len; symbolNumInString++
) { |
| 149 digitNum = len - 1 - symbolNumInString; | 121 digitNum = len - 1 - symbolNumInString; |
| 150 Digit theDigit = blk[digitNum]; | 122 Digit theDigit = blk[digitNum]; |
| 151 if (theDigit < 10) | 123 if (theDigit < 10) |
| 152 s[symbolNumInString] = char('0' + theDigit); | 124 s[symbolNumInString] = char('0' + theDigit); |
| 153 else | 125 else |
| 154 s[symbolNumInString] = char('A' + theDigit - 10); | 126 s[symbolNumInString] = char('A' + theDigit - 10); |
| 155 } | 127 } |
| 156 std::string s2(s); | 128 std::string s2(s); |
| 157 delete [] s; | 129 delete [] s; |
| 158 return s2; | 130 return s2; |
| 159 } | 131 } |
| OLD | NEW |