#include <stdio.h>\r
#include <string.h>\r
#include <stdlib.h>\r
+#include <wchar.h>\r
\r
namespace irr\r
{\r
template <typename T, typename TAlloc = irrAllocator<T> >\r
class string;\r
static size_t multibyteToWString(string<wchar_t>& destination, const char* source, u32 sourceSize);\r
-inline s32 isdigit(s32 c);\r
-\r
-enum eLocaleID\r
-{\r
- IRR_LOCALE_ANSI = 0,\r
- IRR_LOCALE_GERMAN = 1\r
-};\r
-\r
-static eLocaleID locale_current = IRR_LOCALE_ANSI;\r
-static inline void locale_set ( eLocaleID id )\r
-{\r
- locale_current = id;\r
-}\r
+static size_t wStringToMultibyte(string<c8>& destination, const wchar_t* source, u32 sourceSize);\r
+inline bool isdigit(s32 c);\r
\r
//! Returns a character converted to lower case\r
static inline u32 locale_lower ( u32 x )\r
{\r
- switch ( locale_current )\r
- {\r
- case IRR_LOCALE_GERMAN:\r
- case IRR_LOCALE_ANSI:\r
- break;\r
- }\r
// ansi\r
return x >= 'A' && x <= 'Z' ? x + 0x20 : x;\r
}\r
//! Returns a character converted to upper case\r
static inline u32 locale_upper ( u32 x )\r
{\r
- switch ( locale_current )\r
- {\r
- case IRR_LOCALE_GERMAN:\r
- case IRR_LOCALE_ANSI:\r
- break;\r
- }\r
-\r
// ansi\r
return x >= 'a' && x <= 'z' ? x + ( 'A' - 'a' ) : x;\r
}\r
\r
-//! Convert this utf-8-encoded string to the platform's wchar.\r
-/** The resulting string is always NULL-terminated and well-formed.\r
-\param len The size of the output buffer in bytes.\r
-*/\r
-IRRLICHT_API void utf8ToWchar(const char *in, wchar_t *out, const u64 len);\r
-\r
-//! Convert this wchar string to utf-8.\r
-/** The resulting string is always NULL-terminated and well-formed.\r
-\param len The size of the output buffer in bytes.\r
-*/\r
-IRRLICHT_API void wcharToUtf8(const wchar_t *in, char *out, const u64 len);\r
-\r
\r
template <typename T, typename TAlloc>\r
class string\r
if ((length+begin) > size())\r
length = size()-begin;\r
\r
+ // accounting for null terminator.\r
+ s32 substrAllocLength = length + 1;\r
string<T> o;\r
- o.reserve(length+1);\r
+ o.reserve(substrAllocLength);\r
\r
if ( !make_lower )\r
{\r
o.array[i] = locale_lower ( array[i+begin] );\r
}\r
\r
- o.array[length] = 0;\r
+ o.array[substrAllocLength - 1] = 0;\r
o.used = length + 1;\r
\r
return o;\r
}\r
\r
friend size_t multibyteToWString(string<wchar_t>& destination, const char* source, u32 sourceSize);\r
+ friend size_t wStringToMultibyte(string<c8>& destination, const wchar_t* source, u32 sourceSize);\r
\r
private:\r
\r
}\r
}\r
\r
+//! Same as multibyteToWString, but the other way around\r
+static inline size_t wStringToMultibyte(string<c8>& destination, const core::string<wchar_t>& source)\r
+{\r
+ return wStringToMultibyte(destination, source.c_str(), (u32)source.size());\r
+}\r
+\r
+//! Same as multibyteToWString, but the other way around\r
+static inline size_t wStringToMultibyte(string<c8>& destination, const wchar_t* source)\r
+{\r
+ const u32 s = source ? (u32)wcslen(source) : 0;\r
+ return wStringToMultibyte(destination, source, s);\r
+}\r
+\r
+//! Same as multibyteToWString, but the other way around\r
+static size_t wStringToMultibyte(string<c8>& destination, const wchar_t* source, u32 sourceSize)\r
+{\r
+ if ( sourceSize )\r
+ {\r
+ destination.reserve(sourceSize+1);\r
+#if defined(_MSC_VER)\r
+#pragma warning(push)\r
+#pragma warning(disable: 4996) // 'wcstombs': This function or variable may be unsafe. Consider using wcstombs_s instead.\r
+#endif\r
+ const size_t written = wcstombs(destination.array, source, (size_t)sourceSize);\r
+#if defined(_MSC_VER)\r
+#pragma warning(pop)\r
+#endif\r
+ if ( written != (size_t)-1 )\r
+ {\r
+ destination.used = (u32)written+1;\r
+ destination.array[destination.used-1] = 0;\r
+ }\r
+ else\r
+ {\r
+ // Likely character which got converted until the invalid character was encountered are in destination now.\r
+ // And it seems even 0-terminated, but I found no documentation anywhere that this (the 0-termination) is guaranteed :-(\r
+ destination.clear();\r
+ }\r
+ return written;\r
+ }\r
+ else\r
+ {\r
+ destination.clear();\r
+ return 0;\r
+ }\r
+}\r
+\r
\r
} // end namespace core\r
} // end namespace irr\r