-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringLiteral.h
More file actions
71 lines (57 loc) · 1.35 KB
/
StringLiteral.h
File metadata and controls
71 lines (57 loc) · 1.35 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#ifndef STRINGLITERAL_H
#define STRINGLITERAL_H
#include <string>
#include "Expression.h"
class StringLiteral : public Expression
{
std::wstring str;
// trim quotes from both ends
static inline std::wstring trim(const std::wstring &s) {
return s.substr(1, s.size() - 2);
}
static inline std::wstring unescape(const std::wstring& s)
{
std::wstring res;
std::wstring::const_iterator it = s.begin();
while (it != s.end())
{
wchar_t c = *it++;
if (c == '\\' && it != s.end())
{
switch (*it++) {
case L'\\': c = L'\\'; break;
case L'n': c = L'\n'; break;
case L't': c = L'\t'; break;
case L'r': c = L'\r'; break;
case L'\"': c = L'\"'; break;
default:
// invalid escape sequence - skip it. alternatively you can copy it as is, throw an exception...
continue;
}
}
res += c;
}
return res;
}
public:
StringLiteral(std::wstring str) : str(unescape(trim(str)))
{
}
virtual picojson::value asJson(std::shared_ptr<Substitution> subs, bool forConditionSection) const
{
return picojson::value(str);
}
std::wstring getContent() const
{
return str;
}
virtual ExpressionForm getForm() const
{
return STRING_LITERAL;
}
virtual std::wstring getType(std::shared_ptr<Substitution> subs) const
{
return L"string";
}
};
#endif // STRINGLITERAL_H