/* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
public:
ToUTF8Locale() { // Store the old locale so we can reset it in the destructor.
previousLocale_ = std::setlocale(LC_ALL, nullptr);
// Query the system default locale. constchar* defaultLocale = std::setlocale(LC_ALL, ""); if (!defaultLocale) { // std::setlocale returns nullptr on failure. return;
}
// Switch the default locale to be UTF-8 aware. constchar* newLocale = std::setlocale(LC_ALL, "en_US.UTF-8"); if (!newLocale) { // std::setlocale returns nullptr on failure. return;
}
// Return if either the default or new locale don't contain a code-page. if (!defaultCodepage || !newCodepage) { return;
}
// Skip past the '.'.
defaultCodepage++;
newCodepage++;
// UTF-8 is supported when the default locale and new locale support it: // // The default locale needs to support UTF-8, because this test is compiled // using the default locale. // // The new locale needs to support UTF-8 to ensure UTF-8 encoding works at // runtime.
supported_ = EqualsIgnoreCase(defaultCodepage, "UTF-8") &&
EqualsIgnoreCase(newCodepage, "UTF-8");
}
bool supported() const { return supported_; }
~ToUTF8Locale() { // Restore the previous locale. if (previousLocale_) {
std::setlocale(LC_ALL, previousLocale_);
}
}
};
BEGIN_TEST(testCharacterEncoding_narrow_to_utf8) { // Assume the narrow charset is ASCII-compatible. ASCII to UTF-8 conversion is // a no-op. for (std::string_view string : { "", "a", "abc", "abc\0def",
}) { auto utf8 = JS::EncodeNarrowToUtf8(cx, string.data());
CHECK(utf8 != nullptr);
CHECK_EQUAL(std::strlen(utf8.get()), string.length());
CHECK(utf8.get() == string);
} returntrue;
}
END_TEST(testCharacterEncoding_narrow_to_utf8)
BEGIN_TEST(testCharacterEncoding_wide_to_utf8) { // Assume the wide charset is ASCII-compatible. ASCII to UTF-8 conversion is // a no-op. for (std::wstring_view string : {
L"",
L"a",
L"abc",
L"abc\0def",
}) { auto utf8 = JS::EncodeWideToUtf8(cx, string.data());
CHECK(utf8 != nullptr);
CHECK_EQUAL(std::strlen(utf8.get()), string.length());
CHECK(std::equal(
string.begin(), string.end(), utf8.get(),
[](wchar_t x, char y) { return char32_t(x) == char32_t(y); }));
} returntrue;
}
END_TEST(testCharacterEncoding_wide_to_utf8)
BEGIN_TEST(testCharacterEncoding_wide_to_utf8_non_ascii) { // Change the locale to be UTF-8 aware for the emoji string.
ToUTF8Locale utf8locale;
// Skip this test if UTF-8 isn't supported on this system. if (!utf8locale.supported()) { returntrue;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.