Thomas Psota | 6bf7938 | 2019-09-16 16:42:15 +0200 | [diff] [blame] | 1 | // Formatting library for C++ - std::locale support |
| 2 | // |
| 3 | // Copyright (c) 2012 - present, Victor Zverovich |
| 4 | // All rights reserved. |
| 5 | // |
| 6 | // For the license information refer to format.h. |
| 7 | |
| 8 | #ifndef FMT_LOCALE_H_ |
| 9 | #define FMT_LOCALE_H_ |
| 10 | |
| 11 | #include "format.h" |
| 12 | #include <locale> |
| 13 | |
| 14 | FMT_BEGIN_NAMESPACE |
| 15 | |
| 16 | namespace internal { |
| 17 | template <typename Char> |
| 18 | typename buffer_context<Char>::type::iterator vformat_to( |
| 19 | const std::locale &loc, basic_buffer<Char> &buf, |
| 20 | basic_string_view<Char> format_str, |
| 21 | basic_format_args<typename buffer_context<Char>::type> args) { |
| 22 | typedef back_insert_range<basic_buffer<Char> > range; |
| 23 | return vformat_to<arg_formatter<range>>( |
| 24 | buf, to_string_view(format_str), args, internal::locale_ref(loc)); |
| 25 | } |
| 26 | |
| 27 | template <typename Char> |
| 28 | std::basic_string<Char> vformat( |
| 29 | const std::locale &loc, basic_string_view<Char> format_str, |
| 30 | basic_format_args<typename buffer_context<Char>::type> args) { |
| 31 | basic_memory_buffer<Char> buffer; |
| 32 | internal::vformat_to(loc, buffer, format_str, args); |
| 33 | return fmt::to_string(buffer); |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | template <typename S, typename Char = FMT_CHAR(S)> |
| 38 | inline std::basic_string<Char> vformat( |
| 39 | const std::locale &loc, const S &format_str, |
| 40 | basic_format_args<typename buffer_context<Char>::type> args) { |
| 41 | return internal::vformat(loc, to_string_view(format_str), args); |
| 42 | } |
| 43 | |
| 44 | template <typename S, typename... Args> |
| 45 | inline std::basic_string<FMT_CHAR(S)> format( |
| 46 | const std::locale &loc, const S &format_str, const Args &... args) { |
| 47 | return internal::vformat( |
| 48 | loc, to_string_view(format_str), |
| 49 | *internal::checked_args<S, Args...>(format_str, args...)); |
| 50 | } |
| 51 | |
| 52 | template <typename String, typename OutputIt, typename... Args> |
| 53 | inline typename std::enable_if<internal::is_output_iterator<OutputIt>::value, |
| 54 | OutputIt>::type |
| 55 | vformat_to(OutputIt out, const std::locale &loc, const String &format_str, |
| 56 | typename format_args_t<OutputIt, FMT_CHAR(String)>::type args) { |
| 57 | typedef output_range<OutputIt, FMT_CHAR(String)> range; |
| 58 | return vformat_to<arg_formatter<range>>( |
| 59 | range(out), to_string_view(format_str), args, internal::locale_ref(loc)); |
| 60 | } |
| 61 | |
| 62 | template <typename OutputIt, typename S, typename... Args> |
| 63 | inline typename std::enable_if< |
| 64 | internal::is_string<S>::value && |
| 65 | internal::is_output_iterator<OutputIt>::value, OutputIt>::type |
| 66 | format_to(OutputIt out, const std::locale &loc, const S &format_str, |
| 67 | const Args &... args) { |
| 68 | internal::check_format_string<Args...>(format_str); |
| 69 | typedef typename format_context_t<OutputIt, FMT_CHAR(S)>::type context; |
| 70 | format_arg_store<context, Args...> as{args...}; |
| 71 | return vformat_to(out, loc, to_string_view(format_str), |
| 72 | basic_format_args<context>(as)); |
| 73 | } |
| 74 | |
| 75 | FMT_END_NAMESPACE |
| 76 | |
| 77 | #endif // FMT_LOCALE_H_ |