mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-12-05 16:55:32 +01:00
fix(utilities): win1250 <-> utf-8 over the whole code page, not only the polish letters
win1250_to_utf8() copied every byte outside the 18 polish letters raw, which isn't valid utf-8, and utf8_to_win1250() turned the rest into '?', a raw cp1250 byte like 0xE8 (c-caron) swallowing the one or two bytes after it. Both now use the full windows-1250 table data_text_to_utf8() already used: the five bytes the code page leaves undefined become '?', a character the code page doesn't have becomes one '?', a malformed sequence is skipped by the length its lead byte tells, as before. data_text_to_utf8() calls win1250_to_utf8() for its non-utf-8 branch (same output). The audio renderer tries the 1250 reading of a device name only when the name isn't valid utf-8: with the full table a utf-8 name now differs from its 1250 reading too, which would cost one more failed lookup. Follow-up of #252.
This commit is contained in:
+14
-5
@@ -75,6 +75,15 @@ std::optional<ALCint> switch_value( std::string_view const Name ) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
// returns: given device name read as code page 1250 text and turned into utf-8, or nothing if the name is valid utf-8
|
||||
// already (plain ascii included): such a name is meant as utf-8, and read as 1250 it would only be garbled
|
||||
std::optional<std::string> win1250_reading( std::string const &Name ) {
|
||||
|
||||
std::u32string decoded;
|
||||
if( utf8_to_utf32( Name, decoded ) ) { return std::nullopt; }
|
||||
return win1250_to_utf8( Name );
|
||||
}
|
||||
|
||||
// returns: name of an output device without the prefix OpenAL Soft may put before the names of the devices it lists on
|
||||
// Windows. it opens a device named either way, so the name in eu07.ini can come with it or without
|
||||
std::string_view bare_output_name( std::string_view const Name ) {
|
||||
@@ -96,9 +105,9 @@ ALCdevice *open_output_device( bool &Fallback ) {
|
||||
if( auto *device { ::alcOpenDevice( Global.AudioRenderer.c_str() ) } ) {
|
||||
return device;
|
||||
}
|
||||
if( auto const utf8name { win1250_to_utf8( Global.AudioRenderer ) }; utf8name != Global.AudioRenderer ) {
|
||||
if( auto *device { ::alcOpenDevice( utf8name.c_str() ) } ) {
|
||||
Global.AudioRenderer = utf8name;
|
||||
if( auto const utf8name { win1250_reading( Global.AudioRenderer ) } ) {
|
||||
if( auto *device { ::alcOpenDevice( utf8name->c_str() ) } ) {
|
||||
Global.AudioRenderer = *utf8name;
|
||||
return device;
|
||||
}
|
||||
}
|
||||
@@ -463,8 +472,8 @@ openal_renderer::find_preferred_output() const {
|
||||
return *match;
|
||||
};
|
||||
if( auto name { listed( Global.AudioRenderer ) } ) { return name; }
|
||||
if( auto const utf8name { win1250_to_utf8( Global.AudioRenderer ) }; utf8name != Global.AudioRenderer ) {
|
||||
return listed( utf8name );
|
||||
if( auto const utf8name { win1250_reading( Global.AudioRenderer ) } ) {
|
||||
return listed( *utf8name );
|
||||
}
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
@@ -31,8 +31,78 @@ TEST_CASE("win1250: utf-8 written back the way a scenery file has it") {
|
||||
CHECK(utf8_to_win1250("Bia\xC5\x82" "a Podlaska") == "Bia\xB3" "a Podlaska");
|
||||
CHECK(utf8_to_win1250(kUtf8) == kWin1250);
|
||||
CHECK(win1250_to_utf8(utf8_to_win1250(kUtf8)) == kUtf8);
|
||||
// the letters other than the polish ones turn into question marks
|
||||
CHECK(utf8_to_win1250("\xC3\xA9t\xC3\xA9") == "?t?");
|
||||
// the other letters of the code page are written back too (e-acute)
|
||||
CHECK(utf8_to_win1250("\xC3\xA9t\xC3\xA9") == "\xE9t\xE9");
|
||||
// characters the code page doesn't have turn into one question mark each: n-tilde, cyrillic zhe, an emoji
|
||||
CHECK(utf8_to_win1250("ma\xC3\xB1" "ana") == "ma?ana");
|
||||
CHECK(utf8_to_win1250("\xD0\x96uk") == "?uk");
|
||||
CHECK(utf8_to_win1250("a\xF0\x9F\x9A\x82" "b") == "a?b");
|
||||
}
|
||||
|
||||
namespace
|
||||
{
|
||||
// a czech sentence with all the letters of the language which carry marks, in windows-1250 and in utf-8
|
||||
std::string const kCzechWin1250{"P\xF8\xEDli\x9A \x9Elu\x9Dou\xE8k\xFD k\xF9\xF2 \xFAp\xECl \xEF\xE1" "belsk\xE9 \xF3" "dy"};
|
||||
std::string const kCzechUtf8{"P\xC5\x99\xC3\xADli\xC5\xA1 \xC5\xBElu\xC5\xA5ou\xC4\x8Dk\xC3\xBD k\xC5\xAF\xC5\x88 \xC3\xBAp\xC4\x9Bl \xC4\x8F\xC3\xA1"
|
||||
"belsk\xC3\xA9 \xC3\xB3" "dy"};
|
||||
// typographic signs: low and high double quotes, en dash, em dash, ellipsis, euro, degree, section, no-break space
|
||||
std::string const kSignsWin1250{"\x84\x94\x96\x97\x85\x80\xB0\xA7\xA0"};
|
||||
std::string const kSignsUtf8{"\xE2\x80\x9E\xE2\x80\x9D\xE2\x80\x93\xE2\x80\x94\xE2\x80\xA6\xE2\x82\xAC\xC2\xB0\xC2\xA7\xC2\xA0"};
|
||||
} // namespace
|
||||
|
||||
TEST_CASE("win1250: the letters of the other languages of the code page, both ways") {
|
||||
CHECK(win1250_to_utf8(kCzechWin1250) == kCzechUtf8);
|
||||
CHECK(utf8_to_win1250(kCzechUtf8) == kCzechWin1250);
|
||||
// a czech letter (c-caron) used to come back as a question mark which swallowed the two bytes after it
|
||||
CHECK(win1250_to_utf8("\xE8" "ab") == "\xC4\x8D" "ab");
|
||||
CHECK(utf8_to_win1250(win1250_to_utf8("\xE8" "ab")) == "\xE8" "ab");
|
||||
}
|
||||
|
||||
TEST_CASE("win1250: typographic signs, both ways") {
|
||||
CHECK(win1250_to_utf8(kSignsWin1250) == kSignsUtf8);
|
||||
CHECK(utf8_to_win1250(kSignsUtf8) == kSignsWin1250);
|
||||
CHECK(win1250_to_utf8("\x84" "cytat\x94") == "\xE2\x80\x9E" "cytat\xE2\x80\x9D");
|
||||
}
|
||||
|
||||
TEST_CASE("win1250: every byte of the code page goes to utf-8 and back") {
|
||||
// the bytes the code page has no character for
|
||||
std::string const undefined{"\x81\x83\x88\x90\x98"};
|
||||
std::string all;
|
||||
for (auto value{0x20}; value < 0x100; ++value) {
|
||||
all += static_cast<char>(value);
|
||||
}
|
||||
auto const utf8{win1250_to_utf8(all)};
|
||||
std::u32string decoded;
|
||||
REQUIRE(utf8_to_utf32(utf8, decoded));
|
||||
CHECK(decoded.size() == all.size());
|
||||
CHECK(utf8 == data_text_to_utf8(all));
|
||||
auto expected{all};
|
||||
for (auto &character : expected) {
|
||||
if (undefined.find(character) != std::string::npos) {
|
||||
character = '?';
|
||||
}
|
||||
}
|
||||
CHECK(utf8_to_win1250(utf8) == expected);
|
||||
// one by one: a defined byte comes back as itself, an undefined one as a question mark, the neighbours stay
|
||||
for (auto value{0x80}; value < 0x100; ++value) {
|
||||
auto const byte{static_cast<char>(value)};
|
||||
CAPTURE(value);
|
||||
auto const text{std::string{"a"} + byte + "b"};
|
||||
if (undefined.find(byte) != std::string::npos) {
|
||||
CHECK(win1250_to_utf8(text) == "a?b");
|
||||
}
|
||||
else {
|
||||
CHECK(utf8_to_win1250(win1250_to_utf8(text)) == text);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("win1250: malformed utf-8 turns into question marks") {
|
||||
// a lone continuation byte
|
||||
CHECK(utf8_to_win1250("a\xB3" "b") == "a?b");
|
||||
// a sequence cut short by the end of the text
|
||||
CHECK(utf8_to_win1250("a\xC5") == "a?");
|
||||
CHECK(utf8_to_win1250("\xE2\x82") == "?");
|
||||
}
|
||||
|
||||
TEST_CASE("win1250: a text of a data file is turned into utf-8 for the user interface") {
|
||||
|
||||
+59
-52
@@ -430,49 +430,6 @@ void win1250_to_ascii(std::string &Input)
|
||||
}
|
||||
}
|
||||
|
||||
std::string win1250_to_utf8(const std::string &Input)
|
||||
{
|
||||
// the utf-8 sequences written as bytes: the file has a byte order mark and the build doesn't give the compiler /utf-8, so letters
|
||||
// written as letters would end up in the program in windows-1250, the same as the input
|
||||
static std::unordered_map<char, std::string> const charmap{{'\xA5', "\xC4\x84"}, {'\xC6', "\xC4\x86"}, {'\xCA', "\xC4\x98"}, {'\xA3', "\xC5\x81"}, {'\xD1', "\xC5\x83"}, {'\xD3', "\xC3\x93"},
|
||||
{'\x8C', "\xC5\x9A"}, {'\x8F', "\xC5\xB9"}, {'\xAF', "\xC5\xBB"}, {'\xB9', "\xC4\x85"}, {'\xE6', "\xC4\x87"}, {'\xEA', "\xC4\x99"},
|
||||
{'\xB3', "\xC5\x82"}, {'\xF1', "\xC5\x84"}, {'\xF3', "\xC3\xB3"}, {'\x9C', "\xC5\x9B"}, {'\x9F', "\xC5\xBA"}, {'\xBF', "\xC5\xBC"}};
|
||||
std::string output;
|
||||
std::unordered_map<char, std::string>::const_iterator lookup;
|
||||
for (auto &input : Input)
|
||||
{
|
||||
if ((lookup = charmap.find(input)) != charmap.end())
|
||||
output += lookup->second;
|
||||
else
|
||||
output += input;
|
||||
}
|
||||
return output;
|
||||
}
|
||||
|
||||
// counterpart of win1250_to_utf8(). characters outside of the supported set are replaced with question marks
|
||||
std::string utf8_to_win1250(std::string const &Text)
|
||||
{
|
||||
// polish letters, as utf-8 sequences; the same set win1250_to_utf8() handles
|
||||
static std::unordered_map<std::string, char> const charmap{{"\xC4\x84", '\xA5'}, {"\xC4\x86", '\xC6'}, {"\xC4\x98", '\xCA'}, {"\xC5\x81", '\xA3'}, {"\xC5\x83", '\xD1'}, {"\xC3\x93", '\xD3'},
|
||||
{"\xC5\x9A", '\x8C'}, {"\xC5\xB9", '\x8F'}, {"\xC5\xBB", '\xAF'}, {"\xC4\x85", '\xB9'}, {"\xC4\x87", '\xE6'}, {"\xC4\x99", '\xEA'},
|
||||
{"\xC5\x82", '\xB3'}, {"\xC5\x84", '\xF1'}, {"\xC3\xB3", '\xF3'}, {"\xC5\x9B", '\x9C'}, {"\xC5\xBA", '\x9F'}, {"\xC5\xBC", '\xBF'}};
|
||||
std::string output;
|
||||
for (std::size_t idx = 0; idx < Text.size();)
|
||||
{
|
||||
auto const lead{static_cast<unsigned char>(Text[idx])};
|
||||
if (lead < 0x80)
|
||||
{
|
||||
output += Text[idx++];
|
||||
continue;
|
||||
}
|
||||
auto const length{std::min<std::size_t>((lead & 0xE0) == 0xC0 ? 2 : (lead & 0xF0) == 0xE0 ? 3 : (lead & 0xF8) == 0xF0 ? 4 : 1, Text.size() - idx)};
|
||||
auto const lookup{charmap.find(Text.substr(idx, length))};
|
||||
output += (lookup != charmap.end() ? lookup->second : '?');
|
||||
idx += length;
|
||||
}
|
||||
return output;
|
||||
}
|
||||
|
||||
namespace
|
||||
{
|
||||
// unicode code points of the upper half of the windows-1250 code page; 0 marks the unassigned bytes
|
||||
@@ -583,6 +540,64 @@ void append_utf8(std::string &Output, char32_t const Codepoint)
|
||||
}
|
||||
} // namespace
|
||||
|
||||
std::string win1250_to_utf8(const std::string &Input)
|
||||
{
|
||||
std::string output;
|
||||
output.reserve(Input.size() + 8);
|
||||
for (auto const character : Input)
|
||||
{
|
||||
auto const value{static_cast<unsigned char>(character)};
|
||||
if (value < 0x80)
|
||||
{
|
||||
output += character;
|
||||
continue;
|
||||
}
|
||||
// the bytes the code page has no character for come as 0
|
||||
auto const codepoint{win1250_upperhalf[value - 0x80]};
|
||||
append_utf8(output, codepoint != 0 ? codepoint : U'?');
|
||||
}
|
||||
return output;
|
||||
}
|
||||
|
||||
std::string utf8_to_win1250(std::string const &Text)
|
||||
{
|
||||
// the bytes of the upper half of the code page, by the character they stand for
|
||||
static std::unordered_map<char32_t, char> const charmap{[] {
|
||||
std::unordered_map<char32_t, char> map;
|
||||
for (std::size_t idx = 0; idx < win1250_upperhalf.size(); ++idx)
|
||||
{
|
||||
if (win1250_upperhalf[idx] != 0)
|
||||
{
|
||||
map.emplace(win1250_upperhalf[idx], static_cast<char>(0x80 + idx));
|
||||
}
|
||||
}
|
||||
return map;
|
||||
}()};
|
||||
|
||||
std::string output;
|
||||
output.reserve(Text.size());
|
||||
std::u32string decoded;
|
||||
for (std::size_t idx = 0; idx < Text.size();)
|
||||
{
|
||||
auto const lead{static_cast<std::byte>(Text[idx])};
|
||||
if ((lead & std::byte{0x80}) == std::byte{0x00})
|
||||
{
|
||||
output += Text[idx++];
|
||||
continue;
|
||||
}
|
||||
// a malformed sequence is skipped by the length its first byte tells, as far as the text goes
|
||||
auto const length{std::min<std::size_t>(std::max<std::size_t>(utf8_sequence_length(lead), 1), Text.size() - idx)};
|
||||
auto lookup{charmap.end()};
|
||||
if (utf8_to_utf32(Text.substr(idx, length), decoded) && decoded.size() == 1)
|
||||
{
|
||||
lookup = charmap.find(decoded.front());
|
||||
}
|
||||
output += (lookup != charmap.end() ? lookup->second : '?');
|
||||
idx += length;
|
||||
}
|
||||
return output;
|
||||
}
|
||||
|
||||
std::string data_text_to_utf8(std::string const &Text)
|
||||
{
|
||||
if (std::ranges::all_of(Text, [](char const Character) { return static_cast<unsigned char>(Character) < 0x80; }))
|
||||
@@ -598,15 +613,7 @@ std::string data_text_to_utf8(std::string const &Text)
|
||||
return Text;
|
||||
}
|
||||
// the code page the data files are written in
|
||||
win1250_to_utf32(Text, codepoints);
|
||||
std::string output;
|
||||
output.reserve(Text.size() + 8);
|
||||
for (auto const codepoint : codepoints)
|
||||
{
|
||||
// the bytes the code page has no character for come as 0
|
||||
append_utf8(output, codepoint != 0 ? codepoint : U'?');
|
||||
}
|
||||
return output;
|
||||
return win1250_to_utf8(Text);
|
||||
}
|
||||
|
||||
std::string data_name_to_utf8(std::string const &Name, bool const Underscorestospaces)
|
||||
|
||||
@@ -193,16 +193,18 @@ void win1250_to_ascii(std::string &Input);
|
||||
// TODO: unify with win1250_to_ascii()
|
||||
std::string Bezogonkow(std::string Input, bool const Underscorestospaces = false);
|
||||
|
||||
// windows-1250 (ansi) text as utf-8: every character of the code page, the five bytes it has none for as question marks
|
||||
std::string win1250_to_utf8(const std::string &input);
|
||||
// counterpart of win1250_to_utf8(): the polish letters, the other characters outside of ascii as question marks
|
||||
// counterpart of win1250_to_utf8(): utf-8 text as windows-1250. a character the code page doesn't have, or a malformed
|
||||
// sequence (skipped by the length its first byte tells), becomes a question mark
|
||||
std::string utf8_to_win1250(std::string const &Text);
|
||||
// decodes utf-8 encoded text into unicode code points. returns false if the text isn't valid utf-8
|
||||
bool utf8_to_utf32(std::string const &Text, std::u32string &Output);
|
||||
// decodes windows-1250 (ansi) encoded text into unicode code points
|
||||
void win1250_to_utf32(std::string const &Text, std::u32string &Output);
|
||||
// a text of the data files as utf-8, for the user interface. the files are in windows-1250, some of the newer ones in
|
||||
// utf-8: a text which is valid utf-8 stays as it is, any other is read as windows-1250, every letter of the code page
|
||||
// (not only the polish ones win1250_to_utf8() knows) turned into utf-8
|
||||
// utf-8: a text which is valid utf-8 stays as it is, any other is read as windows-1250 and turned into utf-8 with
|
||||
// win1250_to_utf8()
|
||||
std::string data_text_to_utf8(std::string const &Text);
|
||||
// the same for a name (of a station, a train, an event, a node, a file) shown in a line of the user interface: what
|
||||
// Bezogonkow() is for, but the letters keep their marks. control characters become spaces and, when asked, underscores too
|
||||
|
||||
Reference in New Issue
Block a user