Use a heuristic to detect empty strftime result (#367)

This commit is contained in:
Victor Zverovich 2016-08-05 07:27:56 -07:00
parent 1a23f9c274
commit a5d0adf395
2 changed files with 11 additions and 0 deletions

View File

@ -36,6 +36,13 @@ void format(BasicFormatter<char, ArgFormatter> &f,
buffer.resize(start + count); buffer.resize(start + count);
break; break;
} }
if (size >= format.size() * 256) {
// If the buffer is 256 times larger than the format string, assume
// that `strftime` gives an empty result. There doesn't seem to be a
// better way to distinguish the two cases:
// https://github.com/fmtlib/fmt/issues/367
break;
}
const std::size_t MIN_GROWTH = 10; const std::size_t MIN_GROWTH = 10;
buffer.reserve(buffer.capacity() + (size > MIN_GROWTH ? size : MIN_GROWTH)); buffer.reserve(buffer.capacity() + (size > MIN_GROWTH ? size : MIN_GROWTH));
} }

View File

@ -27,3 +27,7 @@ TEST(TimeTest, GrowBuffer) {
std::time_t t = std::time(0); std::time_t t = std::time(0);
fmt::format(s, *std::localtime(&t)); fmt::format(s, *std::localtime(&t));
} }
TEST(TimeTest, EmptyResult) {
EXPECT_EQ("", fmt::format("{}", std::tm()));
}