Commit a5d0adf3 authored by Victor Zverovich's avatar Victor Zverovich

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

parent 1a23f9c2
...@@ -36,6 +36,13 @@ void format(BasicFormatter<char, ArgFormatter> &f, ...@@ -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));
} }
......
...@@ -27,3 +27,7 @@ TEST(TimeTest, GrowBuffer) { ...@@ -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()));
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment