Source

double-conversion / src / double-conversion.cc

The branch 'single' does not exist.
Diff from to

File src/double-conversion.cc

 
 // Parsing integers with radix 2, 4, 8, 16, 32. Assumes current != end.
 template <int radix_log_2>
-static double RadixStringToDouble(const char* current,
-                                  const char* end,
-                                  bool sign,
-                                  bool allow_trailing_junk,
-                                  double junk_string_value,
-                                  const char** trailing_pointer) {
+static double RadixStringToIeee(const char* current,
+                                const char* end,
+                                bool sign,
+                                bool allow_trailing_junk,
+                                double junk_string_value,
+                                bool read_as_double,
+                                const char** trailing_pointer) {
   ASSERT(current != end);
 
+  const int kDoubleSize = Double::kSignificandSize;
+  const int kSingleSize = Single::kSignificandSize;
+  const int kSignificandSize = read_as_double? kDoubleSize: kSingleSize;
+
   // Skip leading 0s.
   while (*current == '0') {
     ++current;
     }
 
     number = number * radix + digit;
-    int overflow = static_cast<int>(number >> 53);
+    int overflow = static_cast<int>(number >> kSignificandSize);
     if (overflow != 0) {
       // Overflow occurred. Need to determine which direction to round the
       // result.
       }
 
       // Rounding up may cause overflow.
-      if ((number & ((int64_t)1 << 53)) != 0) {
+      if ((number & ((int64_t)1 << kSignificandSize)) != 0) {
         exponent++;
         number >>= 1;
       }
     ++current;
   } while (current != end);
 
-  ASSERT(number < ((int64_t)1 << 53));
+  ASSERT(number < ((int64_t)1 << kSignificandSize));
   ASSERT(static_cast<int64_t>(static_cast<double>(number)) == number);
 
   *trailing_pointer = current;
 }
 
 
-double StringToDoubleConverter::StringToDouble(
+double StringToDoubleConverter::StringToIeee(
     const char* input,
     int length,
-    int* processed_characters_count) {
+    int* processed_characters_count,
+    bool read_as_double) {
   const char* current = input;
   const char* end = input + length;
 
       }
 
       const char* tail_pointer = NULL;
-      double result = RadixStringToDouble<4>(current,
-                                             end,
-                                             sign,
-                                             allow_trailing_junk,
-                                             junk_string_value_,
-                                             &tail_pointer);
+      double result = RadixStringToIeee<4>(current,
+                                           end,
+                                           sign,
+                                           allow_trailing_junk,
+                                           junk_string_value_,
+                                           read_as_double,
+                                           &tail_pointer);
       if (tail_pointer != NULL) {
         if (allow_trailing_spaces) AdvanceToNonspace(&tail_pointer, end);
         *processed_characters_count = tail_pointer - input;
   if (octal) {
     double result;
     const char* tail_pointer = NULL;
-    result = RadixStringToDouble<3>(buffer,
-                                    buffer + buffer_pos,
-                                    sign,
-                                    allow_trailing_junk,
-                                    junk_string_value_,
-                                    &tail_pointer);
+    result = RadixStringToIeee<3>(buffer,
+                                  buffer + buffer_pos,
+                                  sign,
+                                  allow_trailing_junk,
+                                  junk_string_value_,
+                                  read_as_double,
+                                  &tail_pointer);
     ASSERT(tail_pointer != NULL);
     *processed_characters_count = current - input;
     return result;
   ASSERT(buffer_pos < kBufferSize);
   buffer[buffer_pos] = '\0';
 
-  double converted = Strtod(Vector<const char>(buffer, buffer_pos), exponent);
+  double converted;
+  if (read_as_double) {
+    converted = Strtod(Vector<const char>(buffer, buffer_pos), exponent);
+  } else {
+    converted = Strtof(Vector<const char>(buffer, buffer_pos), exponent);
+  }
   *processed_characters_count = current - input;
   return sign? -converted: converted;
 }