diff options
author | DS <vorunbekannt75@web.de> | 2021-08-30 21:44:56 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-08-30 21:44:56 +0200 |
commit | 9c4b6f25ab73c413730f9c8d35f34ef7db7e2b33 (patch) | |
tree | 837a56f1a203c7e161b2de91b5d1d587d10d2519 /include/irrString.h | |
parent | 75b4c05741e4cb42a0705341b2b696aa0e9a73c0 (diff) | |
download | irrlicht-9c4b6f25ab73c413730f9c8d35f34ef7db7e2b33.tar.xz |
Fix X11 selections (#55)
This fixes all the issues with the X11 selection in addition to switching the clipboard to always be UTF-8.
Diffstat (limited to 'include/irrString.h')
-rw-r--r-- | include/irrString.h | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/include/irrString.h b/include/irrString.h index 0b57864..a871c24 100644 --- a/include/irrString.h +++ b/include/irrString.h @@ -11,6 +11,7 @@ #include <stdio.h>
#include <string.h>
#include <stdlib.h>
+#include <wchar.h>
namespace irr
{
@@ -36,6 +37,7 @@ outside the string class for explicit use. template <typename T, typename TAlloc = irrAllocator<T> >
class string;
static size_t multibyteToWString(string<wchar_t>& destination, const char* source, u32 sourceSize);
+static size_t wStringToMultibyte(string<c8>& destination, const wchar_t* source, u32 sourceSize);
inline s32 isdigit(s32 c);
enum eLocaleID
@@ -1424,6 +1426,7 @@ public: }
friend size_t multibyteToWString(string<wchar_t>& destination, const char* source, u32 sourceSize);
+ friend size_t wStringToMultibyte(string<c8>& destination, const wchar_t* source, u32 sourceSize);
private:
@@ -1517,6 +1520,53 @@ static size_t multibyteToWString(string<wchar_t>& destination, const char* sourc }
}
+//! Same as multibyteToWString, but the other way around
+static inline size_t wStringToMultibyte(string<c8>& destination, const core::string<wchar_t>& source)
+{
+ return wStringToMultibyte(destination, source.c_str(), (u32)source.size());
+}
+
+//! Same as multibyteToWString, but the other way around
+static inline size_t wStringToMultibyte(string<c8>& destination, const wchar_t* source)
+{
+ const u32 s = source ? (u32)wcslen(source) : 0;
+ return wStringToMultibyte(destination, source, s);
+}
+
+//! Same as multibyteToWString, but the other way around
+static size_t wStringToMultibyte(string<c8>& destination, const wchar_t* source, u32 sourceSize)
+{
+ if ( sourceSize )
+ {
+ destination.reserve(sourceSize+1);
+#if defined(_MSC_VER)
+#pragma warning(push)
+#pragma warning(disable: 4996) // 'wcstombs': This function or variable may be unsafe. Consider using wcstombs_s instead.
+#endif
+ const size_t written = wcstombs(destination.array, source, (size_t)sourceSize);
+#if defined(_MSC_VER)
+#pragma warning(pop)
+#endif
+ if ( written != (size_t)-1 )
+ {
+ destination.used = (u32)written+1;
+ destination.array[destination.used-1] = 0;
+ }
+ else
+ {
+ // Likely character which got converted until the invalid character was encountered are in destination now.
+ // And it seems even 0-terminated, but I found no documentation anywhere that this (the 0-termination) is guaranteed :-(
+ destination.clear();
+ }
+ return written;
+ }
+ else
+ {
+ destination.clear();
+ return 0;
+ }
+}
+
} // end namespace core
} // end namespace irr
|