|
@@ -167,169 +167,67 @@ std::string CLewaimaiString::UrlDecode(const std::string& str)
|
|
|
return strTemp;
|
|
return strTemp;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-/*
|
|
|
|
|
-std::string CLewaimaiString::UnicodeToUTF8(const std::wstring wstr)
|
|
|
|
|
-{
|
|
|
|
|
- std::string ret;
|
|
|
|
|
-
|
|
|
|
|
- try
|
|
|
|
|
- {
|
|
|
|
|
- std::wstring_convert< std::codecvt_utf8<wchar_t> > wcv;
|
|
|
|
|
- ret = wcv.to_bytes(wstr);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- catch(const std::exception& e)
|
|
|
|
|
- {
|
|
|
|
|
- std::cerr << e.what() << std::endl;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- return ret;
|
|
|
|
|
-}*/
|
|
|
|
|
-
|
|
|
|
|
-/*
|
|
|
|
|
-std::wstring CLewaimaiString::UTF8ToUnicode(const std::string str)
|
|
|
|
|
-{
|
|
|
|
|
- std::wstring ret;
|
|
|
|
|
-
|
|
|
|
|
- try
|
|
|
|
|
- {
|
|
|
|
|
- std::wstring_convert< std::codecvt_utf8<wchar_t> > wcv;
|
|
|
|
|
- ret = wcv.from_bytes(str);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- catch(const std::exception& e)
|
|
|
|
|
- {
|
|
|
|
|
- std::cerr << e.what() << std::endl;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- return ret;
|
|
|
|
|
-}*/
|
|
|
|
|
-
|
|
|
|
|
-std::string CLewaimaiString::UnicodeToUTF8(const std::wstring wstr)
|
|
|
|
|
-{
|
|
|
|
|
- char* pElementText;
|
|
|
|
|
- int iTextLen;
|
|
|
|
|
- // wide char to multi char
|
|
|
|
|
- iTextLen = WideCharToMultiByte(CP_UTF8,
|
|
|
|
|
- 0,
|
|
|
|
|
- wstr.c_str(),
|
|
|
|
|
- -1,
|
|
|
|
|
- NULL,
|
|
|
|
|
- 0,
|
|
|
|
|
- NULL,
|
|
|
|
|
- NULL);
|
|
|
|
|
- pElementText = new char[iTextLen + 1];
|
|
|
|
|
- memset((void*)pElementText, 0, sizeof(char) * (iTextLen + 1));
|
|
|
|
|
- ::WideCharToMultiByte(CP_UTF8,
|
|
|
|
|
- 0,
|
|
|
|
|
- wstr.c_str(),
|
|
|
|
|
- -1,
|
|
|
|
|
- pElementText,
|
|
|
|
|
- iTextLen,
|
|
|
|
|
- NULL,
|
|
|
|
|
- NULL);
|
|
|
|
|
- std::string strText;
|
|
|
|
|
- strText = pElementText;
|
|
|
|
|
- delete[] pElementText;
|
|
|
|
|
- return strText;
|
|
|
|
|
|
|
+std::string CLewaimaiString::UnicodeToUTF8(const std::wstring& wstr)
|
|
|
|
|
+{
|
|
|
|
|
+ if (wstr.empty()) return {};
|
|
|
|
|
+ int n = WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), -1, nullptr, 0, nullptr, nullptr);
|
|
|
|
|
+ if (n <= 0) return {}; // UTF-8 非法序列也会返回 0
|
|
|
|
|
+ std::string result(n, 0); // 先留 null 位
|
|
|
|
|
+ WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), -1, &result[0], n, nullptr, nullptr);
|
|
|
|
|
+ result.resize(n - 1); // 去掉结尾 '\0'
|
|
|
|
|
+ return result;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-std::wstring CLewaimaiString::UTF8ToUnicode(const std::string str)
|
|
|
|
|
|
|
+std::wstring CLewaimaiString::UTF8ToUnicode(const std::string& str)
|
|
|
{
|
|
{
|
|
|
- int len = 0;
|
|
|
|
|
- len = str.length();
|
|
|
|
|
- int unicodeLen = ::MultiByteToWideChar(CP_UTF8,
|
|
|
|
|
- 0,
|
|
|
|
|
- str.c_str(),
|
|
|
|
|
- -1,
|
|
|
|
|
- NULL,
|
|
|
|
|
- 0);
|
|
|
|
|
- wchar_t* pUnicode;
|
|
|
|
|
- pUnicode = new wchar_t[unicodeLen + 1];
|
|
|
|
|
- memset(pUnicode, 0, (unicodeLen + 1) * sizeof(wchar_t));
|
|
|
|
|
- ::MultiByteToWideChar(CP_UTF8,
|
|
|
|
|
- 0,
|
|
|
|
|
- str.c_str(),
|
|
|
|
|
- -1,
|
|
|
|
|
- (LPWSTR)pUnicode,
|
|
|
|
|
- unicodeLen);
|
|
|
|
|
- std::wstring rt;
|
|
|
|
|
- rt = (wchar_t*)pUnicode;
|
|
|
|
|
- delete pUnicode;
|
|
|
|
|
-
|
|
|
|
|
- return rt;
|
|
|
|
|
|
|
+ if (str.empty()) return {};
|
|
|
|
|
+ int n = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, nullptr, 0);
|
|
|
|
|
+ if (n <= 0) return {};
|
|
|
|
|
+ std::wstring result(n, 0);
|
|
|
|
|
+ MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, &result[0], n);
|
|
|
|
|
+ result.resize(n - 1);
|
|
|
|
|
+ return result;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-std::string CLewaimaiString::UnicodeToANSI(const std::wstring wstr)
|
|
|
|
|
-{
|
|
|
|
|
- unsigned len = wstr.size() * 4;
|
|
|
|
|
- if(len == 0)
|
|
|
|
|
- {
|
|
|
|
|
- return "";
|
|
|
|
|
- }
|
|
|
|
|
|
|
|
|
|
- setlocale(LC_CTYPE, "");
|
|
|
|
|
- char* p = new char[len];
|
|
|
|
|
- wcstombs_s(&len, p, len, wstr.c_str(), len);
|
|
|
|
|
- std::string str1(p);
|
|
|
|
|
- delete[] p;
|
|
|
|
|
- return str1;
|
|
|
|
|
|
|
+std::string CLewaimaiString::UnicodeToGBK(const std::wstring& wstr)
|
|
|
|
|
+{
|
|
|
|
|
+ if (wstr.empty()) return {};
|
|
|
|
|
+ int n = WideCharToMultiByte(936, 0, wstr.c_str(), -1, nullptr, 0, nullptr, nullptr);
|
|
|
|
|
+ if (n <= 0) return {}; // 转换失败保护
|
|
|
|
|
+ std::string result(n, 0); // 先留 null 的位置
|
|
|
|
|
+ WideCharToMultiByte(936, 0, wstr.c_str(), -1, &result[0], n, nullptr, nullptr);
|
|
|
|
|
+ result.resize(n - 1); // 去掉结尾 '\0',size 准确
|
|
|
|
|
+ return result;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-std::wstring CLewaimaiString::ANSIToUnicode(const std::string str)
|
|
|
|
|
|
|
+std::wstring CLewaimaiString::GBKToUnicode(const std::string& src)
|
|
|
{
|
|
{
|
|
|
- unsigned len = str.size() * 2;// 预留字节数
|
|
|
|
|
- if(len == 0)
|
|
|
|
|
- {
|
|
|
|
|
- return L"";
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- setlocale(LC_CTYPE, ""); //必须调用此函数
|
|
|
|
|
- wchar_t* p = new wchar_t[len];// 申请一段内存存放转换后的字符串
|
|
|
|
|
- mbstowcs_s(&len, p, len, str.c_str(), len);// 转换
|
|
|
|
|
- std::wstring str1(p);
|
|
|
|
|
- delete[] p;// 释放申请的内存
|
|
|
|
|
- return str1;
|
|
|
|
|
|
|
+ if (src.empty()) return {};
|
|
|
|
|
+ int n = MultiByteToWideChar(936, 0, src.c_str(), -1, nullptr, 0);
|
|
|
|
|
+ if (n <= 0) return {};
|
|
|
|
|
+ std::wstring result(n, 0);
|
|
|
|
|
+ MultiByteToWideChar(936, 0, src.c_str(), -1, &result[0], n);
|
|
|
|
|
+ result.resize(n - 1);
|
|
|
|
|
+ return result;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-std::string CLewaimaiString::UTF8ToANSI(const std::string str)
|
|
|
|
|
|
|
+std::string CLewaimaiString::UTF8ToGBK(const std::string& str)
|
|
|
{
|
|
{
|
|
|
std::wstring wsUnicode = CLewaimaiString::UTF8ToUnicode(str);
|
|
std::wstring wsUnicode = CLewaimaiString::UTF8ToUnicode(str);
|
|
|
- std::string sAnsi = CLewaimaiString::UnicodeToANSI(wsUnicode);
|
|
|
|
|
|
|
+ std::string sGBK = CLewaimaiString::UnicodeToGBK(wsUnicode);
|
|
|
|
|
|
|
|
- return sAnsi;
|
|
|
|
|
|
|
+ return sGBK;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-std::string CLewaimaiString::ANSIToUTF8(const std::string str)
|
|
|
|
|
|
|
+std::string CLewaimaiString::GBKToUTF8(const std::string& str)
|
|
|
{
|
|
{
|
|
|
- std::wstring wsUnicode = CLewaimaiString::ANSIToUnicode(str);
|
|
|
|
|
|
|
+ std::wstring wsUnicode = CLewaimaiString::GBKToUnicode(str);
|
|
|
std::string sUtf8 = CLewaimaiString::UnicodeToUTF8(wsUnicode);
|
|
std::string sUtf8 = CLewaimaiString::UnicodeToUTF8(wsUnicode);
|
|
|
|
|
|
|
|
return sUtf8;
|
|
return sUtf8;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-std::string CLewaimaiString::UnicodeToGB2312(const std::wstring& wstr)
|
|
|
|
|
-{
|
|
|
|
|
- std::string result;
|
|
|
|
|
-
|
|
|
|
|
- int n = WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), -1, 0, 0, 0, 0);
|
|
|
|
|
- result.resize(n);
|
|
|
|
|
- ::WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), -1, (char*)result.c_str(), n, 0, 0);
|
|
|
|
|
-
|
|
|
|
|
- return result;
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-std::wstring CLewaimaiString::GB2312ToUnicode(const std::string& src)
|
|
|
|
|
-{
|
|
|
|
|
- std::wstring result;
|
|
|
|
|
-
|
|
|
|
|
- int n = MultiByteToWideChar(CP_ACP, 0, src.c_str(), -1, NULL, 0);
|
|
|
|
|
- result.resize(n);
|
|
|
|
|
- ::MultiByteToWideChar(CP_ACP, 0, src.c_str(), -1, (LPWSTR)result.c_str(), result.length());
|
|
|
|
|
-
|
|
|
|
|
- return result;
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
std::string CLewaimaiString::DoubleToString(const double value, unsigned int precisionAfterPoint)
|
|
std::string CLewaimaiString::DoubleToString(const double value, unsigned int precisionAfterPoint)
|
|
|
{
|
|
{
|
|
|
char str[256];
|
|
char str[256];
|
|
@@ -518,18 +416,51 @@ void CLewaimaiString::string_replace(std::string &strBig, const std::string &str
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
//从文件路径或者url中获取文件名
|
|
//从文件路径或者url中获取文件名
|
|
|
-std::string CLewaimaiString::GetPathOrURLShortName(std::string strFullName)
|
|
|
|
|
|
|
+std::string CLewaimaiString::GetFilenameFromUrl(std::string url)
|
|
|
{
|
|
{
|
|
|
- if (strFullName.empty())
|
|
|
|
|
- {
|
|
|
|
|
- return "";
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ if (url.empty()) return std::string();
|
|
|
|
|
+
|
|
|
|
|
+ // 截取掉查询字符串和片段标识
|
|
|
|
|
+ size_t end = url.find_first_of("?#");
|
|
|
|
|
+ if (end == std::string::npos) end = url.size();
|
|
|
|
|
+
|
|
|
|
|
+ // 找到最后一个 '/',取其后面的部分作为文件名
|
|
|
|
|
+ size_t pos = (end == 0) ? std::string::npos : url.find_last_of('/', end - 1);
|
|
|
|
|
+ size_t start = (pos == std::string::npos) ? 0 : pos + 1;
|
|
|
|
|
+ if (start >= end) return std::string();
|
|
|
|
|
|
|
|
- string_replace(strFullName, "/", "\\");
|
|
|
|
|
|
|
+ std::string name = url.substr(start, end - start);
|
|
|
|
|
|
|
|
- std::string::size_type iPos = strFullName.find_last_of('\\') + 1;
|
|
|
|
|
|
|
+ // 对 %XX 编码进行解码(返回 UTF-8 字节序列)
|
|
|
|
|
+ auto hexVal = [](char c) -> int
|
|
|
|
|
+ {
|
|
|
|
|
+ if (c >= '0' && c <= '9') return c - '0';
|
|
|
|
|
+ if (c >= 'a' && c <= 'f') return 10 + (c - 'a');
|
|
|
|
|
+ if (c >= 'A' && c <= 'F') return 10 + (c - 'A');
|
|
|
|
|
+ return -1;
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
|
|
+ std::string decoded;
|
|
|
|
|
+ decoded.reserve(name.size());
|
|
|
|
|
+ for (size_t i = 0; i < name.size(); ++i)
|
|
|
|
|
+ {
|
|
|
|
|
+ char c = name[i];
|
|
|
|
|
+ if (c == '%' && i + 2 < name.size())
|
|
|
|
|
+ {
|
|
|
|
|
+ int hi = hexVal(name[i + 1]);
|
|
|
|
|
+ int lo = hexVal(name[i + 2]);
|
|
|
|
|
+ if (hi != -1 && lo != -1)
|
|
|
|
|
+ {
|
|
|
|
|
+ char ch = static_cast<char>((hi << 4) | lo);
|
|
|
|
|
+ decoded.push_back(ch);
|
|
|
|
|
+ i += 2;
|
|
|
|
|
+ continue;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ decoded.push_back(c);
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- return strFullName.substr(iPos, strFullName.length() - iPos);
|
|
|
|
|
|
|
+ return decoded;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
//得到文件路径的目录
|
|
//得到文件路径的目录
|
|
@@ -552,7 +483,7 @@ std::string CLewaimaiString::BuZifuLeft(std::string old, int num, const char zif
|
|
|
std::string CLewaimaiString::GetQuweima(std::wstring input)
|
|
std::string CLewaimaiString::GetQuweima(std::wstring input)
|
|
|
{
|
|
{
|
|
|
//先转换成GB2312,区位码都是相对于GB2312的
|
|
//先转换成GB2312,区位码都是相对于GB2312的
|
|
|
- std::string gb_hanzi = CLewaimaiString::UnicodeToGB2312(input);
|
|
|
|
|
|
|
+ std::string gb_hanzi = CLewaimaiString::UnicodeToGBK(input);
|
|
|
|
|
|
|
|
std::string result = "";
|
|
std::string result = "";
|
|
|
|
|
|
|
@@ -668,7 +599,7 @@ bool CLewaimaiString::is_only_number(std::string str)
|
|
|
return true;
|
|
return true;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-std::string CLewaimaiString::generateRandomStr(size_t len, bool useTimeSeed)
|
|
|
|
|
|
|
+std::string CLewaimaiString::GenerateRandomStr(size_t len, bool useTimeSeed)
|
|
|
{
|
|
{
|
|
|
// 1. 定义字符集:大小写字母 + 数字(可自行增删)
|
|
// 1. 定义字符集:大小写字母 + 数字(可自行增删)
|
|
|
const std::string charset = "0123456789"
|
|
const std::string charset = "0123456789"
|
|
@@ -713,4 +644,4 @@ std::string CLewaimaiString::generateRandomStr(size_t len, bool useTimeSeed)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
return result;
|
|
|
-}
|
|
|
|
|
|
|
+}
|