-
Notifications
You must be signed in to change notification settings - Fork 0
/
text.cpp
160 lines (148 loc) · 4.79 KB
/
text.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
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include <algorithm>
#include <unordered_map>
#include "myxml/text.hpp"
namespace myxml
{
text::text(std::shared_ptr<text_impl> impl)
: _impl(impl)
{
}
text::text(std::string &&str)
{
_impl = std::make_shared<text_impl>();
_impl->inner = str;
}
text::text(const std::string &str)
{
_impl = std::make_shared<text_impl>();
_impl->inner = str;
}
std::string text::trimmed()
{
std::string trimmed;
auto is_space = [](char ch)
{ return std::isspace(static_cast<char>(ch)); };
auto &ref = _impl->inner;
auto start = std::find_if_not(ref.begin(), ref.end(), is_space);
auto end = std::find_if_not(ref.rbegin(), ref.rend(), is_space).base();
return (start < end) ? std::string(start, end) : std::string();
}
text text::trim()
{
return this->trimmed();
}
void text::print(std::ostream &os) const
{
this->_impl->print(os);
}
void text::entity_encoding(bool flag)
{
this->_impl->entity_encoding(flag);
}
void text::platform_specific_newline(bool flag)
{
this->_impl->platform_specific_newline(flag);
}
text_impl::text_impl(std::string_view input)
{
if (_config.entity_encoding)
{
// entity encoding
static std::map<std::string, char, std::less<>> entityMap = {
{"<", '<'},
{">", '>'},
{"&", '&'},
{""", '"'},
{"'", '\''},
};
std::size_t len = input.length();
std::size_t start = 0; // start of current segment
for (std::size_t i = 0; i < len; i++)
{
// Newline Normalization
if (input[i] == '\r')
{
this->inner += input.substr(start, i - start);
if (i + 1 < len && input[i + 1] == '\n')
{
i += 1;
}
this->inner += '\n';
start = i + 1;
}
// Entity Decoding
if (input[i] == '&')
{
if (auto semicolonPos = input.find(';', i); semicolonPos != std::string::npos)
{
std::string_view entity = input.substr(i, semicolonPos - i + 1);
if (auto it = entityMap.find(entity); it != entityMap.end())
{
this->inner += input.substr(start, i - start); // append unmodified segment
this->inner += it->second; // append decoded character
i = semicolonPos; // skip past the entity
start = semicolonPos + 1; // update last unappend position
}
}
}
}
this->inner += input.substr(start, len - start); // append the remaining
}
else
{
this->inner = input;
}
}
void text_impl::print(std::ostream &os) const
{
if (!this->_config.entity_encoding && !this->_config.platform_specific_newline)
{
os << this->inner;
}
else
{
static std::unordered_map<char, std::string> entityMap = {
{'<', "<"},
{'>', ">"},
{'&', "&"},
{'"', """},
{'\'', "'"},
};
std::size_t start = 0; // start of current segement
std::size_t len = this->inner.length();
for (std::size_t i = 0; i < len; i++)
{
if (this->_config.entity_encoding)
{
if (auto it = entityMap.find(this->inner[i]); it != entityMap.end())
{
os << this->inner.substr(start, i - start);
os << it->second;
start = i + 1;
}
}
if (this->_config.platform_specific_newline)
{
if (this->inner[i] == '\n')
{
os << this->inner.substr(start, i - start);
os << util::platform_specific_newline();
start = i + 1;
}
}
}
os << this->inner.substr(start, len - start);
}
}
namespace util
{
const std::string_view platform_specific_newline()
{
#ifdef _WIN32
return "\r\n";
#else
return "\n";
#endif
}
}
}