15 #ifndef INCG_IRIS_IUTEST_STRING_STREAM_HPP_F446E84B_1C7D_49C4_9A2D_5002CCE3F486_
16 #define INCG_IRIS_IUTEST_STRING_STREAM_HPP_F446E84B_1C7D_49C4_9A2D_5002CCE3F486_
23 #if IUTEST_HAS_STRINGSTREAM
25 #elif IUTEST_HAS_STRSTREAM
28 #if IUTEST_HAS_IOMANIP
31 #if IUTEST_HAS_EXCEPTIONS
43 bool StringToValue(const ::std::string& s, T& out)
45 #if IUTEST_HAS_STRINGSTREAM
46 ::std::istringstream strm(s);
52 #elif IUTEST_HAS_STD_STR_TO_VALUE
53 out =
static_cast<T
>(::std::stoull(s));
57 const char* p = s.c_str();
59 const unsigned long long v = strtoull(p, &endptr, 10);
62 #if IUTEST_HAS_EXCEPTIONS
63 throw ::std::invalid_argument(p);
70 #if IUTEST_HAS_EXCEPTIONS
71 throw ::std::out_of_range(p);
76 out =
static_cast<T
>(v);
81 inline bool StringToValue(const ::std::string& s,
float& out)
83 #if IUTEST_HAS_STD_STR_TO_VALUE
87 const char* p = s.c_str();
89 #if !defined(IUTEST_OS_WINDOWS_MINGW) || !defined(__STRICT_ANSI__)
90 const floating_point<float> v = strtof(p, &endptr);
92 const floating_point<float> v =
static_cast<float>(strtod(p, &endptr));
96 #if IUTEST_HAS_EXCEPTIONS
97 throw ::std::invalid_argument(p);
102 if((errno == ERANGE) || v.is_inf() )
104 #if IUTEST_HAS_EXCEPTIONS
105 throw ::std::out_of_range(p);
115 inline bool StringToValue(const ::std::string& s,
double& out)
117 #if IUTEST_HAS_STD_STR_TO_VALUE
118 out = ::std::stod(s);
121 const char* p = s.c_str();
123 const floating_point<double> v = strtod(p, &endptr);
126 #if IUTEST_HAS_EXCEPTIONS
127 throw ::std::invalid_argument(p);
132 if((errno == ERANGE) || v.is_inf() )
134 #if IUTEST_HAS_EXCEPTIONS
135 throw ::std::out_of_range(p);
145 #if IUTEST_HAS_LONG_DOUBLE
147 inline bool StringToValue(const ::std::string& s,
long double& out)
149 #if IUTEST_HAS_STD_STR_TO_VALUE
150 out = ::std::stold(s);
153 const char* p = s.c_str();
155 const floating_point<long double> v = strtold(p, &endptr);
158 #if IUTEST_HAS_EXCEPTIONS
159 throw ::std::invalid_argument(p);
164 if((errno == ERANGE) || v.is_inf() )
166 #if IUTEST_HAS_EXCEPTIONS
167 throw ::std::out_of_range(p);
179 #if !IUTEST_HAS_STRINGSTREAM && !IUTEST_HAS_STRSTREAM
181 IUTEST_PRAGMA_CRT_SECURE_WARN_DISABLE_BEGIN()
186 template<class _Elem, class _Traits>
187 class iu_basic_ostream
189 typedef iu_basic_ostream<_Elem, _Traits> _Myt;
192 typedef ::std::basic_string<_Elem, _Traits> string;
199 template<
typename TMP,
typename TN>
202 template<
typename TA,
typename TB>
203 static const TA constant(
const TA a,
const TB b)
209 template<
typename TMP>
210 struct impl_select<TMP, wchar_t>
212 template<
typename TA,
typename TB>
213 static const TB constant(
const TA a,
const TB b)
221 typedef impl_select<void, T> select;
223 #define IIUT_PP_XCS(txt_) xcs<_Elem>::select::constant(txt_, L##txt_)
225 IUTEST_PRAGMA_WARN_PUSH()
226 IUTEST_PRAGMA_WARN_DISABLE_FORMAT_NONLITERAL()
230 static int vastring(E* dst,
const E* fmt, va_list va);
231 static int vastring(
char* dst,
size_t len,
const char* fmt, va_list va) IUTEST_ATTRIBUTE_FORMAT_PRINTF(3, 0)
234 return vsprintf(dst, fmt, va);
236 static int vastring(
wchar_t* dst,
size_t len,
const wchar_t* fmt, va_list va)
238 #ifdef IUTEST_OS_WINDOWS_MINGW
239 return _vsnwprintf(dst, len, fmt, va);
241 return vswprintf(dst, len, fmt, va);
246 static int tostring(E* dst,
size_t len,
const E* fmt, ...)
250 const int ret = vastring(dst, len, fmt, va);
255 IUTEST_PRAGMA_WARN_POP()
258 iu_basic_ostream() {}
259 explicit iu_basic_ostream(
const char* str) : s(str) {}
260 explicit iu_basic_ostream(const ::std::string& str) : s(str) {}
263 inline _Myt& operator<< (
char v)
268 inline _Myt& operator<< (
signed char v)
270 s +=
static_cast<char>(v);
273 inline _Myt& operator<< (
unsigned char v)
275 s +=
static_cast<char>(v);
278 inline _Myt& operator<< (
const _Elem* v)
293 inline _Myt& operator<< (
bool v)
297 impl::tostring(a, 16, IIUT_PP_XCS(
"%i"), v);
300 s += (v ? IIUT_PP_XCS(
"true") : IIUT_PP_XCS(
"false"));
304 inline _Myt& operator<< (
short v)
307 impl::tostring(a, 64, IIUT_PP_XCS(
"%i"), v);
311 inline _Myt& operator<< (
unsigned short v)
314 impl::tostring(a, 64, IIUT_PP_XCS(
"%u"), v);
318 inline _Myt& operator<< (
int v)
321 impl::tostring(a, 64, IIUT_PP_XCS(
"%i"), v);
325 inline _Myt& operator<< (
unsigned int v)
328 impl::tostring(a, 64, IIUT_PP_XCS(
"%u"), v);
332 inline _Myt& operator<< (
long v)
335 impl::tostring(a, 64, IIUT_PP_XCS(
"%i"), v);
339 inline _Myt& operator<< (
unsigned long v)
342 impl::tostring(a, 64, IIUT_PP_XCS(
"%u"), v);
346 inline _Myt& operator<< (
long long int v)
349 impl::tostring(a, 64, IIUT_PP_XCS(
"%lld"), v);
353 inline _Myt& operator<< (
unsigned long long int v)
356 impl::tostring(a, 64, IIUT_PP_XCS(
"%llu"), v);
360 inline _Myt& operator<< (
float v)
363 IUTEST_PRAGMA_WARN_PUSH()
364 IUTEST_PRAGMA_WARN_DISABLE_DOUBLE_PROMOTION()
365 impl::tostring(a, 64, IIUT_PP_XCS("%f"), v);
366 IUTEST_PRAGMA_WARN_POP()
370 inline _Myt& operator<< (
double v)
373 impl::tostring(a, 64, IIUT_PP_XCS(
"%lf"), v);
377 #if IUTEST_HAS_LONG_DOUBLE
378 inline _Myt& operator<< (
long double v)
381 impl::tostring(a, 64, IIUT_PP_XCS(
"%Lf"), v);
386 #if IUTEST_HAS_FLOAT128
387 inline _Myt& operator<< (internal::Float128::Float v)
390 const double d =
static_cast<double>(v);
391 impl::tostring(a, 64, IIUT_PP_XCS(
"%L"), d);
396 inline _Myt& operator<< (
const void* v)
399 impl::tostring(a, 64, IIUT_PP_XCS(
"%t"), v);
403 inline _Myt& operator<< (const ::std::string& v)
409 const string& str()
const {
return s; }
410 void copyfmt(
const _Myt&) {}
415 IUTEST_PRAGMA_CRT_SECURE_WARN_DISABLE_END()
421 #if IUTEST_HAS_STRINGSTREAM
423 typedef ::std::stringstream stlstream;
425 #elif IUTEST_HAS_STRSTREAM
427 IUTEST_PRAGMA_MSC_WARN_PUSH()
428 IUTEST_PRAGMA_MSC_WARN_DISABLE(4250)
429 class stlstream : public ::std::strstream
434 : ::std::strstream(buf, sizeof(buf)-2, ::std::ios::out)
436 explicit stlstream(
const char* str)
437 : ::std::strstream(buf, sizeof(buf)-2, ::std::ios::out)
441 explicit stlstream(const ::std::string& str)
442 : ::std::strstream(buf, sizeof(buf)-2, ::std::ios::out)
447 ::std::string str()
const
449 return const_cast<stlstream*
>(
this)->str();
451 virtual ::std::string str()
453 *
this << ::std::ends;
454 ::std::string str = ::std::strstream::str();
459 IUTEST_PRAGMA_MSC_WARN_POP()
463 IUTEST_PRAGMA_CRT_SECURE_WARN_DISABLE_END()
467 #if IUTEST_HAS_STRINGSTREAM || IUTEST_HAS_STRSTREAM
469 typedef ::std::ostream iu_ostream;
470 typedef detail::stlstream iu_stringstream;
474 typedef detail::iu_basic_ostream<char, ::std::char_traits<char> > iu_ostream;
475 typedef detail::iu_basic_ostream<wchar_t, ::std::char_traits<wchar_t> > iu_wostream;
476 typedef iu_ostream iu_stringstream;
480 #if IUTEST_HAS_IOMANIP
481 typedef iu_ostream& (*iu_basic_iomanip)(iu_ostream&);
484 #if !defined(IUTEST_HAS_BIGGESTINT_OSTREAM)
485 # if IUTEST_HAS_STRINGSTREAM || IUTEST_HAS_STRSTREAM
486 # if (defined(_STLPORT_VERSION) && !defined(_STLP_LONG_LONG)) || (defined(_MSC_VER) && _MSC_VER < 1310)
487 # define IUTEST_HAS_BIGGESTINT_OSTREAM 0
492 #if !defined(IUTEST_HAS_BIGGESTINT_OSTREAM)
493 # define IUTEST_HAS_BIGGESTINT_OSTREAM 1
iris unit test string utilities
iutest root namespace
Definition: iutest_charcode.hpp:33