1
0
Fork 0
mirror of https://gitlab.com/niansa/anyproc.git synced 2025-03-06 20:49:24 +01:00

Better string escaping

This commit is contained in:
niansa/tuxifan 2023-04-27 14:38:56 +02:00
parent 1af9ef986f
commit 59e7532681
2 changed files with 23 additions and 5 deletions

View file

@ -26,6 +26,5 @@ if (ANYPROC_PYBIND)
target_link_libraries(anyproc_py PRIVATE anyproc)
endif()
install(TARGETS anyproc
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR})

View file

@ -26,8 +26,13 @@ public:
std::string fres;
fres.push_back(quotes);
for (const char c : str) {
if (c == quotes) fres.push_back('\\');
fres.push_back(c);
switch (c) {
case '\n': fres.append("\\n"); break;
case '\r': fres.append("\\r"); break;
case '\t': fres.append("\\t"); break;
case '\'': case '"': if (c == quotes) fres.push_back('\\'); [[fallthrough]];
default: fres.push_back(c);
}
}
fres.push_back(quotes);
return fres;
@ -40,8 +45,22 @@ public:
return std::string(str);
}
std::string fres;
for (const char c : std::string_view{str.data()+1, str.size()-2-backTruncateCount}) {
if (c != '\\') fres.push_back(c);
bool after_bs = false;
for (char c : std::string_view{str.data()+1, str.size()-2-backTruncateCount}) {
if (c == '\\') {
after_bs = true;
continue;
}
if (after_bs) {
after_bs = false;
switch (c) {
case 'n': c = '\n'; break;
case 'r': c = '\r'; break;
case 't': c = '\t'; break;
default: continue;
}
}
fres.push_back(c);
}
return fres;
}