#include "inputConverter.h" #include <string> struct InputConverter::pImpl { const Uint16* convert_table; unsigned int max_length; std::basic_string<Uint16> converted; pImpl(const Uint16* convertTable, unsigned int maxLength) : convert_table(convertTable), max_length(maxLength) { } size_t ptnlen(const int index, const Uint16* table) { int length = 0; while (table[index + length] != 0x0) { ++length; } return length; } size_t replace(size_t match_index, int table_index) { int ptn_length = ptnlen(table_index, convert_table); converted.replace(match_index, ptn_length, &convert_table[table_index + max_length]); return ptn_length - ptnlen(table_index + max_length, convert_table); } }; InputConverter::InputConverter(const Uint16* convertTable, unsigned int maxLength) : pimpl(new pImpl(convertTable, maxLength)) { } InputConverter::~InputConverter(void) { } const Uint16* InputConverter::convert(const char* input) { pimpl->converted.clear(); // 変換文字列を代入 int input_length = strlen(input); for (int i = 0; i < input_length; ++i) { pimpl->converted.push_back(input[i]); } int steps = pimpl->max_length * 2; for (size_t i = 0; i < pimpl->converted.size(); ++i) { // 一致したパターンを置換する for (int j = 0; pimpl->convert_table[j] != 0x0; j += steps) { size_t match = pimpl->converted.compare(i, pimpl->ptnlen(j, pimpl->convert_table), &pimpl->convert_table[j]); if (match == 0) { } if (match == 0) { pimpl->replace(i, j); // か + (濁点) といった変換用のために、1文字分戻る --i; break; } } } pimpl->converted.push_back(0x0); return &pimpl->converted[0]; }