From 0fa22ed55d1088eacc5ef063b7307fa1b4d031e6 Mon Sep 17 00:00:00 2001 From: Rosen Penev Date: Sun, 26 Mar 2023 17:53:28 -0700 Subject: [PATCH] remove codecvt Deprecated in C++17 Signed-off-by: Rosen Penev --- src/helper_functions.cpp | 23 +++++++++++++---------- src/helper_functions.hpp | 11 ----------- 2 files changed, 13 insertions(+), 21 deletions(-) diff --git a/src/helper_functions.cpp b/src/helper_functions.cpp index 609cd5cb53..7f62e4e8b0 100644 --- a/src/helper_functions.cpp +++ b/src/helper_functions.cpp @@ -3,9 +3,7 @@ #include "helper_functions.hpp" #include -#include #include -#include #include #include "enforce.hpp" @@ -17,17 +15,22 @@ std::string string_from_unterminated(const char* data, size_t data_length) { return {data, StringLength}; } -namespace Exiv2 { - -std::string utf16ToUtf8(const std::wstring& wstr) { - using convert_typeX = std::codecvt_utf8; - std::wstring_convert converterX; - - std::string str = converterX.to_bytes(wstr); +namespace { +std::string utf16ToUtf8(const std::u16string& wstr) { +#ifdef _MSC_VER +#pragma warning(push) +#pragma warning(disable : 4244) + auto str = std::string(wstr.begin(), wstr.end()); +#pragma warning(pop) +#else + auto str = std::string(wstr.begin(), wstr.end()); +#endif str.erase(std::remove(str.begin(), str.end(), '\0'), str.end()); return str; } +} // namespace +namespace Exiv2 { uint64_t readQWORDTag(const BasicIo::UniquePtr& io) { Internal::enforce(QWORD <= io->size() - io->tell(), Exiv2::ErrorCode::kerCorruptedMetadata); DataBuf FieldBuf = io->read(QWORD); @@ -50,7 +53,7 @@ std::string readStringWcharTag(const BasicIo::UniquePtr& io, size_t length) { Internal::enforce(length <= io->size() - io->tell(), Exiv2::ErrorCode::kerCorruptedMetadata); DataBuf FieldBuf(length + 1); io->readOrThrow(FieldBuf.data(), length, ErrorCode::kerFailedToReadImageData); - std::wstring wst(FieldBuf.begin(), FieldBuf.end()); + std::u16string wst(FieldBuf.begin(), FieldBuf.end()); return utf16ToUtf8(wst); } diff --git a/src/helper_functions.hpp b/src/helper_functions.hpp index a9fffa9507..a7c820e917 100644 --- a/src/helper_functions.hpp +++ b/src/helper_functions.hpp @@ -32,17 +32,6 @@ static constexpr size_t DWORD = 0x4; static constexpr size_t QWORD = 0x8; static constexpr size_t GUID = 0x10; -// @brief - -/*! - @brief The function utf16ToUtf8 takes a wide string wstr as input and converts it to a narrow string - The conversion is performed using the std::wstring_convert class template and a std::codecvt_utf8 facet, which - implements conversion between UTF-8 and wide characters. - @param wstr : wide string - @return Returns std::string object -*/ -std::string utf16ToUtf8(const std::wstring& wstr); - [[nodiscard]] uint64_t readQWORDTag(const Exiv2::BasicIo::UniquePtr& io); [[nodiscard]] uint32_t readDWORDTag(const Exiv2::BasicIo::UniquePtr& io);