iutest  1.17.1.0
iutest_charcode.hpp
[詳解]
1 //======================================================================
2 //-----------------------------------------------------------------------
13 //-----------------------------------------------------------------------
14 //======================================================================
15 #ifndef INCG_IRIS_IUTEST_CHARCODE_HPP_D444FB3E_3AFA_46D0_AD69_33FAAF5615E3_
16 #define INCG_IRIS_IUTEST_CHARCODE_HPP_D444FB3E_3AFA_46D0_AD69_33FAAF5615E3_
17 
18 //======================================================================
19 // include
20 #include "iutest_port.hpp"
21 #include "iutest_constant.hpp"
22 
23 #if IUTEST_HAS_CXX_HDR_CODECVT
24 # include <locale>
25 # include <codecvt>
26 #endif
27 
28 IUTEST_PRAGMA_CRT_SECURE_WARN_DISABLE_BEGIN()
29 
30 namespace iutest {
31 namespace detail
32 {
33 
34 //======================================================================
35 // declare
36 
43 inline ::std::string AnyStringToMultiByteString(const char* str, int num = -1)
44 {
45  return num < 0 ? str : ::std::string(str, num);
46 }
47 
54 ::std::string AnyStringToUTF8(const wchar_t* str, int num=-1);
55 
62 ::std::string AnyStringToMultiByteString(const wchar_t* str, int num=-1);
63 
64 #if IUTEST_HAS_CHAR16_T
65 
72 ::std::string AnyStringToUTF8(const char16_t* str, int num=-1);
73 
80 ::std::string AnyStringToMultiByteString(const char16_t* str, int num=-1);
81 
82 #endif
83 
84 #if IUTEST_HAS_CHAR32_T
85 
92 ::std::string AnyStringToUTF8(const char32_t* str, int num = -1);
93 
100 ::std::string AnyStringToMultiByteString(const char32_t* str, int num = -1);
101 
102 #endif
103 
109 ::std::wstring MultiByteStringToWideString(const char* str);
110 
117 ::std::string MultiByteStringToUTF8(const char* src, int num=-1);
118 
124 template<typename CharType>
125 ::std::string ShowAnyCString(const CharType* any_c_str)
126 {
127  if( any_c_str == NULL )
128  {
129  return kStrings::Null;
130  }
131  return AnyStringToMultiByteString(any_c_str);
132 }
133 
134 #if IUTEST_HAS_CXX_HDR_CODECVT
135 
136 template<typename In, typename Out, typename State>
137 struct codecvt : public ::std::codecvt<In, Out, State> { ~codecvt() {} };
138 
144 #if defined(_MSC_VER)
145 template<typename In, typename Out, typename State>
146 ::std::basic_string<Out> CodeConvert(const In* str, ::std::locale loc = ::std::locale(""))
147 {
148  ::std::wstring_convert< codecvt<In, Out, State>, In> conv(&::std::use_facet< codecvt<In, Out, State> >(loc));
149  return conv.to_bytes(str);
150 }
151 #else
152 template<typename In, typename Out, typename State>
153 ::std::basic_string<Out> CodeConvert(const In* str)
154 {
155  ::std::wstring_convert< codecvt<In, Out, State>, In> conv;
156  //::std::wstring_convert< codecvt<In, Out, State>, In> conv(&::std::use_facet< ::std::codecvt<In, Out, State> >(loc) );
157  return conv.to_bytes(str);
158 }
159 #endif
160 
161 #endif
162 
163 //======================================================================
164 // struct
165 namespace mbs_ptr_impl
166 {
167 
168 template<typename T>
169 struct to_mbs_ptr
170 {
171  const char* ptr(const char* arg) { return arg; }
172 };
173 struct wcs_to_mbs_ptr
174 {
175  ::std::string m_arg;
176  const char* ptr(const wchar_t* arg)
177  {
178  m_arg = ShowAnyCString(arg);
179  return m_arg.c_str();
180  }
181 };
182 template<>
183 struct to_mbs_ptr<wchar_t> : public wcs_to_mbs_ptr {};
184 template<>
185 struct to_mbs_ptr<const wchar_t> : public wcs_to_mbs_ptr {};
186 
187 } // end of namespace mbs_ptr_impl
188 
192 template<typename CharType>
193 struct mbs_ptr : public mbs_ptr_impl::to_mbs_ptr<CharType>
194 {
195 };
196 
197 #if IUTEST_HAS_LIB
198 
199 #if IUTEST_HAS_EXTERN_TEMPLATE
200 
201 IUTEST_PRAGMA_EXTERN_TEMPLATE_WARN_DISABLE_BEGIN()
202 
203 extern template struct mbs_ptr<char>;
204 extern template struct mbs_ptr<wchar_t>;
205 
206 IUTEST_PRAGMA_EXTERN_TEMPLATE_WARN_DISABLE_END()
207 
208 #else
209 
210 template struct mbs_ptr<char>;
211 template struct mbs_ptr<wchar_t>;
212 
213 #endif
214 #endif
215 
216 IUTEST_PRAGMA_CRT_SECURE_WARN_DISABLE_END()
217 
218 } // end of namespace detail
219 } // end of namespace iutest
220 
221 #if !IUTEST_HAS_LIB
222 # include "../impl/iutest_charcode.ipp"
223 #endif
224 
225 #endif // INCG_IRIS_IUTEST_CHARCODE_HPP_D444FB3E_3AFA_46D0_AD69_33FAAF5615E3_
iutest_config.hpp
iris unit test config
iutest
iutest root namespace
Definition: iutest_charcode.hpp:31
iutest_port.hpp
portable
iutest_constant.hpp
iris unit test 定数 定義 ファイル