std::wstring LuaManager::ToWide(lua_State* L, int narg) { return ConvertUTF8ToWide(lua_tostring(L, narg)); }
/* ** Substitutes part of the text */ const WCHAR* CMeasure::CheckSubstitute(const WCHAR* buffer) { static std::wstring str; if (!m_Substitute.empty()) { if (!m_RegExpSubstitute) // Plain Substitutions only { str = buffer; for (size_t i = 0, isize = m_Substitute.size(); i < isize; i += 2) { if (!m_Substitute[i].empty()) { MakePlainSubstitute(str, i); } else if (str.empty()) { // Empty result and empty substitute -> use second str = m_Substitute[i + 1]; } } } else // Contains a RegEx { std::string utf8str = ConvertToUTF8(buffer); int* ovector = new int[OVECCOUNT]; for (size_t i = 0, isize = m_Substitute.size(); i < isize ; i += 2) { pcre* re; const char* error; int erroffset; int rc; int flags = PCRE_UTF8; int offset = 0; re = pcre_compile( ConvertToUTF8(m_Substitute[i].c_str()).c_str(), // the pattern flags, // default options &error, // for error message &erroffset, // for error offset NULL); // use default character tables if (re == NULL) { MakePlainSubstitute(str, i); Log(LOG_NOTICE, ConvertToWide(error).c_str()); } else { do { rc = pcre_exec( re, // the compiled pattern NULL, // no extra data - we didn't study the pattern utf8str.c_str(), // the subject string utf8str.length(), // the length of the subject offset, // start at offset 0 in the subject 0, // default options ovector, // output vector for substring information OVECCOUNT); // number of elements in the output vector if (rc <= 0) { break; } else { std::string result = ConvertToUTF8(m_Substitute[i + 1].c_str()); if (rc > 1) { for (int j = rc - 1 ; j >= 0 ; --j) { size_t new_start = ovector[2 * j]; size_t in_length = ovector[2 * j + 1] - ovector[2 * j]; char tmpName[64]; size_t cut_length = _snprintf_s(tmpName, _TRUNCATE, "\\%i", j);; size_t start = 0, pos; do { pos = result.find(tmpName, start, cut_length); if (pos != std::string::npos) { result.replace(pos, cut_length, utf8str, new_start, in_length); start = pos + in_length; } } while (pos != std::string::npos); } } size_t start = ovector[0]; size_t length = ovector[1] - ovector[0]; utf8str.replace(start, length, result); offset = start + result.length(); } } while (true); // Release memory used for the compiled pattern pcre_free(re); } } delete [] ovector; str = ConvertUTF8ToWide(utf8str.c_str()); } return str.c_str(); } else { return buffer; } }
UTF8ToWideConverter(const char *_value) :value(ConvertUTF8ToWide(_value)) {}