Browse code

Lots of changes in regards to strings, as follows:

* Removed my custom String class, as well as removed the really old
Unicode ConvertUTF, the UTF Converter, and the UTF Encode/Decode
functions.
* Replaced the above with using standard C++ std::wstring_convert and
std::codecvt_utf8.
* Added headers adapted from llvm's libc++ project for allowing the
above C++ classes to exist with GCC and Clang when libc++ isn't being
used.
* Used std::string over std::wstring whenever possible.
* Added header adapted from llvm's libc++ project to create special
versions of the ifstream and ofstream classes that would utilize _wfopen
on non-MSVC Windows compilers, so those compilers could access files on
Windows that use characters outside the current codepage.
* Removed the -static-libgcc and -static-libstdc++ flags from the
Makefile for non-MSVC builds. The generated DLLs will require extra DLLs
from either Cygwin or MinGW (whichever is used to compile), I've only
tested with MinGW and for some reason they crash Winamp on exit, I have
no idea why.

Naram Qashat authored on 2014/09/24 13:58:55
Showing 1 changed files
1 1
deleted file mode 100644
... ...
@@ -1,69 +0,0 @@
1
-// original code is from here: http://www.codeproject.com/Articles/17573/Convert-Between-std-string-and-std-wstring-UTF-8-a
2
-// License: The Code Project Open License (CPOL) http://www.codeproject.com/info/cpol10.aspx
3
-
4
-#include <string>
5
-#include <vector>
6
-#include <stdexcept>
7
-#include "ConvertUTF.h"
8
-
9
-namespace UtfConverter
10
-{
11
-	std::wstring FromUtf8(const std::string &utf8string)
12
-	{
13
-		auto widesize = utf8string.length();
14
-		auto result = std::vector<wchar_t>(widesize + 1, L'\0');
15
-		auto orig = std::vector<char>(widesize + 1, '\0');
16
-		std::copy(utf8string.begin(), utf8string.end(), orig.begin());
17
-		auto sourcestart = reinterpret_cast<const UTF8 *>(&orig[0]), sourceend = sourcestart + widesize;
18
-		ConversionResult res;
19
-		if (sizeof(wchar_t) == 2)
20
-		{
21
-			auto targetstart = reinterpret_cast<UTF16 *>(&result[0]), targetend = targetstart + widesize;
22
-			res = ConvertUTF8toUTF16(&sourcestart, sourceend, &targetstart, targetend, strictConversion);
23
-			*targetstart = 0;
24
-			unsigned end = targetstart - reinterpret_cast<UTF16 *>(&result[0]);
25
-			result.erase(result.begin() + end, result.end());
26
-		}
27
-		else if (sizeof(wchar_t) == 4)
28
-		{
29
-			auto targetstart = reinterpret_cast<UTF32 *>(&result[0]), targetend = targetstart + widesize;
30
-			res = ConvertUTF8toUTF32(&sourcestart, sourceend, &targetstart, targetend, strictConversion);
31
-			*targetstart = 0;
32
-			unsigned end = targetstart - reinterpret_cast<UTF32 *>(&result[0]);
33
-			result.erase(result.begin() + end, result.end());
34
-		}
35
-		else
36
-			throw std::runtime_error("UtfConverter::FromUtf8: sizeof(wchar_t) is not 2 or 4.");
37
-		if (res != conversionOK)
38
-			throw std::runtime_error("UtfConverter::FromUtf8: Conversion failed.");
39
-		return std::wstring(result.begin(), result.end());
40
-	}
41
-
42
-	std::string ToUtf8(const std::wstring &widestring)
43
-	{
44
-		auto widesize = widestring.length(), utf8size = (sizeof(wchar_t) == 2 ? 3 : 4) * widesize + 1;
45
-		auto result = std::vector<char>(utf8size, '\0');
46
-		auto orig = std::vector<wchar_t>(widesize + 1, L'\0');
47
-		std::copy(widestring.begin(), widestring.end(), orig.begin());
48
-		auto targetstart = reinterpret_cast<UTF8 *>(&result[0]), targetend = targetstart + utf8size;
49
-		ConversionResult res;
50
-		if (sizeof(wchar_t) == 2)
51
-		{
52
-			auto sourcestart = reinterpret_cast<const UTF16 *>(&orig[0]), sourceend = sourcestart + widesize;
53
-			res = ConvertUTF16toUTF8(&sourcestart, sourceend, &targetstart, targetend, strictConversion);
54
-		}
55
-		else if (sizeof(wchar_t) == 4)
56
-		{
57
-			auto sourcestart = reinterpret_cast<const UTF32 *>(&orig[0]), sourceend = sourcestart + widesize;
58
-			res = ConvertUTF32toUTF8(&sourcestart, sourceend, &targetstart, targetend, strictConversion);
59
-		}
60
-		else
61
-			throw std::runtime_error("UtfConverter::ToUtf8: sizeof(wchar_t) is not 2 or 4.");
62
-		if (res != conversionOK)
63
-			throw std::runtime_error("UtfConverter::ToUtf8: Conversion failed.");
64
-		*targetstart = 0;
65
-		auto end = targetstart - reinterpret_cast<UTF8 *>(&result[0]);
66
-		result.erase(result.begin() + end, result.end());
67
-		return std::string(result.begin(), result.end());
68
-	}
69
-}
Browse code

