code cleanup

This commit is contained in:
Martin Brodbeck 2024-02-23 09:09:40 +01:00
parent 99da1e0e39
commit 4a3dc6bd63

View file

@ -22,26 +22,26 @@ std::map<char, std::string> morseCode = {
std::string refurbishMessage(const std::string &msg)
{
std::string msgUpper;
msgUpper.resize(msg.length());
std::string msgRefurb;
msgRefurb.resize(msg.length());
// Make the message all upper case
std::transform(msg.cbegin(), msg.cend(), msgUpper.begin(), [](unsigned char c) { return std::toupper(c); });
std::transform(msg.cbegin(), msg.cend(), msgRefurb.begin(), [](unsigned char c) { return std::toupper(c); });
// Encode the special characters as we like it
msgUpper = std::regex_replace(msgUpper, std::regex("<BT>"), "=");
msgUpper = std::regex_replace(msgUpper, std::regex("<KN>"), "k");
msgUpper = std::regex_replace(msgUpper, std::regex("<SK>"), "s");
msgUpper = std::regex_replace(msgUpper, std::regex("<AR>"), "+");
msgUpper = std::regex_replace(msgUpper, std::regex("<KA>"), "a");
msgRefurb = std::regex_replace(msgRefurb, std::regex("<BT>"), "=");
msgRefurb = std::regex_replace(msgRefurb, std::regex("<KN>"), "k");
msgRefurb = std::regex_replace(msgRefurb, std::regex("<SK>"), "s");
msgRefurb = std::regex_replace(msgRefurb, std::regex("<AR>"), "+");
msgRefurb = std::regex_replace(msgRefurb, std::regex("<KA>"), "a");
// Remove all other unknown characters
msgUpper.erase(remove_if(msgUpper.begin(), msgUpper.end(),
msgRefurb.erase(remove_if(msgRefurb.begin(), msgRefurb.end(),
[](const char &c) { return c != ' ' && morseCode.find(c) == morseCode.end(); }),
msgUpper.end());
msgRefurb.end());
// Remove spaces, if there are too many of them
msgUpper = std::regex_replace(msgUpper, std::regex("(\\s+)"), " ");
msgRefurb = std::regex_replace(msgRefurb, std::regex("(\\s+)"), " ");
return msgUpper;
return msgRefurb;
}