#include #include #include #include "morse.h" std::map morseCode = { {'A', ".-"}, {'B', "-..."}, {'C', "-.-."}, {'D', "-.."}, {'E', "."}, {'F', "..-."}, {'G', "--."}, {'H', "...."}, {'I', ".."}, {'J', ".---"}, {'K', "-.-"}, {'L', ".-.."}, {'M', "--"}, {'N', "-."}, {'O', "---"}, {'P', ".--."}, {'Q', "--.-"}, {'R', ".-."}, {'S', "..."}, {'T', "-"}, {'U', "..-"}, {'V', "...-"}, {'W', ".--"}, {'X', "-..-"}, {'Y', "-.--"}, {'Z', "--.."}, {'1', ".----"}, {'2', "..---"}, {'3', "...--"}, {'4', "....-"}, {'5', "....."}, {'6', "-...."}, {'7', "--..."}, {'8', "---.."}, {'9', "----."}, {'0', "-----"}, {',', "--..--"}, {'.', ".-.-.-"}, {'?', "..--.."}, {'/', "-..-."}, {'-', "-....-"}, {':', "---..."}, {'&', ".-..."}, {'\'', ".----."}, {'@', ".--.-."}, {')', "-.--.-"}, {'(', "-.--."}, {'\"', ".-..-."}, {'=', "-...-"}, // '=' == {'k', "-.--."}, // k == {'s', "...-.-"}, // s == {'+', ".-.-."}, // + == {'a', "-.-.-"}, // a == }; std::string refurbishMessage(const std::string &msg) { std::string msgUpper; msgUpper.resize(msg.length()); // Make the message all upper case std::transform(msg.cbegin(), msg.cend(), msgUpper.begin(), [](unsigned char c) { return std::toupper(c); }); // Encode the special characters as we like it msgUpper = std::regex_replace(msgUpper, std::regex(""), "="); msgUpper = std::regex_replace(msgUpper, std::regex(""), "k"); msgUpper = std::regex_replace(msgUpper, std::regex(""), "s"); msgUpper = std::regex_replace(msgUpper, std::regex(""), "+"); msgUpper = std::regex_replace(msgUpper, std::regex(""), "a"); // Remove all other unknown characters msgUpper.erase(remove_if(msgUpper.begin(), msgUpper.end(), [](const char &c) { return c != ' ' && morseCode.find(c) == morseCode.end(); }), msgUpper.end()); // Remove spaces, if there are too many of them msgUpper = std::regex_replace(msgUpper, std::regex("(\\s+)"), " "); return msgUpper; }