diff options
Diffstat (limited to 'src/osd/strconv.cpp')
-rw-r--r-- | src/osd/strconv.cpp | 128 |
1 files changed, 62 insertions, 66 deletions
diff --git a/src/osd/strconv.cpp b/src/osd/strconv.cpp index 9ea2e00bd1a..130f259d70f 100644 --- a/src/osd/strconv.cpp +++ b/src/osd/strconv.cpp @@ -5,54 +5,32 @@ // strconv.cpp - Win32 string conversion // //============================================================ -#if defined(SDLMAME_WIN32) || defined(OSD_WINDOWS) || defined(OSD_UWP) -#include <windows.h> -#endif -#include <algorithm> -#include <assert.h> -// MAMEOS headers + #include "strconv.h" -#if defined(SDLMAME_WIN32) || defined(OSD_WINDOWS) || defined(OSD_UWP) +#include "unicode.h" -namespace -{ - // class designed to provide inputs to WideCharToMultiByte() and MultiByteToWideChar() - template<typename T> - class string_source - { - public: - string_source(const T *str) : m_str(str), m_char_count(-1) - { - assert(str); - } +#include <algorithm> +#include <cassert> - string_source(const std::basic_string<T> &str) : m_str(str.c_str()), m_char_count((int)str.size() + 1) - { - } - const T *string() const { return m_str; }; // returns pointer to actual characters - int char_count() const { return m_char_count; } // returns the character count (including NUL terminater), or -1 if NUL terminated +#if defined(_WIN32) - private: - const T *m_str; - int m_char_count; - }; -}; +#include <windows.h> -namespace osd { -namespace text { + +namespace osd::text { //============================================================ // mbstring_from_wstring //============================================================ -static std::string &mbstring_from_wstring(std::string &dst, UINT code_page, const string_source<wchar_t> &src) +inline std::string &mbstring_from_wstring(std::string &dst, UINT code_page, const std::wstring_view &src) { // convert UTF-16 to the specified code page - int dst_char_count = WideCharToMultiByte(code_page, 0, src.string(), src.char_count(), nullptr, 0, nullptr, nullptr); - dst.resize(dst_char_count - 1); - WideCharToMultiByte(code_page, 0, src.string(), src.char_count(), &dst[0], dst_char_count, nullptr, nullptr); + const int dst_char_count = WideCharToMultiByte(code_page, 0, src.data(), src.length(), nullptr, 0, nullptr, nullptr); + dst.resize(dst_char_count); + WideCharToMultiByte(code_page, 0, src.data(), src.length(), dst.data(), dst_char_count, nullptr, nullptr); return dst; } @@ -62,12 +40,12 @@ static std::string &mbstring_from_wstring(std::string &dst, UINT code_page, cons // wstring_from_mbstring //============================================================ -static std::wstring &wstring_from_mbstring(std::wstring &dst, const string_source<char> &src, UINT code_page) +inline std::wstring &wstring_from_mbstring(std::wstring &dst, const std::string_view &src, UINT code_page) { // convert multibyte string (in specified code page) to UTF-16 - int dst_char_count = MultiByteToWideChar(code_page, 0, src.string(), src.char_count(), nullptr, 0); - dst.resize(dst_char_count - 1); - MultiByteToWideChar(CP_UTF8, 0, src.string(), src.char_count(), &dst[0], dst_char_count - 1); + const int dst_char_count = MultiByteToWideChar(code_page, 0, src.data(), src.length(), nullptr, 0); + dst.resize(dst_char_count); + MultiByteToWideChar(code_page, 0, src.data(), src.length(), dst.data(), dst_char_count); return dst; } @@ -77,13 +55,13 @@ static std::wstring &wstring_from_mbstring(std::wstring &dst, const string_sourc // to_astring //============================================================ -std::string &to_astring(std::string &dst, const std::string &s) +std::string &to_astring(std::string &dst, std::string_view s) { // convert MAME string (UTF-8) to UTF-16 std::wstring wstring = to_wstring(s); // convert UTF-16 to "ANSI code page" string - return mbstring_from_wstring(dst, CP_ACP, string_source<wchar_t>(wstring)); + return mbstring_from_wstring(dst, CP_ACP, wstring); } @@ -98,7 +76,7 @@ std::string &to_astring(std::string &dst, const char *s) std::wstring wstring = to_wstring(s); // convert UTF-16 to "ANSI code page" string - return mbstring_from_wstring(dst, CP_ACP, string_source<wchar_t>(wstring)); + return mbstring_from_wstring(dst, CP_ACP, wstring); } @@ -106,7 +84,7 @@ std::string &to_astring(std::string &dst, const char *s) // to_astring //============================================================ -std::string to_astring(const std::string &s) +std::string to_astring(std::string_view s) { std::string result; to_astring(result, s); @@ -130,11 +108,11 @@ std::string to_astring(const char *s) // from_astring //============================================================ -std::string &from_astring(std::string &dst, const std::string &s) +std::string &from_astring(std::string &dst, std::string_view s) { // convert "ANSI code page" string to UTF-16 std::wstring wstring; - wstring_from_mbstring(wstring, string_source<char>(s), CP_ACP); + wstring_from_mbstring(wstring, s, CP_ACP); // convert UTF-16 to MAME string (UTF-8) return from_wstring(dst, wstring); @@ -149,7 +127,7 @@ std::string &from_astring(std::string &dst, const CHAR *s) { // convert "ANSI code page" string to UTF-16 std::wstring wstring; - wstring_from_mbstring(wstring, string_source<char>(s), CP_ACP); + wstring_from_mbstring(wstring, s, CP_ACP); // convert UTF-16 to MAME string (UTF-8) return from_wstring(dst, wstring); @@ -160,7 +138,7 @@ std::string &from_astring(std::string &dst, const CHAR *s) // from_astring //============================================================ -std::string from_astring(const std::string &s) +std::string from_astring(std::string_view s) { std::string result; from_astring(result, s); @@ -184,10 +162,10 @@ std::string from_astring(const CHAR *s) // to_wstring //============================================================ -std::wstring &to_wstring(std::wstring &dst, const std::string &s) +std::wstring &to_wstring(std::wstring &dst, std::string_view s) { // convert MAME string (UTF-8) to UTF-16 - return wstring_from_mbstring(dst, string_source<char>(s), CP_UTF8); + return wstring_from_mbstring(dst, s, CP_UTF8); } @@ -198,7 +176,7 @@ std::wstring &to_wstring(std::wstring &dst, const std::string &s) std::wstring &to_wstring(std::wstring &dst, const char *s) { // convert MAME string (UTF-8) to UTF-16 - return wstring_from_mbstring(dst, string_source<char>(s), CP_UTF8); + return wstring_from_mbstring(dst, s, CP_UTF8); } @@ -206,7 +184,7 @@ std::wstring &to_wstring(std::wstring &dst, const char *s) // to_wstring //============================================================ -std::wstring to_wstring(const std::string &s) +std::wstring to_wstring(std::string_view s) { std::wstring result; to_wstring(result, s); @@ -230,10 +208,10 @@ std::wstring to_wstring(const char *s) // from_wstring //============================================================ -std::string &from_wstring(std::string &dst, const std::wstring &s) +std::string &from_wstring(std::string &dst, std::wstring_view s) { // convert UTF-16 to MAME string (UTF-8) - return mbstring_from_wstring(dst, CP_UTF8, string_source<wchar_t>(s)); + return mbstring_from_wstring(dst, CP_UTF8, s); } @@ -244,7 +222,7 @@ std::string &from_wstring(std::string &dst, const std::wstring &s) std::string &from_wstring(std::string &dst, const WCHAR *s) { // convert UTF-16 to MAME string (UTF-8) - return mbstring_from_wstring(dst, CP_UTF8, string_source<wchar_t>(s)); + return mbstring_from_wstring(dst, CP_UTF8, s); } @@ -252,7 +230,7 @@ std::string &from_wstring(std::string &dst, const WCHAR *s) // from_wstring //============================================================ -std::string from_wstring(const std::wstring &s) +std::string from_wstring(std::wstring_view s) { std::string result; from_wstring(result, s); @@ -271,8 +249,7 @@ std::string from_wstring(const WCHAR *s) return result; } -}; // namespace text -}; // namespace osd +} // namespace osd::text //============================================================ @@ -281,15 +258,22 @@ std::string from_wstring(const WCHAR *s) int osd_uchar_from_osdchar(char32_t *uchar, const char *osdchar, size_t count) { - WCHAR wch; + // handle UTF-8 internally + if (GetACP() == CP_UTF8) + return uchar_from_utf8(uchar, osdchar, count); + + // FIXME: This makes multiple bad assumptions: + // * Assumes any character can be converted to a single wchar_t. + // * Assumes characters are either one byte or maximum number of bytes. + // * Doesn't handle nominal single-byte encodings with combining characters. CPINFO cp; - if (!GetCPInfo(CP_ACP, &cp)) goto error; // The multibyte char can't be bigger than the max character size - count = std::min(count, size_t(cp.MaxCharSize)); + count = std::min(count, size_t(IsDBCSLeadByte(*osdchar) ? cp.MaxCharSize : 1)); + WCHAR wch; if (MultiByteToWideChar(CP_ACP, 0, osdchar, static_cast<DWORD>(count), &wch, 1) == 0) goto error; @@ -302,23 +286,35 @@ error: } -#else -#include "unicode.h" +#else // _WIN32 + //============================================================ // osd_uchar_from_osdchar //============================================================ int osd_uchar_from_osdchar(char32_t *uchar, const char *osdchar, size_t count) { - wchar_t wch; + // TODO: should this handle count == 0? + if (!*osdchar) + { + // mbstowcs stops on encountering NUL and doesn't include it in the output count + *uchar = char32_t(0); + return 1; + } + // FIXME: mbstowcs depends on global state + wchar_t wch; count = mbstowcs(&wch, (char *)osdchar, 1); - if (count != -1) + if (count != size_t(-1)) + { *uchar = wch; + return int(count); + } else + { *uchar = 0; - - return count; + return -1; + } } -#endif +#endif // _WIN32 |