mirror of
https://github.com/fmtlib/fmt.git
synced 2025-01-27 06:35:37 +00:00
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;
|
std::ostringstream os;
|
||||||
os << std::numeric_limits<unsigned short>::max();
|
os << std::numeric_limits<unsigned short>::max();
|
||||||
EXPECT_EQ(os.str(), FormatDec(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(-42));
|
EXPECT_EQ("-42", FormatDec(-42));
|
||||||
EXPECT_EQ("42", FormatDec(42l));
|
EXPECT_EQ("42", FormatDec(42l));
|
||||||
|
10
format.h
10
format.h
@ -1407,6 +1407,16 @@ inline void FormatDec(char *&buffer, T value) {
|
|||||||
*buffer++ = '-';
|
*buffer++ = '-';
|
||||||
abs_value = 0 - abs_value;
|
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);
|
unsigned num_digits = internal::CountDigits(abs_value);
|
||||||
internal::FormatDecimal(buffer, abs_value, num_digits);
|
internal::FormatDecimal(buffer, abs_value, num_digits);
|
||||||
buffer += num_digits;
|
buffer += num_digits;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user