You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

format.cc 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // Formatting library for C++
  2. //
  3. // Copyright (c) 2012 - 2016, Victor Zverovich
  4. // All rights reserved.
  5. //
  6. // For the license information refer to format.h.
  7. #include "fmt/format-inl.h"
  8. FMT_BEGIN_NAMESPACE
  9. namespace detail {
  10. // DEPRECATED!
  11. template <typename T = void> struct basic_data {
  12. FMT_API static constexpr const char digits[100][2] = {
  13. {'0', '0'}, {'0', '1'}, {'0', '2'}, {'0', '3'}, {'0', '4'}, {'0', '5'},
  14. {'0', '6'}, {'0', '7'}, {'0', '8'}, {'0', '9'}, {'1', '0'}, {'1', '1'},
  15. {'1', '2'}, {'1', '3'}, {'1', '4'}, {'1', '5'}, {'1', '6'}, {'1', '7'},
  16. {'1', '8'}, {'1', '9'}, {'2', '0'}, {'2', '1'}, {'2', '2'}, {'2', '3'},
  17. {'2', '4'}, {'2', '5'}, {'2', '6'}, {'2', '7'}, {'2', '8'}, {'2', '9'},
  18. {'3', '0'}, {'3', '1'}, {'3', '2'}, {'3', '3'}, {'3', '4'}, {'3', '5'},
  19. {'3', '6'}, {'3', '7'}, {'3', '8'}, {'3', '9'}, {'4', '0'}, {'4', '1'},
  20. {'4', '2'}, {'4', '3'}, {'4', '4'}, {'4', '5'}, {'4', '6'}, {'4', '7'},
  21. {'4', '8'}, {'4', '9'}, {'5', '0'}, {'5', '1'}, {'5', '2'}, {'5', '3'},
  22. {'5', '4'}, {'5', '5'}, {'5', '6'}, {'5', '7'}, {'5', '8'}, {'5', '9'},
  23. {'6', '0'}, {'6', '1'}, {'6', '2'}, {'6', '3'}, {'6', '4'}, {'6', '5'},
  24. {'6', '6'}, {'6', '7'}, {'6', '8'}, {'6', '9'}, {'7', '0'}, {'7', '1'},
  25. {'7', '2'}, {'7', '3'}, {'7', '4'}, {'7', '5'}, {'7', '6'}, {'7', '7'},
  26. {'7', '8'}, {'7', '9'}, {'8', '0'}, {'8', '1'}, {'8', '2'}, {'8', '3'},
  27. {'8', '4'}, {'8', '5'}, {'8', '6'}, {'8', '7'}, {'8', '8'}, {'8', '9'},
  28. {'9', '0'}, {'9', '1'}, {'9', '2'}, {'9', '3'}, {'9', '4'}, {'9', '5'},
  29. {'9', '6'}, {'9', '7'}, {'9', '8'}, {'9', '9'}};
  30. FMT_API static constexpr const char hex_digits[] = "0123456789abcdef";
  31. FMT_API static constexpr const char signs[4] = {0, '-', '+', ' '};
  32. FMT_API static constexpr const char left_padding_shifts[5] = {31, 31, 0, 1,
  33. 0};
  34. FMT_API static constexpr const char right_padding_shifts[5] = {0, 31, 0, 1,
  35. 0};
  36. FMT_API static constexpr const unsigned prefixes[4] = {0, 0, 0x1000000u | '+',
  37. 0x1000000u | ' '};
  38. };
  39. #ifdef FMT_SHARED
  40. // Required for -flto, -fivisibility=hidden and -shared to work
  41. extern template struct basic_data<void>;
  42. #endif
  43. #if __cplusplus < 201703L
  44. // DEPRECATED! These are here only for ABI compatiblity.
  45. template <typename T> constexpr const char basic_data<T>::digits[][2];
  46. template <typename T> constexpr const char basic_data<T>::hex_digits[];
  47. template <typename T> constexpr const char basic_data<T>::signs[];
  48. template <typename T> constexpr const char basic_data<T>::left_padding_shifts[];
  49. template <typename T>
  50. constexpr const char basic_data<T>::right_padding_shifts[];
  51. template <typename T> constexpr const unsigned basic_data<T>::prefixes[];
  52. #endif
  53. template <typename T>
  54. int format_float(char* buf, std::size_t size, const char* format, int precision,
  55. T value) {
  56. #ifdef FMT_FUZZ
  57. if (precision > 100000)
  58. throw std::runtime_error(
  59. "fuzz mode - avoid large allocation inside snprintf");
  60. #endif
  61. // Suppress the warning about nonliteral format string.
  62. int (*snprintf_ptr)(char*, size_t, const char*, ...) = FMT_SNPRINTF;
  63. return precision < 0 ? snprintf_ptr(buf, size, format, value)
  64. : snprintf_ptr(buf, size, format, precision, value);
  65. }
  66. template FMT_API dragonbox::decimal_fp<float> dragonbox::to_decimal(float x)
  67. FMT_NOEXCEPT;
  68. template FMT_API dragonbox::decimal_fp<double> dragonbox::to_decimal(double x)
  69. FMT_NOEXCEPT;
  70. } // namespace detail
  71. // Workaround a bug in MSVC2013 that prevents instantiation of format_float.
  72. int (*instantiate_format_float)(double, int, detail::float_specs,
  73. detail::buffer<char>&) = detail::format_float;
  74. #ifndef FMT_STATIC_THOUSANDS_SEPARATOR
  75. template FMT_API detail::locale_ref::locale_ref(const std::locale& loc);
  76. template FMT_API std::locale detail::locale_ref::get<std::locale>() const;
  77. #endif
  78. // Explicit instantiations for char.
  79. template FMT_API auto detail::thousands_sep_impl(locale_ref)
  80. -> thousands_sep_result<char>;
  81. template FMT_API char detail::decimal_point_impl(locale_ref);
  82. template FMT_API void detail::buffer<char>::append(const char*, const char*);
  83. // DEPRECATED!
  84. // There is no correspondent extern template in format.h because of
  85. // incompatibility between clang and gcc (#2377).
  86. template FMT_API void detail::vformat_to(
  87. detail::buffer<char>&, string_view,
  88. basic_format_args<FMT_BUFFER_CONTEXT(char)>, detail::locale_ref);
  89. template FMT_API int detail::snprintf_float(double, int, detail::float_specs,
  90. detail::buffer<char>&);
  91. template FMT_API int detail::snprintf_float(long double, int,
  92. detail::float_specs,
  93. detail::buffer<char>&);
  94. template FMT_API int detail::format_float(double, int, detail::float_specs,
  95. detail::buffer<char>&);
  96. template FMT_API int detail::format_float(long double, int, detail::float_specs,
  97. detail::buffer<char>&);
  98. // Explicit instantiations for wchar_t.
  99. template FMT_API auto detail::thousands_sep_impl(locale_ref)
  100. -> thousands_sep_result<wchar_t>;
  101. template FMT_API wchar_t detail::decimal_point_impl(locale_ref);
  102. template FMT_API void detail::buffer<wchar_t>::append(const wchar_t*,
  103. const wchar_t*);
  104. template struct detail::basic_data<void>;
  105. FMT_END_NAMESPACE