Don't use CountDigits for 1-2 digit numbers.
This commit is contained in:
parent
7abd6443ac
commit
86574285cc
@ -1459,6 +1459,8 @@ TEST(FormatIntTest, FormatDec) {
|
||||
std::ostringstream os;
|
||||
os << std::numeric_limits<unsigned short>::max();
|
||||
EXPECT_EQ(os.str(), FormatDec(std::numeric_limits<unsigned short>::max()));
|
||||
EXPECT_EQ("1", FormatDec(1));
|
||||
EXPECT_EQ("-1", FormatDec(-1));
|
||||
EXPECT_EQ("42", FormatDec(42));
|
||||
EXPECT_EQ("-42", FormatDec(-42));
|
||||
EXPECT_EQ("42", FormatDec(42l));
|
||||
|
12
format.h
12
format.h
@ -301,7 +301,7 @@ class FormatterProxy;
|
||||
// Formats a decimal unsigned integer value writing into buffer.
|
||||
template <typename UInt, typename Char>
|
||||
void FormatDecimal(Char *buffer, UInt value, unsigned num_digits) {
|
||||
--num_digits;
|
||||
--num_digits;
|
||||
while (value >= 100) {
|
||||
// Integer division is slow so do it for a group of two digits instead
|
||||
// of for every digit. The idea comes from the talk by Alexandrescu
|
||||
@ -1407,6 +1407,16 @@ inline void FormatDec(char *&buffer, T value) {
|
||||
*buffer++ = '-';
|
||||
abs_value = 0 - abs_value;
|
||||
}
|
||||
if (abs_value < 100) {
|
||||
if (abs_value < 10) {
|
||||
*buffer++ = static_cast<char>('0' + abs_value);
|
||||
return;
|
||||
}
|
||||
unsigned index = static_cast<unsigned>(abs_value * 2);
|
||||
*buffer++ = internal::DIGITS[index];
|
||||
*buffer++ = internal::DIGITS[index + 1];
|
||||
return;
|
||||
}
|
||||
unsigned num_digits = internal::CountDigits(abs_value);
|
||||
internal::FormatDecimal(buffer, abs_value, num_digits);
|
||||
buffer += num_digits;
|
||||
|
Loading…
Reference in New Issue
Block a user