inputConverter.cpp

Go to the documentation of this file.
00001 
00010 #include "inputConverter.h"
00011 #include <string>
00012 
00013 
00017 struct InputConverter::pImpl {
00018   const Uint16* convert_table;
00019   unsigned int max_length;
00020   std::basic_string<Uint16> converted;
00021 
00022   pImpl(const Uint16* convertTable, unsigned int maxLength)
00023     : convert_table(convertTable), max_length(maxLength) {
00024   }
00025 
00026   size_t ptnlen(const int index, const Uint16* table) {
00027     int length = 0;
00028     while (table[index + length] != 0x0) {
00029       ++length;
00030     }
00031     return length;
00032   }
00033 
00034   size_t replace(size_t match_index, int table_index) {
00035     int ptn_length = ptnlen(table_index, convert_table);
00036     converted.replace(match_index, ptn_length,
00037                       &convert_table[table_index + max_length]);
00038 
00039     return ptn_length - ptnlen(table_index + max_length, convert_table);
00040   }
00041 };
00042 
00043 
00044 InputConverter::InputConverter(const Uint16* convertTable,
00045                                unsigned int maxLength)
00046   : pimpl(new pImpl(convertTable, maxLength)) {
00047 }
00048 
00049 
00050 InputConverter::~InputConverter(void) {
00051 }
00052 
00053 
00054 const Uint16* InputConverter::convert(const char* input) {
00055   pimpl->converted.clear();
00056 
00057   // 変換文字列を代入
00058   int input_length = strlen(input);
00059   for (int i = 0; i < input_length; ++i) {
00060     pimpl->converted.push_back(input[i]);
00061   }
00062 
00063   int steps = pimpl->max_length * 2;
00064   for (size_t i = 0; i < pimpl->converted.size(); ++i) {
00065     // 一致したパターンを置換する
00066     for (int j = 0; pimpl->convert_table[j] != 0x0; j += steps) {
00067       size_t match =
00068         pimpl->converted.compare(i, pimpl->ptnlen(j, pimpl->convert_table),
00069                                  &pimpl->convert_table[j]);
00070       if (match == 0) {
00071         pimpl->replace(i, j);
00072 
00073         // か + (濁点) といった変換用のために、1文字分戻る
00074         --i;
00075         break;
00076       }
00077     }
00078   }
00079 
00080   pimpl->converted.push_back(0x0);
00081   return &pimpl->converted[0];
00082 }
00083 

Generated on Mon Apr 13 22:52:01 2009 by  doxygen 1.5.7.1