Skip to content

Commit

Permalink
refactor(EncodingConverter): remove deprecated conversion methods and…
Browse files Browse the repository at this point in the history
… simplify implementation
  • Loading branch information
SHIINASAMA committed Jan 7, 2025
1 parent fa25adb commit 30dd316
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 39 deletions.
25 changes: 5 additions & 20 deletions sese/convert/EncodingConverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,14 @@
// limitations under the License.

#include "sese/convert/EncodingConverter.h"
#include <codecvt>
#include <locale>

// GCOVR_EXCL_START

static std::wstring_convert<std::codecvt_utf8<wchar_t>> *convert; /* NOLINT */
#include "sese/text/String.h"

std::string sese::EncodingConverter::toString(const std::wstring &wstring) noexcept {
return convert->to_bytes(wstring.c_str());
auto str = text::String::fromUCS2LE(wstring.c_str());
return str.toString();
}

std::wstring sese::EncodingConverter::toWstring(const std::string &string) noexcept {
return convert->from_bytes(string.c_str());
}

int32_t sese::EncodingConverterInitiateTask::init() noexcept {
convert = new std::wstring_convert<std::codecvt_utf8<wchar_t>>;
return 0;
auto str = text::String::fromUTF8(string.c_str());
return str.toWString();
}

int32_t sese::EncodingConverterInitiateTask::destroy() noexcept {
delete convert;
return 0;
}

// GCOVR_EXCL_STOP
14 changes: 1 addition & 13 deletions sese/convert/EncodingConverter.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,30 +25,18 @@
#include "sese/util/NotInstantiable.h"
#include "sese/util/Initializer.h"

#ifdef _WIN32
#pragma warning(disable : 4996)
#pragma warning(disable : 4624)
#endif

namespace sese {

/**
* @brief String encoding conversion
*/
class SESE_DEPRECATED EncodingConverter final : public NotInstantiable {
class EncodingConverter final : public NotInstantiable {
public:
EncodingConverter() = delete;

[[maybe_unused]] static std::string toString(const std::wstring &wstring) noexcept;
[[maybe_unused]] static std::wstring toWstring(const std::string &string) noexcept;
};

/// Encoding converter initialization task
class EncodingConverterInitiateTask final : public InitiateTask {
public:
EncodingConverterInitiateTask() : InitiateTask(__FUNCTION__) {}

int32_t init() noexcept override;
int32_t destroy() noexcept override;
};
} // namespace sese
12 changes: 6 additions & 6 deletions sese/test/TestEncodingConverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,24 @@

#include <sese/convert/EncodingConverter.h>

#include <codecvt>

#include <gtest/gtest.h>

TEST(TestEncoding, String2WString) {
std::string str = "你好";
std::wstring wstr = L"你好";

// ASSERT_EQ(sese::EncodingConverter::toWstring(str), wstr);
std::wstring_convert<std::codecvt_utf8<wchar_t>> convert;
EXPECT_EQ(convert.from_bytes(str), wstr);
// std::wstring_convert<std::codecvt_utf8<wchar_t>> convert;
// EXPECT_EQ(convert.from_bytes(str), wstr);
EXPECT_EQ(sese::EncodingConverter::toWstring(str), wstr);
}

TEST(TestEncoding, WString2String) {
std::string str = "你好";
std::wstring wstr = L"你好";

// ASSERT_EQ(sese::EncodingConverter::toString(wstr), str);
std::wstring_convert<std::codecvt_utf8<wchar_t>> convert;
EXPECT_EQ(convert.to_bytes(wstr), str);
// std::wstring_convert<std::codecvt_utf8<wchar_t>> convert;
// EXPECT_EQ(convert.to_bytes(wstr), str);
EXPECT_EQ(sese::EncodingConverter::toString(wstr), str);
}

0 comments on commit 30dd316

Please sign in to comment.