[英]Function to escape some characters for C++ string
I need a function to escape some characters inside a std::string
and so i made this:我需要一个 function 来转义std::string
中的一些字符,所以我做了这个:
static void escape(std::string& source,const std::vector<std::string> & toEscape, const std::string& escape){
//for each position of the string
for(auto i = 0; i < source.size(); ++i){
// for each substring to escape
for(const auto & cur_to_escape : toEscape){
// if the current position + the size of the current "to_escape" string are less than the string size and it's equal to the substring next of the i'th position
if(i + cur_to_escape.size() < source.size() && source.substr(i, cur_to_escape.size()) == cur_to_escape){
// then for each char of the current "to_escape", escape the current character with the "escape" string given as parameter
/*
* source = asd
* toEscape = {"asd"}
* escape = \
* -> asd -> \asd -> \a\sd -> \a\s\d
* */
for(auto z = 0; z < cur_to_escape.size(); ++z){
source.insert(i, escape);
i+=escape.size();
}
}
}
}
}
and to test it i've used this:为了测试它,我使用了这个:
int main() {
std::string s = "need to escape \" , \\ and \n .";
std::cout<<s;
escape(s, {"\n", "\\", "\""}, "\\");
std::cout<<"\n\n final string: "<<s;
}
and the output is output 是
final string: need to escape \" , \\ and \
.
and so the \n
is not been escaped as intended... and i can't find the problem... any guesses?所以\n
没有按预期转义......我找不到问题......有什么猜测吗?
This is working code, but it is not optimal and can be made faster but probably also bigger.这是工作代码,但它不是最佳的,可以做得更快,但也可能更大。
void escape(std::string& source, const std::vector<std::string>& to_escape, const std::string& escape) {
// for each substring to escape
for (const auto &e : to_escape) {
auto pos = source.find(e);
while (pos != std::string::npos) {
auto to_replace = escape+e;
if (e=="\n") to_replace = escape+"n";
else if (e=="\t") to_replace = escape+"t";
source.replace(pos, e.size(), to_replace);
const auto old_pos = pos;
pos = source.find(e, old_pos + to_replace.size());
}
}
}
“and so the \n is not been escaped as intended” Yes, it is: the new-line character is there, as expected. “所以 \n 没有按预期转义”是的,它是:换行符在那里,正如预期的那样。 If you expect an 'n'
character, you are wrong.如果你期望一个'n'
字符,那你就错了。 '\n'
is a convention used to represent the "invisible" character New-Line ( NL
). '\n'
是用于表示“不可见”字符换行符 ( NL
) 的约定。
Here's a cleaner way to write the same thing ( try it ):这是编写相同内容的更简洁的方法(尝试一下):
std::string escape(const char* src, const std::set<char> escapee, const char marker)
{
std::string r;
while (char c = *src++)
{
if (escapee.find(c) != escapee.end())
r += marker;
r += c; // to get the desired behavior, replace this line with: r += c == '\n' ? 'n' : c;
}
return r;
}
//...
std::string r = escape("\"this\" is a test\nthis is the second line", { '"', '\n' }, '\\');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.