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