diff --git a/core/components/stringutils.cpp b/core/components/stringutils.cpp index becd10d..ee473a4 100644 --- a/core/components/stringutils.cpp +++ b/core/components/stringutils.cpp @@ -139,4 +139,31 @@ string StringUtils::bytet2BinaryBigEnd(uint32_t value, int bitCount, bool remote } } return ret; +} + +string StringUtils::escapeSequence(const string& raw_str) { + string escaped_str; + for (const char& ch : raw_str) { + switch (ch) { + case '\n': + escaped_str.append("\\n"); + break; + case '\r': + escaped_str.append("\\r"); + break; + case '\t': + escaped_str.append("\\t"); + break; + case '\"': + escaped_str.append("\\\""); + break; + case '\\': + escaped_str.append("\\\\"); + break; + default: + escaped_str.push_back(ch); + break; + } + } + return escaped_str; } \ No newline at end of file diff --git a/core/components/stringutils.hpp b/core/components/stringutils.hpp index 1b92331..0cc7dd7 100644 --- a/core/components/stringutils.hpp +++ b/core/components/stringutils.hpp @@ -63,6 +63,8 @@ class StringUtils { */ string bytet2Binary(uint32_t value, int bitCount, bool remoteZero = true); string bytet2BinaryBigEnd(uint32_t value, int bitCount, bool remoteZero = true); + + string escapeSequence(const string& raw_str); }; } // namespace core