From e346b933250fd14e9463ae8c542576222973b659 Mon Sep 17 00:00:00 2001 From: miloyip Date: Sat, 11 Apr 2015 12:10:44 +0800 Subject: [PATCH] Try to fix a potential set fault on some compiler Merge the fix from https://github.com/miloyip/itoa-benchmark/issues/8 --- include/rapidjson/internal/itoa.h | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/include/rapidjson/internal/itoa.h b/include/rapidjson/internal/itoa.h index a28001f..9ecf630 100644 --- a/include/rapidjson/internal/itoa.h +++ b/include/rapidjson/internal/itoa.h @@ -109,12 +109,13 @@ inline char* u32toa(uint32_t value, char* buffer) { } inline char* i32toa(int32_t value, char* buffer) { + uint32_t u = static_cast(value); if (value < 0) { *buffer++ = '-'; - value = -value; + u = ~u + 1; } - return u32toa(static_cast(value), buffer); + return u32toa(u, 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) { + uint64_t u = static_cast(value); if (value < 0) { *buffer++ = '-'; - value = -value; + u = ~u + 1; } - return u64toa(static_cast(value), buffer); + return u64toa(u, buffer); } } // namespace internal