Skip to content
This repository has been archived by the owner on May 13, 2024. It is now read-only.

Commit

Permalink
std::wstring_convert
Browse files Browse the repository at this point in the history
  • Loading branch information
grorp committed Aug 30, 2023
1 parent 2fd0d98 commit 32e0e26
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 29 deletions.
14 changes: 0 additions & 14 deletions examples/AutomatedTest/main.cpp
Original file line number Diff line number Diff line change
@@ -1,21 +1,7 @@
#include <cassert>
#include <iostream>
#include <string>
#include <irrlicht.h>
#include "exampleHelper.h"

// usually provided by Minetest
std::wstring utf8_to_wide(const std::string &input) {
assert(false);
return L"false";
}

// usually provided by Minetest
std::string wide_to_utf8(const std::wstring &input) {
assert(false);
return "false";
}

using namespace irr;

static IrrlichtDevice *device = nullptr;
Expand Down
33 changes: 18 additions & 15 deletions include/irrString.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@
#include <cstdio>
#include <cstring>
#include <cwchar>

extern std::wstring utf8_to_wide(const std::string &input);
extern std::string wide_to_utf8(const std::wstring &input);
#include <locale>
#include <codecvt>

namespace irr
{
Expand Down Expand Up @@ -866,10 +865,10 @@ class string
return ret.size()-oldSize;
}

friend size_t multibyteToWString(irr::core::stringw &destination, const irr::core::stringc &source);
friend size_t multibyteToWString(irr::core::stringw &destination, const char *source);
friend size_t wStringToMultibyte(irr::core::stringc &destination, const irr::core::stringw &source);
friend size_t wStringToMultibyte(irr::core::stringc &destination, const wchar_t *source);
friend size_t multibyteToWString(stringw &destination, const stringc &source);
friend size_t multibyteToWString(stringw &destination, const char *source);
friend size_t wStringToMultibyte(stringc &destination, const stringw &source);
friend size_t wStringToMultibyte(stringc &destination, const wchar_t *source);

private:

Expand Down Expand Up @@ -922,27 +921,31 @@ class string
};


inline size_t multibyteToWString(irr::core::stringw &destination, const irr::core::stringc &source)
inline size_t multibyteToWString(stringw &destination, const stringc &source)
{
destination = utf8_to_wide(source.str);
std::wstring_convert<std::codecvt_utf8<wchar_t>> conv;
destination = conv.from_bytes(source.str);
return destination.size();
}

inline size_t multibyteToWString(irr::core::stringw &destination, const char *source)
inline size_t multibyteToWString(stringw &destination, const char *source)
{
destination = utf8_to_wide(source);
std::wstring_convert<std::codecvt_utf8<wchar_t>> conv;
destination = conv.from_bytes(source);
return destination.size();
}

inline size_t wStringToMultibyte(irr::core::stringc &destination, const irr::core::stringw &source)
inline size_t wStringToMultibyte(stringc &destination, const stringw &source)
{
destination = wide_to_utf8(source.str);
std::wstring_convert<std::codecvt_utf8<wchar_t>> conv;
destination = conv.to_bytes(source.str);
return destination.size();
}

inline size_t wStringToMultibyte(irr::core::stringc &destination, const wchar_t *source)
inline size_t wStringToMultibyte(stringc &destination, const wchar_t *source)
{
destination = wide_to_utf8(source);
std::wstring_convert<std::codecvt_utf8<wchar_t>> conv;
destination = conv.to_bytes(source);
return destination.size();
}

Expand Down

0 comments on commit 32e0e26

Please sign in to comment.