diff options
Diffstat (limited to 'base/string_util.cc')
-rw-r--r-- | base/string_util.cc | 20 |
1 files changed, 19 insertions, 1 deletions
diff --git a/base/string_util.cc b/base/string_util.cc index 5990ef4..a66dae6 100644 --- a/base/string_util.cc +++ b/base/string_util.cc @@ -1182,6 +1182,24 @@ struct IntToStringT { } }; + // This set of templates is very similar to the above templates, but + // for testing whether an integer is negative. + template <typename INT2, bool NEG2> + struct TestNegT { }; + template <typename INT2> + struct TestNegT<INT2, false> { + static bool TestNeg(INT2 value) { + // value is unsigned, and can never be negative. + return false; + } + }; + template <typename INT2> + struct TestNegT<INT2, true> { + static bool TestNeg(INT2 value) { + return value < 0; + } + }; + static STR IntToString(INT value) { // log10(2) ~= 0.3 bytes needed per bit or per byte log10(2**8) ~= 2.4. // So round up to allocate 3 output characters per byte, plus 1 for '-'. @@ -1191,7 +1209,7 @@ struct IntToStringT { // then return the substr of what we ended up using. STR outbuf(kOutputBufSize, 0); - bool is_neg = value < 0; + bool is_neg = TestNegT<INT, NEG>::TestNeg(value); // Even though is_neg will never be true when INT is parameterized as // unsigned, even the presence of the unary operation causes a warning. UINT res = ToUnsignedT<INT, UINT, NEG>::ToUnsigned(value); |