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#include <exception>
14#include <string>
15
16namespace stdex
17{
19 template <class T>
20 inline const wchar_t* sgml2uni(_In_reads_or_z_(count) const T* entity, _In_ size_t count)
21 {
22 assert(entity && count);
23 assert(count < 2 || entity[0] != '#'); // No numeric entities
24
25 for (size_t i = 0, j = _countof(sgml_unicode); i < j; ) {
26 size_t m = (i + j) / 2;
27 if (sgml_unicode[m].sgml[0] < entity[0])
28 i = m + 1;
29 else if (sgml_unicode[m].sgml[0] > entity[0])
30 j = m;
31 else {
32 auto r = strncmp<char, T>(sgml_unicode[m].sgml + 1, _countof(sgml_unicode[0].sgml) - 1, entity + 1, count - 1);
33 if (r < 0)
34 i = m + 1;
35 else if (r > 0)
36 j = m;
37 else {
38 for (; i < m && strncmp<char, T>(sgml_unicode[m - 1].sgml, _countof(sgml_unicode[0].sgml), entity, count) == 0; m--);
39 return sgml_unicode[m].unicode;
40 }
41 }
42 }
43 return nullptr;
44 }
45
46 template <class T>
47 inline const T* sgmlend(
48 _In_reads_or_z_opt_(count) const T* str, _In_ size_t count)
49 {
50 assert(str || !count);
51 for (size_t i = 0; i < count; i++) {
52 if (str[i] == ';')
53 return str + i;
54 if (!str[i] || str[i] == '&' || isspace(str[i]))
55 break;
56 }
57 return nullptr;
58 }
60
61 constexpr int sgml_full = 0x80000000;
62 constexpr int sgml_quot = 0x00000001;
63 constexpr int sgml_apos = 0x00000002;
64 constexpr int sgml_quot_apos = sgml_quot | sgml_apos;
65 constexpr int sgml_amp = 0x00000004;
66 constexpr int sgml_lt_gt = 0x00000008;
67 constexpr int sgml_bsol = 0x00000010;
68 constexpr int sgml_dollar = 0x00000020;
69 constexpr int sgml_percnt = 0x00000040;
70 constexpr int sgml_commat = 0x00000080;
71 constexpr int sgml_num = 0x00000100;
72 constexpr int sgml_lpar_rpar = 0x00000200;
73 constexpr int sgml_lcub_rcub = 0x00000400;
74 constexpr int sgml_lsqb_rsqb = 0x00000800;
75 constexpr int sgml_sgml = sgml_amp | sgml_lt_gt;
76 constexpr int sgml_ml_attrib = sgml_amp | sgml_quot_apos;
77 constexpr int sgml_c = sgml_amp | sgml_bsol | sgml_quot_apos;
78 // constexpr int sgml_ajt_lemma = sgml_amp | sgml_quot | sgml_dollar | sgml_percnt;
79 // constexpr int sgml_ajt_form = sgml_ajt_lemma;
80 // 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;
81
92 template <class T>
93 inline void sgml2wstrcat(
94 _Inout_ std::wstring& dst,
95 _In_reads_or_z_opt_(count_src) const T* src, _In_ size_t count_src,
96 _In_ int skip = 0,
97 _In_ const mapping<size_t>& offset = mapping<size_t>(0, 0),
98 _Inout_opt_ mapping_vector<size_t>* map = nullptr)
99 {
100 assert(src || !count_src);
101
102 const bool
103 skip_quot = (skip & sgml_quot) == 0,
104 skip_apos = (skip & sgml_apos) == 0,
105 skip_amp = (skip & sgml_amp) == 0,
106 skip_lt_gt = (skip & sgml_lt_gt) == 0,
107 skip_bsol = (skip & sgml_bsol) == 0,
108 skip_dollar = (skip & sgml_dollar) == 0,
109 skip_percnt = (skip & sgml_percnt) == 0,
110 skip_commat = (skip & sgml_commat) == 0,
111 skip_num = (skip & sgml_num) == 0,
112 skip_lpar_rpar = (skip & sgml_lpar_rpar) == 0,
113 skip_lcub_rcub = (skip & sgml_lcub_rcub) == 0,
114 skip_lsqb_rsqb = (skip & sgml_lsqb_rsqb) == 0;
115
116 count_src = strnlen(src, count_src);
117 dst.reserve(dst.size() + 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 }
136 else {
137 ucs4_to_surrogate_pair(chr, unicode);
138 chr[2] = 0;
139 }
140#else
141 chr[0] = (wchar_t)unicode;
142 chr[1] = 0;
143#endif
144 entity_w = chr;
145 }
146 else
147 entity_w = sgml2uni(src + i + 1, n);
148
149 if (entity_w &&
150 (skip_quot || (entity_w[0] != L'"')) &&
151 (skip_apos || (entity_w[0] != L'\'')) &&
152 (skip_amp || (entity_w[0] != L'&')) &&
153 (skip_lt_gt || (entity_w[0] != L'<' && entity_w[0] != L'>')) &&
154 (skip_bsol || (entity_w[0] != L'\\')) &&
155 (skip_dollar || (entity_w[0] != L'$')) &&
156 (skip_percnt || (entity_w[0] != L'%')) &&
157 (skip_commat || (entity_w[0] != L'@')) &&
158 (skip_num || (entity_w[0] != L'#')) &&
159 (skip_lpar_rpar || (entity_w[0] != L'(' && entity_w[0] != L')')) &&
160 (skip_lcub_rcub || (entity_w[0] != L'{' && entity_w[0] != L'}')) &&
161 (skip_lsqb_rsqb || (entity_w[0] != L'[' && entity_w[0] != L']')))
162 {
163 if (map) map->push_back(mapping<size_t>(offset.from + i, offset.to + dst.size()));
164 dst.append(entity_w);
165 i = end - src + 1;
166 if (map) map->push_back(mapping<size_t>(offset.from + i, offset.to + dst.size()));
167 continue;
168 }
169 }
170 }
171 dst.append(1, src[i++]);
172 }
173 }
174
175 template <class T>
176 inline _Deprecated_("Use stdex::sgml2wstrcat") void sgml2wstr(
177 _Inout_ std::wstring& dst,
178 _In_reads_or_z_opt_(count_src) const T* src, _In_ size_t count_src,
179 _In_ int skip = 0,
180 _In_ const mapping<size_t>& offset = mapping<size_t>(0, 0),
181 _Inout_opt_ mapping_vector<size_t>* map = nullptr)
182 {
183 sgml2wstrcat(dst, src, count_src, skip, offset, map);
184 }
185
197 template <class T>
198 inline void sgml2wstrcat(
199 _Inout_ std::wstring& dst,
200 _In_ const std::basic_string<T>& src,
201 _In_ int skip = 0,
202 _In_ const mapping<size_t>& offset = mapping<size_t>(0, 0),
203 _Inout_opt_ mapping_vector<size_t>* map = nullptr)
204 {
205 sgml2wstrcat(dst, src.data(), src.size(), skip, offset, map);
206 }
207
208 template <class T>
209 inline _Deprecated_("Use stdex::sgml2wstrcat") void sgml2wstr(
210 _Inout_ std::wstring& dst,
211 _In_ const std::basic_string<T>& src,
212 _In_ int skip = 0,
213 _In_ const mapping<size_t>& offset = mapping<size_t>(0, 0),
214 _Inout_opt_ mapping_vector<size_t>* map = nullptr)
215 {
216 sgml2wstrcat(dst, src, skip, offset, map);
217 }
218
232 template <class T>
233 inline size_t sgml2wstrcat(
234 _Inout_cap_(count_dst) wchar_t* dst, _In_ size_t count_dst,
235 _In_reads_or_z_opt_(count_src) const T* src, _In_ size_t count_src,
236 _In_ int skip = 0,
237 _In_ const mapping<size_t>& offset = mapping<size_t>(0, 0),
238 _Inout_opt_ mapping_vector<size_t>* map = nullptr)
239 {
240 assert(dst || !count_dst);
241 assert(src || !count_src);
242
243 static const std::invalid_argument buffer_overrun("buffer overrun");
244 const bool
245 skip_quot = (skip & sgml_quot) == 0,
246 skip_apos = (skip & sgml_apos) == 0,
247 skip_amp = (skip & sgml_amp) == 0,
248 skip_lt_gt = (skip & sgml_lt_gt) == 0,
249 skip_bsol = (skip & sgml_bsol) == 0,
250 skip_dollar = (skip & sgml_dollar) == 0,
251 skip_percnt = (skip & sgml_percnt) == 0,
252 skip_commat = (skip & sgml_commat) == 0,
253 skip_num = (skip & sgml_num) == 0,
254 skip_lpar_rpar = (skip & sgml_lpar_rpar) == 0,
255 skip_lcub_rcub = (skip & sgml_lcub_rcub) == 0,
256 skip_lsqb_rsqb = (skip & sgml_lsqb_rsqb) == 0;
257
258 size_t j = wcsnlen(dst, count_dst);
259 count_src = strnlen(src, count_src);
260 for (size_t i = 0; i < count_src;) {
261 if (src[i] == '&') {
262 auto end = sgmlend(src + i + 1, count_src - i - 1);
263 if (end) {
264 const wchar_t* entity_w;
265 wchar_t chr[3];
266 size_t n = end - src - i - 1;
267 if (n >= 2 && src[i + 1] == '#') {
268 uint32_t unicode;
269 if (src[i + 2] == 'x' || src[i + 2] == 'X')
270 unicode = strtou32(src + i + 3, n - 2, nullptr, 16);
271 else
272 unicode = strtou32(src + i + 2, n - 1, nullptr, 10);
273#ifdef _WIN32
274 if (unicode < 0x10000) {
275 chr[0] = (wchar_t)unicode;
276 chr[1] = 0;
277 }
278 else {
279 ucs4_to_surrogate_pair(chr, unicode);
280 chr[2] = 0;
281 }
282#else
283 chr[0] = (wchar_t)unicode;
284 chr[1] = 0;
285#endif
286 entity_w = chr;
287 }
288 else
289 entity_w = sgml2uni(src + i + 1, n);
290
291 if (entity_w &&
292 (skip_quot || (entity_w[0] != L'"')) &&
293 (skip_apos || (entity_w[0] != L'\'')) &&
294 (skip_amp || (entity_w[0] != L'&')) &&
295 (skip_lt_gt || (entity_w[0] != L'<' && entity_w[0] != L'>')) &&
296 (skip_bsol || (entity_w[0] != L'\\')) &&
297 (skip_dollar || (entity_w[0] != L'$')) &&
298 (skip_percnt || (entity_w[0] != L'%')) &&
299 (skip_commat || (entity_w[0] != L'@')) &&
300 (skip_num || (entity_w[0] != L'#')) &&
301 (skip_lpar_rpar || (entity_w[0] != L'(' && entity_w[0] != L')')) &&
302 (skip_lcub_rcub || (entity_w[0] != L'{' && entity_w[0] != L'}')) &&
303 (skip_lsqb_rsqb || (entity_w[0] != L'[' && entity_w[0] != L']')))
304 {
305 if (map) map->push_back(mapping<size_t>(offset.from + i, offset.to + j));
306 size_t m = wcslen(entity_w);
307 if (j + m >= count_dst)
308 throw buffer_overrun;
309 memcpy(dst + j, entity_w, m * sizeof(wchar_t)); j += m;
310 i = end - src + 1;
311 if (map) map->push_back(mapping<size_t>(offset.from + i, offset.to + j));
312 continue;
313 }
314 }
315 }
316 if (j + 1 >= count_dst)
317 throw buffer_overrun;
318 dst[j++] = src[i++];
319 }
320 if (j >= count_dst)
321 throw buffer_overrun;
322 dst[j] = 0;
323 return j;
324 }
325
326 template <class T>
327 inline _Deprecated_("Use stdex::sgml2wstrcat") size_t sgml2wstr(
328 _Inout_cap_(count_dst) wchar_t* dst, _In_ size_t count_dst,
329 _In_reads_or_z_opt_(count_src) const T* src, _In_ size_t count_src,
330 _In_ int skip = 0,
331 _In_ const mapping<size_t>& offset = mapping<size_t>(0, 0),
332 _Inout_opt_ mapping_vector<size_t>* map = nullptr)
333 {
334 return sgml2wstrcat(dst, count_dst, src, count_src, skip, offset, map);
335 }
336
347 template <class T>
348 inline void sgml2wstrcpy(
349 _Inout_ std::wstring& dst,
350 _In_reads_or_z_opt_(count_src) const T* src, _In_ size_t count_src,
351 _In_ int skip = 0,
352 _In_ const mapping<size_t>& offset = mapping<size_t>(0, 0),
353 _Inout_opt_ mapping_vector<size_t>* map = nullptr)
354 {
355 dst.clear();
356 if (map)
357 map->clear();
358 sgml2wstrcat(dst, src, count_src, skip, offset, map);
359 }
360
370 template<class _Elem, class _Traits, class _Ax>
371 inline void sgml2wstrcpy(
372 _Inout_ std::wstring& dst,
373 _In_ const std::basic_string<_Elem, _Traits, _Ax>& src,
374 _In_ int skip = 0,
375 _In_ const mapping<size_t>& offset = mapping<size_t>(0, 0),
376 _Inout_opt_ mapping_vector<size_t>* map = nullptr)
377 {
378 sgml2wstrcpy(dst, src.data(), src.size(), skip, offset, map);
379 }
380
394 template <class T>
395 inline size_t sgml2wstrcpy(
396 _Inout_cap_(count_dst) wchar_t* dst, _In_ size_t count_dst,
397 _In_reads_or_z_opt_(count_src) const T* src, _In_ size_t count_src,
398 _In_ int skip = 0,
399 _In_ const mapping<size_t>& offset = mapping<size_t>(0, 0),
400 _Inout_opt_ mapping_vector<size_t>* map = nullptr)
401 {
402 assert(dst || !count_dst);
403 if (count_dst)
404 dst[0] = 0;
405 if (map)
406 map->clear();
407 return sgml2wstrcat(dst, count_dst, src, count_src, skip, offset, map);
408 }
409
421 template <class T>
422 inline std::wstring sgml2wstr(
423 _In_reads_or_z_opt_(count_src) const T* src, _In_ size_t count_src,
424 _In_ int skip = 0,
425 _In_ const mapping<size_t>& offset = mapping<size_t>(0, 0),
426 _Inout_opt_ mapping_vector<size_t>* map = nullptr)
427 {
428 std::wstring dst;
429 sgml2wstrcat(dst, src, count_src, skip, offset, map);
430 return dst;
431 }
432
443 template <class T>
444 inline std::wstring sgml2wstr(
445 _In_ const std::basic_string<T>& src,
446 _In_ int skip = 0,
447 _In_ const mapping<size_t>& offset = mapping<size_t>(0, 0),
448 _Inout_opt_ mapping_vector<size_t>* map = nullptr)
449 {
450 return sgml2wstr(src.c_str(), src.size(), skip, offset, map);
451 }
452
454 inline const char* chr2sgml(_In_reads_or_z_(count) const wchar_t* entity, _In_ size_t count)
455 {
456 assert(entity && count);
457
458 const wchar_t e2 = entity[0];
459 for (size_t i = 0, j = _countof(unicode_sgml); i < j; ) {
460 size_t m = (i + j) / 2;
461 wchar_t e1 = sgml_unicode[unicode_sgml[m]].unicode[0];
462 if (e1 < e2)
463 i = m + 1;
464 else if (e1 > e2)
465 j = m;
466 else {
467 auto r = strncmp(sgml_unicode[unicode_sgml[m]].unicode + 1, _countof(sgml_unicode[0].unicode) - 1, entity + 1, count - 1);
468 if (r < 0)
469 i = m + 1;
470 else if (r > 0)
471 j = m;
472 else {
473 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--);
474 return sgml_unicode[unicode_sgml[m]].sgml;
475 }
476 }
477 }
478 return nullptr;
479 }
481
490 inline void wstr2sgmlcat(
491 _Inout_ std::string& dst,
492 _In_reads_or_z_opt_(count_src) const wchar_t* src, _In_ size_t count_src,
493 _In_ size_t what = 0)
494 {
495 assert(src || !count_src);
496
497 const bool
498 do_ascii = (what & sgml_full) == 0,
499 do_quot = (what & sgml_quot) == 0,
500 do_apos = (what & sgml_apos) == 0,
501 do_lt_gt = (what & sgml_lt_gt) == 0,
502 do_bsol = (what & sgml_bsol) == 0,
503 do_dollar = (what & sgml_dollar) == 0,
504 do_percnt = (what & sgml_percnt) == 0,
505 do_commat = (what & sgml_commat) == 0,
506 do_num = (what & sgml_num) == 0,
507 do_lpar_rpar = (what & sgml_lpar_rpar) == 0,
508 do_lcub_rcub = (what & sgml_lcub_rcub) == 0,
509 do_lsqb_rsqb = (what & sgml_lsqb_rsqb) == 0;
510
511 count_src = wcsnlen(src, count_src);
512 dst.reserve(dst.size() + count_src);
513 for (size_t i = 0; i < count_src;) {
514 size_t n = glyphlen(src + i, count_src - i);
515 if (n == 1 &&
516 do_ascii && (unsigned int)src[i] < 128 &&
517 src[i] != L'&' &&
518 (do_quot || (src[i] != L'"')) &&
519 (do_apos || (src[i] != L'\'')) &&
520 (do_lt_gt || (src[i] != L'<' && src[i] != L'>')) &&
521 (do_bsol || (src[i] != L'\\')) &&
522 (do_dollar || (src[i] != L'$')) &&
523 (do_percnt || (src[i] != L'%')) &&
524 (do_commat || (src[i] != L'@')) &&
525 (do_num || (src[i] != L'#')) &&
526 (do_lpar_rpar || (src[i] != L'(' && src[i] != L')')) &&
527 (do_lcub_rcub || (src[i] != L'{' && src[i] != L'}')) &&
528 (do_lsqb_rsqb || (src[i] != L'[' && src[i] != L']')))
529 {
530 // 7-bit ASCII and no desire to encode it as an SGML entity.
531 dst.append(1, static_cast<char>(src[i++]));
532 }
533 else {
534 const char* entity = chr2sgml(src + i, n);
535 if (entity) {
536 dst.append(1, '&');
537 dst.append(entity);
538 dst.append(1, ';');
539 i += n;
540 }
541 else if (n == 1) {
542 // Trivial character (1 code unit, 1 glyph), no entity available.
543 if ((unsigned int)src[i] < 128)
544 dst.append(1, static_cast<char>(src[i++]));
545 else {
546 char tmp[3 + 8 + 1 + 1];
547 snprintf(tmp, _countof(tmp), "&#x%x;", src[i++]);
548 dst.append(tmp);
549 }
550 }
551 else {
552 // Non-trivial character. Decompose.
553 const size_t end = i + n;
554 while (i < end) {
555 if ((entity = chr2sgml(src + i, 1)) != nullptr) {
556 dst.append(1, '&');
557 dst.append(entity);
558 dst.append(1, ';');
559 i++;
560 }
561 else if ((unsigned int)src[i] < 128)
562 dst.append(1, static_cast<char>(src[i++]));
563 else {
564 uint32_t unicode;
565#ifdef _WIN32
566 if (i + 1 < end && is_surrogate_pair(src + i)) {
567 unicode = surrogate_pair_to_ucs4(src + i);
568 i += 2;
569 }
570 else
571#endif
572 {
573 unicode = src[i++];
574 }
575 char tmp[3 + 8 + 1 + 1];
576 snprintf(tmp, _countof(tmp), "&#x%x;", unicode);
577 dst.append(tmp);
578 }
579 }
580 }
581 }
582 }
583 }
584
585 inline _Deprecated_("Use stdex::wstr2sgmlcat") void wstr2sgml(
586 _Inout_ std::string& dst,
587 _In_reads_or_z_opt_(count_src) const wchar_t* src, _In_ size_t count_src,
588 _In_ size_t what = 0)
589 {
590 wstr2sgmlcat(dst, src, count_src, what);
591 }
592
600 inline void wstr2sgmlcat(
601 _Inout_ std::string& dst,
602 _In_ const std::wstring& src,
603 _In_ size_t what = 0)
604 {
605 wstr2sgmlcat(dst, src.c_str(), src.size(), what);
606 }
607
608 inline _Deprecated_("Use stdex::wstr2sgmlcat") void wstr2sgml(
609 _Inout_ std::string& dst,
610 _In_ const std::wstring& src,
611 _In_ size_t what = 0)
612 {
613 wstr2sgmlcat(dst, src, what);
614 }
615
627 inline size_t wstr2sgmlcat(
628 _Inout_cap_(count_dst) char* dst, _In_ size_t count_dst,
629 _In_reads_or_z_opt_(count_src) const wchar_t* src, _In_ size_t count_src,
630 _In_ size_t what = 0)
631 {
632 assert(dst || !count_dst);
633 assert(src || !count_src);
634
635 static const std::invalid_argument buffer_overrun("buffer overrun");
636 const bool
637 do_ascii = (what & sgml_full) == 0,
638 do_quot = (what & sgml_quot) == 0,
639 do_apos = (what & sgml_apos) == 0,
640 do_lt_gt = (what & sgml_lt_gt) == 0,
641 do_bsol = (what & sgml_bsol) == 0,
642 do_dollar = (what & sgml_dollar) == 0,
643 do_percnt = (what & sgml_percnt) == 0,
644 do_commat = (what & sgml_commat) == 0,
645 do_num = (what & sgml_num) == 0,
646 do_lpar_rpar = (what & sgml_lpar_rpar) == 0,
647 do_lcub_rcub = (what & sgml_lcub_rcub) == 0,
648 do_lsqb_rsqb = (what & sgml_lsqb_rsqb) == 0;
649
650 size_t j = strnlen(dst, count_dst);
651 count_src = wcsnlen(src, count_src);
652 for (size_t i = 0; i < count_src;) {
653 size_t n = glyphlen(src + i, count_src - i);
654 if (n == 1 &&
655 do_ascii && (unsigned int)src[i] < 128 &&
656 src[i] != L'&' &&
657 (do_quot || (src[i] != L'"')) &&
658 (do_apos || (src[i] != L'\'')) &&
659 (do_lt_gt || (src[i] != L'<' && src[i] != L'>')) &&
660 (do_bsol || (src[i] != L'\\')) &&
661 (do_dollar || (src[i] != L'$')) &&
662 (do_percnt || (src[i] != L'%')) &&
663 (do_commat || (src[i] != L'@')) &&
664 (do_num || (src[i] != L'#')) &&
665 (do_lpar_rpar || (src[i] != L'(' && src[i] != L')')) &&
666 (do_lcub_rcub || (src[i] != L'{' && src[i] != L'}')) &&
667 (do_lsqb_rsqb || (src[i] != L'[' && src[i] != L']')))
668 {
669 // 7-bit ASCII and no desire to encode it as an SGML entity.
670 if (j + 1 >= count_dst)
671 throw buffer_overrun;
672 dst[j++] = static_cast<char>(src[i++]);
673 }
674 else {
675 const char* entity = chr2sgml(src + i, n);
676 if (entity) {
677 size_t m = strlen(entity);
678 if (j + m + 2 >= count_dst)
679 throw buffer_overrun;
680 dst[j++] = '&';
681 memcpy(dst + j, entity, m * sizeof(char)); j += m;
682 dst[j++] = ';';
683 i += n;
684 }
685 else if (n == 1) {
686 // Trivial character (1 code unit, 1 glyph), no entity available.
687 if ((unsigned int)src[i] < 128) {
688 if (j + 1 >= count_dst)
689 throw buffer_overrun;
690 dst[j++] = static_cast<char>(src[i++]);
691 }
692 else {
693 char tmp[3 + 8 + 1 + 1];
694 int m = snprintf(tmp, _countof(tmp), "&#x%x;", src[i++]);
695 assert(m >= 0);
696 if (static_cast<size_t>(m) >= count_dst)
697 throw buffer_overrun;
698 memcpy(dst + j, tmp, m * sizeof(char)); j += m;
699 }
700 }
701 else {
702 // Non-trivial character. Decompose.
703 const size_t end = i + n;
704 while (i < end) {
705 if ((entity = chr2sgml(src + i, 1)) != nullptr) {
706 size_t m = strlen(entity);
707 if (j + m + 2 >= count_dst)
708 throw buffer_overrun;
709 dst[j++] = '&';
710 memcpy(dst + j, entity, m * sizeof(char)); j += m;
711 dst[j++] = ';';
712 i++;
713 }
714 else if ((unsigned int)src[i] < 128) {
715 if (j + 1 >= count_dst)
716 throw buffer_overrun;
717 dst[j++] = static_cast<char>(src[i++]);
718 }
719 else {
720 uint32_t unicode;
721#ifdef _WIN32
722 if (i + 1 < end && is_surrogate_pair(src + i)) {
723 unicode = surrogate_pair_to_ucs4(src + i);
724 i += 2;
725 }
726 else
727#endif
728 {
729 unicode = src[i++];
730 }
731 char tmp[3 + 8 + 1 + 1];
732 int m = snprintf(tmp, _countof(tmp), "&#x%x;", unicode);
733 assert(m >= 0);
734 if (static_cast<size_t>(m) >= count_dst)
735 throw buffer_overrun;
736 memcpy(dst + j, tmp, m * sizeof(char)); j += m;
737 }
738 }
739 }
740 }
741 }
742 if (j >= count_dst)
743 throw buffer_overrun;
744 dst[j] = 0;
745 return j;
746 }
747
748 inline _Deprecated_("Use stdex::wstr2sgmlcat") size_t wstr2sgml(
749 _Inout_cap_(count_dst) char* dst, _In_ size_t count_dst,
750 _In_reads_or_z_opt_(count_src) const wchar_t* src, _In_ size_t count_src,
751 _In_ size_t what = 0)
752 {
753 return wstr2sgmlcat(dst, count_dst, src, count_src, what);
754 }
755
764 inline void wstr2sgmlcpy(
765 _Inout_ std::string& dst,
766 _In_reads_or_z_opt_(count_src) const wchar_t* src, _In_ size_t count_src,
767 _In_ size_t what = 0)
768 {
769 dst.clear();
770 wstr2sgmlcat(dst, src, count_src, what);
771 }
772
780 inline void wstr2sgmlcpy(
781 _Inout_ std::string& dst,
782 _In_ const std::wstring& src,
783 _In_ size_t what = 0)
784 {
785 wstr2sgmlcpy(dst, src.data(), src.size(), what);
786 }
787
799 inline size_t wstr2sgmlcpy(
800 _Inout_cap_(count_dst) char* dst, _In_ size_t count_dst,
801 _In_reads_or_z_opt_(count_src) const wchar_t* src, _In_ size_t count_src,
802 _In_ size_t what = 0)
803 {
804 assert(dst || !count_dst);
805 if (count_dst)
806 dst[0] = 0;
807 return wstr2sgmlcat(dst, count_dst, src, count_src, what);
808 }
809
819 inline std::string wstr2sgml(
820 _In_reads_or_z_opt_(count_src) const wchar_t* src, _In_ size_t count_src,
821 _In_ size_t what = 0)
822 {
823 std::string dst;
824 wstr2sgmlcat(dst, src, count_src, what);
825 return dst;
826 }
827
836 inline std::string wstr2sgml(
837 _In_ const std::wstring& src,
838 _In_ size_t what = 0)
839 {
840 return wstr2sgml(src.c_str(), src.size(), what);
841 }
842}