Mercurial > code
changeset 445:f5e62f6c1475
Json: add escape free function
author | David Demelier <markand@malikania.fr> |
---|---|
date | Mon, 26 Oct 2015 19:29:34 +0100 |
parents | fc055d2a4a2c |
children | 8396fd66e57a |
files | C++/modules/Json/Json.cpp C++/modules/Json/Json.h C++/tests/Json/main.cpp |
diffstat | 3 files changed, 94 insertions(+), 15 deletions(-) [+] |
line wrap: on
line diff
--- a/C++/modules/Json/Json.cpp Fri Oct 23 10:11:34 2015 +0200 +++ b/C++/modules/Json/Json.cpp Mon Oct 26 19:29:34 2015 +0100 @@ -173,4 +173,42 @@ m_value = convert(json_load_file, file.path.c_str(), 0); } +std::string escape(std::string value) noexcept +{ + for (auto it = value.begin(); it != value.end(); ++it) { + switch (*it) { + case '\\': + case '/': + case '"': + it = value.insert(it, '\\'); + it++; + break; + case '\b': + value.replace(it, it + 1, "\\b"); + it += 1; + break; + case '\f': + value.replace(it, it + 1, "\\f"); + it += 1; + break; + case '\n': + value.replace(it, it + 1, "\\n"); + it += 1; + break; + case '\r': + value.replace(it, it + 1, "\\r"); + it += 1; + break; + case '\t': + value.replace(it, it + 1, "\\t"); + it += 1; + break; + default: + break; + } + } + + return value; +} + } // !json
--- a/C++/modules/Json/Json.h Fri Oct 23 10:11:34 2015 +0200 +++ b/C++/modules/Json/Json.h Mon Oct 26 19:29:34 2015 +0100 @@ -59,13 +59,14 @@ * @brief Error description. */ class Error : public std::exception { +private: + std::string m_text; + std::string m_source; + int m_line; + int m_column; + int m_position; + public: - std::string text; //!< The error message - std::string source; //!< The source (e.g. name of file) - int line{0}; //!< The line number - int column{0}; //!< The column - int position{0}; //!< The position - /** * Create the error. * @@ -75,13 +76,38 @@ * @param pcolumn the column number * @param pposition the position */ - inline Error(std::string ptext, std::string psource, int pline, int pcolumn, int pposition) noexcept - : text{std::move(ptext)} - , source{std::move(psource)} - , line{pline} - , column{pcolumn} - , position{pposition} + inline Error(std::string text, std::string source, int line, int column, int position) noexcept + : m_text{std::move(text)} + , m_source{std::move(source)} + , m_line{line} + , m_column{column} + , m_position{position} + { + } + + inline const std::string &text() const noexcept + { + return m_text; + } + + inline const std::string &source() const noexcept { + return m_source; + } + + inline int line() const noexcept + { + return m_line; + } + + inline int column() const noexcept + { + return m_column; + } + + inline int position() const noexcept + { + return m_position; } /** @@ -91,7 +117,7 @@ */ const char *what() const noexcept override { - return text.c_str(); + return m_text.c_str(); } }; @@ -1033,6 +1059,14 @@ } }; +/** + * Escape the input. + * + * @param input the input + * @return the escaped string + */ +std::string escape(std::string input) noexcept; + } // !json #endif // !_JSON_H_
--- a/C++/tests/Json/main.cpp Fri Oct 23 10:11:34 2015 +0200 +++ b/C++/tests/Json/main.cpp Mon Oct 26 19:29:34 2015 +0100 @@ -79,6 +79,14 @@ ASSERT_EQ(123, object2.toInt()); } +TEST(Misc, escape) +{ + std::string input{"\\/\"\b\f\n\r\t"}; + std::string expected{"\\\\\\/\\\"\\b\\f\\n\\r\\t"}; + + ASSERT_EQ(expected, json::escape(input)); +} + /* -------------------------------------------------------- * json::Value constructors * -------------------------------------------------------- */ @@ -775,8 +783,7 @@ json::Array array{1, 2, 3}; int i = 1; - for (const json::Value &v : array) - { + for (const json::Value &v : array) { ASSERT_EQ(i++, v.toInt()); } }