diff --git a/Sources/OpenKey/engine/ConvertTool.cpp b/Sources/OpenKey/engine/ConvertTool.cpp index 8fe5ead..4742fb5 100644 --- a/Sources/OpenKey/engine/ConvertTool.cpp +++ b/Sources/OpenKey/engine/ConvertTool.cpp @@ -25,13 +25,13 @@ int convertToolHotKey = 0; static vector _breakCode = {'.', '?', '!'}; -static bool findKeyCode(const Uint32& charCode, const Uint8& code, int& j, int& k) { +static bool findKeyCode(const Uint32& charCode, const Uint8& code, int& codeTableRow, int& codeTableCol) { //find character which has tone/mark for (map>::iterator it = _codeTable[code].begin(); it != _codeTable[code].end(); ++it) { for (int z = 0; z < it->second.size(); z++) { if (charCode == it->second[z]) { - j = it->first; - k = z; + codeTableRow = it->first; + codeTableCol = z; return true; }//end if } @@ -50,8 +50,8 @@ static Uint16 getUnicodeCompoundMarkIndex(const Uint16& mark) { string convertUtil(const string& sourceString) { wstring data = utf8ToWideString(sourceString); - Uint16 t = 0, target; - int j, k, p; + Uint16 sourceCharCode = 0, target; + int codeTableRow, codeTableCol; vector _temp; bool hasBreak = false; bool shouldUpperCase = false; @@ -61,116 +61,102 @@ string convertUtil(const string& sourceString) { shouldUpperCase = false; for (int i = 0; i < data.size(); i++) { - p = 0; + Uint16 sourceCharCode = 0; + int extraCharsConsumed = 0; //find char with tone/mark if (i < data.size() - 1) { switch (convertToolFromCode) { case 2: //VNI case 4: //1258 - t = (Uint16)data[i] | (data[i+1] << 8); - p = 1; + sourceCharCode = (Uint16)data[i] | (data[i+1] << 8); + extraCharsConsumed = 1; break; case 3:{ //Unicode Compound target = getUnicodeCompoundMarkIndex(data[i+1]); if (target > 0){ - t = (Uint16)data[i] | target; - p = 1; + sourceCharCode = (Uint16)data[i] | target; + extraCharsConsumed = 1; } else { - t = (Uint16)data[i]; + sourceCharCode = (Uint16)data[i]; } break; } default: - t = (Uint16)data[i]; + sourceCharCode = (Uint16)data[i]; break; } - - if (findKeyCode(t, convertToolFromCode, j, k)) { - i += p; - target = _codeTable[convertToolToCode][j][k]; - if ((convertToolToAllCaps || shouldUpperCase) && k % 2 != 0) { - target = _codeTable[convertToolToCode][j][k-1]; - } else if ((convertToolToAllNonCaps || !shouldUpperCase) && k % 2 == 0) { - target = _codeTable[convertToolToCode][j][k+1]; - } - - //remove mark/tone - if (convertToolRemoveMark) { - target = keyCodeToCharacter((Uint8)j); - if (convertToolToAllCaps) { - target = towupper(target); - } else if (convertToolToAllNonCaps) { - target = towlower(target); - } - } - - if (convertToolToCode == 0 || convertToolToCode == 1) { //Unicode - _temp.push_back(target); - } else if (convertToolToCode == 2 || convertToolToCode == 4) { //VNI, VN Locale 1258 - if (HIBYTE(target) > 32) { - _temp.push_back((Uint8)target); - _temp.push_back(target>>8); - } else { - _temp.push_back((Uint8)target); - } - } else if (convertToolToCode == 3) { //Unicode Compound - if ((target >> 13) > 0) { - _temp.push_back(target & 0x1FFF); - _temp.push_back(_unicodeCompoundMark[(target>>13) - 1]); - } else { - _temp.push_back(target); - } - } - shouldUpperCase = false; - hasBreak = false; - continue; - } } - - //find primary keycode first - t = (Uint16)data[i]; - if (findKeyCode(t, convertToolFromCode, j, k)) { - target = _codeTable[convertToolToCode][j][k]; - if ((convertToolToAllCaps || shouldUpperCase) && k % 2 != 0) { - target = _codeTable[convertToolToCode][j][k-1]; - } else if ((convertToolToAllNonCaps || !shouldUpperCase) && k % 2 == 0) { - target = _codeTable[convertToolToCode][j][k+1]; + else // i = data.size() - 1 -- last character + { + sourceCharCode = (Uint16)data[i]; + } + + if (findKeyCode(sourceCharCode, convertToolFromCode, codeTableRow, codeTableCol)) { + i += extraCharsConsumed; + target = _codeTable[convertToolToCode][codeTableRow][codeTableCol]; + if ((convertToolToAllCaps || shouldUpperCase) && codeTableCol % 2 != 0) { + target = _codeTable[convertToolToCode][codeTableRow][codeTableCol-1]; + } else if ((convertToolToAllNonCaps) && codeTableCol % 2 == 0) { // || !shouldUpperCase + target = _codeTable[convertToolToCode][codeTableRow][codeTableCol+1]; } //remove mark/tone if (convertToolRemoveMark) { - target = keyCodeToCharacter((Uint8)j); + target = keyCodeToCharacter((Uint8)codeTableRow); if (convertToolToAllCaps) { target = towupper(target); - } else if (convertToolToAllNonCaps){ + } else if (convertToolToAllNonCaps) { target = towlower(target); + } else { + if (codeTableCol % 2 == 0) { + target = towupper(target); + } else { + target = towlower(target); + } } } - _temp.push_back(target); + if (convertToolToCode == 0 || convertToolToCode == 1) { //Unicode + _temp.push_back(target); + } else if (convertToolToCode == 2 || convertToolToCode == 4) { //VNI, VN Locale 1258 + if (HIBYTE(target) > 32) { + _temp.push_back((Uint8)target); + _temp.push_back(target>>8); + } else { + _temp.push_back((Uint8)target); + } + } else if (convertToolToCode == 3) { //Unicode Compound + if ((target >> 13) > 0) { + _temp.push_back(target & 0x1FFF); + _temp.push_back(_unicodeCompoundMark[(target>>13) - 1]); + } else { + _temp.push_back(target); + } + } shouldUpperCase = false; hasBreak = false; - continue; - } - - //if dont find => normal char - if (convertToolToAllCaps || shouldUpperCase) - _temp.push_back(towupper(data[i])); - else if (convertToolToAllNonCaps || !shouldUpperCase) - _temp.push_back(towlower(data[i])); + } // end if (findKeyCode(sourceCharCode else - _temp.push_back(data[i]); - - if (t == '\n' || (hasBreak && t == ' ')) { - if (convertToolToCapsFirstLetter || convertToolToCapsEachWord) + { + //if dont find => normal char + if (convertToolToAllCaps || shouldUpperCase) + _temp.push_back(towupper(data[i])); + else if (convertToolToAllNonCaps) // || !shouldUpperCase + _temp.push_back(towlower(data[i])); + else + _temp.push_back(data[i]); + + if (sourceCharCode == '\n' || (hasBreak && sourceCharCode == ' ')) { + if (convertToolToCapsFirstLetter || convertToolToCapsEachWord) + shouldUpperCase = true; + } else if (sourceCharCode == ' ' && convertToolToCapsEachWord) { shouldUpperCase = true; - } else if (t == ' ' && convertToolToCapsEachWord) { - shouldUpperCase = true; - } else if (std::find(_breakCode.begin(), _breakCode.end(), t) != _breakCode.end()) { - hasBreak = true; - } else { - shouldUpperCase = false; - hasBreak = false; + } else if (std::find(_breakCode.begin(), _breakCode.end(), sourceCharCode) != _breakCode.end()) { + hasBreak = true; + } else { + shouldUpperCase = false; + hasBreak = false; + } } } _temp.push_back(0);