#include <limits> // std::numeric_limits
#include <algorithm> // std::reverse
namespace cppx {
using std::numeric_limits;
using std::reverse;
typedef numeric_limits<long> Long_info;
int const long_digits = Long_info::max_digits10;
int const long_bufsize = long_digits + 2;
inline void unsigned_to_decimal( unsigned long number, char* buffer )
{
if( number == 0 )
{
*buffer++ = '0';
}
else
{
char* p_first = buffer;
while( number != 0 )
{
*buffer++ = '0' + number % 10;
number /= 10;
}
reverse( p_first, buffer );
}
*buffer = '\0';
}
inline auto decimal_from_unsigned( unsigned long number, char* buffer )
-> char const*
{
unsigned_to_decimal( number, buffer );
return buffer;
}
inline void to_decimal( long number, char* buffer )
{
if( number < 0 )
{
buffer[0] = '-';
unsigned_to_decimal( -number, buffer + 1 );
}
else
{
unsigned_to_decimal( number, buffer );
}
}
inline auto decimal_from( long number, char* buffer )
-> char const*
{
to_decimal( number, buffer );
return buffer;
}
} // namespace cppx
#include <iostream>
using namespace std;
auto main() -> int
{
using cppx::decimal_from;
using cppx::long_bufsize;
using cppx::Long_info;
char spec[long_bufsize];
cout << decimal_from( Long_info::min(), spec ) << endl;
cout << decimal_from( Long_info::min() + 1, spec ) << endl;
cout << decimal_from( 0, spec ) << endl;
cout << decimal_from( Long_info::max() - 1, spec ) << endl;
cout << decimal_from( Long_info::max(), spec ) << endl;
}
I2luY2x1ZGUgPGxpbWl0cz4gICAgICAgLy8gc3RkOjpudW1lcmljX2xpbWl0cwojaW5jbHVkZSA8YWxnb3JpdGhtPiAgICAvLyBzdGQ6OnJldmVyc2UKCm5hbWVzcGFjZSBjcHB4IHsKICAgIHVzaW5nIHN0ZDo6bnVtZXJpY19saW1pdHM7CiAgICB1c2luZyBzdGQ6OnJldmVyc2U7CiAgICAKICAgIHR5cGVkZWYgbnVtZXJpY19saW1pdHM8bG9uZz4gICAgTG9uZ19pbmZvOwogICAgaW50IGNvbnN0IGxvbmdfZGlnaXRzICAgPSBMb25nX2luZm86Om1heF9kaWdpdHMxMDsKICAgIGludCBjb25zdCBsb25nX2J1ZnNpemUgID0gbG9uZ19kaWdpdHMgKyAyOwoKICAgIGlubGluZSB2b2lkIHVuc2lnbmVkX3RvX2RlY2ltYWwoIHVuc2lnbmVkIGxvbmcgbnVtYmVyLCBjaGFyKiBidWZmZXIgKQogICAgewogICAgICAgIGlmKCBudW1iZXIgPT0gMCApCiAgICAgICAgewogICAgICAgICAgICAqYnVmZmVyKysgPSAnMCc7CiAgICAgICAgfQogICAgICAgIGVsc2UKICAgICAgICB7CiAgICAgICAgICAgIGNoYXIqIHBfZmlyc3QgPSBidWZmZXI7CiAgICAgICAgICAgIHdoaWxlKCBudW1iZXIgIT0gMCApCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICpidWZmZXIrKyA9ICcwJyArIG51bWJlciAlIDEwOwogICAgICAgICAgICAgICAgbnVtYmVyIC89IDEwOwogICAgICAgICAgICB9CiAgICAgICAgICAgIHJldmVyc2UoIHBfZmlyc3QsIGJ1ZmZlciApOwogICAgICAgIH0KICAgICAgICAqYnVmZmVyID0gJ1wwJzsKICAgIH0KCiAgICBpbmxpbmUgYXV0byBkZWNpbWFsX2Zyb21fdW5zaWduZWQoIHVuc2lnbmVkIGxvbmcgbnVtYmVyLCBjaGFyKiBidWZmZXIgKQogICAgICAgIC0+IGNoYXIgY29uc3QqCiAgICB7CiAgICAgICAgdW5zaWduZWRfdG9fZGVjaW1hbCggbnVtYmVyLCBidWZmZXIgKTsKICAgICAgICByZXR1cm4gYnVmZmVyOwogICAgfQoKICAgIGlubGluZSB2b2lkIHRvX2RlY2ltYWwoIGxvbmcgbnVtYmVyLCBjaGFyKiBidWZmZXIgKQogICAgewogICAgICAgIGlmKCBudW1iZXIgPCAwICkKICAgICAgICB7CiAgICAgICAgICAgIGJ1ZmZlclswXSA9ICctJzsKICAgICAgICAgICAgdW5zaWduZWRfdG9fZGVjaW1hbCggLW51bWJlciwgYnVmZmVyICsgMSApOwogICAgICAgIH0KICAgICAgICBlbHNlCiAgICAgICAgewogICAgICAgICAgICB1bnN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