1
/*
2
 * Copyright (c) 1999
3
 * Silicon Graphics Computer Systems, Inc.
4
 *
5
 * Copyright (c) 1999
6
 * Boris Fomitchev
7
 *
8
 * This material is provided "as is", with absolutely no warranty expressed
9
 * or implied. Any use is at your own risk.
10
 *
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.
16
 *
17
 */
18
19
#include "stlport_prefix.h"
20
21
#include <cmath>
22
#include <ios>
23
#include <locale>
24
25
#define NDIG 82
26
27
#define todigit(x) ((x)+'0')
28
29
#if defined (_STLP_UNIX)
30
31
#  if defined (__sun)
32
#    // Bug in Solaris 9 headers: FILE used in <floatingpoint.h>, but no
33
#    // definition included.
34
#    include <stdio.h>
35
#    include <floatingpoint.h>
36
#  endif
37
38
#  if defined (__sun) || defined (__digital__) || defined (_STLP_SCO_OPENSERVER) || defined (__NCR_SVR)
39
// DEC, SGI & Solaris need this
40
#    include <values.h>
41
#    include <nan.h>
42
#  endif
43
44
#  if defined (__QNXNTO__) || ( defined(__GNUC__) && defined(__APPLE__) ) || defined(_STLP_USE_UCLIBC) /* 0.9.26 */ || \
45
      defined(__FreeBSD__) || defined(__ANDROID__)
46
#    define USE_SPRINTF_INSTEAD
47
#  endif
48
49
#  if defined (_AIX) // JFA 3-Aug-2000
50
#    include <math.h>
51
#    include <float.h>
52
#  endif
53
54
#  include <math.h>
55
#endif
56
57
#include <cstdio>
58
#include <cstdlib>
59
60
#if defined (_STLP_MSVC_LIB) || defined (__MINGW32__) || defined (__BORLANDC__) || defined (__DJGPP) || \
61
    defined (_STLP_SCO_OPENSERVER) || defined (__NCR_SVR)
62
#  include <float.h>
63
#endif
64
65
#if defined (__MRC__) || defined (__SC__)  || defined (_CRAY)  //*TY 02/24/2000 - added support for MPW
66
#  include <fp.h>
67
#endif
68
69
#if defined (__CYGWIN__)
70
#  include <ieeefp.h>
71
#endif
72
73
#if defined (__MSL__)
74
#  include <cstdlib>  // for atoi
75
#  include <cstdio>  // for snprintf
76
#  include <algorithm>
77
#  include <cassert>
78
#endif
79
80
#if defined (__ISCPP__)
81
#  include <cfloat>
82
#endif
83
84
#include <algorithm>
85
86
#if defined (__DMC__)
87
#  define snprintf _snprintf
88
#endif
89
90
_STLP_BEGIN_NAMESPACE
91
92
_STLP_MOVE_TO_PRIV_NAMESPACE
93
94
#if defined(__BEOS__)
95
#  define USE_SPRINTF_INSTEAD
96
#endif
97
98
template <int N>
99
struct _Dig
100
{
101
    enum { dig = _Dig<N/10>::dig + 1 };
102
};
103
104
_STLP_TEMPLATE_NULL
105
struct _Dig<0>
106
{
107
    enum { dig = 0 };
108
};
109
110
#ifdef _STLP_NO_LONG_DOUBLE
111
# define MAXEDIGITS int(_Dig<DBL_MAX_10_EXP>::dig)
112
# define MAXFSIG DBL_DIG
113
# define MAXFCVT (DBL_DIG + 1)
114
#else
115
# define MAXEDIGITS int(_Dig<LDBL_MAX_10_EXP>::dig)
116
# define MAXFSIG LDBL_DIG
117
# define MAXFCVT (LDBL_DIG + 1)
118
#endif
119
120
// Tests for infinity and NaN differ on different OSs.  We encapsulate
121
// these differences here.
122
#if !defined (USE_SPRINTF_INSTEAD)
123
#  if defined (__hpux)
124
#    define _STLP_USE_SIGN_HELPER
125
#  elif defined (__DJGPP) || (defined (_STLP_USE_GLIBC) && ! defined (__MSL__)) || \
126
      defined (__CYGWIN__) || \
127
      defined (__FreeBSD__) || defined (__NetBSD__) || defined (__OpenBSD__)
128
static inline bool _Stl_is_nan_or_inf(double x)
129
#    if defined (isfinite)
130
{ return !isfinite(x); }
131
#    else
132
{ return !finite(x); }
133
#    endif
134
static inline bool _Stl_is_neg_nan(double x)    { return isnan(x) && ( copysign(1., x) < 0 ); }
135
static inline bool _Stl_is_inf(double x)        { return isinf(x); }
136
// inline bool _Stl_is_neg_inf(double x)    { return isinf(x) < 0; }
137
static inline bool _Stl_is_neg_inf(double x)    { return isinf(x) && x < 0; }
138
#  elif (defined (__unix) || defined (__unix__)) && \
139
         !defined (__APPLE__) && !defined (__DJGPP) && !defined(__osf__) && \
140
         !defined (_CRAY)
