• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
8 #include "fmt/format-inl.h"
9 
10 FMT_BEGIN_NAMESPACE
11 namespace detail {
12 
13 template <typename T>
format_float(char * buf,std::size_t size,const char * format,int precision,T value)14 int format_float(char* buf, std::size_t size, const char* format, int precision,
15                  T value) {
16 #ifdef FMT_FUZZ
17   if (precision > 100000)
18     throw std::runtime_error(
19         "fuzz mode - avoid large allocation inside snprintf");
20 #endif
21   // Suppress the warning about nonliteral format string.
22   int (*snprintf_ptr)(char*, size_t, const char*, ...) = FMT_SNPRINTF;
23   return precision < 0 ? snprintf_ptr(buf, size, format, value)
24                        : snprintf_ptr(buf, size, format, precision, value);
25 }
26 
27 template FMT_API dragonbox::decimal_fp<float> dragonbox::to_decimal(float x)
28     FMT_NOEXCEPT;
29 template FMT_API dragonbox::decimal_fp<double> dragonbox::to_decimal(double x)
30     FMT_NOEXCEPT;
31 
32 // DEPRECATED! This function exists for ABI compatibility.
33 template <typename Char>
34 typename basic_format_context<std::back_insert_iterator<buffer<Char>>,
35                               Char>::iterator
vformat_to(buffer<Char> & buf,basic_string_view<Char> format_str,basic_format_args<basic_format_context<std::back_insert_iterator<buffer<type_identity_t<Char>>>,type_identity_t<Char>>> args)36 vformat_to(buffer<Char>& buf, basic_string_view<Char> format_str,
37            basic_format_args<basic_format_context<
38                std::back_insert_iterator<buffer<type_identity_t<Char>>>,
39                type_identity_t<Char>>>
40                args) {
41   using iterator = std::back_insert_iterator<buffer<char>>;
42   using context = basic_format_context<
43       std::back_insert_iterator<buffer<type_identity_t<Char>>>,
44       type_identity_t<Char>>;
45   auto out = iterator(buf);
46   format_handler<iterator, Char, context> h(out, format_str, args, {});
47   parse_format_string<false>(format_str, h);
48   return out;
49 }
50 template basic_format_context<std::back_insert_iterator<buffer<char>>,
51                               char>::iterator
52 vformat_to(buffer<char>&, string_view,
53            basic_format_args<basic_format_context<
54                std::back_insert_iterator<buffer<type_identity_t<char>>>,
55                type_identity_t<char>>>);
56 }  // namespace detail
57 
58 template struct FMT_INSTANTIATION_DEF_API detail::basic_data<void>;
59 
60 // Workaround a bug in MSVC2013 that prevents instantiation of format_float.
61 int (*instantiate_format_float)(double, int, detail::float_specs,
62                                 detail::buffer<char>&) = detail::format_float;
63 
64 #ifndef FMT_STATIC_THOUSANDS_SEPARATOR
65 template FMT_API detail::locale_ref::locale_ref(const std::locale& loc);
66 template FMT_API std::locale detail::locale_ref::get<std::locale>() const;
67 #endif
68 
69 // Explicit instantiations for char.
70 
71 template FMT_API std::string detail::grouping_impl<char>(locale_ref);
72 template FMT_API char detail::thousands_sep_impl(locale_ref);
73 template FMT_API char detail::decimal_point_impl(locale_ref);
74 
75 template FMT_API void detail::buffer<char>::append(const char*, const char*);
76 
77 template FMT_API void detail::vformat_to(
78     detail::buffer<char>&, string_view,
79     basic_format_args<FMT_BUFFER_CONTEXT(char)>, detail::locale_ref);
80 
81 template FMT_API int detail::snprintf_float(double, int, detail::float_specs,
82                                             detail::buffer<char>&);
83 template FMT_API int detail::snprintf_float(long double, int,
84                                             detail::float_specs,
85                                             detail::buffer<char>&);
86 template FMT_API int detail::format_float(double, int, detail::float_specs,
87                                           detail::buffer<char>&);
88 template FMT_API int detail::format_float(long double, int, detail::float_specs,
89                                           detail::buffer<char>&);
90 
91 // Explicit instantiations for wchar_t.
92 
93 template FMT_API std::string detail::grouping_impl<wchar_t>(locale_ref);
94 template FMT_API wchar_t detail::thousands_sep_impl(locale_ref);
95 template FMT_API wchar_t detail::decimal_point_impl(locale_ref);
96 
97 template FMT_API void detail::buffer<wchar_t>::append(const wchar_t*,
98                                                       const wchar_t*);
99 FMT_END_NAMESPACE
100