Try to fix a potential set fault on some compiler

Merge the fix from https://github.com/miloyip/itoa-benchmark/issues/8
This commit is contained in:
miloyip 2015-04-11 12:10:44 +08:00
parent cb59a5a9a2
commit e346b93325

View File

@ -109,12 +109,13 @@ inline char* u32toa(uint32_t value, char* buffer) {
} }
inline char* i32toa(int32_t value, char* buffer) { inline char* i32toa(int32_t value, char* buffer) {
uint32_t u = static_cast<uint32_t>(value);
if (value < 0) { if (value < 0) {
*buffer++ = '-'; *buffer++ = '-';
value = -value; u = ~u + 1;
} }
return u32toa(static_cast<uint32_t>(value), buffer); return u32toa(u, buffer);
} }
inline char* u64toa(uint64_t value, char* buffer) { inline char* u64toa(uint64_t value, char* buffer) {
@ -286,12 +287,13 @@ inline char* u64toa(uint64_t value, char* buffer) {
} }
inline char* i64toa(int64_t value, char* buffer) { inline char* i64toa(int64_t value, char* buffer) {
uint64_t u = static_cast<uint64_t>(value);
if (value < 0) { if (value < 0) {
*buffer++ = '-'; *buffer++ = '-';
value = -value; u = ~u + 1;
} }
return u64toa(static_cast<uint64_t>(value), buffer); return u64toa(u, buffer);
} }
} // namespace internal } // namespace internal