stdex
Additional custom or not Standard C++ covered algorithms
Loading...
Searching...
No Matches
sgml.hpp
1/*
2 SPDX-License-Identifier: MIT
3 Copyright © 2023 Amebis
4*/
5
6#pragma once
7
8#include "mapping.hpp"
9#include "sal.hpp"
10#include "sgml_unicode.hpp"
11#include "string.hpp"
12#include <assert.h>
13
14namespace stdex
15{
17 template <class T>
18 inline const wchar_t* sgml2uni(_In_reads_or_z_(count) const T* entity, _In_ size_t count)
19 {
20 assert(entity && count);
21 assert(count < 2 || entity[0] != '#'); // No numeric entities
22
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])
26 i = m + 1;
27 else if (sgml_unicode[m].sgml[0] > entity[0])
28 j = m;
29 else {
30 auto r = strncmp<char, T>(sgml_unicode[m].sgml + 1, _countof(sgml_unicode[0].sgml) - 1, entity + 1, count - 1);
31 if (r < 0)
32 i = m + 1;
33 else if (r > 0)
34 j = m;
35 else {
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;
38 }
39 }
40 }
41 return nullptr;
42 }
43
44 template <class T>
45 inline const T* sgmlend(
46 _In_reads_or_z_(count) const T* str,
47 _In_ size_t count)
48 {
49 assert(str || !count);
50 for (size_t i = 0; i < count; i++) {
51 if (str[i] == ';')
52 return str + i;
53 if (!str[i] || str[i] == '&' || isspace(str[i]))
54 break;
55 }
56 return nullptr;
57 }
59
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;
77 // constexpr int sgml_ajt_lemma = sgml_amp | sgml_quot | sgml_dollar | sgml_percnt;
78 // constexpr int sgml_ajt_form = sgml_ajt_lemma;
79 // constexpr int sgml_kolos = sgml_amp | sgml_quot | sgml_dollar | sgml_percnt | sgml_lt_gt | sgml_bsol/* | sgml_commat | sgml_num*/ | sgml_lpar_rpar | sgml_lcub_rcub | sgml_lsqb_rsqb;
80
92 template <class T>
93 inline std::wstring sgml2str(
94 _In_reads_or_z_(count_src) const T* src, _In_ size_t count_src,
95 _In_ int skip = 0,
96 _In_ const mapping<size_t>& offset = mapping<size_t>(0, 0),
97 _Inout_opt_ mapping_vector<size_t>* map = nullptr)
98 {
99 assert(src || !count_src);
100
101 const bool
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;
114
115 count_src = strnlen(src, count_src);
116 std::wstring dst;
117 dst.reserve(count_src);
118 for (size_t i = 0; i < count_src;) {
119 if (src[i] == '&') {
120 auto end = sgmlend(src + i + 1, count_src - i - 1);
121 if (end) {
122 const wchar_t* entity_w;
123 wchar_t chr[3];
124 size_t n = end - src - i - 1;
125 if (n >= 2 && src[i + 1] == '#') {
126 uint32_t unicode;
127 if (src[i + 2] == 'x' || src[i + 2] == 'X')
128 unicode = strtou32(src + i + 3, n - 2, nullptr, 16);
129 else
130 unicode = strtou32(src + i + 2, n - 1, nullptr, 10);
131#ifdef _WIN32
132 if (unicode < 0x10000) {
133 chr[0] = (wchar_t)unicode;
134 chr[1] = 0;
135 } else {
136 ucs4_to_surrogate_pair(chr, unicode);
137 chr[2] = 0;
138 }
139#else
140 chr[0] = (wchar_t)unicode;
141 chr[1] = 0;
142#endif
143 entity_w = chr;
144 }
145 else
146 entity_w = sgml2uni(src + i + 1, n);
147
148 if (entity_w &&
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']')))
161 {
162 if (map) map->push_back(mapping<size_t>(offset.from + i, offset.to + dst.size()));
163 dst.append(entity_w);
164 i = end - src + 1;
165 if (map) map->push_back(mapping<size_t>(offset.from + i, offset.to + dst.size()));
166 continue;
167 }
168 }
169 }
170 dst.append(1, src[i++]);
171 }
172 return dst;
173 }
174
176 inline const char* chr2sgml(_In_reads_or_z_(count) const wchar_t* entity, _In_ size_t count)
177 {
178 assert(entity && count);
179
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];
184 if (e1 < e2)
185 i = m + 1;
186 else if (e1 > e2)
187 j = m;
188 else {
189 auto r = strncmp(sgml_unicode[unicode_sgml[m]].unicode + 1, _countof(sgml_unicode[0].unicode) - 1, entity + 1, count - 1);
190 if (r < 0)
191 i = m + 1;
192 else if (r > 0)
193 j = m;
194 else {
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;
197 }
198 }
199 }
200 return nullptr;
201 }
203
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)
217 {
218 assert(src || !count_src);
219
220 const bool
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;
233
234 count_src = wcsnlen(src, count_src);
235 std::string dst;
236 dst.reserve(count_src);
237 for (size_t i = 0; i < count_src;) {
238 size_t n = glyphlen(src + i, count_src - i);
239 if (n == 1 &&
240 do_ascii && (unsigned int)src[i] < 128 &&
241 src[i] != L'&' &&
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']')))
253 {
254 // 7-bit ASCII and no desire to encode it as an SGML entity.
255 dst.append(1, (char)src[i++]);
256 }
257 else {
258 const char* entity = chr2sgml(src + i, n);
259 if (entity) {
260 dst.append(1, '&');
261 dst.append(entity);
262 dst.append(1, ';');
263 i += n;
264 }
265 else if (n == 1) {
266 // Trivial character (1 code unit, 1 glyph), no entity available.
267 if ((unsigned int)src[i] < 128)
268 dst.append(1, (char)src[i++]);
269 else {
270 char tmp[3 + 8 + 1 + 1];
271 snprintf(tmp, _countof(tmp), "&#x%x;", src[i++]);
272 dst.append(tmp);
273 }
274 }
275 else {
276 // Non-trivial character. Decompose.
277 const size_t end = i + n;
278 while (i < end) {
279 if ((entity = chr2sgml(src + i, 1)) != nullptr) {
280 dst.append(1, '&');
281 dst.append(entity);
282 dst.append(1, ';');
283 i++;
284 }
285 else if ((unsigned int)src[i] < 128)
286 dst.append(1, (char)src[i++]);
287 else {
288 uint32_t unicode;
289#ifdef _WIN32
290 if (i + 1 < end && is_surrogate_pair(src + i)) {
291 unicode = surrogate_pair_to_ucs4(src + i);
292 i += 2;
293 } else
294#endif
295 {
296 unicode = src[i++];
297 }
298 char tmp[3 + 8 + 1 + 1];
299 snprintf(tmp, _countof(tmp), "&#x%x;", unicode);
300 dst.append(tmp);
301 }
302 }
303 }
304 }
305 }
306 return dst;
307 }
308}