Text encoding moves to formats/text_encoding; app sources built once on MSVC

- json_quote, original_to_utf8 and utf8_to_original lived in ui/manager, the
  manager dialog, although every screen, the console client and the app use
  them. They move byte for byte to formats/text_encoding (namespace bmh);
  ui/manager.hpp keeps them visible as bmh::ui::... .
- The app sources (career session, window/desktop actions, controller) are
  object libraries on MSVC too, compiled with /utf-8 like the game. Before,
  the MSVC build compiled the 9,000 line career_session.cpp again for each of
  about forty tests and tools.

Verified: 211 of 211 tests; 9 headless careers identical; the UI walk
differs only on the stadium screen, as expected from the 96,000 place
limits merged from main (reference re-recorded).

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
ClaudeandClaude committed 2026-11-10 15:00:15 +01:00
1 parent e174f99594
commit 16b9b24c84
5 files changed
+112 -64

No files matched your search

+57
View File
@@ -0,0 +1,57 @@
// SPDX-License-Identifier: GPL-3.0-only
// Text encoding helpers: see text_encoding.hpp.
#include "formats/text_encoding.hpp"
#include <array>
#include <iomanip>
#include <sstream>
#include "formats/binary.hpp"
namespace bmh {
namespace {
// The seven German letters and the ISO 646-DE bytes that stand for them, in the same order.
constexpr std::array<std::string_view, 7> german = {"Ä", "Ö", "Ü", "ä", "ö", "ü", "ß"};
constexpr std::string_view original_german = "[\\]{|}~";
} // namespace
std::string json_quote(std::string_view value) {
std::ostringstream s; s << '"';
for (unsigned char c : value) {
if (c == '"' || c == '\\') s << '\\' << c;
else if (c < 32) s << "\\u" << std::hex << std::setw(4) << std::setfill('0') << int(c) << std::dec;
else s << c;
}
s << '"'; return s.str();
}
std::string original_to_utf8(std::string_view value) {
std::string out;
for (char c : value) {
auto i = original_german.find(c);
if (i != std::string_view::npos) out += german[i];
else if (static_cast<unsigned char>(c) < 128) out += c;
else out += "�"; // Unsupported original byte remains visibly unknown.
}
return out;
}
std::string utf8_to_original(std::string_view value) {
std::string out;
for (size_t i = 0; i < value.size();) {
bool found = false;
for (size_t j = 0; j < german.size(); ++j)
if (value.substr(i, german[j].size()) == german[j]) {
out += original_german[j]; i += german[j].size(); found = true; break;
}
if (found) continue;
const unsigned char c = value[i++];
if (c >= 128) throw FormatError("manager name: unsupported UTF-8 character");
out += char(c);
}
return out;
}
} // namespace bmh
+28
View File
@@ -0,0 +1,28 @@
// SPDX-License-Identifier: GPL-3.0-only
#pragma once
// Text encoding of the original game and of the JSON the front ends emit.
//
// The original stores German text in ISO 646-DE: the seven bytes [ \ ] { | } ~ stand for
// Ä Ö Ü ä ö ü ß, everything else is ASCII. The engine and the screens keep text in that original
// encoding (the bitmap fonts are indexed by it); UTF-8 is used only where text leaves the program
// (JSON state, console, window titles) or enters it (typed names).
//
// These functions lived in ui/manager.hpp until 1.0.12, although nearly every screen, the console
// client and the app use them; ui/manager.hpp still makes them visible as bmh::ui::... .
#include <string>
#include <string_view>
namespace bmh {
// A JSON string literal (with the quotes) for UTF-8 text: escapes '"', '\' and control characters.
std::string json_quote(std::string_view utf8);
// Original (ISO 646-DE) bytes -> UTF-8. A byte above 127 is not part of the original encoding
// and becomes U+FFFD, so that it stays visible.
std::string original_to_utf8(std::string_view original);
// UTF-8 -> original bytes; throws FormatError for a character the original cannot store.
std::string utf8_to_original(std::string_view utf8);
} // namespace bmh