mirror of
https://github.com/fmtlib/fmt.git
synced 2024-12-25 15:21:54 +00:00
Add FormatInt::size, and support for long and long long.
This commit is contained in:
parent
2a3e010340
commit
d473a79581
@ -1410,7 +1410,13 @@ TEST(FormatterTest, Examples) {
|
||||
|
||||
TEST(FormatIntTest, FormatInt) {
|
||||
EXPECT_EQ("42", fmt::FormatInt(42).str());
|
||||
EXPECT_EQ(2, fmt::FormatInt(42).size());
|
||||
EXPECT_EQ("-42", fmt::FormatInt(-42).str());
|
||||
EXPECT_EQ(3, fmt::FormatInt(-42).size());
|
||||
EXPECT_EQ("42", fmt::FormatInt(42ul).str());
|
||||
EXPECT_EQ("-42", fmt::FormatInt(-42l).str());
|
||||
EXPECT_EQ("42", fmt::FormatInt(42ull).str());
|
||||
EXPECT_EQ("-42", fmt::FormatInt(-42ll).str());
|
||||
std::ostringstream os;
|
||||
os << std::numeric_limits<int64_t>::max();
|
||||
EXPECT_EQ(os.str(), fmt::FormatInt(std::numeric_limits<int64_t>::max()).str());
|
||||
|
15
format.h
15
format.h
@ -1266,12 +1266,12 @@ class FormatInt {
|
||||
private:
|
||||
// Buffer should be large enough to hold all digits (digits10 + 1),
|
||||
// a sign and a null character.
|
||||
enum {BUFFER_SIZE = std::numeric_limits<uint64_t>::digits10 + 3};
|
||||
enum {BUFFER_SIZE = std::numeric_limits<unsigned long long>::digits10 + 3};
|
||||
char buffer_[BUFFER_SIZE];
|
||||
char *str_;
|
||||
|
||||
// Formats value in reverse and returns the number of digits.
|
||||
char *FormatDecimal(uint64_t value) {
|
||||
char *FormatDecimal(unsigned long long value) {
|
||||
char *buffer_end = buffer_ + BUFFER_SIZE;
|
||||
*--buffer_end = '\0';
|
||||
while (value >= 100) {
|
||||
@ -1293,8 +1293,8 @@ class FormatInt {
|
||||
return buffer_end;
|
||||
}
|
||||
|
||||
void FormatSigned(int64_t value) {
|
||||
uint64_t abs_value = value;
|
||||
void FormatSigned(long long value) {
|
||||
unsigned long long abs_value = value;
|
||||
bool negative = value < 0;
|
||||
if (negative)
|
||||
abs_value = 0 - value;
|
||||
@ -1305,12 +1305,15 @@ class FormatInt {
|
||||
|
||||
public:
|
||||
explicit FormatInt(int value) { FormatSigned(value); }
|
||||
explicit FormatInt(int64_t value) { FormatSigned(value); }
|
||||
explicit FormatInt(long value) { FormatSigned(value); }
|
||||
explicit FormatInt(long long value) { FormatSigned(value); }
|
||||
explicit FormatInt(unsigned value) : str_(FormatDecimal(value)) {}
|
||||
explicit FormatInt(uint64_t value) : str_(FormatDecimal(value)) {}
|
||||
explicit FormatInt(unsigned long value) : str_(FormatDecimal(value)) {}
|
||||
explicit FormatInt(unsigned long long value) : str_(FormatDecimal(value)) {}
|
||||
|
||||
const char *c_str() const { return str_; }
|
||||
std::string str() const { return str_; }
|
||||
std::size_t size() const { return buffer_ - str_ + BUFFER_SIZE - 1; }
|
||||
};
|
||||
|
||||
/**
|
||||
|
Loading…
Reference in New Issue
Block a user