Mercurial > code
diff C++/modules/Json/Json.cpp @ 451:902b034df6e3
Json: escape string construct a new string
author | David Demelier <markand@malikania.fr> |
---|---|
date | Mon, 02 Nov 2015 13:46:56 +0100 |
parents | f5e62f6c1475 |
children | b681299e6987 |
line wrap: on
line diff
--- a/C++/modules/Json/Json.cpp Sat Oct 31 10:38:10 2015 +0100 +++ b/C++/modules/Json/Json.cpp Mon Nov 02 13:46:56 2015 +0100 @@ -173,42 +173,43 @@ m_value = convert(json_load_file, file.path.c_str(), 0); } -std::string escape(std::string value) noexcept +std::string escape(const std::string &value) { + std::string result; + for (auto it = value.begin(); it != value.end(); ++it) { switch (*it) { case '\\': + result += "\\\\"; + break; case '/': + result += "\\/"; + break; case '"': - it = value.insert(it, '\\'); - it++; + result += "\\\""; break; case '\b': - value.replace(it, it + 1, "\\b"); - it += 1; + result += "\\b"; break; case '\f': - value.replace(it, it + 1, "\\f"); - it += 1; + result += "\\f"; break; case '\n': - value.replace(it, it + 1, "\\n"); - it += 1; + result += "\\n"; break; case '\r': - value.replace(it, it + 1, "\\r"); - it += 1; + result += "\\r"; break; case '\t': - value.replace(it, it + 1, "\\t"); - it += 1; + result += "\\t"; break; default: + result += *it; break; } } - return value; + return result; } } // !json