This source file includes following definitions.
- StringToWString
- WStringToString
- WStringToTString
- TStringToWString
- StringToTString
- noexcept
- TStringToString
- noexcept
1 #ifndef libjmmcg_core_unicode_conversions_hpp
2 #define libjmmcg_core_unicode_conversions_hpp
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 #include "ttypes.hpp"
28
29 #include <locale>
30 #include <memory>
31
32 namespace jmmcg {
33
34 struct widen : public std::unary_function<char,wchar_t> {
35 widen()
36 : ct(std::use_facet<std::ctype<wchar_t> >(std::locale())) {
37 }
38 wchar_t operator()(char c) const {
39 return ct.widen(c);
40 }
41
42 private:
43 std::ctype<wchar_t> const & ct;
44 };
45
46 struct narrow : public std::unary_function<wchar_t,char> {
47 narrow()
48 : ct(std::use_facet<std::ctype<wchar_t> >(std::locale())) {
49 }
50 char operator()(wchar_t c) const {
51 return ct.narrow(c,' ');
52 }
53
54 private:
55 std::ctype<wchar_t> const & ct;
56 };
57
58 inline std::wstring __fastcall
59 StringToWString(const std::string &in) {
60 std::wstring ret(in.size(), std::wstring::value_type(0));
61
62
63 #if defined(_MSC_VER) && (_MSC_VER < 1300)
64 std::ctype<wchar_t>().widen(in.data(),in.data()+in.size(),&(*ret.begin()));
65 #else
66 std::transform(in.begin(),in.end(),ret.begin(),widen());
67 #endif
68 return ret;
69 }
70
71 inline std::string __fastcall
72 WStringToString(
73 const std::wstring &in
74 #if defined(_MSC_VER) && (_MSC_VER < 1300)
75 ,const char dflt='@'
76 #endif
77 ) {
78 std::string ret(in.size(), std::string::value_type(0));
79
80
81 #if defined(_MSC_VER) && (_MSC_VER < 1300)
82 std::ctype<wchar_t>().narrow(in.data(),in.data()+in.size(),dflt,&(*ret.begin()));
83 #else
84 std::transform(in.begin(),in.end(),ret.begin(),narrow());
85 #endif
86 return ret;
87 }
88
89 inline tstring __fastcall
90 WStringToTString(const std::wstring &in)
91 #ifdef _UNICODE
92 noexcept(true) {
93 return in;
94 #else
95 {
96 return WStringToString(in);
97 #endif
98 }
99
100 inline std::wstring __fastcall
101 TStringToWString(const tstring &in)
102 #ifdef _UNICODE
103 noexcept(true) {
104 return in;
105 #else
106 {
107 return StringToWString(in);
108 #endif
109 }
110
111 inline tstring __fastcall
112 StringToTString(const std::string &in)
113 #ifdef _UNICODE
114 {
115 return StringToWString(in);
116 #else
117 noexcept(true) {
118 return in;
119 #endif
120 }
121
122 inline std::string __fastcall
123 TStringToString(const tstring &in)
124 #ifdef _UNICODE
125 {
126 return WStringToString(in);
127 #else
128 noexcept(true) {
129 return in;
130 #endif
131 }
132
133 }
134
135 #ifdef _MSC_VER
136
137 #pragma warning(default:4284)
138
139 #endif
140
141 #endif