OpenVINO™ is an open source toolkit for optimizing and deploying AI inference
C++ aicomputer-visiondeep-learningdeploy-aidiffusion-modelsgenerative-aigood-first-issueinferencellm-inferencenatural-language-processingnlpopenvinooptimize-aiperformance-boostrecommendation-systemspeech-recognitionstable-diffusiontransformersyolo
[core] Use view as input for string <-> wstring utils (#33414)
Pawel Raasz1a68214b2555
diff --git a/src/common/util/include/openvino/util/wstring_convert_util.hpp b/src/common/util/include/openvino/util/wstring_convert_util.hppindex f5f8512bcf8..bd30d2e426e 100644--- a/src/common/util/include/openvino/util/wstring_convert_util.hpp+++ b/src/common/util/include/openvino/util/wstring_convert_util.hpp@@ -5,6 +5,7 @@#pragma once#include <string>+#include <string_view>#include "openvino/util/util.hpp"@@ -16,13 +17,14 @@ namespace ov::util {* @param wstr A wide-char string* @return A multi-byte string*/-std::string wstring_to_string(const std::wstring& wstr);+std::string wstring_to_string(const std::wstring_view wstr);+/*** @brief Conversion from single-byte chain to wide character string.- * @param str A null-terminated string+ * @param str A multi-byte string* @return A wide-char string*/-std::wstring string_to_wstring(const std::string& str);+std::wstring string_to_wstring(const std::string_view str);#endifdiff --git a/src/common/util/src/wstring_convert_util.cpp b/src/common/util/src/wstring_convert_util.cppindex f3e8fbb5d5a..ae9ad4b1b97 100644--- a/src/common/util/src/wstring_convert_util.cpp+++ b/src/common/util/src/wstring_convert_util.cpp@@ -6,6 +6,7 @@#include <cstdint>#include <stdexcept>+#ifdef _WIN32# include <windows.h>#endif@@ -18,12 +19,12 @@ constexpr auto codepoint_2nd_shift = 6U;constexpr auto codepoint_3rd_shift = 12U;constexpr auto codepoint_4th_shift = 18U;-std::string wstring_to_string(const std::wstring& wstr) {+std::string wstring_to_string(const std::wstring_view wstr) {# ifdef _WIN32- int size_needed = WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (int)wstr.size(), NULL, 0, NULL, NULL);- std::string strTo(size_needed, 0);- WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (int)wstr.size(), &strTo[0], size_needed, NULL, NULL);- return strTo;+ const auto wstr_size = static_cast<int>(wstr.size());+ const auto size_needed = WideCharToMultiByte(CP_UTF8, 0, wstr.data(), wstr_size, NULL, 0, NULL, NULL);+ std::string result(size_needed, 0);+ WideCharToMultiByte(CP_UTF8, 0, wstr.data(), wstr_size, result.data(), size_needed, NULL, NULL);# elsestd::string result;result.reserve(wstr.size() * (sizeof(wchar_t) >= 4 ? 4 : 3)); // Worst case for UTF-8@@ -54,18 +55,17 @@ std::string wstring_to_string(const std::wstring& wstr) {}}result.shrink_to_fit();- return result;# endif+ return result;}-std::wstring string_to_wstring(const std::string& string) {- const char* str = string.c_str();+std::wstring string_to_wstring(const std::string_view string) {+ const char* str = string.data();# ifdef _WIN32- int strSize = static_cast<int>(std::strlen(str));- int size_needed = MultiByteToWideChar(CP_UTF8, 0, str, strSize, NULL, 0);- std::wstring wstrTo(size_needed, 0);- MultiByteToWideChar(CP_UTF8, 0, str, strSize, &wstrTo[0], size_needed);- return wstrTo;+ const auto str_size = static_cast<int>(string.size());+ const auto size_needed = MultiByteToWideChar(CP_UTF8, 0, str, str_size, NULL, 0);+ std::wstring result(size_needed, 0);+ MultiByteToWideChar(CP_UTF8, 0, str, str_size, result.data(), size_needed);# elseconst auto check_utf8_seq_size = [](const char* first, const char* last, const std::ptrdiff_t seq_size) {@@ -105,8 +105,8 @@ std::wstring string_to_wstring(const std::string& string) {result.push_back(static_cast<wchar_t>(codepoint));}- return result;# endif+ return result;}#endif // OPENVINO_ENABLE_UNICODE_PATH_SUPPORT} // namespace ov::util