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] Refactor common and wstring convert utils (#35843)
Pawel Raasza9b762847b6f
diff --git a/src/common/util/include/openvino/util/common_util.hpp b/src/common/util/include/openvino/util/common_util.hppindex 3907068f5d4..c766e7ce2bd 100644--- a/src/common/util/include/openvino/util/common_util.hpp+++ b/src/common/util/include/openvino/util/common_util.hpp@@ -379,18 +379,19 @@ std::optional<T> view_to_number(std::string_view sv) noexcept {return result.ec == std::errc() ? std::make_optional(value) : std::nullopt;} else {if (sv == "inf") {- return std::numeric_limits<T>::infinity();+ return std::make_optional(std::numeric_limits<T>::infinity());} else if (sv == "-inf") {- return -std::numeric_limits<T>::infinity();+ return std::make_optional(-std::numeric_limits<T>::infinity());} else if (sv == "nan") {- return std::numeric_limits<T>::quiet_NaN();+ return std::make_optional(std::numeric_limits<T>::quiet_NaN());+ } else {+ StringViewStreamBuf buf{sv};+ std::istream stream{&buf};+ stream.imbue(std::locale::classic());+ T value{};+ stream >> value;+ return stream ? std::make_optional<T>(value) : std::nullopt;}- StringViewStreamBuf buf{sv};- std::istream stream{&buf};- stream.imbue(std::locale::classic());- T value{};- stream >> value;- return stream ? std::make_optional<T>(value) : std::nullopt;}}diff --git a/src/common/util/src/wstring_convert_util.cpp b/src/common/util/src/os/lin/wstring_convert_util.cppsimilarity index 85%rename from src/common/util/src/wstring_convert_util.cpprename to src/common/util/src/os/lin/wstring_convert_util.cppindex cd900a7cdd2..5f360e9fc5a 100644--- a/src/common/util/src/wstring_convert_util.cpp+++ b/src/common/util/src/os/lin/wstring_convert_util.cpp@@ -7,10 +7,6 @@#include <cstdint>#include <stdexcept>-#ifdef _WIN32-# include <windows.h>-#endif-namespace ov::util {#ifdef OPENVINO_ENABLE_UNICODE_PATH_SUPPORT@@ -20,12 +16,6 @@ constexpr auto codepoint_3rd_shift = 12U;constexpr auto codepoint_4th_shift = 18U;std::string wstring_to_string(const std::wstring_view wstr) {-# ifdef _WIN32- 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@@ -55,19 +45,11 @@ std::string wstring_to_string(const std::wstring_view wstr) {}}result.shrink_to_fit();-# endifreturn result;}std::wstring string_to_wstring(const std::string_view string) {const char* str = string.data();-# ifdef _WIN32- 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);-# else-const auto check_utf8_seq_size = [](const char* first, const char* last, const std::ptrdiff_t seq_size) {if (seq_size > std::distance(first, last)) {throw std::runtime_error("Invalid UTF-8 sequence");@@ -104,8 +86,6 @@ std::wstring string_to_wstring(const std::string_view string) {result.push_back(static_cast<wchar_t>(codepoint));}--# endifreturn result;}#endif // OPENVINO_ENABLE_UNICODE_PATH_SUPPORTdiff --git a/src/common/util/src/os/win/wstring_convert_util.cpp b/src/common/util/src/os/win/wstring_convert_util.cppnew file mode 100644index 00000000000..2bcb8b576e2--- /dev/null+++ b/src/common/util/src/os/win/wstring_convert_util.cpp@@ -0,0 +1,29 @@+// Copyright (C) 2018-2026 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#include "openvino/util/wstring_convert_util.hpp"++#include <cstdint>+#include <windows.h>++namespace ov::util {+#ifdef OPENVINO_ENABLE_UNICODE_PATH_SUPPORT+std::string wstring_to_string(const std::wstring_view wstr) {+ 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);+ return result;+}++std::wstring string_to_wstring(const std::string_view string) {+ const char* str = string.data();+ 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);+ return result;+}+#endif // OPENVINO_ENABLE_UNICODE_PATH_SUPPORT+} // namespace ov::utildiff --git a/src/tests/test_utils/common_test_utils/tests/common_util_test.cpp b/src/tests/test_utils/common_test_utils/tests/common_util_test.cppindex b80a2ec61c4..04399c5ace3 100644--- a/src/tests/test_utils/common_test_utils/tests/common_util_test.cpp+++ b/src/tests/test_utils/common_test_utils/tests/common_util_test.cpp@@ -161,8 +161,16 @@ TEST_F(CommonUtilsTest, view_to_number_invalid_input){TEST_F(CommonUtilsTest, view_to_floating_point_number){EXPECT_FLOAT_EQ(123.456f, util::view_to_number<float>("123.456").value());+ EXPECT_FLOAT_EQ(-123.456f, util::view_to_number<float>("-123.456").value());+ EXPECT_FLOAT_EQ(-std::numeric_limits<float>::infinity(), util::view_to_number<float>("-inf").value());+ EXPECT_FLOAT_EQ(std::numeric_limits<float>::infinity(), util::view_to_number<float>("inf").value());+ EXPECT_TRUE(std::isnan(util::view_to_number<float>("nan").value()));+EXPECT_DOUBLE_EQ(123.456, util::view_to_number<double>("123.456").value());EXPECT_DOUBLE_EQ(-123.456, util::view_to_number<double>("-123.456").value());+ EXPECT_DOUBLE_EQ(-std::numeric_limits<double>::infinity(), util::view_to_number<double>("-inf").value());+ EXPECT_DOUBLE_EQ(std::numeric_limits<double>::infinity(), util::view_to_number<double>("inf").value());+ EXPECT_TRUE(std::isnan(util::view_to_number<double>("nan").value()));}TEST_F(CommonUtilsTest, ends_with) {