| 3250 | */ |
| 3251 | template<class string_type> |
| 3252 | void ToStringBase(string_type & result, uint b = 10, bool negative = false) const |
| 3253 | { |
| 3254 | UInt<value_size> temp(*this); |
| 3255 | uint rest, table_id, index, digits; |
| 3256 | double digits_d; |
| 3257 | char character; |
| 3258 | |
| 3259 | result.clear(); |
| 3260 | |
| 3261 | if( b<2 || b>16 ) |
| 3262 | return; |
| 3263 | |
| 3264 | if( !FindLeadingBit(table_id, index) ) |
| 3265 | { |
| 3266 | result = '0'; |
| 3267 | return; |
| 3268 | } |
| 3269 | |
| 3270 | if( negative ) |
| 3271 | result = '-'; |
| 3272 | |
| 3273 | digits_d = table_id; // for not making an overflow in uint type |
| 3274 | digits_d *= TTMATH_BITS_PER_UINT; |
| 3275 | digits_d += index + 1; |
| 3276 | digits_d *= ToStringLog2(b); |
| 3277 | digits = static_cast<uint>(digits_d) + 3; // plus some epsilon |
| 3278 | |
| 3279 | if( result.capacity() < digits ) |
| 3280 | result.reserve(digits); |
| 3281 | |
| 3282 | do |
| 3283 | { |
| 3284 | temp.DivInt(b, &rest); |
| 3285 | character = static_cast<char>(Misc::DigitToChar(rest)); |
| 3286 | result.insert(result.end(), character); |
| 3287 | } |
| 3288 | while( !temp.IsZero() ); |
| 3289 | |
| 3290 | size_t i1 = negative ? 1 : 0; // the first is a hyphen (when negative is true) |
| 3291 | size_t i2 = result.size() - 1; |
| 3292 | |
| 3293 | for( ; i1 < i2 ; ++i1, --i2 ) |
| 3294 | { |
| 3295 | char tempc = static_cast<char>(result[i1]); |
| 3296 | result[i1] = result[i2]; |
| 3297 | result[i2] = tempc; |
| 3298 | } |
| 3299 | } |
| 3300 | |
| 3301 | |
| 3302 | |