Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 63 additions & 17 deletions include/boost/beast2/format.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,21 +46,55 @@ struct format_impl
next()
{
has_placeholder = false;
bool unmatched_open = false;
bool unmatched_close = false;
while (p != end)
{
if (*p++ != '{')
continue;
if (p == end)
break;
if (*p++ == '}')
if(unmatched_open)
{
if(*p == '{')
{
p++;
core::string_view seg(p0, (p - 1) - p0);
p0 = p;
return seg;
}
if(*p == '}')
{
p++;
core::string_view seg(p0, (p - 2) - p0);
p0 = p;
has_placeholder = true;
return seg;
}
throw std::runtime_error("invalid format string, unmatched {");
}
if(unmatched_close)
{
if(*p == '}')
{
p++;
core::string_view seg(p0, (p - 1) - p0);
p0 = p;
return seg;
}
throw std::runtime_error("invalid format string, unmatched }");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems kind of harsh... throwing? Anyway if we are going to throw we need to use the helpers in detail/except.hpp.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Throwing is consistent with the behaviour of both fmtlib and std::format. Code that works with this implementation therefore has a clear migration path in the future.
Will change to use the helpers.

}
if (*p == '{')
{
unmatched_open = true;
}
if(*p == '}')
{
core::string_view seg(
p0, (p - 2) - p0);
p0 = p;
has_placeholder = true;
return seg;
unmatched_close = true;
}
p++;
}
if (unmatched_open)
throw std::runtime_error("invalid format string, unmatched {");
if(unmatched_close)
throw std::runtime_error("invalid format string, unmatched }");

core::string_view seg(
p0, end - p0);
p0 = end;
Expand All @@ -71,10 +105,14 @@ struct format_impl
void do_arg(Arg const& arg)
{
core::string_view seg = next();
if (seg.size())
os.write(seg.data(), static_cast<
std::streamsize>(seg.size()));
if (has_placeholder)
while(seg.size())
{
os.write(seg.data(), static_cast<std::streamsize>(seg.size()));
if(has_placeholder)
break;
seg = next();
}
if(has_placeholder)
os << arg;
};

Expand All @@ -83,9 +121,17 @@ struct format_impl
{
using expander = int[];
(void)expander{0, (do_arg(args), 0)...};
if (p0 < end)
os.write(p0, static_cast<
std::streamsize>(end - p0));

core::string_view seg;
do
{
seg = next();
if(has_placeholder)
throw std::runtime_error("too few arguments");
if(seg.size())
os.write(seg.data(), static_cast<std::streamsize>(seg.size()));
}
while(seg.size());
}
};

Expand Down
45 changes: 36 additions & 9 deletions test/unit/format.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,49 @@ struct format_test
BOOST_TEST_EQ(s, match);
}

template<class... Args>
void e(
core::string_view match,
core::string_view fs,
Args const&... args)
{
BOOST_TEST_THROWS(f(match, fs, args...), std::runtime_error);
}

void run()
{
// Bad format strings, string arg.
e("{}", "{}");
e("{", "{");
e("}", "}");
e("}{", "}{");
e("{", "{", "x");
e("}", "}", "x");
e("}{", "}{", "x");
e("{", "{", "x");
e("}", "}", "x");
e("}{", "}{", "x");
e("1{}2{}3","1{}2{}3");
e("1a2{}3", "1{}2{}3", "a");

// Good format strings, string arg.
f("x", "x");
f("{}", "{}");
f("{", "{");
f("}", "}");
f("}{", "}{");
f("{", "{{");
f("}", "}}");
f("}{", "}}{{");
f("x", "x");
f("x", "{}", "x");
f("{", "{", "x");
f("}", "}", "x");
f("}{", "}{", "x");
f("1{}2{}3","1{}2{}3");
f("1a2{}3", "1{}2{}3", "a");
f("x", "{}", "x");
f("{", "{{", "x");
f("}", "}}", "x");
f("}{", "}}{{", "x");
f("1a2b3", "1{}2{}3", "a", "b");
f("1a2b3", "1{}2{}3", "a", "b", "c");
f("hello world!", "hello {}!", "world");
f("hello world! {} ", "hello {}! {{}} ", "world");

// Good format string, char arg
f("x", "{}", 'x');
}
};

Expand Down
Loading