iutest  1.17.99.14
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 // IWYU pragma: begin_exports
21 #include "iutest_port.hpp"
22 #include "iutest_constant.hpp"
23 // IWYU pragma: end_exports
24 
25 #if IUTEST_HAS_CXX_HDR_CODECVT
26 # include <locale>
27 # include <codecvt>
28 #endif
29 
30 IUTEST_PRAGMA_CRT_SECURE_WARN_DISABLE_BEGIN()
31 
32 namespace iutest {
33 namespace detail
34 {
35 
36 //======================================================================
37 // declare
38 
45 inline ::std::string AnyStringToMultiByteString(const char* str, int num = -1)
46 {
47  return num < 0 ? str : ::std::string(str, static_cast<size_t>(num));
48 }
49 
56 inline ::std::string AnyStringToMultiByteString(const signed char* str, int num = -1)
57 {
58  return AnyStringToMultiByteString(reinterpret_cast<const char*>(str), num);
59 }
60 
67 ::std::string AnyStringToUTF8(const wchar_t* str, int num=-1);
68 
75 ::std::string AnyStringToMultiByteString(const wchar_t* str, int num=-1);
76 
77 #if IUTEST_HAS_CHAR16_T
78 
85 ::std::string AnyStringToUTF8(const char16_t* str, int num=-1);
86 
93 ::std::string AnyStringToMultiByteString(const char16_t* str, int num=-1);
94 
95 #endif
96 
97 #if IUTEST_HAS_CHAR32_T
98 
105 ::std::string AnyStringToUTF8(const char32_t* str, int num = -1);
106 
113 ::std::string AnyStringToMultiByteString(const char32_t* str, int num = -1);
114 
115 #endif
116 
122 ::std::wstring MultiByteStringToWideString(const char* str);
123 
130 ::std::string MultiByteStringToUTF8(const char* str, int num=-1);
131 
137 template<typename CharType>
138 ::std::string ShowAnyCString(const CharType* any_c_str)
139 {
140  if( any_c_str == NULL )
141  {
142  return kStrings::Null;
143  }
144  return AnyStringToMultiByteString(any_c_str);
145 }
146 
147 #if IUTEST_HAS_CXX_HDR_CODECVT
148 
149 template<typename In, typename Out, typename State>
150 struct codecvt : public ::std::codecvt<In, Out, State> { ~codecvt() {} };
151 
157 #if defined(_MSC_VER)
158 template<typename In, typename Out, typename State>
159 ::std::basic_string<Out> CodeConvert(const In* str, ::std::locale loc = ::std::locale(""))
160 {
161  ::std::wstring_convert< codecvt<In, Out, State>, In> conv(&::std::use_facet< codecvt<In, Out, State> >(loc));
162  return conv.to_bytes(str);
163 }
164 #else
165 template<typename In, typename Out, typename State>
166 ::std::basic_string<Out> CodeConvert(const In* str)
167 {
168  ::std::wstring_convert< codecvt<In, Out, State>, In> conv;
169  //::std::wstring_convert< codecvt<In, Out, State>, In> conv(&::std::use_facet< ::std::codecvt<In, Out, State> >(loc) );
170  return conv.to_bytes(str);
171 }
172 #endif
173 
174 #endif
175 
176 //======================================================================
177 // struct
178 namespace mbs_ptr_impl
179 {
180 
181 template<typename T>
182 struct to_mbs_ptr
183 {
184  const char* ptr(const char* arg) { return arg; }
185 };
186 struct wcs_to_mbs_ptr
187 {
188  ::std::string m_arg;
189  const char* ptr(const wchar_t* arg)
190  {
191  m_arg = ShowAnyCString(arg);
192  return m_arg.c_str();
193  }
194 };
195 template<>
196 struct to_mbs_ptr<wchar_t> : public wcs_to_mbs_ptr {};
197 template<>
198 struct to_mbs_ptr<const wchar_t> : public wcs_to_mbs_ptr {};
199 
200 } // end of namespace mbs_ptr_impl
201 
205 template<typename CharType>
206 struct mbs_ptr : public mbs_ptr_impl::to_mbs_ptr<CharType>
207 {
208 };
209 
210 #if IUTEST_HAS_LIB
211 
212 #if IUTEST_HAS_EXTERN_TEMPLATE
213 
214 IUTEST_PRAGMA_EXTERN_TEMPLATE_WARN_DISABLE_BEGIN()
215 
216 extern template struct mbs_ptr<char>;
217 extern template struct mbs_ptr<wchar_t>;
218 
219 IUTEST_PRAGMA_EXTERN_TEMPLATE_WARN_DISABLE_END()
220 
221 #else
222 
223 template struct mbs_ptr<char>;
224 template struct mbs_ptr<wchar_t>;
225 
226 #endif
227 #endif
228 
229 IUTEST_PRAGMA_CRT_SECURE_WARN_DISABLE_END()
230 
231 } // end of namespace detail
232 } // end of namespace iutest
233 
234 #if !IUTEST_HAS_LIB
235 # include "../impl/iutest_charcode.ipp" // IWYU pragma: export
236 #endif
237 
238 #endif // INCG_IRIS_IUTEST_CHARCODE_HPP_D444FB3E_3AFA_46D0_AD69_33FAAF5615E3_
iris unit test 定数 定義 ファイル
iutest root namespace
Definition: iutest_charcode.hpp:33