YSTest  PreAlpha_b400_20130424
The YSLib Test Project
 全部  命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义  
string.h
浏览该文件的文档.
1 /*
2  Copyright by FrankHB 2009 - 2013.
3 
4  This file is part of the YSLib project, and may only be used,
5  modified, and distributed under the terms of the YSLib project
6  license, LICENSE.TXT. By continuing to use, modify, or distribute
7  this file you indicate that you have read the license and
8  understand and accept it fully.
9 */
10 
33 #ifndef YB_INC_libdefect_string_h_
34 #define YB_INC_libdefect_string_h_ 1
35 
36 #include <string>
37 
38 // See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52015 .
39 // NOTE: Fixed @ 4.8.
40 
41 #if defined(__GLIBCXX__) && __GLIBCXX__ <= 20120920 \
42  && defined(__GXX_EXPERIMENTAL_CXX0X__) \
43  && !(defined(_GLIBCXX_USE_C99) && !defined(_GLIBCXX_HAVE_BROKEN_VSWPRINTF))
44 
45 #include <ext/string_conversions.h>
46 
47 namespace std _GLIBCXX_VISIBILITY(default)
48 {
49 
50 #ifndef _GLIBCXX_USE_C99
51 
52 extern "C" long long int
53 (strtoll)(const char* __restrict, char** __restrict, int) throw();
54 extern "C" unsigned long long int
55 (strtoull)(const char* __restrict, char** __restrict, int) throw();
56 using ::strtof;
57 using ::strtold;
58 extern "C" int
59 (vsnprintf)(char* __restrict, std::size_t, const char* __restrict,
60  __gnuc_va_list) throw();
61 using ::wcstol;
62 using ::wcstoul;
63 using ::wcstoll;
64 using ::wcstoull;
65 using ::wcstof;
66 using ::wcstod;
67 using ::wcstold;
68 
69 #endif
70 
71 
72 _GLIBCXX_BEGIN_NAMESPACE_VERSION
73 
74 // 21.4 Numeric Conversions [string.conversions].
75 
76 #define YB_LIBDEFECT_STOI(_s, _n, _t, _cfname) \
77  inline _t \
78  _n(const _s& __str, size_t* __idx = 0, int __base = 10) \
79  { \
80  return __gnu_cxx::__stoa(&_cfname, #_n, __str.c_str(), __idx, __base); \
81  }
82 
83 inline int
84 stoi(const string& __str, size_t* __idx = 0, int __base = 10)
85 {
86  return __gnu_cxx::__stoa<long, int>(&std::strtol, "stoi", __str.c_str(),
87  __idx, __base);
88 }
89 YB_LIBDEFECT_STOI(string, stol, long, std::strtol)
90 YB_LIBDEFECT_STOI(string, stoul, unsigned long, std::strtoul)
91 YB_LIBDEFECT_STOI(string, stoll, long long, std::strtoll)
92 YB_LIBDEFECT_STOI(string, stoull, unsigned long long, std::strtoull)
93 #ifdef _GLIBCXX_USE_WCHAR_T
94 inline int
95 stoi(const wstring& __str, size_t* __idx = 0, int __base = 10)
96 {
97  return __gnu_cxx::__stoa<long, int>(&std::wcstol, "stoi", __str.c_str(),
98  __idx, __base);
99 }
100 YB_LIBDEFECT_STOI(wstring, stol, long, std::wcstol)
101 YB_LIBDEFECT_STOI(wstring, stoul, unsigned long, std::wcstoul)
102 YB_LIBDEFECT_STOI(wstring, stoll, long long, std::wcstoll)
103 YB_LIBDEFECT_STOI(wstring, stoull, unsigned long long, std::wcstoull)
104 #endif
105 
106 #undef YB_LIBDEFECT_STOI
107 
108 
109 #define YB_LIBDEFECT_STOF(_s, _n, _t, _cfname) \
110  inline _t \
111  _n(const _s& __str, size_t* __idx = 0) \
112  { \
113  return __gnu_cxx::__stoa(&_cfname, #_n, __str.c_str(), __idx); \
114  }
115 
116 // NOTE: strtof vs strtod.
117 YB_LIBDEFECT_STOF(string, stof, float, std::strtof)
118 YB_LIBDEFECT_STOF(string, stod, double, std::strtod)
119 YB_LIBDEFECT_STOF(string, stold, long double, std::strtold)
120 #ifdef _GLIBCXX_USE_WCHAR_T
121 // NOTE: wcstof vs wcstod.
122 YB_LIBDEFECT_STOF(wstring, stof, float, std::wcstof)
123 YB_LIBDEFECT_STOF(wstring, stod, double, std::wcstod)
124 YB_LIBDEFECT_STOF(wstring, stold, long double, std::wcstold)
125 #endif
126 
127 #undef YB_LIBDEFECT_STOF
128 
129 
130 // NOTE: (v)snprintf vs sprintf.
131 #define YB_LIBDEFECT_TOSTRI(_s, _t, _fmt, _cfname) \
132  inline _s \
133  to_##_s(_t __val) \
134  { \
135  return __gnu_cxx::__to_xstring<_s>(&_cfname, \
136  sizeof(_t) * 4, _fmt, __val); \
137  }
138 
139 // DR 1261.
140 YB_LIBDEFECT_TOSTRI(string, int, "%d", std::vsnprintf)
141 YB_LIBDEFECT_TOSTRI(string, unsigned, "%u", std::vsnprintf)
142 YB_LIBDEFECT_TOSTRI(string, long, "%ld", std::vsnprintf)
143 YB_LIBDEFECT_TOSTRI(string, unsigned long, "%lu", std::vsnprintf)
144 YB_LIBDEFECT_TOSTRI(string, long long, "%lld", std::vsnprintf)
145 YB_LIBDEFECT_TOSTRI(string, unsigned long long, "%llu", std::vsnprintf)
146 #if !defined(_GLIBCXX_HAVE_BROKEN_VSWPRINTF) && defined(_GLIBCXX_USE_WCHAR_T)
147 YB_LIBDEFECT_TOSTRI(wstring, int, L"%d", std::vswprintf)
148 YB_LIBDEFECT_TOSTRI(wstring, unsigned, L"%u", std::vswprintf)
149 YB_LIBDEFECT_TOSTRI(wstring, long, L"%ld", std::vswprintf)
150 YB_LIBDEFECT_TOSTRI(wstring, unsigned long, L"%lu", std::vswprintf)
151 YB_LIBDEFECT_TOSTRI(wstring, long long, L"%lld", std::vswprintf)
152 YB_LIBDEFECT_TOSTRI(wstring, unsigned long long, L"%llu", std::vswprintf)
153 #endif
154 
155 #undef YB_LIBDEFECT_TOSTRI
156 
157 
158 #define YB_LIBDEFECT_TOSTRF(_s, _t, _fmt, _cfname) \
159  inline _s \
160  to_##_s(_t __val) \
161  { \
162  return __gnu_cxx::__to_xstring<_s>(&_cfname, \
163  __gnu_cxx::__numeric_traits<_t>::__max_exponent10 + 20, \
164  _fmt, __val); \
165  }
166 
167 YB_LIBDEFECT_TOSTRF(string, float, "%f", std::vsnprintf)
168 YB_LIBDEFECT_TOSTRF(string, double, "%f", std::vsnprintf)
169 YB_LIBDEFECT_TOSTRF(string, long double, "%f", std::vsnprintf)
170 #if !defined(_GLIBCXX_HAVE_BROKEN_VSWPRINTF) && defined(_GLIBCXX_USE_WCHAR_T)
171 YB_LIBDEFECT_TOSTRF(wstring, float, L"%f", std::vswprintf)
172 YB_LIBDEFECT_TOSTRF(wstring, double, L"%f", std::vswprintf)
173 YB_LIBDEFECT_TOSTRF(wstring, long double, L"%f", std::vswprintf)
174 #endif
175 
176 #undef YB_LIBDEFECT_TOSTRF
177 
178 _GLIBCXX_END_NAMESPACE_VERSION
179 
180 } // namespace std;
181 
182 #endif /* ... __GXX_EXPERIMENTAL_CXX0X__ ... */
183 
184 #endif
185