Fix printInt when value=INT_MIN (was overflowing)

This commit is contained in:
Guillaume Beraudo 2014-06-19 14:47:20 +02:00
parent 129f7b7441
commit 481e445e8a

View File

@ -40,18 +40,19 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// precision: position after decimal point // precision: position after decimal point
// length: maximum number of digits including comma and decimals // length: maximum number of digits including comma and decimals
// work with negative values to prevent overflowing when taking -value
template <int length, int precision> static inline char *printInt(char *buffer, int value) template <int length, int precision> static inline char *printInt(char *buffer, int value)
{ {
bool minus = false; bool minus = true;
if (value < 0) if (value > 0)
{ {
minus = true; minus = false;
value = -value; value = -value;
} }
buffer += length - 1; buffer += length - 1;
for (int i = 0; i < precision; i++) for (int i = 0; i < precision; i++)
{ {
*buffer = '0' + (value % 10); *buffer = '0' - (value % 10);
value /= 10; value /= 10;
buffer--; buffer--;
} }
@ -59,7 +60,7 @@ template <int length, int precision> static inline char *printInt(char *buffer,
buffer--; buffer--;
for (int i = precision + 1; i < length; i++) for (int i = precision + 1; i < length; i++)
{ {
*buffer = '0' + (value % 10); *buffer = '0' - (value % 10);
value /= 10; value /= 10;
if (value == 0) if (value == 0)
break; break;