stdex
Additional custom or not Standard C++ covered algorithms
Loading...
Searching...
No Matches
sgml.cpp
1/*
2 SPDX-License-Identifier: MIT
3 Copyright © 2023 Amebis
4*/
5
6#include "pch.h"
7
8using namespace std;
9#ifdef _WIN32
10using namespace Microsoft::VisualStudio::CppUnitTestFramework;
11#endif
12
13namespace UnitTests
14{
15 TEST_CLASS(sgml)
16 {
17 public:
18 TEST_METHOD(sgml2wstr)
19 {
20 Assert::AreEqual(L"This is a test.", stdex::sgml2wstr("This is a test.", (size_t)-1).c_str());
21 Assert::AreEqual(L"Th\u00ed\u0161 i\u22c5 a te\u0073\u0304t.&unknown;😀😅", stdex::sgml2wstr("Thíš i⋅ a te&smacr;t.&unknown;😀😅", (size_t)-1).c_str());
22 Assert::AreEqual(L"This", stdex::sgml2wstr("This is a test.", 4).c_str());
23 Assert::AreEqual(L"T\u0068\u0301", stdex::sgml2wstr("T&hacute;is is a test.", 9).c_str());
24 Assert::AreEqual(L"T&hac", stdex::sgml2wstr("T&hacute;is is a test.", 5).c_str());
25 Assert::AreEqual(L"The "quoted" & text.", stdex::sgml2wstr("The "quoted" & text.", (size_t)-1, stdex::sgml_c).c_str());
26
27 stdex::mapping_vector<size_t> map;
28 constexpr size_t i = 0;
29 constexpr size_t j = 0;
30 stdex::sgml2wstr("Th&iacute;&scaron; i&sdot; &#97; te&smacr;t.&unknown;&#x1F600;&#X1f605;", (size_t)-1, 0, stdex::mapping<size_t>(i, j), &map);
31 Assert::IsTrue(stdex::mapping_vector<size_t>{
32 { i + 2, j + 2 },
33 { i + 10, j + 3 },
34 { i + 10, j + 3 },
35 { i + 18, j + 4 },
36 { i + 20, j + 6 },
37 { i + 26, j + 7 },
38 { i + 27, j + 8 },
39 { i + 32, j + 9 },
40 { i + 35, j + 12 },
41 { i + 42, j + 14 },
42 { i + 53, j + 25 },
43#ifdef _WIN32 // wchar_t* is UTF-16
44 { i + 62, j + 27 },
45 { i + 62, j + 27 },
46 { i + 71, j + 29 },
47#else // wchar_t* is UTF-32
48 { i + 62, j + 26 },
49 { i + 62, j + 26 },
50 { i + 71, j + 27 },
51#endif
52 } == map);
53 }
54
55 TEST_METHOD(wstr2sgml)
56 {
57 Assert::AreEqual("This is a test.", stdex::wstr2sgml(L"This is a test.", (size_t)-1).c_str());
58 Assert::AreEqual("Th&iacute;&scaron; i&sdot; a te&smacr;t.&amp;unknown;&#x1f600;&#x1f605;", stdex::wstr2sgml(L"Th\u00ed\u0161 i\u22c5 a te\u0073\u0304t.&unknown;😀😅", (size_t)-1).c_str());
59 Assert::AreEqual("This", stdex::wstr2sgml(L"This is a test.", 4).c_str());
60 Assert::AreEqual("te&smacr;", stdex::wstr2sgml(L"te\u0073\u0304t", 4).c_str());
61 Assert::AreEqual("tes", stdex::wstr2sgml(L"te\u0073\u0304t", 3).c_str());
62 Assert::AreEqual("&#x2318;&permil;&#x362;", stdex::wstr2sgml(L"⌘‰͢", (size_t)-1).c_str());
63 Assert::AreEqual("$\"<>&amp;", stdex::wstr2sgml(L"$\"<>&", (size_t)-1).c_str());
64 Assert::AreEqual("$&quot;<>&amp;", stdex::wstr2sgml(L"$\"<>&", (size_t)-1, stdex::sgml_c).c_str());
65 }
66 };
67}
Maps index in source string to index in destination string.
Definition mapping.hpp:17