|
@@ -779,84 +779,52 @@ namespace robotics {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
/**
|
|
/**
|
|
- * @brief
|
|
|
|
- * @param str
|
|
|
|
- * @return
|
|
|
|
- */
|
|
|
|
- inline std::wstring gbk_to_unicode(const std::string& str) {
|
|
|
|
- std::wstring result;
|
|
|
|
- std::mbstate_t state{ };
|
|
|
|
- const char* src = str.data();
|
|
|
|
- size_t len = std::mbsrtowcs(nullptr, &src, 0, &state);
|
|
|
|
- if (len != static_cast<size_t>(-1)) {
|
|
|
|
- std::unique_ptr<wchar_t[]> buff(new wchar_t[len + 1]);
|
|
|
|
- len = std::mbsrtowcs(buff.get(), &src, len, &state);
|
|
|
|
- if (len != static_cast<size_t>(-1)) {
|
|
|
|
- result.assign(buff.get(), len);
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- return result;
|
|
|
|
|
|
+ * @brief 将 GBK 编码的字符串转换为 Unicode(宽字符串)。
|
|
|
|
+ * @param value 要转换的 GBK 编码字符串。
|
|
|
|
+ * @return 转换后的 Unicode(std::wstring)字符串。
|
|
|
|
+ */
|
|
|
|
+ inline std::wstring gbk_to_unicode(const std::string& value) {
|
|
|
|
+ return boost::locale::conv::to_utf<wchar_t>(value, "GBK");
|
|
}
|
|
}
|
|
/**
|
|
/**
|
|
- * @brief
|
|
|
|
- * @param wstr
|
|
|
|
- * @return
|
|
|
|
|
|
+ * @brief 将 Unicode 字符串(std::wstring)转换为 GBK 编码的 std::string。
|
|
|
|
+ * @param value 要转换的 Unicode 字符串(std::wstring 类型)。
|
|
|
|
+ * @return 转换后的 GBK 编码字符串(std::string 类型)。
|
|
*/
|
|
*/
|
|
- inline std::string unicode_to_gbk(std::wstring wstr) {
|
|
|
|
- std::string result;
|
|
|
|
- std::mbstate_t state{ };
|
|
|
|
- const wchar_t* src = wstr.data();
|
|
|
|
- size_t len = std::wcsrtombs(nullptr, &src, 0, &state);
|
|
|
|
- if (len != static_cast<size_t>(-1)) {
|
|
|
|
- std::unique_ptr<char[]> buff(new char[len + 1]);
|
|
|
|
- len = std::wcsrtombs(buff.get(), &src, len, &state);
|
|
|
|
- if (len != static_cast<size_t>(-1)) {
|
|
|
|
- result.assign(buff.get(), len);
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- return result;
|
|
|
|
|
|
+ inline std::string unicode_to_gbk(const std::wstring& value) {
|
|
|
|
+ return boost::locale::conv::from_utf<wchar_t>(value, "GBK");
|
|
}
|
|
}
|
|
/**
|
|
/**
|
|
- * @brief OK
|
|
|
|
- * @param wstr
|
|
|
|
- * @return
|
|
|
|
|
|
+ * @brief 将 Unicode 宽字符串(std::wstring)转换为 UTF-8 编码的 std::string。
|
|
|
|
+ * @param value 要转换的 Unicode 宽字符串。
|
|
|
|
+ * @return 转换后的 UTF-8 编码的 std::string。
|
|
*/
|
|
*/
|
|
- inline std::string unicode_to_utf8(const std::wstring& wstr) {
|
|
|
|
- try {
|
|
|
|
- std::wstring_convert<std::codecvt_utf8<wchar_t>> wcv;
|
|
|
|
- return wcv.to_bytes(wstr);
|
|
|
|
- }
|
|
|
|
- catch (...) {}
|
|
|
|
- return "";
|
|
|
|
|
|
+ inline std::string unicode_to_utf8(const std::wstring& value) {
|
|
|
|
+ return boost::locale::conv::utf_to_utf<char>(value);
|
|
}
|
|
}
|
|
/**
|
|
/**
|
|
- * @brief
|
|
|
|
- * @param str
|
|
|
|
- * @return
|
|
|
|
|
|
+ * @brief 将 UTF-8 编码的字符串转换为 Unicode(宽字符串)。
|
|
|
|
+ * @param value 要转换的 UTF-8 编码字符串。
|
|
|
|
+ * @return 转换后的 Unicode(std::wstring)字符串。
|
|
*/
|
|
*/
|
|
- inline std::wstring utf8_to_unicode(const std::string& str) {
|
|
|
|
- try {
|
|
|
|
- std::wstring_convert<std::codecvt_utf8<wchar_t>> wcv;
|
|
|
|
- return wcv.from_bytes(str);
|
|
|
|
- }
|
|
|
|
- catch (...) {}
|
|
|
|
- return L"";
|
|
|
|
|
|
+ inline std::wstring utf8_to_unicode(const std::string& value) {
|
|
|
|
+ return boost::locale::conv::utf_to_utf<wchar_t>(value);
|
|
}
|
|
}
|
|
/**
|
|
/**
|
|
- * @brief
|
|
|
|
- * @param str
|
|
|
|
- * @return
|
|
|
|
- */
|
|
|
|
- inline std::string utf8_to_gbk(const std::string& str) {
|
|
|
|
- return unicode_to_gbk(utf8_to_unicode(str));
|
|
|
|
|
|
+ * @brief utf8转ansi
|
|
|
|
+ * @param value
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ inline std::string utf8_to_gbk(std::string const& value) {
|
|
|
|
+ return unicode_to_gbk(utf8_to_unicode(value));
|
|
}
|
|
}
|
|
/**
|
|
/**
|
|
- * @brief
|
|
|
|
- * @param str
|
|
|
|
|
|
+ * @brief ansi转utf8
|
|
|
|
+ * @param value
|
|
* @return
|
|
* @return
|
|
*/
|
|
*/
|
|
- inline std::string gbk_to_utf8(const std::string& str) {
|
|
|
|
- return unicode_to_utf8(gbk_to_unicode(str));
|
|
|
|
|
|
+ inline std::string gbk_to_utf8(std::string const& value) {
|
|
|
|
+ return unicode_to_utf8(gbk_to_unicode(value));
|
|
}
|
|
}
|
|
/**
|
|
/**
|
|
* @brief 转ut8
|
|
* @brief 转ut8
|