我在我的 C++ 项目中处理了很多 Unicode 文件路径。我在我的代码中执行检查,如果它们足够好以适合多字节字符串,我将其保留为普通字符串 (std::string) 变量,如果字符串不适合多字节,我将其用作宽字符字符串。
我的问题是我是否可以将路径完全用作 wstrings ..?它会影响性能吗,我必须使用 wstring 进行一些字符串操作、文件打开、创建、重命名和删除。因此,与其检查多字节或宽字符字符串,我想直接将它用作 wstring,这将为我节省很多 if/else。
bool IsUnicodeWString(const std::wstring &_WStr)
{
WCHAR* posUnicodePath = (WCHAR*)_WStr.c_str();
size_t multiByteLen = wcstombs(NULL, posUnicodePath, 0) + 1;
int tempLength = 0;
if (multiByteLen > 0)
{
TCHAR* _tmpTChar = new TCHAR[multiByteLen + 1];
memset(_tmpTChar, '\0', multiByteLen + 1);
tempLength = wcstombs(_tmpTChar, posUnicodePath, multiByteLen);
if (tempLength == std::string::npos)
{
multiByteLen = 0;
}
delete[] _tmpTChar;
}
if(multiByteLen == 0 || multiByteLen == std::string::npos) { // Is Unicode file
return true;
}
else{
return false;
}
}
if(IsUnicodeWString) {
// Use wstring [ Operations - String Manipulations,FilePath used for Open,Read,Write,Create,Delete,Rename,etc]
} else {
//string [ Operations - String Manipulations,FilePath used for Open,Read,Write,Create,Delete,Rename,etc]
}
请分享你的想法...