More verbose debugging.
[p5sagit/p5-mst-13.2.git] / util.c
diff --git a/util.c b/util.c
index 6a01a46..c92bf87 100644 (file)
--- a/util.c
+++ b/util.c
@@ -3972,28 +3972,28 @@ Perl_my_fflush_all(pTHX)
     extern void _fwalk(int (*)(FILE *));
     _fwalk(&fflush);
     return 0;
-#   else
-    long open_max = -1;
+# else
 #  if defined(FFLUSH_ALL) && defined(HAS_STDIO_STREAM_ARRAY)
+    long open_max = -1;
 #   ifdef PERL_FFLUSH_ALL_FOPEN_MAX
     open_max = PERL_FFLUSH_ALL_FOPEN_MAX;
 #   else
-#   if defined(HAS_SYSCONF) && defined(_SC_OPEN_MAX)
+#    if defined(HAS_SYSCONF) && defined(_SC_OPEN_MAX)
     open_max = sysconf(_SC_OPEN_MAX);
-#   else
-#    ifdef FOPEN_MAX
+#     else
+#      ifdef FOPEN_MAX
     open_max = FOPEN_MAX;
-#    else
-#     ifdef OPEN_MAX
+#      else
+#       ifdef OPEN_MAX
     open_max = OPEN_MAX;
-#     else
-#      ifdef _NFILE
+#       else
+#        ifdef _NFILE
     open_max = _NFILE;
+#        endif
+#       endif
 #      endif
 #     endif
 #    endif
-#   endif
-#   endif
     if (open_max > 0) {
       long i;
       for (i = 0; i < open_max; i++)
@@ -4018,21 +4018,183 @@ Perl_my_atof(pTHX_ const char* s)
     if ((PL_hints & HINT_LOCALE) && PL_numeric_local) {
        NV y;
 
-       Perl_atof2(s, x);
+       Perl_atof2(aTHX_ s, &x);
        SET_NUMERIC_STANDARD();
-       Perl_atof2(s, y);
+       Perl_atof2(aTHX_ s, &y);
        SET_NUMERIC_LOCAL();
        if ((y < 0.0 && y < x) || (y > 0.0 && y > x))
            return y;
     }
     else
-       Perl_atof2(s, x);
+       Perl_atof2(aTHX_ s, &x);
 #else
-    Perl_atof2(s, x);
+    Perl_atof2(aTHX_ s, &x);
 #endif
     return x;
 }
 
