-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathescString.cpp
39 lines (36 loc) · 909 Bytes
/
escString.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include "escString.h"
#include "token.h"
using namespace std;
/*
octal \ooo
hexadecimal \xhhh
*/
map < string, int > escapes = {
{ "\n" , 10 },
{ "\\n" , 10 },
{ "\\\\" , 92 },
{ "\\t" , 9 },
{ "\\v" , 11 },
{ "\\'" , 39 },
{ "\\b" , 127 },
{ "\\\"" , 34 },
{ "\\r" , 13 },
{ "\\0" , 0 },
{ "\\f" , 12 },
{ "\\a" , 7 }
};
string formatString(string str) {
string processedstr = str;
stringstream finalstr;
for (auto& esc : escapes) {
size_t isin = processedstr.find(esc.first);
while (isin != string::npos) {
finalstr << processedstr.substr(0, isin);
finalstr << string("\", " + to_string(esc.second) + ", \"");
processedstr = processedstr.substr(isin + esc.first.size(), processedstr.size() - 1);
isin = processedstr.find(esc.first);
}
}
if (finalstr.str() == "") return str + ", 0";
return finalstr.str() + processedstr;
}