Skip to content

Commit

Permalink
remove codecvt
Browse files Browse the repository at this point in the history
Deprecated in C++17

Signed-off-by: Rosen Penev <rosenp@gmail.com>
  • Loading branch information
neheb committed Mar 27, 2023
1 parent 2e33dad commit 0fa22ed
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 21 deletions.
23 changes: 13 additions & 10 deletions src/helper_functions.cpp
Expand Up @@ -3,9 +3,7 @@
#include "helper_functions.hpp"

#include <cmath>
#include <codecvt>
#include <cstring>
#include <locale>
#include <numeric>
#include "enforce.hpp"

Expand All @@ -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<wchar_t>;
std::wstring_convert<convert_typeX, wchar_t> 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);
Expand All @@ -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);
}

Expand Down
11 changes: 0 additions & 11 deletions src/helper_functions.hpp
Expand Up @@ -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);
Expand Down

0 comments on commit 0fa22ed

Please sign in to comment.