Function format_key

Synopsis

#include <toml/serializer.hpp>

template <typename charT, typename traits, typename Alloc>
std::basic_string< charT, traits, Alloc > format_key(const std::basic_string< charT, traits, Alloc > &k)

Description

No description yet.

Source

Lines 27-57 in toml/serializer.hpp.

template<typename charT, typename traits, typename Alloc>
std::basic_string<charT, traits, Alloc>
format_key(const std::basic_string<charT, traits, Alloc>& k)
{
    // check the key can be a bare (unquoted) key
    detail::location loc(k, std::vector<char>(k.begin(), k.end()));
    detail::lex_unquoted_key::invoke(loc);
    if(loc.iter() == loc.end())
    {
        return k; // all the tokens are consumed. the key is unquoted-key.
    }

    //if it includes special characters, then format it in a "quoted" key.
    std::basic_string<charT, traits, Alloc> serialized("\"");
    for(const char c : k)
    {
        switch(c)
        {
            case '\\': {serialized += "\\\\"; break;}
            case '\"': {serialized += "\\\""; break;}
            case '\b': {serialized += "\\b";  break;}
            case '\t': {serialized += "\\t";  break;}
            case '\f': {serialized += "\\f";  break;}
            case '\n': {serialized += "\\n";  break;}
            case '\r': {serialized += "\\r";  break;}
            default  : {serialized += c;      break;}
        }
    }
    serialized += "\"";
    return serialized;
}





Add Discussion as Guest

Log in