Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++11 Equivalent to Boost.Format

Anything like Boost.Format in the C++11 standard? I've been able to avoid using Boost with a better C++11 option for every other need I've had.

For that matter, Boost.Format doesn't hold a candle to the syntax of Python format(). Something like that would be even better.

like image 505
Chris Redford Avatar asked May 01 '14 17:05

Chris Redford


3 Answers

There is a proposal for something similar to boost-format. However, it's neither part of C++11 nor C++14, nor has anything related to string formatting be added.

Here you can find the latest proposal. In contrast to boost-format, it's based on variadic templates.

  • http://open-std.org/JTC1/SC22/WG21/docs/papers/2013/n3716.html
like image 40
nosid Avatar answered Nov 15 '22 19:11

nosid


C++11, 14 and 17 don't provide anything like that.

However, C++20 provides std::format which is similar in spirit to Boost Format but with the design permitting more efficient implementation. The {fmt} library is an implementation of this formatting facility and it only requires C++11:

std::string s = fmt::format("I'd rather be {1} than {0}.", "right", "happy");

Disclaimer: I'm the author of {fmt} and C++20 std::format

like image 112
vitaut Avatar answered Nov 15 '22 20:11

vitaut


Python-like format string function implementation with c++11 regex and variadic templates.

/**
   Helper code to unpack variadic arguments
*/
namespace internal
{
    template<typename T>
    void unpack(std::vector<std::string> &vbuf, T t)
    {
        std::stringstream buf;
        buf << t;
        vbuf.push_back(buf.str());
    }
    template<typename T, typename ...Args>
    void unpack(std::vector<std::string> &vbuf, T t, Args &&... args)
    {
        std::stringstream buf;
        buf << t;
        vbuf.push_back(buf.str());
        unpack(vbuf, std::forward<Args>(args)...);
    }
}

/**
    Python-like string formatting
 */
template<typename ... Args>
std::string format(const std::string& fmt, Args ... args)
{
    std::vector<std::string> vbuf;  // store arguments as strings
    std::string in(fmt), out;    // unformatted and formatted strings
    std::regex re_arg("\\{\\b\\d+\\b\\}");  // search for {0}, {1}, ...
    std::regex re_idx("\\b\\d+\\b");        // search for 0, 1, ...
    std::smatch m_arg, m_idx;               // store matches
    size_t idx = 0;                         // index of argument inside {...}

    // Unpack arguments and store them in vbuf
    internal::unpack(vbuf, std::forward<Args>(args)...);

    // Replace all {x} with vbuf[x]
    while (std::regex_search(in, m_arg, re_arg)) {
        out += m_arg.prefix();
        auto text = m_arg[0].str();
        if (std::regex_search(text, m_idx, re_idx)) {
            idx = std::stoi(m_idx[0].str());
        }
        if(idx < vbuf.size()) {
            out += std::regex_replace(m_arg[0].str(), re_arg, vbuf[idx]);
        }
        in = m_arg.suffix();
    }
    out += in;
    return out;
}

Example: cpp.sh/9cvtz

like image 2
galarius Avatar answered Nov 15 '22 19:11

galarius