Negation was not a good way to handle negative integers; fix #3729

There's a number that negation does not work (-2147483648 in 32bit
environment).
This commit is contained in:
Yukihiro "Matz" Matsumoto
2017-07-05 18:13:37 +09:00
parent b643a1a8bc
commit b68e7a605c
+5 -6
View File
@@ -860,11 +860,11 @@ retry:
else {
sc = '-';
width--;
v = -v;
}
mrb_assert(base == 10);
snprintf(nbuf, sizeof(nbuf), "%" MRB_PRId, v);
s = nbuf;
if (v < 0) s++; /* skip minus sign */
}
else {
s = nbuf;
@@ -974,13 +974,12 @@ retry:
if (prec > len) {
CHECK(prec - len);
if (v < 0) {
char c = sign_bits(base, p);
FILL(c, prec - len);
}
else if ((flags & (FMINUS|FPREC)) != FMINUS) {
if ((flags & (FMINUS|FPREC)) != FMINUS) {
char c = '0';
FILL(c, prec - len);
} else if (v < 0) {
char c = sign_bits(base, p);
FILL(c, prec - len);
}
}
PUSH(s, len);