Debian Codegraph @

js/src/jsapi-tests/testCharacterEncoding.cpp

raw
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5 #include "mozilla/TextUtils.h"
6
7 #include <clocale>
8 #include <cstring>
9 #include <cwchar>
10 #include <initializer_list>
11 #include <iterator>
12 #include <string_view>
13
14 #include "js/CharacterEncoding.h"
15 #include "jsapi-tests/tests.h"
16
17 static bool EqualsIgnoreCase(const char* xs, const char* ys) {
18 while (*xs && *ys) {
19 char x = *xs++;
20 char y = *ys++;
21
22 // Convert both to lower-case.
23 if (mozilla::IsAsciiAlpha(x) && mozilla::IsAsciiAlpha(y)) {
24 x |= 0x20;
25 y |= 0x20;
26 }
27
28 // Fail if the characters aren't the same.
29 if (x != y) {
30 return false;
31 }
32 }
33
34 // Both strings must be read to the end.
35 return !*xs && !*ys;
36 }
37
38 class ToUTF8Locale {
39 const char* previousLocale_ = nullptr;
40 bool supported_ = false;
41
42 public:
43 ToUTF8Locale() {
44 // Store the old locale so we can reset it in the destructor.
45 previousLocale_ = std::setlocale(LC_ALL, nullptr);
46
47 // Query the system default locale.
48 const char* defaultLocale = std::setlocale(LC_ALL, "");
49 if (!defaultLocale) {
50 // std::setlocale returns nullptr on failure.
51 return;
52 }
53
54 // Switch the default locale to be UTF-8 aware.
55 const char* newLocale = std::setlocale(LC_ALL, "en_US.UTF-8");
56 if (!newLocale) {
57 // std::setlocale returns nullptr on failure.
58 return;
59 }
60
61 const char* defaultCodepage = std::strchr(defaultLocale, '.');
62 const char* newCodepage = std::strchr(newLocale, '.');
63
64 // Return if either the default or new locale don't contain a code-page.
65 if (!defaultCodepage || !newCodepage) {
66 return;
67 }
68
69 // Skip past the '.'.
70 defaultCodepage++;
71 newCodepage++;
72
73 // UTF-8 is supported when the default locale and new locale support it:
74 //
75 // The default locale needs to support UTF-8, because this test is compiled
76 // using the default locale.
77 //
78 // The new locale needs to support UTF-8 to ensure UTF-8 encoding works at
79 // runtime.
80 supported_ = EqualsIgnoreCase(defaultCodepage, "UTF-8") &&
81 EqualsIgnoreCase(newCodepage, "UTF-8");
82 }
83
84 bool supported() const { return supported_; }
85
86 ~ToUTF8Locale() {
87 // Restore the previous locale.
88 if (previousLocale_) {
89 std::setlocale(LC_ALL, previousLocale_);
90 }
91 }
92 };
93
94 BEGIN_TEST(testCharacterEncoding_narrow_to_utf8) {
95 // Assume the narrow charset is ASCII-compatible. ASCII to UTF-8 conversion is
96 // a no-op.
97 for (std::string_view string : {
98 "",
99 "a",
100 "abc",
101 "abc\0def",
102 }) {
103 auto utf8 = JS::EncodeNarrowToUtf8(cx, string.data());
104 CHECK(utf8 != nullptr);
105 CHECK_EQUAL(std::strlen(utf8.get()), string.length());
106 CHECK(utf8.get() == string);
107 }
108 return true;
109 }
110 END_TEST(testCharacterEncoding_narrow_to_utf8)
111
112 BEGIN_TEST(testCharacterEncoding_wide_to_utf8) {
113 // Assume the wide charset is ASCII-compatible. ASCII to UTF-8 conversion is
114 // a no-op.
115 for (std::wstring_view string : {
116 L"",
117 L"a",
118 L"abc",
119 L"abc\0def",
120 }) {
121 auto utf8 = JS::EncodeWideToUtf8(cx, string.data());
122 CHECK(utf8 != nullptr);
123 CHECK_EQUAL(std::strlen(utf8.get()), string.length());
124 CHECK(std::equal(
125 string.begin(), string.end(), utf8.get(),
126 [](wchar_t x, char y) { return char32_t(x) == char32_t(y); }));
127 }
128 return true;
129 }
130 END_TEST(testCharacterEncoding_wide_to_utf8)
131
132 BEGIN_TEST(testCharacterEncoding_wide_to_utf8_non_ascii) {
133 // Change the locale to be UTF-8 aware for the emoji string.
134 ToUTF8Locale utf8locale;
135
136 // Skip this test if UTF-8 isn't supported on this system.
137 if (!utf8locale.supported()) {
138 return true;
139 }
140
141 {
142 std::wstring_view string = L"ä";
143 auto utf8 = JS::EncodeWideToUtf8(cx, string.data());
144 CHECK(utf8 != nullptr);
145
146 CHECK_EQUAL(std::strlen(utf8.get()), 2U);
147 CHECK_EQUAL(utf8[0], char(0xC3));
148 CHECK_EQUAL(utf8[1], char(0xA4));
149 }
150 {
151 std::wstring_view string = L"💩";
152 auto utf8 = JS::EncodeWideToUtf8(cx, string.data());
153 CHECK(utf8 != nullptr);
154
155 CHECK_EQUAL(std::strlen(utf8.get()), 4U);
156 CHECK_EQUAL(utf8[0], char(0xF0));
157 CHECK_EQUAL(utf8[1], char(0x9F));
158 CHECK_EQUAL(utf8[2], char(0x92));
159 CHECK_EQUAL(utf8[3], char(0xA9));
160 }
161 return true;
162 }
163 END_TEST(testCharacterEncoding_wide_to_utf8_non_ascii)
164
165 BEGIN_TEST(testCharacterEncoding_utf8_to_narrow) {
166 // Assume the narrow charset is ASCII-compatible. ASCII to UTF-8 conversion is
167 // a no-op.
168 for (std::string_view string : {
169 "",
170 "a",
171 "abc",
172 "abc\0def",
173 }) {
174 auto narrow = JS::EncodeUtf8ToNarrow(cx, string.data());
175 CHECK(narrow != nullptr);
176 CHECK_EQUAL(std::strlen(narrow.get()), string.length());
177 CHECK(narrow.get() == string);
178 }
179 return true;
180 }
181 END_TEST(testCharacterEncoding_utf8_to_narrow)
182
183 BEGIN_TEST(testCharacterEncoding_utf8_to_wide) {
184 // Assume the wide charset is ASCII-compatible. ASCII to UTF-8 conversion is
185 // a no-op.
186 for (std::string_view string : {
187 "",
188 "a",
189 "abc",
190 "abc\0def",
191 }) {
192 auto wide = JS::EncodeUtf8ToWide(cx, string.data());
193 CHECK(wide != nullptr);
194 CHECK_EQUAL(std::wcslen(wide.get()), string.length());
195 CHECK(std::equal(
196 string.begin(), string.end(), wide.get(),
197 [](char x, wchar_t y) { return char32_t(x) == char32_t(y); }));
198 }
199 return true;
200 }
201 END_TEST(testCharacterEncoding_utf8_to_wide)
202
203 BEGIN_TEST(testCharacterEncoding_narrow_roundtrip) {
204 // Change the locale to be UTF-8 aware for the emoji string.
205 ToUTF8Locale utf8locale;
206
207 // Skip this test if UTF-8 isn't supported on this system.
208 if (!utf8locale.supported()) {
209 return true;
210 }
211
212 for (std::string_view string : {
213 "",
214 "a",
215 "abc",
216 "ä",
217 "💩",
218 }) {
219 auto utf8 = JS::EncodeNarrowToUtf8(cx, string.data());
220 CHECK(utf8 != nullptr);
221
222 auto narrow = JS::EncodeUtf8ToNarrow(cx, utf8.get());
223 CHECK(narrow != nullptr);
224
225 CHECK(narrow.get() == string);
226 }
227 return true;
228 }
229 END_TEST(testCharacterEncoding_narrow_roundtrip)
230
231 BEGIN_TEST(testCharacterEncoding_wide_roundtrip) {
232 // Change the locale to be UTF-8 aware for the emoji string.
233 ToUTF8Locale utf8locale;
234
235 // Skip this test if UTF-8 isn't supported on this system.
236 if (!utf8locale.supported()) {
237 return true;
238 }
239
240 for (std::wstring_view string : {
241 L"",
242 L"a",
243 L"abc",
244 L"ä",
245 L"💩",
246 }) {
247 auto utf8 = JS::EncodeWideToUtf8(cx, string.data());
248 CHECK(utf8 != nullptr);
249
250 auto wide = JS::EncodeUtf8ToWide(cx, utf8.get());
251 CHECK(wide != nullptr);
252
253 CHECK(wide.get() == string);
254 }
255 return true;
256 }
257 END_TEST(testCharacterEncoding_wide_roundtrip)