10#include "sgml_unicode.hpp"
18 inline const wchar_t* sgml2uni(_In_reads_or_z_(count)
const T* entity, _In_
size_t count)
20 assert(entity && count);
21 assert(count < 2 || entity[0] !=
'#');
23 for (
size_t i = 0, j = _countof(sgml_unicode); i < j; ) {
24 size_t m = (i + j) / 2;
25 if (sgml_unicode[m].sgml[0] < entity[0])
27 else if (sgml_unicode[m].sgml[0] > entity[0])
30 auto r = strncmp<char, T>(sgml_unicode[m].sgml + 1, _countof(sgml_unicode[0].sgml) - 1, entity + 1, count - 1);
36 for (; i < m && strncmp<char, T>(sgml_unicode[m - 1].sgml, _countof(sgml_unicode[0].sgml), entity, count) == 0; m--);
37 return sgml_unicode[m].unicode;
45 inline const T* sgmlend(
46 _In_reads_or_z_(count)
const T* str,
49 assert(str || !count);
50 for (
size_t i = 0; i < count; i++) {
53 if (!str[i] || str[i] ==
'&' || isspace(str[i]))
60 constexpr int sgml_full = 0x80000000;
61 constexpr int sgml_quot = 0x00000001;
62 constexpr int sgml_apos = 0x00000002;
63 constexpr int sgml_quot_apos = sgml_quot | sgml_apos;
64 constexpr int sgml_amp = 0x00000004;
65 constexpr int sgml_lt_gt = 0x00000008;
66 constexpr int sgml_bsol = 0x00000010;
67 constexpr int sgml_dollar = 0x00000020;
68 constexpr int sgml_percnt = 0x00000040;
69 constexpr int sgml_commat = 0x00000080;
70 constexpr int sgml_num = 0x00000100;
71 constexpr int sgml_lpar_rpar = 0x00000200;
72 constexpr int sgml_lcub_rcub = 0x00000400;
73 constexpr int sgml_lsqb_rsqb = 0x00000800;
74 constexpr int sgml_sgml = sgml_amp | sgml_lt_gt;
75 constexpr int sgml_ml_attrib = sgml_amp | sgml_quot_apos;
76 constexpr int sgml_c = sgml_amp | sgml_bsol | sgml_quot_apos;
93 inline std::wstring sgml2str(
94 _In_reads_or_z_(count_src)
const T* src, _In_
size_t count_src,
96 _In_
const mapping<size_t>& offset = mapping<size_t>(0, 0),
97 _Inout_opt_ mapping_vector<size_t>* map =
nullptr)
99 assert(src || !count_src);
102 skip_quot = (skip & sgml_quot) == 0,
103 skip_apos = (skip & sgml_apos) == 0,
104 skip_amp = (skip & sgml_amp) == 0,
105 skip_lt_gt = (skip & sgml_lt_gt) == 0,
106 skip_bsol = (skip & sgml_bsol) == 0,
107 skip_dollar = (skip & sgml_dollar) == 0,
108 skip_percnt = (skip & sgml_percnt) == 0,
109 skip_commat = (skip & sgml_commat) == 0,
110 skip_num = (skip & sgml_num) == 0,
111 skip_lpar_rpar = (skip & sgml_lpar_rpar) == 0,
112 skip_lcub_rcub = (skip & sgml_lcub_rcub) == 0,
113 skip_lsqb_rsqb = (skip & sgml_lsqb_rsqb) == 0;
115 count_src = strnlen(src, count_src);
117 dst.reserve(count_src);
118 for (
size_t i = 0; i < count_src;) {
120 auto end = sgmlend(src + i + 1, count_src - i - 1);
122 const wchar_t* entity_w;
124 size_t n = end - src - i - 1;
125 if (n >= 2 && src[i + 1] ==
'#') {
127 if (src[i + 2] ==
'x' || src[i + 2] ==
'X')
128 unicode = strtou32(src + i + 3, n - 2,
nullptr, 16);
130 unicode = strtou32(src + i + 2, n - 1,
nullptr, 10);
132 if (unicode < 0x10000) {
133 chr[0] = (wchar_t)unicode;
136 ucs4_to_surrogate_pair(chr, unicode);
140 chr[0] = (wchar_t)unicode;
146 entity_w = sgml2uni(src + i + 1, n);
149 (skip_quot || (entity_w[0] != L
'"')) &&
150 (skip_apos || (entity_w[0] != L
'\'')) &&
151 (skip_amp || (entity_w[0] != L
'&')) &&
152 (skip_lt_gt || (entity_w[0] != L
'<' && entity_w[0] != L
'>')) &&
153 (skip_bsol || (entity_w[0] != L
'\\')) &&
154 (skip_dollar || (entity_w[0] != L
'$')) &&
155 (skip_percnt || (entity_w[0] != L
'%')) &&
156 (skip_commat || (entity_w[0] != L
'@')) &&
157 (skip_num || (entity_w[0] != L
'#')) &&
158 (skip_lpar_rpar || (entity_w[0] != L
'(' && entity_w[0] != L
')')) &&
159 (skip_lcub_rcub || (entity_w[0] != L
'{' && entity_w[0] != L
'}')) &&
160 (skip_lsqb_rsqb || (entity_w[0] != L
'[' && entity_w[0] != L
']')))
162 if (map) map->push_back(mapping<size_t>(offset.from + i, offset.to + dst.size()));
163 dst.append(entity_w);
165 if (map) map->push_back(mapping<size_t>(offset.from + i, offset.to + dst.size()));
170 dst.append(1, src[i++]);
176 inline const char* chr2sgml(_In_reads_or_z_(count)
const wchar_t* entity, _In_
size_t count)
178 assert(entity && count);
180 const wchar_t e2 = entity[0];
181 for (
size_t i = 0, j = _countof(unicode_sgml); i < j; ) {
182 size_t m = (i + j) / 2;
183 wchar_t e1 = sgml_unicode[unicode_sgml[m]].unicode[0];
189 auto r = strncmp(sgml_unicode[unicode_sgml[m]].unicode + 1, _countof(sgml_unicode[0].unicode) - 1, entity + 1, count - 1);
195 for (; i < m && sgml_unicode[unicode_sgml[m - 1]].unicode[0] == e2 && strncmp(sgml_unicode[unicode_sgml[m - 1]].unicode + 1, _countof(sgml_unicode[0].unicode) - 1, entity + 1, count - 1) == 0; m--);
196 return sgml_unicode[unicode_sgml[m]].sgml;
213 inline std::string str2sgml(
214 _In_reads_or_z_(count_src)
const wchar_t* src,
215 _In_
size_t count_src,
216 _In_
size_t what = 0)
218 assert(src || !count_src);
221 do_ascii = (what & sgml_full) == 0,
222 do_quot = (what & sgml_quot) == 0,
223 do_apos = (what & sgml_apos) == 0,
224 do_lt_gt = (what & sgml_lt_gt) == 0,
225 do_bsol = (what & sgml_bsol) == 0,
226 do_dollar = (what & sgml_dollar) == 0,
227 do_percnt = (what & sgml_percnt) == 0,
228 do_commat = (what & sgml_commat) == 0,
229 do_num = (what & sgml_num) == 0,
230 do_lpar_rpar = (what & sgml_lpar_rpar) == 0,
231 do_lcub_rcub = (what & sgml_lcub_rcub) == 0,
232 do_lsqb_rsqb = (what & sgml_lsqb_rsqb) == 0;
234 count_src = wcsnlen(src, count_src);
236 dst.reserve(count_src);
237 for (
size_t i = 0; i < count_src;) {
238 size_t n = glyphlen(src + i, count_src - i);
240 do_ascii && (
unsigned int)src[i] < 128 &&
242 (do_quot || (src[i] != L
'"')) &&
243 (do_apos || (src[i] != L
'\'')) &&
244 (do_lt_gt || (src[i] != L
'<' && src[i] != L
'>')) &&
245 (do_bsol || (src[i] != L
'\\')) &&
246 (do_dollar || (src[i] != L
'$')) &&
247 (do_percnt || (src[i] != L
'%')) &&
248 (do_commat || (src[i] != L
'@')) &&
249 (do_num || (src[i] != L
'#')) &&
250 (do_lpar_rpar || (src[i] != L
'(' && src[i] != L
')')) &&
251 (do_lcub_rcub || (src[i] != L
'{' && src[i] != L
'}')) &&
252 (do_lsqb_rsqb || (src[i] != L
'[' && src[i] != L
']')))
255 dst.append(1, (
char)src[i++]);
258 const char* entity = chr2sgml(src + i, n);
267 if ((
unsigned int)src[i] < 128)
268 dst.append(1, (
char)src[i++]);
270 char tmp[3 + 8 + 1 + 1];
271 snprintf(tmp, _countof(tmp),
"&#x%x;", src[i++]);
277 const size_t end = i + n;
279 if ((entity = chr2sgml(src + i, 1)) !=
nullptr) {
285 else if ((
unsigned int)src[i] < 128)
286 dst.append(1, (
char)src[i++]);
290 if (i + 1 < end && is_surrogate_pair(src + i)) {
291 unicode = surrogate_pair_to_ucs4(src + i);
298 char tmp[3 + 8 + 1 + 1];
299 snprintf(tmp, _countof(tmp),
"&#x%x;", unicode);