1212#include < limits>
1313
1414#include " butil/logging.h"
15+ #include " butil/numerics/safe_conversions.h" // safe_abs
1516#include " butil/scoped_clear_errno.h"
1617#include " butil/strings/utf_string_conversions.h"
1718#include " butil/third_party/dmg_fp/dmg_fp.h"
@@ -22,30 +23,6 @@ namespace {
2223
2324template <typename STR , typename INT , typename UINT , bool NEG >
2425struct IntToStringT {
25- // This is to avoid a compiler warning about unary minus on unsigned type.
26- // For example, say you had the following code:
27- // template <typename INT>
28- // INT abs(INT value) { return value < 0 ? -value : value; }
29- // Even though if INT is unsigned, it's impossible for value < 0, so the
30- // unary minus will never be taken, the compiler will still generate a
31- // warning. We do a little specialization dance...
32- template <typename INT2 , typename UINT2 , bool NEG2 >
33- struct ToUnsignedT {};
34-
35- template <typename INT2 , typename UINT2 >
36- struct ToUnsignedT <INT2 , UINT2 , false > {
37- static UINT2 ToUnsigned (INT2 value) {
38- return static_cast <UINT2 >(value);
39- }
40- };
41-
42- template <typename INT2 , typename UINT2 >
43- struct ToUnsignedT <INT2 , UINT2 , true > {
44- static UINT2 ToUnsigned (INT2 value) {
45- return static_cast <UINT2 >(value < 0 ? -value : value);
46- }
47- };
48-
4926 // This set of templates is very similar to the above templates, but
5027 // for testing whether an integer is negative.
5128 template <typename INT2 , bool NEG2 >
@@ -74,9 +51,7 @@ struct IntToStringT {
7451 STR outbuf (kOutputBufSize , 0 );
7552
7653 bool is_neg = TestNegT<INT , NEG >::TestNeg (value);
77- // Even though is_neg will never be true when INT is parameterized as
78- // unsigned, even the presence of the unary operation causes a warning.
79- UINT res = ToUnsignedT<INT , UINT , NEG >::ToUnsigned (value);
54+ UINT res = safe_abs (value);
8055
8156 typename STR ::iterator it (outbuf.end ());
8257 do {
0 commit comments