build/stlport/src/num_put_float.cpp

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

     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  */
    19 #include "stlport_prefix.h"
    21 #include <cmath>
    22 #include <ios>
    23 #include <locale>
    25 #if defined (__DECCXX)
    26 #  define NDIG 400
    27 #else
    28 #  define NDIG 82
    29 #endif
    31 #define todigit(x) ((x)+'0')
    33 #if defined (_STLP_UNIX)
    35 #  if defined (__sun)
    36 #    include <floatingpoint.h>
    37 #  endif
    39 #  if defined (__sun) || defined (__digital__) || defined (__sgi) || defined (_STLP_SCO_OPENSERVER) || defined (__NCR_SVR)
    40 // DEC, SGI & Solaris need this
    41 #    include <values.h>
    42 #    include <nan.h>
    43 #  endif
    45 #  if defined (__QNXNTO__) || ( defined(__GNUC__) && defined(__APPLE__) ) || defined(_STLP_USE_UCLIBC) /* 0.9.26 */ || \
    46       defined(__FreeBSD__)
    47 #    define USE_SPRINTF_INSTEAD
    48 #  endif
    50 #  if defined (_AIX) // JFA 3-Aug-2000
    51 #    include <math.h>
    52 #    include <float.h>
    53 #  endif
    55 #  include <math.h>
    56 #endif
    58 #include <cstdio>
    59 #include <cstdlib>
    61 #if defined (_STLP_MSVC_LIB) || defined (__MINGW32__) || defined (__BORLANDC__) || defined (__DJGPP) || \
    62     defined (_STLP_SCO_OPENSERVER) || defined (__NCR_SVR)
    63 #  include <float.h>
    64 #endif
    66 #if defined (__MRC__) || defined (__SC__)  || defined (_CRAY)  //*TY 02/24/2000 - added support for MPW
    67 #  include <fp.h>
    68 #endif
    70 #if defined (__CYGWIN__)
    71 #  include <ieeefp.h>
    72 #endif
    74 #if defined (__MSL__)
    75 #  include <cstdlib>  // for atoi
    76 #  include <cstdio>  // for snprintf
    77 #  include <algorithm>
    78 #  include <cassert>
    79 #endif
    81 #if defined (__ISCPP__)
    82 #  include <cfloat>
    83 #endif
    85 #include <algorithm>
    87 #if defined (__DMC__)
    88 #  define snprintf _snprintf
    89 #endif
    91 _STLP_BEGIN_NAMESPACE
    93 _STLP_MOVE_TO_PRIV_NAMESPACE
    95 #if defined (__MWERKS__) || defined(__BEOS__)
    96 #  define USE_SPRINTF_INSTEAD
    97 #endif
    99 template <int N>
   100 struct _Dig
   101 {
   102     enum { dig = _Dig<N/10>::dig + 1 };
   103 };
   105 _STLP_TEMPLATE_NULL
   106 struct _Dig<0>
   107 {
   108     enum { dig = 0 };
   109 };
   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)
   115 #else
   116 # define MAXEDIGITS int(_Dig<LDBL_MAX_10_EXP>::dig)
   117 # define MAXFSIG LDBL_DIG
   118 # define MAXFCVT (LDBL_DIG + 1)
   119 #endif
   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__) || \
   129       defined (__HP_aCC)
   130 static inline bool _Stl_is_nan_or_inf(double x)
   131 #    if defined (isfinite)
   132 { return !isfinite(x); }
   133 #    else
   134 { return !finite(x); }
   135 #    endif
   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;
   153 }
   154 static inline bool _Stl_is_neg_inf(double x)    { return _fpclass(x) == _FPCLASS_NINF; }
   155 #    else
   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 ; }
   158 #    endif
   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)); }
   171 #    endif
   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); }
   199 #    else
   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; }
   204 #    endif
   205 #  else // nothing from above
   206 #    define USE_SPRINTF_INSTEAD
   207 #  endif
   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
   223 #      else
   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); }
   228 #      endif
   229 #    endif
   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; }
   240 #    endif
   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); }
   252 #    endif
   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); }
   264 #    endif
   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); }
   276 #    endif
   277 #    define _STLP_CVT_NEED_SYNCHRONIZATION
   278 #  elif defined (__unix) && !defined (__APPLE__) && !defined (_CRAY) && \
   279         !defined (__ANDROID__)
   280 static inline char* _Stl_ecvtR(double x, int n, int* pt, int* sign, char* buf)
   281 { return ecvt_r(x, n, pt, sign, buf); }
   282 static inline char* _Stl_fcvtR(double x, int n, int* pt, int* sign, char* buf)
   283 { return fcvt_r(x, n, pt, sign, buf); }
   284 #    if !defined (_STLP_NO_LONG_DOUBLE)
   285 static inline char* _Stl_ecvtR(long double x, int n, int* pt, int* sign, char* buf)
   286 { return qecvt_r(x, n, pt, sign, buf); }
   287 static inline char* _Stl_fcvtR(long double x, int n, int* pt, int* sign, char* buf)
   288 { return qfcvt_r(x, n, pt, sign, buf); }
   289 #    endif
   290 #  elif defined (_STLP_MSVC_LIB) || defined (__MINGW32__) || defined (__BORLANDC__)
   291 #    if defined (_STLP_USE_SAFE_STRING_FUNCTIONS)
   292 #      define _STLP_APPEND(a, b) a##b
   293 #      define _STLP_BUF_PARAMS , char* buf, size_t bsize
   294 #      define _STLP_SECURE_FUN(F, X, N, PT, SIGN) _STLP_APPEND(F, _s)(buf, bsize, X, N, PT, SIGN); return buf
   295 #    else
   296 #      define _STLP_BUF_PARAMS
   297 #      define _STLP_SECURE_FUN(F, X, N, PT, SIGN) return F(X, N, PT, SIGN)
   298 #      define _STLP_CVT_NEED_SYNCHRONIZATION
   299 #    endif
   300 static inline char* _Stl_ecvtR(double x, int n, int* pt, int* sign _STLP_BUF_PARAMS)
   301 { _STLP_SECURE_FUN(_ecvt, x, n, pt, sign); }
   302 static inline char* _Stl_fcvtR(double x, int n, int* pt, int* sign _STLP_BUF_PARAMS)
   303 { _STLP_SECURE_FUN(_fcvt, x, n, pt, sign); }
   304 #    if !defined (_STLP_NO_LONG_DOUBLE)
   305 #      if defined (_STLP_USE_SAFE_STRING_FUNCTIONS)
   306 #        define _STLP_PARAMS , buf, bsize
   307 #      else
   308 #        define _STLP_PARAMS
   309 #      endif
   310 static inline char* _Stl_ecvtR(long double x, int n, int* pt, int* sign _STLP_BUF_PARAMS)
   311 { return _Stl_ecvtR(__STATIC_CAST(double, x), n, pt, sign _STLP_PARAMS); }
   312 static inline char* _Stl_fcvtR(long double x, int n, int* pt, int* sign _STLP_BUF_PARAMS)
   313 { return _Stl_fcvtR(__STATIC_CAST(double, x), n, pt, sign _STLP_PARAMS); }
   314 #      undef _STLP_PARAMS
   315 #    endif
   316 #    undef _STLP_SECURE_FUN
   317 #    undef _STLP_BUF_PARAMS
   318 #    undef _STLP_APPEND
   319 #    if defined (__BORLANDC__) /* || defined (__GNUC__) MinGW do not support 'L' modifier so emulation do not work */
   320 #      define _STLP_EMULATE_LONG_DOUBLE_CVT
   321 #    endif
   322 #  elif defined (__ISCPP__)
   323 static inline char* _Stl_ecvtR(double x, int n, int* pt, int* sign, char* buf)
   324 { return _fp_ecvt( x, n, pt, sign, buf); }
   325 static inline char* _Stl_fcvtR(double x, int n, int* pt, int* sign, char* buf)
   326 { return _fp_fcvt(x, n, pt, sign, buf); }
   327 #    if !defined (_STLP_NO_LONG_DOUBLE)
   328 static inline char* _Stl_ecvtR(long double x, int n, int* pt, int* sign, char* buf)
   329 { return _fp_ecvt( x, n, pt, sign, buf); }
   330 static inline char* _Stl_fcvtR(long double x, int n, int* pt, int* sign, char* buf)
   331 { return _fp_fcvt(x, n, pt, sign, buf); }
   332 #    endif
   333 #  elif defined (_AIX) || defined (__FreeBSD__) || defined (__NetBSD__) || defined (__OpenBSD__) || \
   334         defined (__MRC__) || defined (__SC__) || defined (_CRAY) || \
   335         defined (_STLP_SCO_OPENSERVER) || defined (__NCR_SVR) || \
   336         defined (__DMC__)
   337 static inline char* _Stl_ecvtR(double x, int n, int* pt, int* sign)
   338 { return ecvt(x, n, pt, sign ); }
   339 static inline char* _Stl_fcvtR(double x, int n, int* pt, int* sign)
   340 { return fcvt(x, n, pt, sign); }
   341 #    if !defined (_STLP_NO_LONG_DOUBLE)
   342 static inline char* _Stl_ecvtR(long double x, int n, int* pt, int* sign)
   343 { return ecvt(x, n, pt, sign ); }
   344 static inline char* _Stl_fcvtR(long double x, int n, int* pt, int* sign)
   345 { return fcvt(x, n, pt, sign); }
   346 #    endif
   347 #    define _STLP_CVT_NEED_SYNCHRONIZATION
   348 #  else
   349 #    error Missing _Stl_ecvtR and _Stl_fcvtR implementations.
   350 #  endif
   352 #if defined (_STLP_CVT_NEED_SYNCHRONIZATION)
   353 /* STLport synchronize access to *cvt functions but those methods might
   354  * be called from outside, in this case we will still have a race condition. */
   355 #  if defined (_STLP_THREADS)
   356 static _STLP_STATIC_MUTEX& put_float_mutex() {
   357   static _STLP_STATIC_MUTEX __put_float_mutex _STLP_MUTEX_INITIALIZER;
   358   return __put_float_mutex;
   359 }
   360 static inline char* _Stl_ecvtR(double x, int n, int* pt, int* sign, char* buf) {
   361   _STLP_auto_lock lock(put_float_mutex());
   362   strcpy(buf, _Stl_ecvtR(x, n, pt, sign)); return buf;
   363 }
   364 static inline char* _Stl_fcvtR(double x, int n, int* pt, int* sign, char* buf) {
   365   _STLP_auto_lock lock(put_float_mutex());
   366   strcpy(buf, _Stl_fcvtR(x, n, pt, sign)); return buf;
   367 }
   368 #    if !defined (_STLP_NO_LONG_DOUBLE) && !defined (_STLP_EMULATE_LONG_DOUBLE_CVT)
   369 static inline char* _Stl_ecvtR(long double x, int n, int* pt, int* sign, char* buf) {
   370   _STLP_auto_lock lock(put_float_mutex());
   371   strcpy(buf, _Stl_ecvtR(x, n, pt, sign)); return buf;
   372 }
   373 static inline char* _Stl_fcvtR(long double x, int n, int* pt, int* sign, char* buf) {
   374   _STLP_auto_lock lock(put_float_mutex());
   375   strcpy(buf, _Stl_fcvtR(x, n, pt, sign)); return buf;
   376 }
   377 #    endif
   378 #  else
   379 static inline char* _Stl_ecvtR(double x, int n, int* pt, int* sign, char*)
   380 { return _Stl_ecvtR(x, n, pt, sign); }
   381 static inline char* _Stl_fcvtR(double x, int n, int* pt, int* sign, char*)
   382 { return _Stl_fcvtR(x, n, pt, sign); }
   383 #    if !defined (_STLP_NO_LONG_DOUBLE) && !defined (_STLP_EMULATE_LONG_DOUBLE_CVT)
   384 static inline char* _Stl_ecvtR(long double x, int n, int* pt, int* sign, char*)
   385 { return _Stl_ecvtR(x, n, pt, sign); }
   386 static inline char* _Stl_fcvtR(long double x, int n, int* pt, int* sign, char*)
   387 { return _Stl_fcvtR(x, n, pt, sign); }
   388 #    endif
   389 #  endif
   390 #endif
   392 #  if !defined (_STLP_USE_SAFE_STRING_FUNCTIONS) && !defined (_STLP_NEED_CVT_BUFFER_SIZE)
   393 #    define _STLP_CVT_BUFFER(B) B
   394 #  else
   395 #    define _STLP_CVT_BUFFER(B) _STLP_ARRAY_AND_SIZE(B)
   396 #  endif
   398 #  if defined (_STLP_EMULATE_LONG_DOUBLE_CVT)
   399 static void __fill_fmtbuf(char* fmtbuf, ios_base::fmtflags flags, char long_modifier);
   401 // Emulation of ecvt/fcvt functions using sprintf:
   402 static char* _Stl_ecvtR(long double x, int n, int* pt, int* sign, char* buf) {
   403   // If long double value can be safely converted to double without losing precision
   404   // we use the ecvt function for double:
   405   double y = __STATIC_CAST(double, x); 
   406   if (x == y)
   407     return _Stl_ecvtR(y, n, pt, sign, buf);
   409   char fmtbuf[32];
   410   __fill_fmtbuf(fmtbuf, 0, 'L');
   411   sprintf(buf, fmtbuf, n, x < 0.0l ? -x : x);
   412   /* We are waiting for something having the form x.xxxe+yyyy */
   413   *pt = 0;
   414   *sign = 0;
   415   int i = -1;
   416   int offset = 0;
   417   while (buf[++i] != 0 && n != 0) {
   418     if (buf[i] >= '0' && buf[i] <= '9') {
   419       --n;
   420       if (offset != 0)
   421         buf[i - offset] = buf[i];
   422     }
   423     else {
   424       if (offset != 0) break;
   425       ++offset;
   426       *pt = i;
   427     }
   428   }
   429   if (offset != 0)
   430     buf[i - offset] = 0;
   431   // Extract exponent part in point position:
   432   int e = 0;
   433   while (buf[++i] != 0) {
   434     if (buf[i] >= '0' && buf[i] <= '9') {
   435       e = e * 10 + (buf[i] - '0');
   436     }
   437   }
   438   *pt += e;
   439   return buf;
   440 }
   442 static char* _Stl_fcvtR(long double x, int n, int* pt, int* sign, char* buf) {
   443   // If long double value can be safely converted to double without losing precision
   444   // we use the fcvt function for double:
   445   double y = __STATIC_CAST(double, x);
   446   if (x == y)
   447     return _Stl_fcvtR(y, n, pt, sign, buf);
   449   char fmtbuf[32];
   450   __fill_fmtbuf(fmtbuf, ios_base::fixed, 'L');
   451   sprintf(buf, fmtbuf, n, x < 0.0l ? -x : x);
   452   *pt = 0;
   453   *sign = 0;
   454   int i = -1;
   455   int offset = 0;
   456   while (buf[++i] != 0 && (offset == 0 || n != 0)) {
   457     if (buf[i] >= '0' && buf[i] <= '9') {
   458       if (offset != 0) {
   459         --n;
   460         buf[i - offset] = buf[i];
   461       }
   462     }
   463     else {
   464       ++offset;
   465       *pt = i;
   466     }
   467   }
   468   if (offset != 0)
   469     buf[i - offset] = 0;
   470   else
   471     *pt = i;
   472   return buf;
   473 }
   474 #endif
   476 //----------------------------------------------------------------------
   477 // num_put
   479 // __format_float formats a mantissa and exponent as returned by
   480 // one of the conversion functions (ecvt_r, fcvt_r, qecvt_r, qfcvt_r)
   481 // according to the specified precision and format flags.  This is
   482 // based on doprnt but is much simpler since it is concerned only
   483 // with floating point input and does not consider all formats.  It
   484 // also does not deal with blank padding, which is handled by
   485 // __copy_float_and_fill.
   487 static size_t __format_float_scientific( __iostring& buf, const char *bp,
   488                                          int decpt, int sign, bool is_zero,
   489                                          ios_base::fmtflags flags,
   490                                          int precision) {
   491   // sign if required
   492   if (sign)
   493     buf += '-';
   494   else if (flags & ios_base::showpos)
   495     buf += '+';
   497   // first digit of mantissa
   498   buf += *bp++;
   500   // start of grouping position, grouping won't occur in scientific notation
   501   // as it is impossible to have something like 1234.0e04 but we return a correct
   502   // group position for coherency with __format_float_fixed.
   503   size_t __group_pos = buf.size();
   505   // decimal point if required
   506   if (precision != 0 || flags & ios_base::showpoint) {
   507     buf += '.';
   508   }
   510   // rest of mantissa
   511   while (*bp != 0 && precision--)
   512     buf += *bp++;
   514   // trailing 0 if needed
   515   if (precision > 0)
   516     buf.append(precision, '0');
   518   // exponent size = number of digits + exponent sign + exponent symbol + trailing zero
   519   char expbuf[MAXEDIGITS + 3];
   520   //We start filling at the buffer end
   521   char *suffix = expbuf + MAXEDIGITS + 2;
   522   *suffix = 0;
   523   if (!is_zero) {
   524     int nn = decpt - 1;
   525     if (nn < 0)
   526       nn = -nn;
   527     for (; nn > 9; nn /= 10)
   528       *--suffix = (char) todigit(nn % 10);
   529     *--suffix = (char) todigit(nn);
   530   }
   532   // prepend leading zeros to exponent
   533   // C89 Standard says that it should be at least 2 digits, C99 Standard says that
   534   // we stop prepend zeros if more than 3 digits. To repect both STLport prepend zeros
   535   // until it is 2 digits.
   536   while (suffix > &expbuf[MAXEDIGITS])
   537     *--suffix = '0';
   539   // put in the exponent sign
   540   *--suffix = (char) ((decpt > 0 || is_zero ) ? '+' : '-');
   542   // put in the e
   543   *--suffix = flags & ios_base::uppercase ? 'E' : 'e';
   545   // copy the suffix
   546   buf += suffix;
   547   return __group_pos;
   548 }
   550 static size_t __format_float_fixed( __iostring &buf, const char *bp,
   551                                     int decpt, int sign,
   552                                     ios_base::fmtflags flags,
   553                                     int precision) {
   554   if ( sign && (decpt > -precision) && (*bp != 0) )
   555     buf += '-';
   556   else if ( flags & ios_base::showpos )
   557     buf += '+';
   559   // digits before decimal point
   560   int nnn = decpt;
   561   do {
   562     buf += (nnn <= 0 || *bp == 0) ? '0' : *bp++;
   563   } while ( --nnn > 0 );
   565   // start of grouping position
   566   size_t __group_pos = buf.size();
   568   // decimal point if needed
   569   if ( flags & ios_base::showpoint || precision > 0 ) {
   570     buf += '.';
   571   }
   573   // digits after decimal point if any
   574   while ( *bp != 0 && --precision >= 0 ) {
   575     buf += (++decpt <= 0) ? '0' : *bp++;
   576   }
   578   // trailing zeros if needed
   579   if (precision > 0)
   580     buf.append(precision, '0');
   582   return __group_pos;
   583 }
   585 #if defined (_STLP_USE_SIGN_HELPER)
   586 template<class _FloatT>
   587 struct float_sign_helper {
   588   float_sign_helper(_FloatT __x)
   589   { _M_number._num = __x; }
   591   bool is_negative() const {
   592     const unsigned short sign_mask(1 << (sizeof(unsigned short) * CHAR_BIT - 1));
   593     return (get_sign_word() & sign_mask) != 0;
   594   }
   595 private:
   596   union {
   597     unsigned short _Words[8];
   598     _FloatT _num;
   599   } _M_number;
   601   unsigned short get_word_higher() const _STLP_NOTHROW
   602   { return _M_number._Words[0]; }
   603   unsigned short get_word_lower() const _STLP_NOTHROW
   604   { return _M_number._Words[(sizeof(_FloatT) >= 12 ? 10 : sizeof(_FloatT)) / sizeof(unsigned short) - 1]; }
   605   unsigned short get_sign_word() const _STLP_NOTHROW
   606 #  if defined (_STLP_BIG_ENDIAN)
   607   { return get_word_higher(); }
   608 #  else /* _STLP_LITTLE_ENDIAN */
   609   { return get_word_lower(); }
   610 #  endif
   611 };
   612 #endif
   614 template <class _FloatT>
   615 static size_t __format_nan_or_inf(__iostring& buf, _FloatT x, ios_base::fmtflags flags) {
   616   static const char* inf[2] = { "inf", "Inf" };
   617   static const char* nan[2] = { "nan", "NaN" };
   618   const char** inf_or_nan;
   619 #if !defined (_STLP_USE_SIGN_HELPER)
   620   if (_Stl_is_inf(x)) {            // Infinity
   621     inf_or_nan = inf;
   622     if (_Stl_is_neg_inf(x))
   623       buf += '-';
   624     else if (flags & ios_base::showpos)
   625       buf += '+';
   626   } else {                      // NaN
   627     inf_or_nan = nan;
   628     if (_Stl_is_neg_nan(x))
   629       buf += '-';
   630     else if (flags & ios_base::showpos)
   631       buf += '+';
   632   }
   633 #else
   634   typedef numeric_limits<_FloatT> limits;
   635   if (x == limits::infinity() || x == -limits::infinity()) {
   636     inf_or_nan = inf;
   637   } else {                    // NaN
   638     inf_or_nan = nan;
   639   }
   640   float_sign_helper<_FloatT> helper(x);
   641   if (helper.is_negative())
   642     buf += '-';
   643   else if (flags & ios_base::showpos)
   644     buf += '+';
   645 #endif
   646   size_t ret = buf.size();
   647   buf += inf_or_nan[flags & ios_base::uppercase ? 1 : 0];
   648   return ret;
   649 }
   651 static inline size_t __format_float(__iostring &buf, const char * bp,
   652                                     int decpt, int sign, bool is_zero,
   653                                     ios_base::fmtflags flags,
   654                                     int precision) {
   655   size_t __group_pos = 0;
   656   switch (flags & ios_base::floatfield) {
   657     case ios_base::scientific:
   658       __group_pos = __format_float_scientific( buf, bp, decpt, sign, is_zero,
   659                                                flags, precision);
   660       break;
   661     case ios_base::fixed:
   662       __group_pos = __format_float_fixed( buf, bp, decpt, sign,
   663                                           flags, precision);
   664       break;
   665     default: // g format
   666       // establish default precision
   667       if (flags & ios_base::showpoint || precision > 0) {
   668         if (precision == 0) precision = 1;
   669       } else
   670         precision = 6;
   672       // reset exponent if value is zero
   673       if (is_zero)
   674         decpt = 1;
   676       int kk = precision;
   677       if (!(flags & ios_base::showpoint)) {
   678         size_t n = strlen(bp);
   679         if (n < (size_t)kk)
   680           kk = (int)n;
   681         while (kk >= 1 && bp[kk-1] == '0')
   682           --kk;
   683       }
   685       if (decpt < -3 || decpt > precision) {
   686         precision = kk - 1;
   687         __group_pos = __format_float_scientific( buf, bp, decpt, sign, is_zero,
   688                                                  flags, precision);
   689       } else {
   690         precision = kk - decpt;
   691         __group_pos = __format_float_fixed( buf, bp, decpt, sign,
   692                                             flags, precision);
   693       }
   694       break;
   695   } /* switch */
   696   return __group_pos;
   697 }
   699 #endif
   701 #if defined (USE_SPRINTF_INSTEAD) || defined (_STLP_EMULATE_LONG_DOUBLE_CVT)
   702 struct GroupPos {
   703   bool operator () (char __c) const {
   704     return __c == '.' ||
   705            __c == 'e' || __c == 'E';
   706   }
   707 };
   709 // Creates a format string for sprintf()
   710 static void __fill_fmtbuf(char* fmtbuf, ios_base::fmtflags flags, char long_modifier) {
   711   fmtbuf[0] = '%';
   712   int i = 1;
   714   if (flags & ios_base::showpos)
   715     fmtbuf[i++] = '+';
   717   if (flags & ios_base::showpoint)
   718     fmtbuf[i++] = '#';
   720   fmtbuf[i++] = '.';
   721   fmtbuf[i++] = '*';
   723   if (long_modifier)
   724     fmtbuf[i++] = long_modifier;
   726   switch (flags & ios_base::floatfield)
   727     {
   728     case ios_base::scientific:
   729       fmtbuf[i++] = (flags & ios_base::uppercase) ?  'E' : 'e';
   730       break;
   731     case ios_base::fixed:
   732 #  if defined (__FreeBSD__)
   733       fmtbuf[i++] = 'f';
   734 #  else
   735       fmtbuf[i++] = (flags & ios_base::uppercase) ? 'F' : 'f';
   736 #  endif
   737       break;
   738     default:
   739       fmtbuf[i++] = (flags & ios_base::uppercase) ?  'G' : 'g';
   740       break;
   741     }
   743   fmtbuf[i] = 0;
   744 }
   746 #endif  /* USE_SPRINTF_INSTEAD */
   748 template <class _FloatT>
   749 static size_t  __write_floatT(__iostring &buf, ios_base::fmtflags flags, int precision,
   750                               _FloatT x
   751 #if defined (USE_SPRINTF_INSTEAD)
   752                               , char modifier) {
   753   /* In theory, if we want 'arbitrary' precision, we should use 'arbitrary'
   754    * buffer size below, but really we limited by exponent part in double.
   755    *    - ptr
   756    */
   757   typedef numeric_limits<_FloatT> limits;
   758   char static_buf[limits::max_exponent10 + 6]; // 6: -xxx.yyyE-zzz (sign, dot, E, exp sign, \0)
   759   char fmtbuf[32];
   760   __fill_fmtbuf(fmtbuf, flags, modifier);
   761   snprintf(_STLP_ARRAY_AND_SIZE(static_buf), fmtbuf, precision, x);
   762   buf = static_buf;
   763   return find_if(buf.begin(), buf.end(), GroupPos()) - buf.begin();
   764 #else
   765                               ) {
   766   typedef numeric_limits<_FloatT> limits;
   767   //If numeric_limits support is correct we use the exposed values to detect NaN and infinity:
   768   if (limits::has_infinity && limits::has_quiet_NaN) {
   769     if (!(x == x) || // NaN check
   770         (x == limits::infinity() || x == -limits::infinity())) {
   771       return __format_nan_or_inf(buf, x, flags);
   772     }
   773   }
   774   // numeric_limits support is not good enough, we rely on platform dependent function
   775   // _Stl_is_nan_or_inf that do not support long double.
   776   else if (_Stl_is_nan_or_inf(x)) {
   777     return __format_nan_or_inf(buf, x, flags);
   778   }
   779 #  if defined (__MINGW32__)
   780   //For the moment MinGW is limited to display at most numeric_limits<double>::max()
   781   if (x > numeric_limits<double>::max() ||
   782       x < -numeric_limits<double>::max()) {
   783     return __format_nan_or_inf(buf, x, flags);
   784   }
   785 #  endif
   787   /* Buffer size is max number of digits which is the addition of:
   788    * - max_exponent10: max number of digits in fixed mode
   789    * - digits10 + 2: max number of significant digits
   790    * - trailing '\0'
   791    */
   792   char cvtbuf[limits::max_exponent10 + limits::digits10 + 2 + 1];
   793   char *bp;
   794   int decpt, sign;
   796   switch (flags & ios_base::floatfield) {
   797   case ios_base::fixed:
   798     {
   799       /* Here, number of digits represents digits _after_ decimal point.
   800        * In order to limit static buffer size we have to give 2 different values depending on x value. 
   801        * For small values (abs(x) < 1) we need as many digits as requested by precision limited by the maximum number of digits
   802        * which is min_exponent10 + digits10 + 2
   803        * For bigger values we won't have more than limits::digits10 + 2 digits after decimal point. */
   804       int digits10 = (x > -1.0 && x < 1.0 ? -limits::min_exponent10 + limits::digits10 + 2
   805                                           : limits::digits10 + 2);
   806       bp = _Stl_fcvtR(x, (min) (precision, digits10), &decpt, &sign, _STLP_CVT_BUFFER(cvtbuf) );
   807     }
   808     break;
   809   case ios_base::scientific:
   810   default:
   811     /* Here, number of digits is total number of digits which is limited to digits10 + 2. */
   812     {
   813       int digits10 = limits::digits10 + 2;
   814       bp = _Stl_ecvtR(x, (min) (precision, digits10), &decpt, &sign, _STLP_CVT_BUFFER(cvtbuf) );
   815     }
   816     break;
   817   }
   818   return __format_float(buf, bp, decpt, sign, x == 0.0, flags, precision);
   819 #endif
   820 }
   822 size_t  _STLP_CALL
   823 __write_float(__iostring &buf, ios_base::fmtflags flags, int precision,
   824               double x) {
   825   return __write_floatT(buf, flags, precision, x
   826 #if defined (USE_SPRINTF_INSTEAD)
   827                                                , 0
   828 #endif
   829                                                   );
   830 }
   832 #if !defined (_STLP_NO_LONG_DOUBLE)
   833 size_t _STLP_CALL
   834 __write_float(__iostring &buf, ios_base::fmtflags flags, int precision,
   835               long double x) {
   836   return __write_floatT(buf, flags, precision, x
   837 #if defined (USE_SPRINTF_INSTEAD)
   838                                                , 'L'
   839 #endif
   840                                                     );
   841 }
   842 #endif
   844 void _STLP_CALL __get_floor_digits(__iostring &out, _STLP_LONGEST_FLOAT_TYPE __x) {
   845   typedef numeric_limits<_STLP_LONGEST_FLOAT_TYPE> limits;
   846 #if defined (USE_SPRINTF_INSTEAD)
   847   char cvtbuf[limits::max_exponent10 + 6];
   848 #  if !defined (_STLP_NO_LONG_DOUBLE)
   849   snprintf(_STLP_ARRAY_AND_SIZE(cvtbuf), "%Lf", __x); // check for 1234.56!
   850 #  else
   851   snprintf(_STLP_ARRAY_AND_SIZE(cvtbuf), "%f", __x);  // check for 1234.56!
   852 #  endif
   853   char *p = strchr( cvtbuf, '.' );
   854   if ( p == 0 ) {
   855     out.append( cvtbuf );
   856   } else {
   857     out.append( cvtbuf, p );
   858   }
   859 #else
   860   char cvtbuf[limits::max_exponent10 + 1];
   861   char * bp;
   862   int decpt, sign;
   863   bp = _Stl_fcvtR(__x, 0, &decpt, &sign, _STLP_CVT_BUFFER(cvtbuf));
   865   if (sign) {
   866     out += '-';
   867   }
   868   out.append(bp, bp + decpt);
   869 #endif
   870 }
   873 #if !defined (_STLP_NO_WCHAR_T)
   874 void _STLP_CALL __convert_float_buffer( __iostring const& str, __iowstring &out,
   875                                         const ctype<wchar_t>& ct, wchar_t dot, bool __check_dot) {
   876   string::const_iterator str_ite(str.begin()), str_end(str.end());
   878   //First loop, check the dot char
   879   if (__check_dot) {
   880     while (str_ite != str_end) {
   881       if (*str_ite != '.') {
   882         out += ct.widen(*str_ite++);
   883       } else {
   884         out += dot;
   885         break;
   886       }
   887     }
   888   } else {
   889     if (str_ite != str_end) {
   890       out += ct.widen(*str_ite);
   891     }
   892   }
   894   if (str_ite != str_end) {
   895     //Second loop, dot has been found, no check anymore
   896     while (++str_ite != str_end) {
   897       out += ct.widen(*str_ite);
   898     }
   899   }
   900 }
   902 #endif
   904 void _STLP_CALL
   905 __adjust_float_buffer(__iostring &str, char dot) {
   906   if ('.' != dot) {
   907     size_t __dot_pos = str.find('.');
   908     if (__dot_pos != string::npos) {
   909       str[__dot_pos] = dot;
   910     }
   911   }
   912 }
   914 _STLP_MOVE_TO_STD_NAMESPACE
   915 _STLP_END_NAMESPACE
   917 // Local Variables:
   918 // mode:C++
   919 // End:

mercurial