+NV
+S_mulexp10(NV value, I32 exponent)
+{
+    NV result = value;
+    NV power = 10.0;
+    I32 bit;
+
+    if (exponent > 0) {
+       for (bit = 1; exponent; bit <<= 1) {
+           if (exponent & bit) {
+               exponent ^= bit;
+               result *= power;
+           }
+           power *= power;
+       }
+    }
+    else if (exponent < 0) {
+       exponent = -exponent;
+       for (bit = 1; exponent; bit <<= 1) {
+           if (exponent & bit) {
+               exponent ^= bit;
+               result /= power;
+           }
+           power *= power;
+       }
+    }
+    return result;
+}
+
+char*
+Perl_my_atof2(pTHX_ const char* orig, NV* value)
+{
+    NV result = 0.0;
+    bool negative = 0;
+    char* s = (char*)orig;
+    char* point = "."; /* locale-dependent decimal point equivalent */
+    STRLEN pointlen = 1;
+    bool seendigit = 0;
+    I32 expextra = 0;
+    I32 exponent = 0;
+    I32 i;
+/* this is arbitrary */
+#define PARTLIM 6
+/* we want the largest integers we can usefully use */
+#if defined(HAS_QUAD) && defined(USE_64_BIT_INT)
+#   define PARTSIZE ((int)TYPE_DIGITS(U64)-1)
+    U64 part[PARTLIM];
+#else
+#   define PARTSIZE ((int)TYPE_DIGITS(U32)-1)
+    U32 part[PARTLIM];
+#endif
+    I32 ipart = 0;     /* index into part[] */
+    I32 offcount;      /* number of digits in least significant part */
+
+    if (PL_numeric_radix_sv)
+       point = SvPV(PL_numeric_radix_sv, pointlen);
+
+    /* sign */
+    switch (*s) {
+       case '-':
+           negative = 1;
+           /* fall through */
+       case '+':
+           ++s;
+    }
+
+    part[0] = offcount = 0;
+    if (isDIGIT(*s)) {
+       seendigit = 1;  /* get this over with */
+
+       /* skip leading zeros */
+       while (*s == '0')
+           ++s;
+    }
+
+    /* integer digits */
+    while (isDIGIT(*s)) {
+       if (++offcount > PARTSIZE) {
+           if (++ipart < PARTLIM) {
+               part[ipart] = 0;
+               offcount = 1;   /* ++0 */
+           }
+           else {
+               /* limits of precision reached */
+               --ipart;
+               --offcount;
+               if (*s >= '5')
+                   ++part[ipart];
+               while (isDIGIT(*s)) {
+                   ++expextra;
+                   ++s;
+               }
+               /* warn of loss of precision? */
+               break;
+           }
+       }
+       part[ipart] = part[ipart] * 10 + (*s++ - '0');
+    }
+
+    /* decimal point */
+    if (memEQ(s, point, pointlen)) {
+       s += pointlen;
+       if (isDIGIT(*s))
+           seendigit = 1;      /* get this over with */
+
+       /* decimal digits */
+       while (isDIGIT(*s)) {
+           if (++offcount > PARTSIZE) {
+               if (++ipart < PARTLIM) {
+                   part[ipart] = 0;
+                   offcount = 1;       /* ++0 */
+               }
+               else {
+                   /* limits of precision reached */
+                   --ipart;
+                   --offcount;
+                   if (*s >= '5')
+                       ++part[ipart];
+                   while (isDIGIT(*s))
+                       ++s;
+                   /* warn of loss of precision? */
+                   break;
+               }
+           }
+           --expextra;
+           part[ipart] = part[ipart] * 10 + (*s++ - '0');
+       }
+    }
+
+    /* combine components of mantissa */
+    for (i = 0; i <= ipart; ++i)
+       result += S_mulexp10((NV)part[ipart - i],
+               i ? offcount + (i - 1) * PARTSIZE : 0);
+
+    if (seendigit && (*s == 'e' || *s == 'E')) {
+       bool expnegative = 0;
+
+       ++s;
+       switch (*s) {
+           case '-':
+               expnegative = 1;
+               /* fall through */
+           case '+':
+               ++s;
+       }
+       while (isDIGIT(*s))
+           exponent = exponent * 10 + (*s++ - '0');
+       if (expnegative)
+           exponent = -exponent;
+    }
+
+    /* now apply the exponent */
+    exponent += expextra;
+    result = S_mulexp10(result, exponent);
+
+    /* now apply the sign */
+    if (negative)
+       result = -result;
+    *value = result;
+    return s;
+}
+
 void
 Perl_report_evil_fh(pTHX_ GV *gv, IO *io, I32 op)
 {
@@ -4380,7 +4542,7 @@ Perl_my_strftime(pTHX_ char *fmt, int sec, int min, int hour, int mday, int mon,
   New(0, buf, buflen, char);
   len = strftime(buf, buflen, fmt, &mytm);
   /*
-  ** The following is needed to handle to the situation where 
+  ** The following is needed to handle to the situation where
   ** tmpbuf overflows.  Basically we want to allocate a buffer
   ** and try repeatedly.  The reason why it is so complicated
   ** is that getting a return value of 0 from strftime can indicate
@@ -4399,7 +4561,7 @@ Perl_my_strftime(pTHX_ char *fmt, int sec, int min, int hour, int mday, int mon,
     /* Possibly buf overflowed - try again with a bigger buf */
     int     fmtlen = strlen(fmt);
     int            bufsize = fmtlen + buflen;
-    
+
     New(0, buf, bufsize, char);
     while (buf) {
       buflen = strftime(buf, bufsize, fmt, &mytm);
@@ -4654,7 +4816,7 @@ Perl_sv_realpath(pTHX_ SV *sv, char *path, STRLEN len)
                       dotdots, Strerror(errno));
             SV_CWD_RETURN_UNDEF;
         }
-    
+
         if (pst.st_dev == cst.st_dev && pst.st_ino == cst.st_ino) {
             /* We've reached the root: previous is same as current */
             break;
@@ -4667,13 +4829,13 @@ Perl_sv_realpath(pTHX_ SV *sv, char *path, STRLEN len)
                           dotdots, Strerror(errno));
                 SV_CWD_RETURN_UNDEF;
             }
-    
+
             SETERRNO(0,SS$_NORMAL); /* for readdir() */
             while ((dp = PerlDir_read(parent)) != NULL) {
                 if (SV_CWD_ISDOT(dp)) {
                     continue;
                 }
-        
+
                 Copy(dotdots, name, dotdotslen, char);
                 name[dotdotslen] = '/';
 #ifdef DIRNAMLEN
@@ -4683,14 +4845,14 @@ Perl_sv_realpath(pTHX_ SV *sv, char *path, STRLEN len)
 #endif
                 Copy(dp->d_name, name + dotdotslen + 1, namelen, char);
                 name[dotdotslen + 1 + namelen] = 0;
-        
+
                 if (PerlLIO_lstat(name, &tst) < 0) {
                     PerlDir_close(parent);
                     Perl_warn(aTHX_ "sv_realpath: lstat(\"%s\"): %s",
                               name, Strerror(errno));
                     SV_CWD_RETURN_UNDEF;
                 }
-        
+
                 if (tst.st_dev == pst.st_dev && tst.st_ino == pst.st_ino)
                     break;