From 7e82d7e05c43a22d7662ca109f534ea6dc26cac1 Mon Sep 17 00:00:00 2001 From: zhaohe Date: Thu, 1 Jun 2023 14:19:45 +0800 Subject: [PATCH] update stringUtils --- core/components/stringutils.cpp | 27 +++++++++++++++++++++++++++ core/components/stringutils.hpp | 2 ++ 2 files changed, 29 insertions(+) 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