Minor cleanups in the framework.

Naram Qashat authored on 2014/09/08 15:06:52
Showing 1 changed files
... ...
@@ -14,11 +14,11 @@ namespace UtfConverter
14 14
 		auto result = std::vector<wchar_t>(widesize + 1, L'\0');
15 15
 		auto orig = std::vector<char>(widesize + 1, '\0');
16 16
 		std::copy(utf8string.begin(), utf8string.end(), orig.begin());
17
-		auto *sourcestart = reinterpret_cast<const UTF8 *>(&orig[0]), *sourceend = sourcestart + widesize;
17
+		auto sourcestart = reinterpret_cast<const UTF8 *>(&orig[0]), sourceend = sourcestart + widesize;
18 18
 		ConversionResult res;
19 19
 		if (sizeof(wchar_t) == 2)
20 20
 		{
21
-			auto *targetstart = reinterpret_cast<UTF16 *>(&result[0]), *targetend = targetstart + widesize;
21
+			auto targetstart = reinterpret_cast<UTF16 *>(&result[0]), targetend = targetstart + widesize;
22 22
 			res = ConvertUTF8toUTF16(&sourcestart, sourceend, &targetstart, targetend, strictConversion);
23 23
 			*targetstart = 0;
24 24
 			unsigned end = targetstart - reinterpret_cast<UTF16 *>(&result[0]);
... ...
@@ -26,7 +26,7 @@ namespace UtfConverter
26 26
 		}
27 27
 		else if (sizeof(wchar_t) == 4)
