| 2164 | } |
| 2165 | |
| 2166 | void APInt::toString(SmallVectorImpl<char> &Str, unsigned Radix, |
| 2167 | bool Signed, bool formatAsCLiteral) const { |
| 2168 | assert((Radix == 10 || Radix == 8 || Radix == 16 || Radix == 2 || |
| 2169 | Radix == 36) && |
| 2170 | "Radix should be 2, 8, 10, 16, or 36!"); |
| 2171 | |
| 2172 | const char *Prefix = ""; |
| 2173 | if (formatAsCLiteral) { |
| 2174 | switch (Radix) { |
| 2175 | case 2: |
| 2176 | // Binary literals are a non-standard extension added in gcc 4.3: |
| 2177 | // http://gcc.gnu.org/onlinedocs/gcc-4.3.0/gcc/Binary-constants.html |
| 2178 | Prefix = "0b"; |
| 2179 | break; |
| 2180 | case 8: |
| 2181 | Prefix = "0"; |
| 2182 | break; |
| 2183 | case 10: |
| 2184 | break; // No prefix |
| 2185 | case 16: |
| 2186 | Prefix = "0x"; |
| 2187 | break; |
| 2188 | default: |
| 2189 | llvm_unreachable("Invalid radix!"); |
| 2190 | } |
| 2191 | } |
| 2192 | |
| 2193 | // First, check for a zero value and just short circuit the logic below. |
| 2194 | if (*this == 0) { |
| 2195 | while (*Prefix) { |
| 2196 | Str.push_back(*Prefix); |
| 2197 | ++Prefix; |
| 2198 | }; |
| 2199 | Str.push_back('0'); |
| 2200 | return; |
| 2201 | } |
| 2202 | |
| 2203 | static const char Digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; |
| 2204 | |
| 2205 | if (isSingleWord()) { |
| 2206 | char Buffer[65]; |
| 2207 | char *BufPtr = std::end(Buffer); |
| 2208 | |
| 2209 | uint64_t N; |
| 2210 | if (!Signed) { |
| 2211 | N = getZExtValue(); |
| 2212 | } else { |
| 2213 | int64_t I = getSExtValue(); |
| 2214 | if (I >= 0) { |
| 2215 | N = I; |
| 2216 | } else { |
| 2217 | Str.push_back('-'); |
| 2218 | N = -(uint64_t)I; |
| 2219 | } |
| 2220 | } |
| 2221 | |
| 2222 | while (*Prefix) { |
| 2223 | Str.push_back(*Prefix); |
no test coverage detected