Skip to content

Commit

Permalink
Set exceptions for boost::format and remove HintFmt::interpolate
Browse files Browse the repository at this point in the history
  • Loading branch information
9999years committed Feb 8, 2024
1 parent 18e8f4f commit 42bcb79
Showing 1 changed file with 20 additions and 25 deletions.
45 changes: 20 additions & 25 deletions src/libutil/fmt.hh
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,17 @@ inline void formatHelper(F & f, const T & x, const Args & ... args)
// Interpolate one argument and then recurse.
formatHelper(f % x, args...);
}

/**
* Set the correct exceptions for `fmt`.
*/
void setExceptions(boost::format & fmt)
{
fmt.exceptions(
boost::io::all_error_bits ^
boost::io::too_many_args_bit ^
boost::io::too_few_args_bit);
}
}

/**
Expand Down Expand Up @@ -68,7 +79,7 @@ template<typename... Args>
inline std::string fmt(const std::string & fs, const Args & ... args)
{
boost::format f(fs);
f.exceptions(boost::io::all_error_bits ^ boost::io::too_many_args_bit);
setExceptions(f);
formatHelper(f, args...);
return f.str();
}
Expand Down Expand Up @@ -121,24 +132,6 @@ private:
boost::format fmt;

public:
/**
* Construct a `HintFmt` from a format string, with values to be
* interpolated later with `%`.
*
* This isn't exposed as a single-argument constructor to avoid
* accidentally constructing `HintFmt`s with user-controlled strings. See
* the note on `fmt` for more information.
*/
static HintFmt interpolate(const std::string & formatString)
{
HintFmt result((boost::format(formatString)));
result.fmt.exceptions(
boost::io::all_error_bits ^
boost::io::too_many_args_bit ^
boost::io::too_few_args_bit);
return result;
}

/**
* Format the given string literally, without interpolating format
* placeholders.
Expand All @@ -152,18 +145,20 @@ public:
*/
template<typename... Args>
HintFmt(const std::string & format, const Args & ... args)
: fmt(format)
{
formatHelper(*this, args...);
}
: HintFmt(boost::format(format), args...)
{ }

HintFmt(const HintFmt & hf)
: fmt(hf.fmt)
{ }

HintFmt(boost::format && fmt)
template<typename... Args>
HintFmt(boost::format && fmt, const Args & ... args)
: fmt(std::move(fmt))
{ }
{
setExceptions(fmt);
formatHelper(*this, args...);
}

template<class T>
HintFmt & operator%(const T & value)
Expand Down

0 comments on commit 42bcb79

Please sign in to comment.