如何解析带有原始转义序列的字符串? [英] How to parse a string with raw escape sequences?
问题描述
假设有两个字符串:
string parse(const string& s) {
// how to write this function?
}
int main() {
string s1 = R"(hello\n\"this is a string with escape sequences\"\n)";
string s2 = "hello\n\"this is a string with escape sequences\"\n";
assert(parse(s1) == s2);
}
我的问题是,如何编写函数 parse ()
,以使断言成功,除了一些手工代码遍历字符串和检查每一个可能的转义序列?
My question is, how to write the function parse()
in order to make the assertion succeed, other than some hand-made code traversing the string and checking against every possible escape sequence? Is there any existing idiom for doing this?
推荐答案
使用正则表达式来实现换码序列的替换可能是最简单的,但如果你坚持这样做,你可以总是将字符串写入文件作为一个有效的C ++程序的一部分打印字符串到文件,然后从文件中读取它。
(这也可以改进工作没有临时文件)
It's probably the easiest to just implement the replacement of escape sequences using a regular expression, but if you insist on doing it without, you can always write the string to a file as part of a valid C++ program that prints the string to a file, then read it from the file. (This can also be improved to work without temp files at all)
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <assert.h>
using std::string;
string parse(const string& s) {
std::ofstream ftmp("tmpsrc.cpp");
ftmp << "#include <iostream>\nint main(int argc, char* argv[]){\n";
ftmp << " std::cout << \"" << s << "\";\nreturn 0;}\n\n";
ftmp.close();
system("g++ -o tmpprint tmpsrc.cpp");
system("./tmpprint > tmpstr.txt");
std::ifstream fin("tmpstr.txt",std::ios::in|std::ios::binary);
fin.seekg(0,std::ios::end);
int size=fin.tellg();
fin.seekg(0);
string res;
res.resize(size);
fin.read(&res[0],size);
fin.close();
// Add delete of temp files here
return res;
}
int main() {
string s1 = R"(hello\n\"this is a string with escape sequences\"\n)";
string s2 = "hello\n\"this is a string with escape sequences\"\n";
assert(parse(s1) == s2);
}
这篇关于如何解析带有原始转义序列的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!