Mercurial > code
changeset 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 | f5b491a14a28 |
children | 7781bb45e749 |
files | C++/modules/Json/Json.cpp C++/modules/Json/Json.h |
diffstat | 2 files changed, 49 insertions(+), 20 deletions(-) [+] |
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
--- a/C++/modules/Json/Json.h Sat Oct 31 10:38:10 2015 +0100 +++ b/C++/modules/Json/Json.h Mon Nov 02 13:46:56 2015 +0100 @@ -70,11 +70,11 @@ /** * Create the error. * - * @param ptext the text message - * @param psource the source (e.g. file name) - * @param pline the line number - * @param pcolumn the column number - * @param pposition the position + * @param text the text message + * @param source the source (e.g. file name) + * @param line the line number + * @param column the column number + * @param position the position */ inline Error(std::string text, std::string source, int line, int column, int position) noexcept : m_text{std::move(text)} @@ -85,26 +85,51 @@ { } + /** + * Get the error message. + * + * @return the text + */ inline const std::string &text() const noexcept { return m_text; } + /** + * Get the source (e.g. a file name). + * + * @return the source + */ inline const std::string &source() const noexcept { return m_source; } + /** + * Get the line. + * + * @return the line + */ inline int line() const noexcept { return m_line; } + /** + * Get the column. + * + * @return the column + */ inline int column() const noexcept { return m_column; } + /** + * Get the position. + * + * @return the position + */ inline int position() const noexcept { return m_position; @@ -135,6 +160,9 @@ */ Type m_type; + /** + * Union of values. + */ union { double m_number; bool m_boolean; @@ -1065,7 +1093,7 @@ * @param input the input * @return the escaped string */ -std::string escape(std::string input) noexcept; +std::string escape(const std::string &input); } // !json