3 * Silicon Graphics Computer Systems, Inc.
8 * This material is provided "as is", with absolutely no warranty expressed
9 * or implied. Any use is at your own risk.
11 * Permission to use or copy this software for any purpose is hereby granted
12 * without fee, provided the above notices are retained on all copies.
13 * Permission to modify the code and to distribute modified code is granted,
14 * provided the above notices are retained, and a notice that the code was
15 * modified is included with the above copyright notice.
19 #include "stlport_prefix.h"
25 #if defined (__DECCXX)
31 #define todigit(x) ((x)+'0')
33 #if defined (_STLP_UNIX)
36 # include <floatingpoint.h>
39 # if defined (__sun) || defined (__digital__) || defined (__sgi) || defined (_STLP_SCO_OPENSERVER) || defined (__NCR_SVR)
40 // DEC, SGI & Solaris need this
45 # if defined (__QNXNTO__) || ( defined(__GNUC__) && defined(__APPLE__) ) || defined(_STLP_USE_UCLIBC) /* 0.9.26 */ || \
47 # define USE_SPRINTF_INSTEAD
50 # if defined (_AIX) // JFA 3-Aug-2000
61 #if defined (_STLP_MSVC_LIB) || defined (__MINGW32__) || defined (__BORLANDC__) || defined (__DJGPP) || \
62 defined (_STLP_SCO_OPENSERVER) || defined (__NCR_SVR)
66 #if defined (__MRC__) || defined (__SC__) || defined (_CRAY) //*TY 02/24/2000 - added support for MPW
70 #if defined (__CYGWIN__)
75 # include <cstdlib> // for atoi
76 # include <cstdio> // for snprintf
81 #if defined (__ISCPP__)
88 # define snprintf _snprintf
93 _STLP_MOVE_TO_PRIV_NAMESPACE
95 #if defined (__MWERKS__) || defined(__BEOS__)
96 # define USE_SPRINTF_INSTEAD
102 enum { dig = _Dig<N/10>::dig + 1 };
111 #ifdef _STLP_NO_LONG_DOUBLE
112 # define MAXEDIGITS int(_Dig<DBL_MAX_10_EXP>::dig)
113 # define MAXFSIG DBL_DIG
114 # define MAXFCVT (DBL_DIG + 1)
116 # define MAXEDIGITS int(_Dig<LDBL_MAX_10_EXP>::dig)
117 # define MAXFSIG LDBL_DIG
118 # define MAXFCVT (LDBL_DIG + 1)
121 // Tests for infinity and NaN differ on different OSs. We encapsulate
122 // these differences here.
123 #if !defined (USE_SPRINTF_INSTEAD)
124 # if defined (__hpux) && defined (__GNUC__)
125 # define _STLP_USE_SIGN_HELPER
126 # elif defined (__DJGPP) || (defined (_STLP_USE_GLIBC) && ! defined (__MSL__)) || \
127 defined (__CYGWIN__) || \
128 defined (__FreeBSD__) || defined (__NetBSD__) || defined (__OpenBSD__) || \
130 static inline bool _Stl_is_nan_or_inf(double x)
131 # if defined (isfinite)
132 { return !isfinite(x); }
134 { return !finite(x); }
136 static inline bool _Stl_is_neg_nan(double x) { return isnan(x) && ( copysign(1., x) < 0 ); }
137 static inline bool _Stl_is_inf(double x) { return isinf(x); }
138 // inline bool _Stl_is_neg_inf(double x) { return isinf(x) < 0; }
139 static inline bool _Stl_is_neg_inf(double x) { return isinf(x) && x < 0; }
140 # elif (defined (__unix) || defined (__unix__)) && \
141 !defined (__APPLE__) && !defined (__DJGPP) && !defined(__osf__) && \
142 !defined (_CRAY) && !defined(__ANDROID__)
143 static inline bool _Stl_is_nan_or_inf(double x) { return IsNANorINF(x); }
144 static inline bool _Stl_is_inf(double x) { return IsNANorINF(x) && IsINF(x); }
145 static inline bool _Stl_is_neg_inf(double x) { return (IsINF(x)) && (x < 0.0); }
146 static inline bool _Stl_is_neg_nan(double x) { return IsNegNAN(x); }
147 # elif defined (_STLP_MSVC_LIB) || defined (__MINGW32__) || defined (__BORLANDC__)
148 static inline bool _Stl_is_nan_or_inf(double x) { return !_finite(x); }
149 # if !defined (__BORLANDC__)
150 static inline bool _Stl_is_inf(double x) {
151 int fclass = _fpclass(x);
152 return fclass == _FPCLASS_NINF || fclass == _FPCLASS_PINF;
154 static inline bool _Stl_is_neg_inf(double x) { return _fpclass(x) == _FPCLASS_NINF; }
156 static inline bool _Stl_is_inf(double x) { return _Stl_is_nan_or_inf(x) && !_isnan(x);}
157 static inline bool _Stl_is_neg_inf(double x) { return _Stl_is_inf(x) && x < 0 ; }
159 static inline bool _Stl_is_neg_nan(double x) { return _isnan(x) && _copysign(1., x) < 0 ; }
160 # if defined (__BORLANDC__)
161 static inline bool _Stl_is_nan_or_inf(long double x) { return !_finitel(x); }
162 static inline bool _Stl_is_inf(long double x) { return _Stl_is_nan_or_inf(x) && !_isnanl(x);}
163 static inline bool _Stl_is_neg_inf(long double x) { return _Stl_is_inf(x) && x < 0 ; }
164 static inline bool _Stl_is_neg_nan(long double x) { return _isnanl(x) && _copysignl(1.l, x) < 0 ; }
165 # elif !defined (_STLP_NO_LONG_DOUBLE)
166 // Simply there to avoid warning long double -> double implicit conversion:
167 static inline bool _Stl_is_nan_or_inf(long double x) { return _Stl_is_nan_or_inf(__STATIC_CAST(double, x)); }
168 static inline bool _Stl_is_inf(long double x) { return _Stl_is_inf(__STATIC_CAST(double, x));}
169 static inline bool _Stl_is_neg_inf(long double x) { return _Stl_is_neg_inf(__STATIC_CAST(double, x)); }
170 static inline bool _Stl_is_neg_nan(long double x) { return _Stl_is_neg_nan(__STATIC_CAST(double, x)); }
172 # elif defined (__MRC__) || defined (__SC__) || defined (__DMC__)
173 static bool _Stl_is_nan_or_inf(double x) { return isnan(x) || !isfinite(x); }
174 static bool _Stl_is_inf(double x) { return !isfinite(x); }
175 static bool _Stl_is_neg_inf(double x) { return !isfinite(x) && signbit(x); }
176 static bool _Stl_is_neg_nan(double x) { return isnan(x) && signbit(x); }
177 # elif /* defined(__FreeBSD__) || defined(__OpenBSD__) || */ (defined(__GNUC__) && defined(__APPLE__))
178 static inline bool _Stl_is_nan_or_inf(double x) { return !finite(x); }
179 static inline bool _Stl_is_inf(double x) { return _Stl_is_nan_or_inf(x) && ! isnan(x); }
180 static inline bool _Stl_is_neg_inf(double x) { return _Stl_is_inf(x) && x < 0 ; }
181 static inline bool _Stl_is_neg_nan(double x) { return isnan(x) && copysign(1., x) < 0 ; }
182 # elif defined( _AIX ) // JFA 11-Aug-2000
183 static bool _Stl_is_nan_or_inf(double x) { return isnan(x) || !finite(x); }
184 static bool _Stl_is_inf(double x) { return !finite(x); }
185 // bool _Stl_is_neg_inf(double x) { return _class(x) == FP_MINUS_INF; }
186 static bool _Stl_is_neg_inf(double x) { return _Stl_is_inf(x) && ( copysign(1., x) < 0 ); }
187 static bool _Stl_is_neg_nan(double x) { return isnan(x) && ( copysign(1., x) < 0 ); }
188 # elif defined (__ISCPP__)
189 static inline bool _Stl_is_nan_or_inf (double x) { return _fp_isINF(x) || _fp_isNAN(x); }
190 static inline bool _Stl_is_inf (double x) { return _fp_isINF(x); }
191 static inline bool _Stl_is_neg_inf (double x) { return _fp_isINF(x) && x < 0; }
192 static inline bool _Stl_is_neg_nan (double x) { return _fp_isNAN(x) && x < 0; }
193 # elif defined (_CRAY)
194 # if defined (_CRAYIEEE)
195 static inline bool _Stl_is_nan_or_inf(double x) { return isnan(x) || isinf(x); }
196 static inline bool _Stl_is_inf(double x) { return isinf(x); }
197 static inline bool _Stl_is_neg_inf(double x) { return isinf(x) && signbit(x); }
198 static inline bool _Stl_is_neg_nan(double x) { return isnan(x) && signbit(x); }
200 static inline bool _Stl_is_nan_or_inf(double x) { return false; }
201 static inline bool _Stl_is_inf(double x) { return false; }
202 static inline bool _Stl_is_neg_inf(double x) { return false; }
203 static inline bool _Stl_is_neg_nan(double x) { return false; }
205 # else // nothing from above
206 # define USE_SPRINTF_INSTEAD
208 #endif // !USE_SPRINTF_INSTEAD
210 #if !defined (USE_SPRINTF_INSTEAD)
211 // Reentrant versions of floating-point conversion functions. The argument
212 // lists look slightly different on different operating systems, so we're
213 // encapsulating the differences here.
215 # if defined (__CYGWIN__) || defined(__DJGPP)
216 static inline char* _Stl_ecvtR(double x, int n, int* pt, int* sign, char* buf)
217 { return ecvtbuf(x, n, pt, sign, buf); }
218 static inline char* _Stl_fcvtR(double x, int n, int* pt, int* sign, char* buf)
219 { return fcvtbuf(x, n, pt, sign, buf); }
220 # if !defined (_STLP_NO_LONG_DOUBLE)
221 # if defined (__CYGWIN__)
222 # define _STLP_EMULATE_LONG_DOUBLE_CVT
224 static inline char* _Stl_ecvtR(long double x, int n, int* pt, int* sign, char* buf)
225 { return ecvtbuf(x, n, pt, sign, buf); }
226 static inline char* _Stl_fcvtR(long double x, int n, int* pt, int* sign, char* buf)
227 { return fcvtbuf(x, n, pt, sign, buf); }
230 # elif defined (_STLP_USE_GLIBC)
231 static inline char* _Stl_ecvtR(double x, int n, int* pt, int* sign, char* buf, size_t bsize)
232 { return ecvt_r(x, n, pt, sign, buf, bsize) == 0 ? buf : 0; }
233 static inline char* _Stl_fcvtR(double x, int n, int* pt, int* sign, char* buf, size_t bsize)
234 { return fcvt_r(x, n, pt, sign, buf, bsize) == 0 ? buf : 0; }
235 # ifndef _STLP_NO_LONG_DOUBLE
236 static inline char* _Stl_ecvtR(long double x, int n, int* pt, int* sign, char* buf, size_t bsize)
237 { return qecvt_r(x, n, pt, sign, buf, bsize) == 0 ? buf : 0; }
238 static inline char* _Stl_fcvtR(long double x, int n, int* pt, int* sign, char* buf, size_t bsize)
239 { return qfcvt_r(x, n, pt, sign, buf, bsize) == 0 ? buf : 0; }
241 # define _STLP_NEED_CVT_BUFFER_SIZE
242 # elif defined (__sun)
243 static inline char* _Stl_ecvtR(double x, int n, int* pt, int* sign, char* buf)
244 { return econvert(x, n, pt, sign, buf); }
245 static inline char* _Stl_fcvtR(double x, int n, int* pt, int* sign, char* buf)
246 { return fconvert(x, n, pt, sign, buf); }
247 # ifndef _STLP_NO_LONG_DOUBLE
248 static inline char* _Stl_ecvtR(long double x, int n, int* pt, int* sign, char* buf)
249 { return qeconvert(&x, n, pt, sign, buf); }
250 static inline char* _Stl_fcvtR(long double x, int n, int* pt, int* sign, char* buf)
251 { return qfconvert(&x, n, pt, sign, buf); }
253 # elif defined (__DECCXX)
254 static inline char* _Stl_ecvtR(double x, int n, int* pt, int* sign, char* buf, size_t bsize)
255 { return (ecvt_r(x, n, pt, sign, buf, bsize) == 0 ? buf : 0); }
256 static inline char* _Stl_fcvtR(double x, int n, int* pt, int* sign, char* buf, size_t bsize)
257 { return (fcvt_r(x, n, pt, sign, buf, bsize) == 0 ? buf : 0); }
258 # ifndef _STLP_NO_LONG_DOUBLE
259 // fbp : no "long double" conversions !
260 static inline char* _Stl_ecvtR(long double x, int n, int* pt, int* sign, char* buf, size_t bsize)
261 { return (ecvt_r((double)x, n, pt, sign, buf, bsize) == 0 ? buf : 0) ; }
262 static inline char* _Stl_fcvtR(long double x, int n, int* pt, int* sign, char* buf, size_t bsize)
263 { return (fcvt_r((double)x, n, pt, sign, buf, bsize) == 0 ? buf : 0); }
265 # define _STLP_NEED_CVT_BUFFER_SIZE
266 # elif defined (__hpux)
267 static inline char* _Stl_ecvtR(double x, int n, int* pt, int* sign)
268 { return ecvt(x, n, pt, sign); }
269 static inline char* _Stl_fcvtR(double x, int n, int* pt, int* sign)
270 { return fcvt(x, n, pt, sign); }
271 # if !defined (_STLP_NO_LONG_DOUBLE)
272 static inline char* _Stl_ecvtR(long double x, int n, int* pt, int* sign)
273 { return _ldecvt(*(long_double*)&x, n, pt, sign); }
274 static inline char* _Stl_fcvtR(long double x, int n, int* pt, int* sign)
275 { return _ldfcvt(*(long_double*)&x, n, pt, sign); }
277 # define _STLP_CVT_NEED_SYNCHRONIZATION
278 # elif defined (__unix) && !defined (__APPLE__) && !defined (_CRAY)
279 static inline char* _Stl_ecvtR(double x, int n, int* pt, int* sign, char* buf)
280 { return ecvt_r(x, n, pt, sign, buf); }
281 static inline char* _Stl_fcvtR(double x, int n, int* pt, int* sign, char* buf)
282 { return fcvt_r(x, n, pt, sign, buf); }
283 # if !defined (_STLP_NO_LONG_DOUBLE)
284 static inline char* _Stl_ecvtR(long double x, int n, int* pt, int* sign, char* buf)
285 { return qecvt_r(x, n, pt, sign, buf); }
286 static inline char* _Stl_fcvtR(long double x, int n, int* pt, int* sign, char* buf)
287 { return qfcvt_r(x, n, pt, sign, buf); }
289 # elif defined (_STLP_MSVC_LIB) || defined (__MINGW32__) || defined (__BORLANDC__)
290 # if defined (_STLP_USE_SAFE_STRING_FUNCTIONS)
291 # define _STLP_APPEND(a, b) a##b
292 # define _STLP_BUF_PARAMS , char* buf, size_t bsize
293 # define _STLP_SECURE_FUN(F, X, N, PT, SIGN) _STLP_APPEND(F, _s)(buf, bsize, X, N, PT, SIGN); return buf
295 # define _STLP_BUF_PARAMS
296 # define _STLP_SECURE_FUN(F, X, N, PT, SIGN) return F(X, N, PT, SIGN)
297 # define _STLP_CVT_NEED_SYNCHRONIZATION
299 static inline char* _Stl_ecvtR(double x, int n, int* pt, int* sign _STLP_BUF_PARAMS)
300 { _STLP_SECURE_FUN(_ecvt, x, n, pt, sign); }
301 static inline char* _Stl_fcvtR(double x, int n, int* pt, int* sign _STLP_BUF_PARAMS)
302 { _STLP_SECURE_FUN(_fcvt, x, n, pt, sign); }
303 # if !defined (_STLP_NO_LONG_DOUBLE)
304 # if defined (_STLP_USE_SAFE_STRING_FUNCTIONS)
305 # define _STLP_PARAMS , buf, bsize
307 # define _STLP_PARAMS
309 static inline char* _Stl_ecvtR(long double x, int n, int* pt, int* sign _STLP_BUF_PARAMS)
310 { return _Stl_ecvtR(__STATIC_CAST(double, x), n, pt, sign _STLP_PARAMS); }
311 static inline char* _Stl_fcvtR(long double x, int n, int* pt, int* sign _STLP_BUF_PARAMS)
312 { return _Stl_fcvtR(__STATIC_CAST(double, x), n, pt, sign _STLP_PARAMS); }
315 # undef _STLP_SECURE_FUN
316 # undef _STLP_BUF_PARAMS
318 # if defined (__BORLANDC__) /* || defined (__GNUC__) MinGW do not support 'L' modifier so emulation do not work */
319 # define _STLP_EMULATE_LONG_DOUBLE_CVT
321 # elif defined (__ISCPP__)
322 static inline char* _Stl_ecvtR(double x, int n, int* pt, int* sign, char* buf)
323 { return _fp_ecvt( x, n, pt, sign, buf); }
324 static inline char* _Stl_fcvtR(double x, int n, int* pt, int* sign, char* buf)
325 { return _fp_fcvt(x, n, pt, sign, buf); }
326 # if !defined (_STLP_NO_LONG_DOUBLE)
327 static inline char* _Stl_ecvtR(long double x, int n, int* pt, int* sign, char* buf)
328 { return _fp_ecvt( x, n, pt, sign, buf); }
329 static inline char* _Stl_fcvtR(long double x, int n, int* pt, int* sign, char* buf)
330 { return _fp_fcvt(x, n, pt, sign, buf); }
332 # elif defined (_AIX) || defined (__FreeBSD__) || defined (__NetBSD__) || defined (__OpenBSD__) || \
333 defined (__MRC__) || defined (__SC__) || defined (_CRAY) || \
334 defined (_STLP_SCO_OPENSERVER) || defined (__NCR_SVR) || \
336 static inline char* _Stl_ecvtR(double x, int n, int* pt, int* sign)
337 { return ecvt(x, n, pt, sign ); }
338 static inline char* _Stl_fcvtR(double x, int n, int* pt, int* sign)
339 { return fcvt(x, n, pt, sign); }
340 # if !defined (_STLP_NO_LONG_DOUBLE)
341 static inline char* _Stl_ecvtR(long double x, int n, int* pt, int* sign)
342 { return ecvt(x, n, pt, sign ); }
343 static inline char* _Stl_fcvtR(long double x, int n, int* pt, int* sign)
344 { return fcvt(x, n, pt, sign); }
346 # define _STLP_CVT_NEED_SYNCHRONIZATION
348 # error Missing _Stl_ecvtR and _Stl_fcvtR implementations.
351 #if defined (_STLP_CVT_NEED_SYNCHRONIZATION)
352 /* STLport synchronize access to *cvt functions but those methods might
353 * be called from outside, in this case we will still have a race condition. */
354 # if defined (_STLP_THREADS)
355 static _STLP_STATIC_MUTEX& put_float_mutex() {
356 static _STLP_STATIC_MUTEX __put_float_mutex _STLP_MUTEX_INITIALIZER;
357 return __put_float_mutex;
359 static inline char* _Stl_ecvtR(double x, int n, int* pt, int* sign, char* buf) {
360 _STLP_auto_lock lock(put_float_mutex());
361 strcpy(buf, _Stl_ecvtR(x, n, pt, sign)); return buf;
363 static inline char* _Stl_fcvtR(double x, int n, int* pt, int* sign, char* buf) {
364 _STLP_auto_lock lock(put_float_mutex());
365 strcpy(buf, _Stl_fcvtR(x, n, pt, sign)); return buf;
367 # if !defined (_STLP_NO_LONG_DOUBLE) && !defined (_STLP_EMULATE_LONG_DOUBLE_CVT)
368 static inline char* _Stl_ecvtR(long double x, int n, int* pt, int* sign, char* buf) {
369 _STLP_auto_lock lock(put_float_mutex());
370 strcpy(buf, _Stl_ecvtR(x, n, pt, sign)); return buf;
372 static inline char* _Stl_fcvtR(long double x, int n, int* pt, int* sign, char* buf) {
373 _STLP_auto_lock lock(put_float_mutex());
374 strcpy(buf, _Stl_fcvtR(x, n, pt, sign)); return buf;
378 static inline char* _Stl_ecvtR(double x, int n, int* pt, int* sign, char*)
379 { return _Stl_ecvtR(x, n, pt, sign); }
380 static inline char* _Stl_fcvtR(double x, int n, int* pt, int* sign, char*)
381 { return _Stl_fcvtR(x, n, pt, sign); }
382 # if !defined (_STLP_NO_LONG_DOUBLE) && !defined (_STLP_EMULATE_LONG_DOUBLE_CVT)
383 static inline char* _Stl_ecvtR(long double x, int n, int* pt, int* sign, char*)
384 { return _Stl_ecvtR(x, n, pt, sign); }
385 static inline char* _Stl_fcvtR(long double x, int n, int* pt, int* sign, char*)
386 { return _Stl_fcvtR(x, n, pt, sign); }
391 # if !defined (_STLP_USE_SAFE_STRING_FUNCTIONS) && !defined (_STLP_NEED_CVT_BUFFER_SIZE)
392 # define _STLP_CVT_BUFFER(B) B
394 # define _STLP_CVT_BUFFER(B) _STLP_ARRAY_AND_SIZE(B)
397 # if defined (_STLP_EMULATE_LONG_DOUBLE_CVT)
398 static void __fill_fmtbuf(char* fmtbuf, ios_base::fmtflags flags, char long_modifier);
400 // Emulation of ecvt/fcvt functions using sprintf:
401 static char* _Stl_ecvtR(long double x, int n, int* pt, int* sign, char* buf) {
402 // If long double value can be safely converted to double without losing precision
403 // we use the ecvt function for double:
404 double y = __STATIC_CAST(double, x);
406 return _Stl_ecvtR(y, n, pt, sign, buf);
409 __fill_fmtbuf(fmtbuf, 0, 'L');
410 sprintf(buf, fmtbuf, n, x < 0.0l ? -x : x);
411 /* We are waiting for something having the form x.xxxe+yyyy */
416 while (buf[++i] != 0 && n != 0) {
417 if (buf[i] >= '0' && buf[i] <= '9') {
420 buf[i - offset] = buf[i];
423 if (offset != 0) break;
430 // Extract exponent part in point position:
432 while (buf[++i] != 0) {
433 if (buf[i] >= '0' && buf[i] <= '9') {
434 e = e * 10 + (buf[i] - '0');
441 static char* _Stl_fcvtR(long double x, int n, int* pt, int* sign, char* buf) {
442 // If long double value can be safely converted to double without losing precision
443 // we use the fcvt function for double:
444 double y = __STATIC_CAST(double, x);
446 return _Stl_fcvtR(y, n, pt, sign, buf);
449 __fill_fmtbuf(fmtbuf, ios_base::fixed, 'L');
450 sprintf(buf, fmtbuf, n, x < 0.0l ? -x : x);
455 while (buf[++i] != 0 && (offset == 0 || n != 0)) {
456 if (buf[i] >= '0' && buf[i] <= '9') {
459 buf[i - offset] = buf[i];
475 //----------------------------------------------------------------------
478 // __format_float formats a mantissa and exponent as returned by
479 // one of the conversion functions (ecvt_r, fcvt_r, qecvt_r, qfcvt_r)
480 // according to the specified precision and format flags. This is
481 // based on doprnt but is much simpler since it is concerned only
482 // with floating point input and does not consider all formats. It
483 // also does not deal with blank padding, which is handled by
484 // __copy_float_and_fill.
486 static size_t __format_float_scientific( __iostring& buf, const char *bp,
487 int decpt, int sign, bool is_zero,
488 ios_base::fmtflags flags,
493 else if (flags & ios_base::showpos)
496 // first digit of mantissa
499 // start of grouping position, grouping won't occur in scientific notation
500 // as it is impossible to have something like 1234.0e04 but we return a correct
501 // group position for coherency with __format_float_fixed.
502 size_t __group_pos = buf.size();
504 // decimal point if required
505 if (precision != 0 || flags & ios_base::showpoint) {
510 while (*bp != 0 && precision--)
513 // trailing 0 if needed
515 buf.append(precision, '0');
517 // exponent size = number of digits + exponent sign + exponent symbol + trailing zero
518 char expbuf[MAXEDIGITS + 3];
519 //We start filling at the buffer end
520 char *suffix = expbuf + MAXEDIGITS + 2;
526 for (; nn > 9; nn /= 10)
527 *--suffix = (char) todigit(nn % 10);
528 *--suffix = (char) todigit(nn);
531 // prepend leading zeros to exponent
532 // C89 Standard says that it should be at least 2 digits, C99 Standard says that
533 // we stop prepend zeros if more than 3 digits. To repect both STLport prepend zeros
534 // until it is 2 digits.
535 while (suffix > &expbuf[MAXEDIGITS])
538 // put in the exponent sign
539 *--suffix = (char) ((decpt > 0 || is_zero ) ? '+' : '-');
542 *--suffix = flags & ios_base::uppercase ? 'E' : 'e';
549 static size_t __format_float_fixed( __iostring &buf, const char *bp,
551 ios_base::fmtflags flags,
553 if ( sign && (decpt > -precision) && (*bp != 0) )
555 else if ( flags & ios_base::showpos )
558 // digits before decimal point
561 buf += (nnn <= 0 || *bp == 0) ? '0' : *bp++;
562 } while ( --nnn > 0 );
564 // start of grouping position
565 size_t __group_pos = buf.size();
567 // decimal point if needed
568 if ( flags & ios_base::showpoint || precision > 0 ) {
572 // digits after decimal point if any
573 while ( *bp != 0 && --precision >= 0 ) {
574 buf += (++decpt <= 0) ? '0' : *bp++;
577 // trailing zeros if needed
579 buf.append(precision, '0');
584 #if defined (_STLP_USE_SIGN_HELPER)
585 template<class _FloatT>
586 struct float_sign_helper {
587 float_sign_helper(_FloatT __x)
588 { _M_number._num = __x; }
590 bool is_negative() const {
591 const unsigned short sign_mask(1 << (sizeof(unsigned short) * CHAR_BIT - 1));
592 return (get_sign_word() & sign_mask) != 0;
596 unsigned short _Words[8];
600 unsigned short get_word_higher() const _STLP_NOTHROW
601 { return _M_number._Words[0]; }
602 unsigned short get_word_lower() const _STLP_NOTHROW
603 { return _M_number._Words[(sizeof(_FloatT) >= 12 ? 10 : sizeof(_FloatT)) / sizeof(unsigned short) - 1]; }
604 unsigned short get_sign_word() const _STLP_NOTHROW
605 # if defined (_STLP_BIG_ENDIAN)
606 { return get_word_higher(); }
607 # else /* _STLP_LITTLE_ENDIAN */
608 { return get_word_lower(); }
613 template <class _FloatT>
614 static size_t __format_nan_or_inf(__iostring& buf, _FloatT x, ios_base::fmtflags flags) {
615 static const char* inf[2] = { "inf", "Inf" };
616 static const char* nan[2] = { "nan", "NaN" };
617 const char** inf_or_nan;
618 #if !defined (_STLP_USE_SIGN_HELPER)
619 if (_Stl_is_inf(x)) { // Infinity
621 if (_Stl_is_neg_inf(x))
623 else if (flags & ios_base::showpos)
627 if (_Stl_is_neg_nan(x))
629 else if (flags & ios_base::showpos)
633 typedef numeric_limits<_FloatT> limits;
634 if (x == limits::infinity() || x == -limits::infinity()) {
639 float_sign_helper<_FloatT> helper(x);
640 if (helper.is_negative())
642 else if (flags & ios_base::showpos)
645 size_t ret = buf.size();
646 buf += inf_or_nan[flags & ios_base::uppercase ? 1 : 0];
650 static inline size_t __format_float(__iostring &buf, const char * bp,
651 int decpt, int sign, bool is_zero,
652 ios_base::fmtflags flags,
654 size_t __group_pos = 0;
655 switch (flags & ios_base::floatfield) {
656 case ios_base::scientific:
657 __group_pos = __format_float_scientific( buf, bp, decpt, sign, is_zero,
660 case ios_base::fixed:
661 __group_pos = __format_float_fixed( buf, bp, decpt, sign,
665 // establish default precision
666 if (flags & ios_base::showpoint || precision > 0) {
667 if (precision == 0) precision = 1;
671 // reset exponent if value is zero
676 if (!(flags & ios_base::showpoint)) {
677 size_t n = strlen(bp);
680 while (kk >= 1 && bp[kk-1] == '0')
684 if (decpt < -3 || decpt > precision) {
686 __group_pos = __format_float_scientific( buf, bp, decpt, sign, is_zero,
689 precision = kk - decpt;
690 __group_pos = __format_float_fixed( buf, bp, decpt, sign,
700 #if defined (USE_SPRINTF_INSTEAD) || defined (_STLP_EMULATE_LONG_DOUBLE_CVT)
702 bool operator () (char __c) const {
704 __c == 'e' || __c == 'E';
708 // Creates a format string for sprintf()
709 static void __fill_fmtbuf(char* fmtbuf, ios_base::fmtflags flags, char long_modifier) {
713 if (flags & ios_base::showpos)
716 if (flags & ios_base::showpoint)
723 fmtbuf[i++] = long_modifier;
725 switch (flags & ios_base::floatfield)
727 case ios_base::scientific:
728 fmtbuf[i++] = (flags & ios_base::uppercase) ? 'E' : 'e';
730 case ios_base::fixed:
731 # if defined (__FreeBSD__)
734 fmtbuf[i++] = (flags & ios_base::uppercase) ? 'F' : 'f';
738 fmtbuf[i++] = (flags & ios_base::uppercase) ? 'G' : 'g';
745 #endif /* USE_SPRINTF_INSTEAD */
747 template <class _FloatT>
748 static size_t __write_floatT(__iostring &buf, ios_base::fmtflags flags, int precision,
750 #if defined (USE_SPRINTF_INSTEAD)
752 /* In theory, if we want 'arbitrary' precision, we should use 'arbitrary'
753 * buffer size below, but really we limited by exponent part in double.
756 typedef numeric_limits<_FloatT> limits;
757 char static_buf[limits::max_exponent10 + 6]; // 6: -xxx.yyyE-zzz (sign, dot, E, exp sign, \0)
759 __fill_fmtbuf(fmtbuf, flags, modifier);
760 snprintf(_STLP_ARRAY_AND_SIZE(static_buf), fmtbuf, precision, x);
762 return find_if(buf.begin(), buf.end(), GroupPos()) - buf.begin();
765 typedef numeric_limits<_FloatT> limits;
766 //If numeric_limits support is correct we use the exposed values to detect NaN and infinity:
767 if (limits::has_infinity && limits::has_quiet_NaN) {
768 if (!(x == x) || // NaN check
769 (x == limits::infinity() || x == -limits::infinity())) {
770 return __format_nan_or_inf(buf, x, flags);
773 // numeric_limits support is not good enough, we rely on platform dependent function
774 // _Stl_is_nan_or_inf that do not support long double.
775 else if (_Stl_is_nan_or_inf(x)) {
776 return __format_nan_or_inf(buf, x, flags);
778 # if defined (__MINGW32__)
779 //For the moment MinGW is limited to display at most numeric_limits<double>::max()
780 if (x > numeric_limits<double>::max() ||
781 x < -numeric_limits<double>::max()) {
782 return __format_nan_or_inf(buf, x, flags);
786 /* Buffer size is max number of digits which is the addition of:
787 * - max_exponent10: max number of digits in fixed mode
788 * - digits10 + 2: max number of significant digits
791 char cvtbuf[limits::max_exponent10 + limits::digits10 + 2 + 1];
795 switch (flags & ios_base::floatfield) {
796 case ios_base::fixed:
798 /* Here, number of digits represents digits _after_ decimal point.
799 * In order to limit static buffer size we have to give 2 different values depending on x value.
800 * For small values (abs(x) < 1) we need as many digits as requested by precision limited by the maximum number of digits
801 * which is min_exponent10 + digits10 + 2
802 * For bigger values we won't have more than limits::digits10 + 2 digits after decimal point. */
803 int digits10 = (x > -1.0 && x < 1.0 ? -limits::min_exponent10 + limits::digits10 + 2
804 : limits::digits10 + 2);
805 bp = _Stl_fcvtR(x, (min) (precision, digits10), &decpt, &sign, _STLP_CVT_BUFFER(cvtbuf) );
808 case ios_base::scientific:
810 /* Here, number of digits is total number of digits which is limited to digits10 + 2. */
812 int digits10 = limits::digits10 + 2;
813 bp = _Stl_ecvtR(x, (min) (precision, digits10), &decpt, &sign, _STLP_CVT_BUFFER(cvtbuf) );
817 return __format_float(buf, bp, decpt, sign, x == 0.0, flags, precision);
822 __write_float(__iostring &buf, ios_base::fmtflags flags, int precision,
824 return __write_floatT(buf, flags, precision, x
825 #if defined (USE_SPRINTF_INSTEAD)
831 #if !defined (_STLP_NO_LONG_DOUBLE)
833 __write_float(__iostring &buf, ios_base::fmtflags flags, int precision,
835 return __write_floatT(buf, flags, precision, x
836 #if defined (USE_SPRINTF_INSTEAD)
843 void _STLP_CALL __get_floor_digits(__iostring &out, _STLP_LONGEST_FLOAT_TYPE __x) {
844 typedef numeric_limits<_STLP_LONGEST_FLOAT_TYPE> limits;
845 #if defined (USE_SPRINTF_INSTEAD)
846 char cvtbuf[limits::max_exponent10 + 6];
847 # if !defined (_STLP_NO_LONG_DOUBLE)
848 snprintf(_STLP_ARRAY_AND_SIZE(cvtbuf), "%Lf", __x); // check for 1234.56!
850 snprintf(_STLP_ARRAY_AND_SIZE(cvtbuf), "%f", __x); // check for 1234.56!
852 char *p = strchr( cvtbuf, '.' );
854 out.append( cvtbuf );
856 out.append( cvtbuf, p );
859 char cvtbuf[limits::max_exponent10 + 1];
862 bp = _Stl_fcvtR(__x, 0, &decpt, &sign, _STLP_CVT_BUFFER(cvtbuf));
867 out.append(bp, bp + decpt);
872 #if !defined (_STLP_NO_WCHAR_T)
873 void _STLP_CALL __convert_float_buffer( __iostring const& str, __iowstring &out,
874 const ctype<wchar_t>& ct, wchar_t dot, bool __check_dot) {
875 string::const_iterator str_ite(str.begin()), str_end(str.end());
877 //First loop, check the dot char
879 while (str_ite != str_end) {
880 if (*str_ite != '.') {
881 out += ct.widen(*str_ite++);
888 if (str_ite != str_end) {
889 out += ct.widen(*str_ite);
893 if (str_ite != str_end) {
894 //Second loop, dot has been found, no check anymore
895 while (++str_ite != str_end) {
896 out += ct.widen(*str_ite);
904 __adjust_float_buffer(__iostring &str, char dot) {
906 size_t __dot_pos = str.find('.');
907 if (__dot_pos != string::npos) {
908 str[__dot_pos] = dot;
913 _STLP_MOVE_TO_STD_NAMESPACE