zxs 3 months ago
parent
commit
2a2d5e4195
2 changed files with 67 additions and 34 deletions
  1. 2 2
      CMakeLists.txt
  2. 65 32
      robot/robotics/utils.hpp

+ 2 - 2
CMakeLists.txt

@@ -22,6 +22,6 @@ elseif(UNIX)
 endif()
 # 包含子项目。
 # add_subdirectory ("robot")
-add_subdirectory ("logger")
+# add_subdirectory ("logger")
 # add_subdirectory ("drivers")
-# add_subdirectory ("generator")
+add_subdirectory ("generator")

+ 65 - 32
robot/robotics/utils.hpp

@@ -18,6 +18,7 @@
 #include <openssl/des.h>
 #include <openssl/aes.h>
 //boost
+#include <boost/locale.hpp>
 #include <boost/uuid/uuid.hpp>
 #include <boost/uuid/uuid_io.hpp>
 #include <boost/algorithm/hex.hpp>
@@ -737,52 +738,84 @@ namespace robotics::v3 {
 			}
 		}
 		/**
-		 * @brief 
-		 * @param str 
-		 * @return 
-		 */
-		inline std::string utf8_to_gbk(const std::string& str) {
-			return boost::locale::conv::between(str, "GBK", "UTF-8");
+	 * @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 
-		 * @param str 
-		 * @return 
+		 * @brief
+		 * @param wstr
+		 * @return
 		 */
-		inline std::string gbk_to_utf8(const std::string& str) {
-			return boost::locale::conv::between(str, "UTF-8", "GBK");
+		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;
 		}
 		/**
-		 * @brief 
-		 * @param str 
-		 * @return 
+		 * @brief OK
+		 * @param wstr
+		 * @return
 		 */
-		inline std::wstring gbk_to_unicode(const std::string& str) {
-			return boost::locale::conv::to_utf<wchar_t>(str, "GBK");
+		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 "";
 		}
 		/**
-		 * @brief 
-		 * @param wstr 
-		 * @return 
+		 * @brief
+		 * @param str
+		 * @return
 		 */
-		inline std::string unicode_to_gbk(std::wstring wstr) {
-			return boost::locale::conv::from_utf(wstr, "GBK");
+		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"";
 		}
 		/**
-		 * @brief 
-		 * @param wstr 
-		 * @return 
-		 */
-		inline std::string unicode_to_utf8(const std::wstring& wstr) {
-			return boost::locale::conv::from_utf(wstr, "UTF-8");
+		* @brief
+		* @param str
+		* @return
+		*/
+		inline std::string utf8_to_gbk(const std::string& str) {
+			return unicode_to_gbk(utf8_to_unicode(str));
 		}
 		/**
-		 * @brief 
-		 * @param str 
-		 * @return 
+		 * @brief
+		 * @param str
+		 * @return
 		 */
-		inline std::wstring utf8_to_unicode(const std::string& str) {
-			return boost::locale::conv::utf_to_utf<wchar_t>(str);
+		inline std::string gbk_to_utf8(const std::string& str) {
+			return unicode_to_utf8(gbk_to_unicode(str));
 		}
 		/**
 		 * @brief 转ut8