28 28
 		{
29
-			auto *targetstart = reinterpret_cast<UTF32 *>(&result[0]), *targetend = targetstart + widesize;
29
+			auto targetstart = reinterpret_cast<UTF32 *>(&result[0]), targetend = targetstart + widesize;
30 30
 			res = ConvertUTF8toUTF32(&sourcestart, sourceend, &targetstart, targetend, strictConversion);
31 31
 			*targetstart = 0;
32 32
 			unsigned end = targetstart - reinterpret_cast<UTF32 *>(&result[0]);
... ...
@@ -45,16 +45,16 @@ namespace UtfConverter
45 45
 		auto result = std::vector<char>(utf8size, '\0');
46 46
 		auto orig = std::vector<wchar_t>(widesize + 1, L'\0');
47 47
 		std::copy(widestring.begin(), widestring.end(), orig.begin());
48
-		auto *targetstart = reinterpret_cast<UTF8 *>(&result[0]), *targetend = targetstart + utf8size;
48
+		auto targetstart = reinterpret_cast<UTF8 *>(&result[0]), targetend = targetstart + utf8size;
49 49
 		ConversionResult res;
50 50
 		if (sizeof(wchar_t) == 2)
51 51
 		{
52
-			auto *sourcestart = reinterpret_cast<const UTF16 *>(&orig[0]), *sourceend = sourcestart + widesize;
52
+			auto sourcestart = reinterpret_cast<const UTF16 *>(&orig[0]), sourceend = sourcestart + widesize;
53 53
 			res = ConvertUTF16toUTF8(&sourcestart, sourceend, &targetstart, targetend, strictConversion);
54 54
 		}
55 55
 		else if (sizeof(wchar_t) == 4)
56 56
 		{
57
-			auto *sourcestart = reinterpret_cast<const UTF32 *>(&orig[0]), *sourceend = sourcestart + widesize;
57
+			auto sourcestart = reinterpret_cast<const UTF32 *>(&orig[0]), sourceend = sourcestart + widesize;
58 58
 			res = ConvertUTF32toUTF8(&sourcestart, sourceend, &targetstart, targetend, strictConversion);
59 59
 		}
60 60
 		else
Browse code

Import actual code.

Naram Qashat authored on 2013/03/26 02:41:19
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,69 @@
1
+// original code is from here: http://www.codeproject.com/Articles/17573/Convert-Between-std-string-and-std-wstring-UTF-8-a
2
+// License: The Code Project Open License (CPOL) http://www.codeproject.com/info/cpol10.aspx
3
+
4
+#include <string>
5
+#include <vector>
6
+#include <stdexcept>
7
+#include "ConvertUTF.h"
8
+
9
+namespace UtfConverter
10
+{
11
+	std::wstring FromUtf8(const std::string &utf8string)
12
+	{
13
+		auto widesize = utf8string.length();
14
+		auto result = std::vector<wchar_t>(widesize + 1, L'\0');
15
+		auto orig = std::vector<char>(widesize + 1, '\0');
16
+		std::copy(utf8string.begin(), utf8string.end(), orig.begin());
17
+		auto *sourcestart = reinterpret_cast<const UTF8 *>(&orig[0]), *sourceend = sourcestart + widesize;
18
+		ConversionResult res;
19
+		if (sizeof(wchar_t) == 2)
20
+		{
21
+			auto *targetstart = reinterpret_cast<UTF16 *>(&result[0]), *targetend = targetstart + widesize;
22
+			res = ConvertUTF8toUTF16(&sourcestart, sourceend, &targetstart, targetend, strictConversion);
23
+			*targetstart = 0;
24
+			unsigned end = targetstart - reinterpret_cast<UTF16 *>(&result[0]);
25
+			result.erase(result.begin() + end, result.end());
26
+		}
27
+		else if (sizeof(wchar_t) == 4)
28
+		{
29
+			auto *targetstart = reinterpret_cast<UTF32 *>(&result[0]), *targetend = targetstart + widesize;
30
+			res = ConvertUTF8toUTF32(&sourcestart, sourceend, &targetstart, targetend, strictConversion);
31
+			*targetstart = 0;
32
+			unsigned end = targetstart - reinterpret_cast<UTF32 *>(&result[0]);
33
+			result.erase(result.begin() + end, result.end());
34
+		}
35
+		else
36
+			throw std::runtime_error("UtfConverter::FromUtf8: sizeof(wchar_t) is not 2 or 4.");
37
+		if (res != conversionOK)
38
+			throw std::runtime_error("UtfConverter::FromUtf8: Conversion failed.");
39
+		return std::wstring(result.begin(), result.end());
40
+	}
41
+
42
+	std::string ToUtf8(const std::wstring &widestring)
43
+	{
44
+		auto widesize = widestring.length(), utf8size = (sizeof(wchar_t) == 2 ? 3 : 4) * widesize + 1;
45
+		auto result = std::vector<char>(utf8size, '\0');
46
+		auto orig = std::vector<wchar_t>(widesize + 1, L'\0');
47
+		std::copy(widestring.begin(), widestring.end(), orig.begin());
48
+		auto *targetstart = reinterpret_cast<UTF8 *>(&result[0]), *targetend = targetstart + utf8size;
49
+		ConversionResult res;
50
+		if (sizeof(wchar_t) == 2)
51
+		{
52
+			auto *sourcestart = reinterpret_cast<const UTF16 *>(&orig[0]), *sourceend = sourcestart + widesize;
53
+			res = ConvertUTF16toUTF8(&sourcestart, sourceend, &targetstart, targetend, strictConversion);
54
+		}
55
+		else if (sizeof(wchar_t) == 4)
56
+		{
57
+			auto *sourcestart = reinterpret_cast<const UTF32 *>(&orig[0]), *sourceend = sourcestart + widesize;
58
+			res = ConvertUTF32toUTF8(&sourcestart, sourceend, &targetstart, targetend, strictConversion);
59
+		}
60
+		else
61
+			throw std::runtime_error("UtfConverter::ToUtf8: sizeof(wchar_t) is not 2 or 4.");
62
+		if (res != conversionOK)
63
+			throw std::runtime_error("UtfConverter::ToUtf8: Conversion failed.");
64
+		*targetstart = 0;
65
+		auto end = targetstart - reinterpret_cast<UTF8 *>(&result[0]);
66
+		result.erase(result.begin() + end, result.end());
67
+		return std::string(result.begin(), result.end());
68
+	}
69
+}