141
static inline bool _Stl_is_nan_or_inf(double x) { return IsNANorINF(x); }
142
static inline bool _Stl_is_inf(double x)        { return IsNANorINF(x) && IsINF(x); }
143
static inline bool _Stl_is_neg_inf(double x)    { return (IsINF(x)) && (x < 0.0); }
144
static inline bool _Stl_is_neg_nan(double x)    { return IsNegNAN(x); }
145
#  elif defined (_STLP_MSVC_LIB) || defined (__MINGW32__) || defined (__BORLANDC__)
146
static inline bool _Stl_is_nan_or_inf(double x) { return !_finite(x); }
147
#    if !defined (__BORLANDC__)
148
static inline bool _Stl_is_inf(double x)        {
149
  int fclass = _fpclass(x);
150
  return fclass == _FPCLASS_NINF || fclass == _FPCLASS_PINF;
151
}
152
static inline bool _Stl_is_neg_inf(double x)    { return _fpclass(x) == _FPCLASS_NINF; }
153
#    else
154
static inline bool _Stl_is_inf(double x)        {  return _Stl_is_nan_or_inf(x) && !_isnan(x);}
155
static inline bool _Stl_is_neg_inf(double x)    {  return _Stl_is_inf(x) && x < 0 ; }
156
#    endif
157
static inline bool _Stl_is_neg_nan(double x)    { return _isnan(x) && _copysign(1., x) < 0 ; }
158
#    if defined (__BORLANDC__)
159
static inline bool _Stl_is_nan_or_inf(long double x) { return !_finitel(x); }
160
static inline bool _Stl_is_inf(long double x)        {  return _Stl_is_nan_or_inf(x) && !_isnanl(x);}
161
static inline bool _Stl_is_neg_inf(long double x)    {  return _Stl_is_inf(x) && x < 0 ; }
162
static inline bool _Stl_is_neg_nan(long double x)    { return _isnanl(x) && _copysignl(1.l, x) < 0 ; }
163
#    elif !defined (_STLP_NO_LONG_DOUBLE)
164
// Simply there to avoid warning long double -> double implicit conversion:
165
static inline bool _Stl_is_nan_or_inf(long double x) { return _Stl_is_nan_or_inf(__STATIC_CAST(double, x)); }
166
static inline bool _Stl_is_inf(long double x)        {  return _Stl_is_inf(__STATIC_CAST(double, x));}
167
static inline bool _Stl_is_neg_inf(long double x)    {  return _Stl_is_neg_inf(__STATIC_CAST(double, x)); }
168
static inline bool _Stl_is_neg_nan(long double x)    { return _Stl_is_neg_nan(__STATIC_CAST(double, x)); }
169
#    endif
170
#  elif defined (__MRC__) || defined (__SC__) || defined (__DMC__)
171
static bool _Stl_is_nan_or_inf(double x) { return isnan(x) || !isfinite(x); }
172
static bool _Stl_is_inf(double x)        { return !isfinite(x); }
173
static bool _Stl_is_neg_inf(double x)    { return !isfinite(x) && signbit(x); }
174
static bool _Stl_is_neg_nan(double x)    { return isnan(x) && signbit(x); }
175
#  elif /* defined(__FreeBSD__) || defined(__OpenBSD__) || */ (defined(__GNUC__) && defined(__APPLE__))
176
static inline bool _Stl_is_nan_or_inf(double x) { return !finite(x); }
177
static inline bool _Stl_is_inf(double x)        {   return _Stl_is_nan_or_inf(x) && ! isnan(x); }
178
static inline bool _Stl_is_neg_inf(double x)    {   return _Stl_is_inf(x) && x < 0 ; }
179
static inline bool _Stl_is_neg_nan(double x)    { return isnan(x) && copysign(1., x) < 0 ; }
180
#  elif defined( _AIX ) // JFA 11-Aug-2000
181
static bool _Stl_is_nan_or_inf(double x) { return isnan(x) || !finite(x); }
182
static bool _Stl_is_inf(double x)        { return !finite(x); }
183
// bool _Stl_is_neg_inf(double x)    { return _class(x) == FP_MINUS_INF; }
184
static bool _Stl_is_neg_inf(double x)    { return _Stl_is_inf(x) && ( copysign(1., x) < 0 );  }
185
static bool _Stl_is_neg_nan(double x)    { return isnan(x) && ( copysign(1., x) < 0 );  }
186
#  elif defined (__ISCPP__)
187
static inline bool _Stl_is_nan_or_inf  (double x) { return _fp_isINF(x) || _fp_isNAN(x); }
188
static inline bool _Stl_is_inf         (double x) { return _fp_isINF(x); }
189
static inline bool _Stl_is_neg_inf     (double x) { return _fp_isINF(x) && x < 0; }
190
static inline bool _Stl_is_neg_nan     (double x) { return _fp_isNAN(x) && x < 0; }
191
#  elif defined (_CRAY)
192
#    if defined (_CRAYIEEE)
193
static inline bool _Stl_is_nan_or_inf(double x) { return isnan(x) || isinf(x); }
194
static inline bool _Stl_is_inf(double x)        { return isinf(x); }
195
static inline bool _Stl_is_neg_inf(double x)    { return isinf(x) && signbit(x); }
196
static inline bool _Stl_is_neg_nan(double x)    { return isnan(x) && signbit(x); }
197
#    else
198
static inline bool _Stl_is_nan_or_inf(double x) { return false; }
199
static inline bool _Stl_is_inf(double x)        { return false; }
200
static inline bool _Stl_is_neg_inf(double x)    { return false; }
201
static inline bool _Stl_is_neg_nan(double x)    { return false; }
202
#    endif
203
#  else // nothing from above
204
#    define USE_SPRINTF_INSTEAD
205
#  endif
206
#endif // !USE_SPRINTF_INSTEAD
207
208
#if !defined (USE_SPRINTF_INSTEAD)
209
// Reentrant versions of floating-point conversion functions.  The argument
210
// lists look slightly different on different operating systems, so we're
211
// encapsulating the differences here.
212
213
#  if defined (__CYGWIN__) || defined(__DJGPP)
214
static inline char* _Stl_ecvtR(double x, int n, int* pt, int* sign, char* buf)
215
{ return ecvtbuf(x, n, pt, sign, buf); }
216
static inline char* _Stl_fcvtR(double x, int n, int* pt, int* sign, char* buf)
217
{ return fcvtbuf(x, n, pt, sign, buf); }
218
#    if !defined (_STLP_NO_LONG_DOUBLE)
219
#      if defined (__CYGWIN__)
220
#        define _STLP_EMULATE_LONG_DOUBLE_CVT
221
#      else
222
static inline char* _Stl_ecvtR(long double x, int n, int* pt, int* sign, char* buf)
223
{ return ecvtbuf(x, n, pt, sign, buf); }
224
static inline char* _Stl_fcvtR(long double x, int n, int* pt, int* sign, char* buf)
225
{ return fcvtbuf(x, n, pt, sign, buf); }
226
#      endif
227
#    endif
228
#  elif defined (_STLP_USE_GLIBC)
229
static inline char* _Stl_ecvtR(double x, int n, int* pt, int* sign, char* buf, size_t bsize)
230
{ return ecvt_r(x, n, pt, sign, buf, bsize) == 0 ? buf : 0; }
231
static inline char* _Stl_fcvtR(double x, int n, int* pt, int* sign, char* buf, size_t bsize)
232
{ return fcvt_r(x, n, pt, sign, buf, bsize) == 0 ? buf : 0; }
233
#    ifndef _STLP_NO_LONG_DOUBLE
234
static inline char* _Stl_ecvtR(long double x, int n, int* pt, int* sign, char* buf, size_t bsize)
235
{ return qecvt_r(x, n, pt, sign, buf, bsize) == 0 ? buf : 0; }
236
static inline char* _Stl_fcvtR(long double x, int n, int* pt, int* sign, char* buf, size_t bsize)
237
{ return qfcvt_r(x, n, pt, sign, buf, bsize) == 0 ? buf : 0; }
238
#    endif
239
#    define _STLP_NEED_CVT_BUFFER_SIZE
240
#  elif defined (__sun)
241
static inline char* _Stl_ecvtR(double x, int n, int* pt, int* sign, char* buf)
242
{ return econvert(x, n, pt, sign, buf); }
243
static inline char* _Stl_fcvtR(double x, int n, int* pt, int* sign, char* buf)
244
{ return fconvert(x, n, pt, sign, buf); }
245
#    ifndef _STLP_NO_LONG_DOUBLE
246
static inline char* _Stl_ecvtR(long double x, int n, int* pt, int* sign, char* buf)
247
{ return qeconvert(&x, n, pt, sign, buf); }
248
static inline char* _Stl_fcvtR(long double x, int n, int* pt, int* sign, char* buf)
249
{ return qfconvert(&x, n, pt, sign, buf); }
250
#    endif
251
#  elif defined (__hpux)
252
static inline char* _Stl_ecvtR(double x, int n, int* pt, int* sign)
253
{ return ecvt(x, n, pt, sign); }
254
static inline char* _Stl_fcvtR(double x, int n, int* pt, int* sign)
255
{ return fcvt(x, n, pt, sign); }
256
#    if !defined (_STLP_NO_LONG_DOUBLE)
257
static inline char* _Stl_ecvtR(long double x, int n, int* pt, int* sign)
258
{ return _ldecvt(*(long_double*)&x, n, pt, sign); }
259
static inline char* _Stl_fcvtR(long double x, int n, int* pt, int* sign)
260
{ return _ldfcvt(*(long_double*)&x, n, pt, sign); }
261
#    endif
262
#    define _STLP_CVT_NEED_SYNCHRONIZATION
263
#  elif defined (__unix) && !defined (__APPLE__) && !defined (_CRAY)
264
static inline char* _Stl_ecvtR(double x, int n, int* pt, int* sign, char* buf)
265
{ return ecvt_r(x, n, pt, sign, buf); }
266
static inline char* _Stl_fcvtR(double x, int n, int* pt, int* sign, char* buf)
267
{ return fcvt_r(x, n, pt, sign, buf); }
268
#    if !defined (_STLP_NO_LONG_DOUBLE)
269
static inline char* _Stl_ecvtR(long double x, int n, int* pt, int* sign, char* buf)
270
{ return qecvt_r(x, n, pt, sign, buf); }
271
static inline char* _Stl_fcvtR(long double x, int n, int* pt, int* sign, char* buf)
272
{ return qfcvt_r(x, n, pt, sign, buf); }
273
#    endif
274
#  elif defined (_STLP_MSVC_LIB) || defined (__MINGW32__) || defined (__BORLANDC__)
275
#    if defined (_STLP_USE_SAFE_STRING_FUNCTIONS)
276
#      define _STLP_APPEND(a, b) a##b
277
#      define _STLP_BUF_PARAMS , char* buf, size_t bsize
278
#      define _STLP_SECURE_FUN(F, X, N, PT, SIGN) _STLP_APPEND(F, _s)(buf, bsize, X, N, PT, SIGN); return buf
279
#    else
280
#      define _STLP_BUF_PARAMS
281
#      define _STLP_SECURE_FUN(F, X, N, PT, SIGN) return F(X, N, PT, SIGN)
282
#      define _STLP_CVT_NEED_SYNCHRONIZATION
283
#    endif
284
static inline char* _Stl_ecvtR(double x, int n, int* pt, int* sign _STLP_BUF_PARAMS)
285
{ _STLP_SECURE_FUN(_ecvt, x, n, pt, sign); }
286
static inline char* _Stl_fcvtR(double x, int n, int* pt, int* sign _STLP_BUF_PARAMS)
287
{ _STLP_SECURE_FUN(_fcvt, x, n, pt, sign); }
288
#    if !defined (_STLP_NO_LONG_DOUBLE)
289
#      if defined (_STLP_USE_SAFE_STRING_FUNCTIONS)
290
#        define _STLP_PARAMS , buf, bsize
291
#      else
292
#        define _STLP_PARAMS
293
#      endif
294
static inline char* _Stl_ecvtR(long double x, int n, int* pt, int* sign _STLP_BUF_PARAMS)
295
{ return _Stl_ecvtR(__STATIC_CAST(double, x), n, pt, sign _STLP_PARAMS); }
296
static inline char* _Stl_fcvtR(long double x, int n, int* pt, int* sign _STLP_BUF_PARAMS)
297
{ return _Stl_fcvtR(__STATIC_CAST(double, x), n, pt, sign _STLP_PARAMS); }
298
#      undef _STLP_PARAMS
299
#    endif
300
#    undef _STLP_SECURE_FUN
301
#    undef _STLP_BUF_PARAMS
302
#    undef _STLP_APPEND
303
#    if defined (__BORLANDC__) /* || defined (__GNUC__) MinGW do not support 'L' modifier so emulation do not work */
304
#      define _STLP_EMULATE_LONG_DOUBLE_CVT
305
#    endif
306
#  elif defined (__ISCPP__)
307
static inline char* _Stl_ecvtR(double x, int n, int* pt, int* sign, char* buf)
308
{ return _fp_ecvt( x, n, pt, sign, buf); }
309
static inline char* _Stl_fcvtR(double x, int n, int* pt, int* sign, char* buf)
310
{ return _fp_fcvt(x, n, pt, sign, buf); }
311
#    if !defined (_STLP_NO_LONG_DOUBLE)
312
static inline char* _Stl_ecvtR(long double x, int n, int* pt, int* sign, char* buf)
313
{ return _fp_ecvt( x, n, pt, sign, buf); }
314
static inline char* _Stl_fcvtR(long double x, int n, int* pt, int* sign, char* buf)
315
{ return _fp_fcvt(x, n, pt, sign, buf); }
316
#    endif
317
#  elif defined (_AIX) || defined (__FreeBSD__) || defined (__NetBSD__) || defined (__OpenBSD__) || \
318
        defined (__MRC__) || defined (__SC__) || defined (_CRAY) || \
