Function escape_string

Synopsis

#include <include/cpptoml.h>

static std::string escape_string(const std::string &str)

Description

Escape a string for output.

Source

Lines 3415-3461 in include/cpptoml.h.

static std::string escape_string(const std::string& str)
{
    std::string res;
    for (auto it = str.begin(); it != str.end(); ++it)
    {
        if (*it == '\b')
        {
            res += "\\b";
        }
        else if (*it == '\t')
        {
            res += "\\t";
        }
        else if (*it == '\n')
        {
            res += "\\n";
        }
        else if (*it == '\f')
        {
            res += "\\f";
        }
        else if (*it == '\r')
        {
            res += "\\r";
        }
        else if (*it == '"')
        {
            res += "\\\"";
        }
        else if (*it == '\\')
        {
            res += "\\\\";
        }
        else if (static_cast<uint32_t>(*it) <= UINT32_C(0x001f))
        {
            res += "\\u";
            std::stringstream ss;
            ss << std::hex << static_cast<uint32_t>(*it);
            res += ss.str();
        }
        else
        {
            res += *it;
        }
    }
    return res;
}





Add Discussion as Guest

Log in