mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-11-27 21:45:16 +01:00
feat(utilities): a text of the data files as utf-8 for the user interface
data_text_to_utf8() turns a text read from a data file into utf-8: one which already is valid utf-8 stays, any other is read as windows-1250, the whole code page (win1250_to_utf8() knows the polish letters only, and utf8_to_win1250() counts on that, so the two stay as they are). A windows-1250 text which is valid utf-8 by chance (two capital letters with marks in a row) is told by what it would decode to. data_name_to_utf8() is the same for a name shown in one line: control characters and, when asked, underscores become spaces, as Bezogonkow() has them, but the letters keep their marks. utf8_length() and utf8_fit() count, cut and fill by characters, for the columns of a fixed width. Nothing uses them yet (MaSzyna-EU07/maszyna#94).
This commit is contained in:
@@ -8,6 +8,8 @@ namespace
|
||||
std::string const kWin1250{"\xA5\xC6\xCA\xA3\xD1\xD3\x8C\x8F\xAF\xB9\xE6\xEA\xB3\xF1\xF3\x9C\x9F\xBF"};
|
||||
std::string const kUtf8{"\xC4\x84\xC4\x86\xC4\x98\xC5\x81\xC5\x83\xC3\x93\xC5\x9A\xC5\xB9\xC5\xBB"
|
||||
"\xC4\x85\xC4\x87\xC4\x99\xC5\x82\xC5\x84\xC3\xB3\xC5\x9B\xC5\xBA\xC5\xBC"};
|
||||
// the name of a station the way a timetable has it: Biala_Podlaska with its l-stroke, in windows-1250
|
||||
std::string const kBiala{"Bia\xB3" "a_Podlaska"};
|
||||
} // namespace
|
||||
|
||||
TEST_CASE("win1250: the polish letters of a scenery file read as utf-8") {
|
||||
@@ -32,3 +34,82 @@ TEST_CASE("win1250: utf-8 written back the way a scenery file has it") {
|
||||
// the letters other than the polish ones turn into question marks
|
||||
CHECK(utf8_to_win1250("\xC3\xA9t\xC3\xA9") == "?t?");
|
||||
}
|
||||
|
||||
TEST_CASE("win1250: a text of a data file is turned into utf-8 for the user interface") {
|
||||
// plain ascii stays
|
||||
CHECK(data_text_to_utf8("tor 1, km 0.178") == "tor 1, km 0.178");
|
||||
CHECK(data_text_to_utf8("").empty());
|
||||
// windows-1250: the polish letters
|
||||
CHECK(data_text_to_utf8(kWin1250) == kUtf8);
|
||||
CHECK(data_text_to_utf8(kBiala) == "Bia\xC5\x82" "a_Podlaska");
|
||||
// and the letters of the other languages of the code page: a-acute, z-caron, i-acute, u-diaeresis, o-double-acute
|
||||
CHECK(data_text_to_utf8("n\xE1" "dra\x9E\xED") == "n\xC3\xA1" "dra\xC5\xBE\xC3\xAD");
|
||||
CHECK(data_text_to_utf8("\xFC\xF5") == "\xC3\xBC\xC5\x91");
|
||||
// a character outside of the two-byte range (the euro sign), and a byte the code page has nothing for
|
||||
CHECK(data_text_to_utf8("5 \x80") == "5 \xE2\x82\xAC");
|
||||
CHECK(data_text_to_utf8("a\x81" "b") == "a?b");
|
||||
// a text already in utf-8 stays as it is
|
||||
CHECK(data_text_to_utf8(kUtf8) == kUtf8);
|
||||
CHECK(data_text_to_utf8("Bia\xC5\x82" "a Podlaska") == "Bia\xC5\x82" "a Podlaska");
|
||||
CHECK(data_text_to_utf8("\xE2\x94\x82 60 \xE2\x94\x82") == "\xE2\x94\x82 60 \xE2\x94\x82");
|
||||
// capital letters with marks in a row make valid utf-8 by chance (O-acute and L-stroke: D3 A3), but of a
|
||||
// cyrillic letter; they are read as the windows-1250 they are
|
||||
CHECK(data_text_to_utf8("W\xD3\xA3KA") == "W\xC3\x93\xC5\x81KA");
|
||||
CHECK(data_text_to_utf8("R\xD3\xAF" "A") == "R\xC3\x93\xC5\xBB" "A");
|
||||
// line ends and tabs of a longer text stay
|
||||
CHECK(data_text_to_utf8("jedna\n\tdruga \xB3") == "jedna\n\tdruga \xC5\x82");
|
||||
// what comes out is valid utf-8
|
||||
std::u32string decoded;
|
||||
std::string all;
|
||||
for (auto value{0x20}; value < 0x100; ++value) {
|
||||
all += static_cast<char>(value);
|
||||
}
|
||||
REQUIRE(utf8_to_utf32(data_text_to_utf8(all), decoded));
|
||||
CHECK(decoded.size() == all.size());
|
||||
}
|
||||
|
||||
TEST_CASE("win1250: a name of a data file is shown in one line, with its letters") {
|
||||
// what Bezogonkow() did for the user interface, but for the marks: control characters and, when asked,
|
||||
// underscores turn into spaces
|
||||
CHECK(data_name_to_utf8("a\tb\nc") == "a b c");
|
||||
CHECK(data_name_to_utf8(kBiala) == "Bia\xC5\x82" "a_Podlaska");
|
||||
CHECK(data_name_to_utf8(kBiala, true) == "Bia\xC5\x82" "a Podlaska");
|
||||
CHECK(data_name_to_utf8("Warszawa_Zachodnia", true) == "Warszawa Zachodnia");
|
||||
CHECK(data_name_to_utf8("Warszawa_Zachodnia") == "Warszawa_Zachodnia");
|
||||
CHECK(data_name_to_utf8("Bia\xC5\x82" "a_Podlaska\r", true) == "Bia\xC5\x82" "a Podlaska ");
|
||||
// for a name without letters outside of ascii it gives what Bezogonkow() gave
|
||||
for (auto const *name : {"Warszawa_Zachodnia", "Gorna", "tor 1\t2", "", "Os1234"}) {
|
||||
CAPTURE(name);
|
||||
CHECK(data_name_to_utf8(name) == Bezogonkow(name));
|
||||
CHECK(data_name_to_utf8(name, true) == Bezogonkow(name, true));
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("win1250: a column of the timetable panel is as wide in letters as it was in bytes") {
|
||||
CHECK(utf8_length("") == 0);
|
||||
CHECK(utf8_length("Gorna") == 5);
|
||||
CHECK(utf8_length("Bia\xC5\x82" "a Podlaska") == 14);
|
||||
CHECK(utf8_length(kUtf8) == 18);
|
||||
CHECK(utf8_length("\xE2\x94\x82 60 \xE2\x94\x82") == 6);
|
||||
// a byte which is no part of a valid sequence counts as a character
|
||||
CHECK(utf8_length("a\xB3" "b") == 3);
|
||||
CHECK(utf8_length("a\xC5") == 2);
|
||||
|
||||
// filled with spaces
|
||||
CHECK(utf8_fit("Gorna", 8) == "Gorna ");
|
||||
CHECK(utf8_fit("", 3) == " ");
|
||||
auto const caption{data_name_to_utf8(kBiala, true)};
|
||||
auto const fitted{utf8_fit(caption, 34)};
|
||||
CHECK(utf8_length(fitted) == 34);
|
||||
CHECK(fitted.size() == 35); // one letter of two bytes
|
||||
CHECK(fitted.starts_with(caption));
|
||||
// cut between the letters, never inside of one
|
||||
CHECK(utf8_fit("Bia\xC5\x82" "a Podlaska", 4) == "Bia\xC5\x82");
|
||||
CHECK(utf8_fit("Bia\xC5\x82" "a Podlaska", 3) == "Bia");
|
||||
CHECK(utf8_fit(kUtf8, 2) == "\xC4\x84\xC4\x86");
|
||||
CHECK(utf8_fit("Gorna", 0).empty());
|
||||
// a name of ascii fits the way the panel cut it before
|
||||
std::string const longname{"Bardzo_Dluga_Nazwa_Stacji_Wezlowej_Osobowej"};
|
||||
CHECK(utf8_fit(longname, 34) == (longname + " ").substr(0, 34));
|
||||
CHECK(utf8_fit("Gorna", 34) == (std::string{"Gorna"} + " ").substr(0, 34));
|
||||
}
|
||||
|
||||
@@ -457,6 +457,120 @@ void win1250_to_utf32(std::string const &Text, std::u32string &Output)
|
||||
}
|
||||
}
|
||||
|
||||
namespace
|
||||
{
|
||||
void append_utf8(std::string &Output, char32_t const Codepoint)
|
||||
{
|
||||
if (Codepoint < 0x80)
|
||||
{
|
||||
Output += static_cast<char>(Codepoint);
|
||||
}
|
||||
else if (Codepoint < 0x800)
|
||||
{
|
||||
Output += static_cast<char>(0xC0 | (Codepoint >> 6));
|
||||
Output += static_cast<char>(0x80 | (Codepoint & 0x3F));
|
||||
}
|
||||
else if (Codepoint < 0x10000)
|
||||
{
|
||||
Output += static_cast<char>(0xE0 | (Codepoint >> 12));
|
||||
Output += static_cast<char>(0x80 | ((Codepoint >> 6) & 0x3F));
|
||||
Output += static_cast<char>(0x80 | (Codepoint & 0x3F));
|
||||
}
|
||||
else
|
||||
{
|
||||
Output += static_cast<char>(0xF0 | (Codepoint >> 18));
|
||||
Output += static_cast<char>(0x80 | ((Codepoint >> 12) & 0x3F));
|
||||
Output += static_cast<char>(0x80 | ((Codepoint >> 6) & 0x3F));
|
||||
Output += static_cast<char>(0x80 | (Codepoint & 0x3F));
|
||||
}
|
||||
}
|
||||
} // namespace
|
||||
|
||||
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; }))
|
||||
{
|
||||
return Text;
|
||||
}
|
||||
std::u32string codepoints;
|
||||
// a text in windows-1250 can happen to be valid utf-8 as well: two capital letters with marks in a row, as the bytes
|
||||
// D3 A3 of "WOLKA" with its marks, make a sequence of two bytes. such a pair decodes to a letter between U+0180 and
|
||||
// U+07FF (cyrillic, greek and the like), which the texts of the data files, when they are in utf-8, don't have
|
||||
if (utf8_to_utf32(Text, codepoints) && std::ranges::none_of(codepoints, [](char32_t const Codepoint) { return Codepoint >= 0x180 && Codepoint < 0x800; }))
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
std::string data_name_to_utf8(std::string const &Name, bool const Underscorestospaces)
|
||||
{
|
||||
auto name{data_text_to_utf8(Name)};
|
||||
// in utf-8 the bytes below 0x80 are characters of their own, never a part of another one
|
||||
for (auto &character : name)
|
||||
{
|
||||
if (static_cast<unsigned char>(character) < 0x20 || (Underscorestospaces && character == '_'))
|
||||
{
|
||||
character = ' ';
|
||||
}
|
||||
}
|
||||
return name;
|
||||
}
|
||||
|
||||
namespace
|
||||
{
|
||||
// number of bytes of the character of a text in utf-8 which starts at given position; 1 for a byte which starts no valid sequence
|
||||
std::size_t utf8_character_size(std::string_view const Text, std::size_t const Position)
|
||||
{
|
||||
auto const length{utf8_sequence_length(static_cast<std::byte>(Text[Position]))};
|
||||
if (length < 2 || Position + length > Text.size())
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
for (std::size_t offset = 1; offset < length; ++offset)
|
||||
{
|
||||
if ((static_cast<std::byte>(Text[Position + offset]) & std::byte{0xC0}) != std::byte{0x80})
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
return length;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
std::size_t utf8_length(std::string_view const Text)
|
||||
{
|
||||
std::size_t length{0};
|
||||
for (std::size_t position = 0; position < Text.size(); position += utf8_character_size(Text, position))
|
||||
{
|
||||
++length;
|
||||
}
|
||||
return length;
|
||||
}
|
||||
|
||||
std::string utf8_fit(std::string_view const Text, std::size_t const Width)
|
||||
{
|
||||
std::size_t length{0};
|
||||
std::size_t position{0};
|
||||
while (position < Text.size() && length < Width)
|
||||
{
|
||||
position += utf8_character_size(Text, position);
|
||||
++length;
|
||||
}
|
||||
std::string output{Text.substr(0, position)};
|
||||
output.append(Width - length, ' ');
|
||||
return output;
|
||||
}
|
||||
|
||||
// Ra: tymczasowe rozwiązanie kwestii zagranicznych (czeskich) napisów
|
||||
// NOTE: the entry of 0xB1 (the plus-minus sign of windows-1250, left as it is) is written as its byte. written as the
|
||||
// character it took two bytes in the builds which keep the texts of the sources in utf-8 (GCC, Clang, MSVC with /utf-8),
|
||||
|
||||
@@ -175,6 +175,17 @@ std::string utf8_to_win1250(std::string const &Text);
|
||||
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
|
||||
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
|
||||
std::string data_name_to_utf8(std::string const &Name, bool Underscorestospaces = false);
|
||||
// number of characters (code points) of a text in utf-8; a byte which isn't a part of a valid sequence counts as one
|
||||
std::size_t utf8_length(std::string_view Text);
|
||||
// a text in utf-8 cut or filled with spaces to given number of characters, for the columns of the fixed width
|
||||
std::string utf8_fit(std::string_view Text, std::size_t Width);
|
||||
|
||||
inline std::string extract_value(std::string const &Key, std::string const &Input)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user