mirror of
https://github.com/vimpunk/mio.git
synced 2025-12-06 16:57:01 +08:00
Updated UTF-8 conversion functions
The GCC compiler is now supported
This commit is contained in:
parent
6d020039c7
commit
f05574baf6
@ -42,13 +42,7 @@
|
|||||||
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <vector>
|
|
||||||
#include <string>
|
|
||||||
|
|
||||||
#if __cplusplus >= 201103L && __cplusplus <= 201703L
|
#if __cplusplus >= 201103L && __cplusplus <= 201703L
|
||||||
|
|
||||||
#include <charconv>
|
|
||||||
|
|
||||||
inline std::wstring cpp2017_string2wstring(const std::string &_string)
|
inline std::wstring cpp2017_string2wstring(const std::string &_string)
|
||||||
{
|
{
|
||||||
using convert_typeX = std::codecvt_utf8<wchar_t>;
|
using convert_typeX = std::codecvt_utf8<wchar_t>;
|
||||||
@ -87,8 +81,13 @@ inline std::wstring string2wstring(const std::string& _string)
|
|||||||
std::size_t target_wstring_count = source_string_count + (found_not_ascii_count / 2);
|
std::size_t target_wstring_count = source_string_count + (found_not_ascii_count / 2);
|
||||||
|
|
||||||
wide_character_buffer.resize(target_wstring_count);
|
wide_character_buffer.resize(target_wstring_count);
|
||||||
|
|
||||||
|
#if defined(_MSC_VER)
|
||||||
std::size_t _converted_count = 0;
|
std::size_t _converted_count = 0;
|
||||||
::mbstowcs_s(&_converted_count, &wide_character_buffer[0], target_wstring_count, _string.c_str(), ((size_t)-1));
|
::mbstowcs_s(&_converted_count, &wide_character_buffer[0], target_wstring_count, _string.c_str(), ((size_t)-1));
|
||||||
|
#else
|
||||||
|
::mbstowcs(&wide_character_buffer[0], _string.c_str(), target_wstring_count);
|
||||||
|
#endif
|
||||||
|
|
||||||
std::size_t _target_wstring_size = 0;
|
std::size_t _target_wstring_size = 0;
|
||||||
for(auto begin = wide_character_buffer.begin(), end = wide_character_buffer.end(); begin != end && *begin != L'\0'; begin++)
|
for(auto begin = wide_character_buffer.begin(), end = wide_character_buffer.end(); begin != end && *begin != L'\0'; begin++)
|
||||||
@ -97,12 +96,13 @@ inline std::wstring string2wstring(const std::string& _string)
|
|||||||
}
|
}
|
||||||
std::wstring _wstring{ wide_character_buffer.data(), _target_wstring_size };
|
std::wstring _wstring{ wide_character_buffer.data(), _target_wstring_size };
|
||||||
|
|
||||||
|
#if defined(_MSC_VER)
|
||||||
if(_converted_count == 0)
|
if(_converted_count == 0)
|
||||||
{
|
{
|
||||||
throw std::runtime_error("The function string2wstring is not work !");
|
throw std::runtime_error("The function string2wstring is not work !");
|
||||||
}
|
}
|
||||||
else
|
#endif
|
||||||
{
|
|
||||||
if(found_not_ascii_count > 0)
|
if(found_not_ascii_count > 0)
|
||||||
{
|
{
|
||||||
//Need Contains character('\0') then check size
|
//Need Contains character('\0') then check size
|
||||||
@ -127,7 +127,7 @@ inline std::wstring string2wstring(const std::string& _string)
|
|||||||
return _wstring;
|
return _wstring;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
inline std::string wstring2string(const std::wstring& _wstring)
|
inline std::string wstring2string(const std::wstring& _wstring)
|
||||||
@ -150,8 +150,13 @@ inline std::string wstring2string(const std::wstring& _wstring)
|
|||||||
std::size_t target_string_count = source_wstring_count + found_not_ascii_count * 2;
|
std::size_t target_string_count = source_wstring_count + found_not_ascii_count * 2;
|
||||||
|
|
||||||
character_buffer.resize(target_string_count);
|
character_buffer.resize(target_string_count);
|
||||||
::size_t _converted_count = 0;
|
|
||||||
|
#if defined(_MSC_VER)
|
||||||
|
std::size_t _converted_count = 0;
|
||||||
::wcstombs_s(&_converted_count, &character_buffer[0], target_string_count, _wstring.c_str(), ((size_t)-1));
|
::wcstombs_s(&_converted_count, &character_buffer[0], target_string_count, _wstring.c_str(), ((size_t)-1));
|
||||||
|
#else
|
||||||
|
::wcstombs(&character_buffer[0], _wstring.c_str(), target_string_count);
|
||||||
|
#endif
|
||||||
|
|
||||||
std::size_t _target_string_size = 0;
|
std::size_t _target_string_size = 0;
|
||||||
for(auto begin = character_buffer.begin(), end = character_buffer.end(); begin != end && *begin != '\0'; begin++)
|
for(auto begin = character_buffer.begin(), end = character_buffer.end(); begin != end && *begin != '\0'; begin++)
|
||||||
@ -160,12 +165,13 @@ inline std::string wstring2string(const std::wstring& _wstring)
|
|||||||
}
|
}
|
||||||
std::string _string{ character_buffer.data(), _target_string_size };
|
std::string _string{ character_buffer.data(), _target_string_size };
|
||||||
|
|
||||||
|
#if defined(_MSC_VER)
|
||||||
if(_converted_count == 0)
|
if(_converted_count == 0)
|
||||||
{
|
{
|
||||||
throw std::runtime_error("The function wstring2string is not work !");
|
throw std::runtime_error("The function wstring2string is not work !");
|
||||||
}
|
}
|
||||||
else
|
#endif
|
||||||
{
|
|
||||||
if(found_not_ascii_count > 0)
|
if(found_not_ascii_count > 0)
|
||||||
{
|
{
|
||||||
if(((_target_string_size + 1) - source_wstring_count) != (found_not_ascii_count * 2))
|
if(((_target_string_size + 1) - source_wstring_count) != (found_not_ascii_count * 2))
|
||||||
@ -189,7 +195,6 @@ inline std::string wstring2string(const std::wstring& _wstring)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
#ifndef MIO_PAGE_HEADER
|
#ifndef MIO_PAGE_HEADER
|
||||||
#define MIO_PAGE_HEADER
|
#define MIO_PAGE_HEADER
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user