string_conversions.h
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #ifndef _STRING_CONVERSIONS_H
00026 #define _STRING_CONVERSIONS_H 1
00027
00028 #pragma GCC system_header
00029
00030 #include <ext/numeric_traits.h>
00031 #include <bits/functexcept.h>
00032 #include <cstddef>
00033 #include <cstdlib>
00034 #include <cwchar>
00035 #include <cstdio>
00036 #include <cerrno>
00037
00038 _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
00039
00040
00041 template<typename _TRet, typename _Ret = _TRet, typename _CharT,
00042 typename... _Base>
00043 _Ret
00044 __stoa(_TRet (*__convf) (const _CharT*, _CharT**, _Base...),
00045 const char* __name, const _CharT* __str, std::size_t* __idx,
00046 _Base... __base)
00047 {
00048 _Ret __ret;
00049
00050 _CharT* __endptr;
00051 errno = 0;
00052 const _TRet __tmp = __convf(__str, &__endptr, __base...);
00053
00054 if (__endptr == __str)
00055 std::__throw_invalid_argument(__name);
00056 else if (errno == ERANGE
00057 || (std::__are_same<_Ret, int>::__value
00058 && (__tmp < __numeric_traits<int>::__min
00059 || __tmp > __numeric_traits<int>::__max)))
00060 std::__throw_out_of_range(__name);
00061 else
00062 __ret = __tmp;
00063
00064 if (__idx)
00065 *__idx = __endptr - __str;
00066
00067 return __ret;
00068 }
00069
00070
00071 template<typename _String, typename _CharT = typename _String::value_type>
00072 _String
00073 __to_xstring(int (*__convf) (_CharT*, std::size_t, const _CharT*,
00074 __builtin_va_list), std::size_t __n,
00075 const _CharT* __fmt, ...)
00076 {
00077
00078
00079 _CharT* __s = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
00080 * __n));
00081
00082 __builtin_va_list __args;
00083 __builtin_va_start(__args, __fmt);
00084
00085 const int __len = __convf(__s, __n, __fmt, __args);
00086
00087 __builtin_va_end(__args);
00088
00089 return _String(__s, __s + __len);
00090 }
00091
00092 _GLIBCXX_END_NAMESPACE
00093
00094 #endif // _STRING_CONVERSIONS_H