Skip to content

Instantly share code, notes, and snippets.

@3735943886
Created August 31, 2017 02:55
Show Gist options
  • Select an option

  • Save 3735943886/0da175ac64a61818674701f3e5935565 to your computer and use it in GitHub Desktop.

Select an option

Save 3735943886/0da175ac64a61818674701f3e5935565 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <string>
#include <regex>
#include <map>
 
 
/*
 * English 2 Korean
 * from http://www.phpschool.com/link/tipntech/55742
 *
 * Dependencies : string, regex, map
 */
std::string e2k(std::string in, size_t *count = nullptr)
{
    // static Variables
    static std::string regexp_cho = 
        "r|R|s|e|E|f|a|q|Q|t|T|d|w|W|c|z|x|v|g";
    static std::string regexp_jung = 
        "hk|ho|hl|nj|np|nl|ml|k|o|i|O|j|p|u|P|h|y|n|b|m|l";
    static std::string regexp_jong = 
        "rt|sw|sg|fr|fa|fq|ft|fx|fv|fg|qt|r|R|s|e|f|a|q|t|T|d|w|c|z|x|v|g|";
    static std::regex reg = std::regex(
        std::string("(").append(regexp_cho).append(")(").append(regexp_jung).append(")((").
        append(regexp_jong).append(")(?=(").append(regexp_cho).append(")(").
        append(regexp_jung).append("))|(").append(regexp_jong).append("))"));
 
    static std::map<std::string, unsigned short> eng_cho =
    {
        {"r", 0}, {"R", 1}, {"s", 2}, {"e", 3}, {"E", 4},
        {"f", 5}, {"a", 6}, {"q", 7}, {"Q", 8}, {"t", 9},
        {"T", 10}, {"d", 11}, {"w", 12}, {"W", 13}, {"c", 14},
        {"z", 15}, {"x", 16}, {"v", 17}, {"g", 18}
    };
    static std::map<std::string, unsigned short> eng_jung =
    {
        {"k", 0}, {"o", 1}, {"i", 2}, {"O", 3}, {"j", 4},
        {"p", 5}, {"u", 6}, {"P", 7}, {"h", 8}, {"hk", 9},
        {"ho", 10}, {"hl", 11}, {"y", 12}, {"n", 13}, {"nj", 14},
        {"np", 15}, {"nl", 16}, {"b", 17}, {"m", 18}, {"ml", 19},
        {"l", 20}
    };
    static std::map<std::string, unsigned short> eng_jong =
    {
        {"", 0}, {"r", 1}, {"R", 2}, {"rt", 3}, {"s", 4},
        {"sw", 5}, {"sg", 6}, {"e", 7}, {"f", 8}, {"fr", 9},
        {"fa", 10}, {"fq", 11}, {"ft", 12}, {"fx", 13}, {"fv", 14},
        {"fg", 15}, {"a", 16}, {"q", 17}, {"qt", 18}, {"t", 19},
        {"T", 20}, {"d", 21}, {"w", 22}, {"c", 23}, {"z", 24},
        {"x", 25}, {"v", 26}, {"g", 27}
    };
 
    // Local Variables
    std::smatch sub;
    unsigned short w;
    unsigned long u8;
    if(count != nullptr) (*count) = 0;
 
    while(regex_search(in, sub, reg))
    {
        if(count != nullptr) (*count)++;
 
        // Little Endian
        w =  (eng_cho[sub[1]] * 0x24c + eng_jung[sub[2]] * 0x1c + eng_jong[sub[3]] + 0xac00);
        u8 = 0x8080e0 | ((w & 0x3f) << 16) | ((w & 0xfc0) << 2) | (w >> 12);
    
        in = std::regex_replace(in, reg, (char *)&u8, std::regex_constants::format_first_only);
    }
 
    return in;
}
 
int main()
{
    size_t count;
    std::string in = "doaogks 3tpscl...";
    std::cout << e2k(in, &count) << std::endl;
    std::cout << in << ", " << count << std::endl;
    return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment