| 41 | // This is deprecated and is kept only to preserve ABI compatibility. |
| 42 | template <typename Double> |
| 43 | char* sprintf_format(Double value, internal::buffer<char>& buf, |
| 44 | sprintf_specs specs) { |
| 45 | // Buffer capacity must be non-zero, otherwise MSVC's vsnprintf_s will fail. |
| 46 | FMT_ASSERT(buf.capacity() != 0, "empty buffer"); |
| 47 | |
| 48 | // Build format string. |
| 49 | enum { max_format_size = 10 }; // longest format: %#-*.*Lg |
| 50 | char format[max_format_size]; |
| 51 | char* format_ptr = format; |
| 52 | *format_ptr++ = '%'; |
| 53 | if (specs.alt || !specs.type) *format_ptr++ = '#'; |
| 54 | if (specs.precision >= 0) { |
| 55 | *format_ptr++ = '.'; |
| 56 | *format_ptr++ = '*'; |
| 57 | } |
| 58 | if (std::is_same<Double, long double>::value) *format_ptr++ = 'L'; |
| 59 | |
| 60 | char type = specs.type; |
| 61 | |
| 62 | if (type == '%') |
| 63 | type = 'f'; |
| 64 | else if (type == 0 || type == 'n') |
| 65 | type = 'g'; |
| 66 | #if FMT_MSC_VER |
| 67 | if (type == 'F') { |
| 68 | // MSVC's printf doesn't support 'F'. |
| 69 | type = 'f'; |
| 70 | } |
| 71 | #endif |
| 72 | *format_ptr++ = type; |
| 73 | *format_ptr = '\0'; |
| 74 | |
| 75 | // Format using snprintf. |
| 76 | char* start = nullptr; |
| 77 | char* decimal_point_pos = nullptr; |
| 78 | for (;;) { |
| 79 | std::size_t buffer_size = buf.capacity(); |
| 80 | start = &buf[0]; |
| 81 | int result = |
| 82 | format_float(start, buffer_size, format, specs.precision, value); |
| 83 | if (result >= 0) { |
| 84 | unsigned n = internal::to_unsigned(result); |
| 85 | if (n < buf.capacity()) { |
| 86 | // Find the decimal point. |
| 87 | auto p = buf.data(), end = p + n; |
| 88 | if (*p == '+' || *p == '-') ++p; |
| 89 | if (specs.type != 'a' && specs.type != 'A') { |
| 90 | while (p < end && *p >= '0' && *p <= '9') ++p; |
| 91 | if (p < end && *p != 'e' && *p != 'E') { |
| 92 | decimal_point_pos = p; |
| 93 | if (!specs.type) { |
| 94 | // Keep only one trailing zero after the decimal point. |
| 95 | ++p; |
| 96 | if (*p == '0') ++p; |
| 97 | while (p != end && *p >= '1' && *p <= '9') ++p; |
| 98 | char* where = p; |
| 99 | while (p != end && *p == '0') ++p; |
| 100 | if (p == end || *p < '0' || *p > '9') { |
nothing calls this directly
no test coverage detected