319
        defined (_STLP_SCO_OPENSERVER) || defined (__NCR_SVR) || \
320
        defined (__DMC__)
321
static inline char* _Stl_ecvtR(double x, int n, int* pt, int* sign)
322
{ return ecvt(x, n, pt, sign ); }
323
static inline char* _Stl_fcvtR(double x, int n, int* pt, int* sign)
324
{ return fcvt(x, n, pt, sign); }
325
#    if !defined (_STLP_NO_LONG_DOUBLE)
326
static inline char* _Stl_ecvtR(long double x, int n, int* pt, int* sign)
327
{ return ecvt(x, n, pt, sign ); }
328
static inline char* _Stl_fcvtR(long double x, int n, int* pt, int* sign)
329
{ return fcvt(x, n, pt, sign); }
330
#    endif
331
#    define _STLP_CVT_NEED_SYNCHRONIZATION
332
#  else
333
#    error Missing _Stl_ecvtR and _Stl_fcvtR implementations.
334
#  endif
335
336
#if defined (_STLP_CVT_NEED_SYNCHRONIZATION)
337
/* STLport synchronize access to *cvt functions but those methods might
338
 * be called from outside, in this case we will still have a race condition. */
339
#  if defined (_STLP_THREADS)
340
static _STLP_STATIC_MUTEX& put_float_mutex() {
341
  static _STLP_STATIC_MUTEX __put_float_mutex _STLP_MUTEX_INITIALIZER;
342
  return __put_float_mutex;
343
}
344
static inline char* _Stl_ecvtR(double x, int n, int* pt, int* sign, char* buf) {
345
  _STLP_auto_lock lock(put_float_mutex());
346
  strcpy(buf, _Stl_ecvtR(x, n, pt, sign)); return buf;
347
}
348
static inline char* _Stl_fcvtR(double x, int n, int* pt, int* sign, char* buf) {
349
  _STLP_auto_lock lock(put_float_mutex());
350
  strcpy(buf, _Stl_fcvtR(x, n, pt, sign)); return buf;
351
}
352
#    if !defined (_STLP_NO_LONG_DOUBLE) && !defined (_STLP_EMULATE_LONG_DOUBLE_CVT)
353
static inline char* _Stl_ecvtR(long double x, int n, int* pt, int* sign, char* buf) {
354
  _STLP_auto_lock lock(put_float_mutex());
355
  strcpy(buf, _Stl_ecvtR(x, n, pt, sign)); return buf;
356
}
357
static inline char* _Stl_fcvtR(long double x, int n, int* pt, int* sign, char* buf) {
358
  _STLP_auto_lock lock(put_float_mutex());
359
  strcpy(buf, _Stl_fcvtR(x, n, pt, sign)); return buf;
360
}
361
#    endif
362
#  else
363
static inline char* _Stl_ecvtR(double x, int n, int* pt, int* sign, char*)
364
{ return _Stl_ecvtR(x, n, pt, sign); }
365
static inline char* _Stl_fcvtR(double x, int n, int* pt, int* sign, char*)
366
{ return _Stl_fcvtR(x, n, pt, sign); }
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*)
369
{ return _Stl_ecvtR(x, n, pt, sign); }
370
static inline char* _Stl_fcvtR(long double x, int n, int* pt, int* sign, char*)
371
{ return _Stl_fcvtR(x, n, pt, sign); }
372
#    endif
373
#  endif
374
#endif
375
376
#  if !defined (_STLP_USE_SAFE_STRING_FUNCTIONS) && !defined (_STLP_NEED_CVT_BUFFER_SIZE)
377
#    define _STLP_CVT_BUFFER(B) B
378
#  else
379
#    define _STLP_CVT_BUFFER(B) _STLP_ARRAY_AND_SIZE(B)
380
#  endif
381
382
#  if defined (_STLP_EMULATE_LONG_DOUBLE_CVT)
383
static void __fill_fmtbuf(char* fmtbuf, ios_base::fmtflags flags, char long_modifier);
384
385
// Emulation of ecvt/fcvt functions using sprintf:
386
static char* _Stl_ecvtR(long double x, int n, int* pt, int* sign, char* buf) {
387
  // If long double value can be safely converted to double without losing precision
388
  // we use the ecvt function for double:
389
  double y = __STATIC_CAST(double, x); 
390
  if (x == y)
391
    return _Stl_ecvtR(y, n, pt, sign, buf);
392
393
  char fmtbuf[32];
394
  __fill_fmtbuf(fmtbuf, 0, 'L');
395
  sprintf(buf, fmtbuf, n, x < 0.0l ? -x : x);
396
  /* We are waiting for something having the form x.xxxe+yyyy */
397
  *pt = 0;
398
  *sign = 0;
399
  int i = -1;
400
  int offset = 0;
401
  while (buf[++i] != 0 && n != 0) {
402
    if (buf[i] >= '0' && buf[i] <= '9') {
403
      --n;
404
      if (offset != 0)
405
        buf[i - offset] = buf[i];
406
    }
407
    else {
408
      if (offset != 0) break;
409
      ++offset;
410
      *pt = i;
411
    }
412
  }
413
  if (offset != 0)
414
    buf[i - offset] = 0;
415
  // Extract exponent part in point position:
416
  int e = 0;
417
  while (buf[++i] != 0) {
418
    if (buf[i] >= '0' && buf[i] <= '9') {
419
      e = e * 10 + (buf[i] - '0');
420
    }
421
  }
422
  *pt += e;
423
  return buf;
424
}
425
426
static char* _Stl_fcvtR(long double x, int n, int* pt, int* sign, char* buf) {
427
  // If long double value can be safely converted to double without losing precision
428
  // we use the fcvt function for double:
429
  double y = __STATIC_CAST(double, x);
430
  if (x == y)
431
    return _Stl_fcvtR(y, n, pt, sign, buf);
432
433
  char fmtbuf[32];
434
  __fill_fmtbuf(fmtbuf, ios_base::fixed, 'L');
435
  sprintf(buf, fmtbuf, n, x < 0.0l ? -x : x);
436
  *pt = 0;
437
  *sign = 0;
438
  int i = -1;
439
  int offset = 0;
440
  while (buf[++i] != 0 && (offset == 0 || n != 0)) {
441
    if (buf[i] >= '0' && buf[i] <= '9') {
442
      if (offset != 0) {
443
        --n;
444
        buf[i - offset] = buf[i];
445
      }
446
    }
447
    else {
448
      ++offset;
449
      *pt = i;
450
    }
451
  }
452
  if (offset != 0)
453
    buf[i - offset] = 0;
454
  else
455
    *pt = i;
456
  return buf;
457
}
458
#endif
459
460
//----------------------------------------------------------------------
461
// num_put
462
463
// __format_float formats a mantissa and exponent as returned by
464
// one of the conversion functions (ecvt_r, fcvt_r, qecvt_r, qfcvt_r)
465
// according to the specified precision and format flags.  This is
466
// based on doprnt but is much simpler since it is concerned only
467
// with floating point input and does not consider all formats.  It
468
// also does not deal with blank padding, which is handled by
469
// __copy_float_and_fill.
470
471
static size_t __format_float_scientific( __iostring& buf, const char *bp,
472
                                         int decpt, int sign, bool is_zero,
473
                                         ios_base::fmtflags flags,
474
                                         int precision) {
475
  // sign if required
476
  if (sign)
477
    buf += '-';
478
  else if (flags & ios_base::showpos)
479
    buf += '+';
480
481
  // first digit of mantissa
482
  buf += *bp++;
483
484
  // start of grouping position, grouping won't occur in scientific notation
485
  // as it is impossible to have something like 1234.0e04 but we return a correct
486
  // group position for coherency with __format_float_fixed.
487
  size_t __group_pos = buf.size();
488
489
  // decimal point if required
490
  if (precision != 0 || flags & ios_base::showpoint) {
491
    buf += '.';
492
  }
493
494
  // rest of mantissa
495
  while (*bp != 0 && precision--)
496
    buf += *bp++;
497
498
  // trailing 0 if needed
499
  if (precision > 0)
500
    buf.append(precision, '0');
501
502
  // exponent size = number of digits + exponent sign + exponent symbol + trailing zero
503
  char expbuf[MAXEDIGITS + 3];
504
  //We start filling at the buffer end
505
  char *suffix = expbuf + MAXEDIGITS + 2;
506
  *suffix = 0;
507
  if (!is_zero) {
508
    int nn = decpt - 1;
509
    if (nn < 0)
510
      nn = -nn;
511
    for (; nn > 9; nn /= 10)
512
      *--suffix = (char) todigit(nn % 10);
513
    *--suffix = (char) todigit(nn);
514
  }
515
516
  // prepend leading zeros to exponent
517
  // C89 Standard says that it should be at least 2 digits, C99 Standard says that
518
  // we stop prepend zeros if more than 3 digits. To repect both STLport prepend zeros
519
  // until it is 2 digits.
520
  while (suffix > &expbuf[MAXEDIGITS])
521
    *--suffix = '0';
522
523
  // put in the exponent sign
524
  *--suffix = (char) ((decpt > 0 || is_zero ) ? '+' : '-');
525
526
  // put in the e
527
  *--suffix = flags & ios_base::uppercase ? 'E' : 'e';
528
529
  // copy the suffix
530
  buf += suffix;
531
  return __group_pos;
532
}
533
534
static size_t __format_float_fixed( __iostring &buf, const char *bp,
535
                                    int decpt, int sign,
536
                                    ios_base::fmtflags flags,
537
                                    int precision) {
538
  if ( sign && (decpt > -precision) && (*bp != 0) )
539
    buf += '-';
540
  else if ( flags & ios_base::showpos )
541
    buf += '+';
542
543
  // digits before decimal point
544
  int nnn = decpt;
545
  do {
546
    buf += (nnn <= 0 || *bp == 0) ? '0' : *bp++;
547
  } while ( --nnn > 0 );
548
549
  // start of grouping position
550
  size_t __group_pos = buf.size();
551
552
  // decimal point if needed
553
  if ( flags & ios_base::showpoint || precision > 0 ) {
554
    buf += '.';
555
  }
556
557
  // digits after decimal point if any
558
  while ( *bp != 0 && --precision >= 0 ) {
559
    buf += (++decpt <= 0) ? '0' : *bp++;
560
  }
561
562
  // trailing zeros if needed
563
  if (precision > 0)
564
    buf.append(precision, '0');
565
566
  return __group_pos;
567
}
568
569
#if defined (_STLP_USE_SIGN_HELPER)
570
template<class _FloatT>
571
struct float_sign_helper {
572
  float_sign_helper(_FloatT __x)
573
  { _M_number._num = __x; }
574
575
  bool is_negative() const {
576
    const unsigned short sign_mask(1 << (sizeof(unsigned short) * CHAR_BIT - 1));
577
    return (get_sign_word() & sign_mask) != 0;
578
  }
579
private:
580
  union {
581
    unsigned short _Words[8];
582
    _FloatT _num;
583
  } _M_number;
584
585
  unsigned short get_word_higher() const _STLP_NOTHROW
586
  { return _M_number._Words[0]; }
587
  unsigned short get_word_lower() const _STLP_NOTHROW
588
  { return _M_number._Words[(sizeof(_FloatT) >= 12 ? 10 : sizeof(_FloatT)) / sizeof(unsigned short) - 1]; }
589
  unsigned short get_sign_word() const _STLP_NOTHROW
590
#  if defined (_STLP_BIG_ENDIAN)
591
  { return get_word_higher(); }
592
#  else /* _STLP_LITTLE_ENDIAN */
593
  { return get_word_lower(); }
594
#  endif
595
};
596
#endif
597
598
template <class _FloatT>
599
static size_t __format_nan_or_inf(__iostring& buf, _FloatT x, ios_base::fmtflags flags) {
600
  static const char* inf[2] = { "inf", "Inf" };
601
  static const char* nan[2] = { "nan", "NaN" };
602
  const char** inf_or_nan;
603
#if !defined (_STLP_USE_SIGN_HELPER)
604
  if (_Stl_is_inf(x)) {            // Infinity
605
    inf_or_nan = inf;
606
    if (_Stl_is_neg_inf(x))
607
      buf += '-';
608
    else if (flags & ios_base::showpos)
609
      buf += '+';
610
  } else {                      // NaN
611
    inf_or_nan = nan;
612
    if (_Stl_is_neg_nan(x))
613
      buf += '-';
614
    else if (flags & ios_base::showpos)
615
      buf += '+';
616
  }
617
#else
618
  typedef numeric_limits<_FloatT> limits;
619
  if (x == limits::infinity() || x == -limits::infinity()) {
620
    inf_or_nan = inf;
621
  } else {                    // NaN
622
    inf_or_nan = nan;
623
  }
624
  float_sign_helper<_FloatT> helper(x);
625
  if (helper.is_negative())
626
    buf += '-';
627
  else if (flags & ios_base::showpos)
628
    buf += '+';
629
#endif
630
  size_t ret = buf.size();
631
  buf += inf_or_nan[flags & ios_base::uppercase ? 1 : 0];
632
  return ret;
633
}
634
635
static inline size_t __format_float(__iostring &buf, const char * bp,
636
                                    int decpt, int sign, bool is_zero,
637
                                    ios_base::fmtflags flags,
638
                                    int precision) {
639
  size_t __group_pos = 0;
640
  switch (flags & ios_base::floatfield) {
641
    case ios_base::scientific:
642
      __group_pos = __format_float_scientific( buf, bp, decpt, sign, is_zero,
643
                                               flags, precision);
644
      break;
645
    case ios_base::fixed:
646
      __group_pos = __format_float_fixed( buf, bp, decpt, sign,
647
                                          flags, precision);
648
      break;
649
    default: // g format
650
      // establish default precision
651
      if (flags & ios_base::showpoint || precision > 0) {
652
        if (precision == 0) precision = 1;
653
      } else
654
        precision = 6;
655
656
      // reset exponent if value is zero
657
      if (is_zero)
658
        decpt = 1;
659
660
      int kk = precision;
661
      if (!(flags & ios_base::showpoint)) {
662
        size_t n = strlen(bp);
663
        if (n < (size_t)kk)
664
          kk = (int)n;
665
        while (kk >= 1 && bp[kk-1] == '0')
666
          --kk;
667
      }
668
669
      if (decpt < -3 || decpt > precision) {
670
        precision = kk - 1;
671
        __group_pos = __format_float_scientific( buf, bp, decpt, sign, is_zero,
672
                                                 flags, precision);
673
      } else {
674
        precision = kk - decpt;
675
        __group_pos = __format_float_fixed( buf, bp, decpt, sign,
676
                                            flags, precision);
677
      }
678
      break;
679
  } /* switch */
680
  return __group_pos;
681
}
682
683
#endif
684
685
#if defined (USE_SPRINTF_INSTEAD) || defined (_STLP_EMULATE_LONG_DOUBLE_CVT)
686
struct GroupPos {
687
  bool operator () (char __c) const {
688
    return __c == '.' ||
689
           __c == 'e' || __c == 'E';
690
  }
691
};
692
693
// Creates a format string for sprintf()
694
static void __fill_fmtbuf(char* fmtbuf, ios_base::fmtflags flags, char long_modifier) {
695
  fmtbuf[0] = '%';
696
  int i = 1;
697
698
  if (flags & ios_base::showpos)
699
    fmtbuf[i++] = '+';
700
701
  if (flags & ios_base::showpoint)
702
    fmtbuf[i++] = '#';
703
704
  fmtbuf[i++] = '.';
705
  fmtbuf[i++] = '*';
706
707
  if (long_modifier)
708
    fmtbuf[i++] = long_modifier;
709
710
  switch (flags & ios_base::floatfield)
711
    {
712
    case ios_base::scientific:
713
      fmtbuf[i++] = (flags & ios_base::uppercase) ?  'E' : 'e';
714
      break;
715
    case ios_base::fixed:
716
#  if defined (__FreeBSD__)
717
      fmtbuf[i++] = 'f';
718
#  else
719
      fmtbuf[i++] = (flags & ios_base::uppercase) ? 'F' : 'f';
720
#  endif
721
      break;
722
    default:
723
      fmtbuf[i++] = (flags & ios_base::uppercase) ?  'G' : 'g';
724
      break;
725
    }
726
727
  fmtbuf[i] = 0;
728
}
729
730
#endif  /* USE_SPRINTF_INSTEAD */
731
732
template <class _FloatT>
733
static size_t  __write_floatT(__iostring &buf, ios_base::fmtflags flags, int precision,
734
                              _FloatT x
735
#if defined (USE_SPRINTF_INSTEAD)
736
                              , char modifier) {
737
  /* In theory, if we want 'arbitrary' precision, we should use 'arbitrary'
738
   * buffer size below, but really we limited by exponent part in double.
739
   *    - ptr
740
   */
741
  typedef numeric_limits<_FloatT> limits;
742
  char static_buf[limits::max_exponent10 + 6]; // 6: -xxx.yyyE-zzz (sign, dot, E, exp sign, \0)
743
  char fmtbuf[32];
744
  __fill_fmtbuf(fmtbuf, flags, modifier);
745
  snprintf(_STLP_ARRAY_AND_SIZE(static_buf), fmtbuf, precision, x);
746
  buf = static_buf;
747
  return find_if(buf.begin(), buf.end(), GroupPos()) - buf.begin();
748
#else
749
                              ) {
750
  typedef numeric_limits<_FloatT> limits;
751
  //If numeric_limits support is correct we use the exposed values to detect NaN and infinity:
752
  if (limits::has_infinity && limits::has_quiet_NaN) {
753
    if (!(x == x) || // NaN check
754
        (x == limits::infinity() || x == -limits::infinity())) {
755
      return __format_nan_or_inf(buf, x, flags);
756
    }
757
  }
758
  // numeric_limits support is not good enough, we rely on platform dependent function
759
  // _Stl_is_nan_or_inf that do not support long double.
760
  else if (_Stl_is_nan_or_inf(x)) {
761
    return __format_nan_or_inf(buf, x, flags);
762
  }
763
#  if defined (__MINGW32__)
764
  //For the moment MinGW is limited to display at most numeric_limits<double>::max()
765
  if (x > numeric_limits<double>::max() ||
766
      x < -numeric_limits<double>::max()) {
767
    return __format_nan_or_inf(buf, x, flags);
768
  }
769
#  endif
770
771
  /* Buffer size is max number of digits which is the addition of:
772
   * - max_exponent10: max number of digits in fixed mode
773
   * - digits10 + 2: max number of significant digits
774
   * - trailing '\0'
775
   */
776
  char cvtbuf[limits::max_exponent10 + limits::digits10 + 2 + 1];
777
  char *bp;
778
  int decpt, sign;
779
780
  switch (flags & ios_base::floatfield) {
781
  case ios_base::fixed:
782
    {
783
      /* Here, number of digits represents digits _after_ decimal point.
784
       * In order to limit static buffer size we have to give 2 different values depending on x value. 
785
       * For small values (abs(x) < 1) we need as many digits as requested by precision limited by the maximum number of digits
786
       * which is min_exponent10 + digits10 + 2
787
       * For bigger values we won't have more than limits::digits10 + 2 digits after decimal point. */
788
      int digits10 = (x > -1.0 && x < 1.0 ? -limits::min_exponent10 + limits::digits10 + 2
789
                                          : limits::digits10 + 2);
790
      bp = _Stl_fcvtR(x, (min) (precision, digits10), &decpt, &sign, _STLP_CVT_BUFFER(cvtbuf) );
791
    }
792
    break;
793
  case ios_base::scientific:
794
  default:
795
    /* Here, number of digits is total number of digits which is limited to digits10 + 2. */
796
    {
797
      int digits10 = limits::digits10 + 2;
798
      bp = _Stl_ecvtR(x, (min) (precision, digits10), &decpt, &sign, _STLP_CVT_BUFFER(cvtbuf) );
799
    }
800
    break;
801
  }
802
  return __format_float(buf, bp, decpt, sign, x == 0.0, flags, precision);
803
#endif
804
}
805
806
size_t  _STLP_CALL
807
__write_float(__iostring &buf, ios_base::fmtflags flags, int precision,
808
              double x) {
809
  return __write_floatT(buf, flags, precision, x
810
#if defined (USE_SPRINTF_INSTEAD)
811
                                               , 0
812
#endif
813
                                                  );
814
}
815
816
#if !defined (_STLP_NO_LONG_DOUBLE)
817
size_t _STLP_CALL
818
__write_float(__iostring &buf, ios_base::fmtflags flags, int precision,
819
              long double x) {
820
  return __write_floatT(buf, flags, precision, x
821
#if defined (USE_SPRINTF_INSTEAD)
822
                                               , 'L'
823
#endif
824
                                                    );
825
}
826
#endif
827
828
void _STLP_CALL __get_floor_digits(__iostring &out, _STLP_LONGEST_FLOAT_TYPE __x) {
829
  typedef numeric_limits<_STLP_LONGEST_FLOAT_TYPE> limits;
830
#if defined (USE_SPRINTF_INSTEAD)
831
  char cvtbuf[limits::max_exponent10 + 6];
832
#  if !defined (_STLP_NO_LONG_DOUBLE)
833
  snprintf(_STLP_ARRAY_AND_SIZE(cvtbuf), "%Lf", __x); // check for 1234.56!
834
#  else
835
  snprintf(_STLP_ARRAY_AND_SIZE(cvtbuf), "%f", __x);  // check for 1234.56!
836
#  endif
837
  char *p = strchr( cvtbuf, '.' );
838
  if ( p == 0 ) {
839
    out.append( cvtbuf );
840
  } else {
841
    out.append( cvtbuf, p );
842
  }
843
#else
844
  char cvtbuf[limits::max_exponent10 + 1];
845
  char * bp;
846
  int decpt, sign;
847
  bp = _Stl_fcvtR(__x, 0, &decpt, &sign, _STLP_CVT_BUFFER(cvtbuf));
848
849
  if (sign) {
850
    out += '-';
851
  }
852
  out.append(bp, bp + decpt);
853
#endif
854
}
855
856
857
#if !defined (_STLP_NO_WCHAR_T)
858
void _STLP_CALL __convert_float_buffer( __iostring const& str, __iowstring &out,
859
                                        const ctype<wchar_t>& ct, wchar_t dot, bool __check_dot) {
860
  string::const_iterator str_ite(str.begin()), str_end(str.end());
861
862
  //First loop, check the dot char
863
  if (__check_dot) {
864
    while (str_ite != str_end) {
865
      if (*str_ite != '.') {
866
        out += ct.widen(*str_ite++);
867
      } else {
868
        out += dot;
869
        break;
870
      }
871
    }
872
  } else {
873
    if (str_ite != str_end) {
874
      out += ct.widen(*str_ite);
875
    }
876
  }
877
878
  if (str_ite != str_end) {
879
    //Second loop, dot has been found, no check anymore
880
    while (++str_ite != str_end) {
881
      out += ct.widen(*str_ite);
882
    }
883
  }
884
}
885
886
#endif
887
888
void _STLP_CALL
889
__adjust_float_buffer(__iostring &str, char dot) {
890
  if ('.' != dot) {
891
    size_t __dot_pos = str.find('.');
892
    if (__dot_pos != string::npos) {
893
      str[__dot_pos] = dot;
894
    }
895
  }
896
}
897
898
_STLP_MOVE_TO_STD_NAMESPACE
899
_STLP_END_NAMESPACE
900
901
// Local Variables:
902
// mode:C++
903
// End: