diff --git a/src/nhttpd/yhttpd_core/helper.cpp b/src/nhttpd/yhttpd_core/helper.cpp index ee457860f..6361bd0c7 100644 --- a/src/nhttpd/yhttpd_core/helper.cpp +++ b/src/nhttpd/yhttpd_core/helper.cpp @@ -296,6 +296,53 @@ std::string json_out_error(std::string _error) { // JSON: convert string to JSON-String //----------------------------------------------------------------------------- +std::string json_convert_string(std::string value) +{ + std::string result; + for (size_t i = 0; i < value.length(); i++) + { + unsigned char c = unsigned(value[i]); + switch(c) + { + case '\"': + result += "\\\""; + break; + case '\\': + result += "\\\\"; + break; + case '\b': + result += "\\b"; + break; + case '\f': + result += "\\f"; + break; + case '\n': + result += "\\n"; + break; + case '\r': + result += "\\r"; + break; + case '\t': + result += "\\t"; + break; + default: + if ( isControlCharacter( c ) ) + { + std::ostringstream oss; + oss << "\\u" << std::hex << std::uppercase << std::setfill('0') << std::setw(4) << static_cast(c); + result += oss.str(); + } + else + { + result += c; + } + break; + } + } + return result; +} + +#if 0 std::string json_convert_string(std::string s) { std::stringstream ss; for (size_t i = 0; i < s.length(); ) { @@ -350,6 +397,7 @@ std::string json_convert_string(std::string s) { } return ss.str(); } +#endif // 0 std::string yExecuteScript(std::string cmd) { std::string script, para, result; diff --git a/src/nhttpd/yhttpd_core/helper.h b/src/nhttpd/yhttpd_core/helper.h index 0984cf54c..d322c04ad 100644 --- a/src/nhttpd/yhttpd_core/helper.h +++ b/src/nhttpd/yhttpd_core/helper.h @@ -48,6 +48,8 @@ std::string json_out_pair(std::string _key, std::string _value); std::string json_out_success(std::string _result); std::string json_out_error(std::string _error); std::string json_convert_string(std::string s); +/// Returns true if ch is a control character (in range [0,32]). +static inline bool isControlCharacter(char ch) { return ch > 0 && ch <= 0x1F;} //----------------------------------------------------------------------------- // Script Helpers