| 2113 | } |
| 2114 | |
| 2115 | void APInt::fromString(unsigned numbits, StringRef str, uint8_t radix) { |
| 2116 | // Check our assumptions here |
| 2117 | assert(!str.empty() && "Invalid string length"); |
| 2118 | assert((radix == 10 || radix == 8 || radix == 16 || radix == 2 || |
| 2119 | radix == 36) && |
| 2120 | "Radix should be 2, 8, 10, 16, or 36!"); |
| 2121 | |
| 2122 | StringRef::iterator p = str.begin(); |
| 2123 | size_t slen = str.size(); |
| 2124 | bool isNeg = *p == '-'; |
| 2125 | if (*p == '-' || *p == '+') { |
| 2126 | p++; |
| 2127 | slen--; |
| 2128 | assert(slen && "String is only a sign, needs a value."); |
| 2129 | } |
| 2130 | assert((slen <= numbits || radix != 2) && "Insufficient bit width"); |
| 2131 | assert(((slen-1)*3 <= numbits || radix != 8) && "Insufficient bit width"); |
| 2132 | assert(((slen-1)*4 <= numbits || radix != 16) && "Insufficient bit width"); |
| 2133 | assert((((slen-1)*64)/22 <= numbits || radix != 10) && |
| 2134 | "Insufficient bit width"); |
| 2135 | |
| 2136 | // Allocate memory if needed |
| 2137 | if (isSingleWord()) |
| 2138 | U.VAL = 0; |
| 2139 | else |
| 2140 | U.pVal = getClearedMemory(getNumWords()); |
| 2141 | |
| 2142 | // Figure out if we can shift instead of multiply |
| 2143 | unsigned shift = (radix == 16 ? 4 : radix == 8 ? 3 : radix == 2 ? 1 : 0); |
| 2144 | |
| 2145 | // Enter digit traversal loop |
| 2146 | for (StringRef::iterator e = str.end(); p != e; ++p) { |
| 2147 | unsigned digit = getDigit(*p, radix); |
| 2148 | assert(digit < radix && "Invalid character in digit string"); |
| 2149 | |
| 2150 | // Shift or multiply the value by the radix |
| 2151 | if (slen > 1) { |
| 2152 | if (shift) |
| 2153 | *this <<= shift; |
| 2154 | else |
| 2155 | *this *= radix; |
| 2156 | } |
| 2157 | |
| 2158 | // Add in the digit we just interpreted |
| 2159 | *this += digit; |
| 2160 | } |
| 2161 | // If its negative, put it in two's complement form |
| 2162 | if (isNeg) |
| 2163 | this->negate(); |
| 2164 | } |
| 2165 | |
| 2166 | void APInt::toString(SmallVectorImpl<char> &Str, unsigned Radix, |
| 2167 | bool Signed, bool formatAsCLiteral) const { |
nothing calls this directly
no test coverage detected