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 <locale>
12 #include "format.h"
13
14 FMT_BEGIN_NAMESPACE
15
16 namespace internal {
17 template <typename Char>
vformat_to(const std::locale & loc,buffer<Char> & buf,basic_string_view<Char> format_str,basic_format_args<buffer_context<Char>> args)18 typename buffer_context<Char>::iterator vformat_to(
19 const std::locale& loc, buffer<Char>& buf,
20 basic_string_view<Char> format_str,
21 basic_format_args<buffer_context<Char>> args) {
22 using range = buffer_range<Char>;
23 return vformat_to<arg_formatter<range>>(buf, to_string_view(format_str), args,
24 internal::locale_ref(loc));
25 }
26
27 template <typename Char>
vformat(const std::locale & loc,basic_string_view<Char> format_str,basic_format_args<buffer_context<Char>> args)28 std::basic_string<Char> vformat(const std::locale& loc,
29 basic_string_view<Char> format_str,
30 basic_format_args<buffer_context<Char>> args) {
31 basic_memory_buffer<Char> buffer;
32 internal::vformat_to(loc, buffer, format_str, args);
33 return fmt::to_string(buffer);
34 }
35 } // namespace internal
36
37 template <typename S, typename Char = char_t<S>>
vformat(const std::locale & loc,const S & format_str,basic_format_args<buffer_context<Char>> args)38 inline std::basic_string<Char> vformat(
39 const std::locale& loc, const S& format_str,
40 basic_format_args<buffer_context<Char>> args) {
41 return internal::vformat(loc, to_string_view(format_str), args);
42 }
43
44 template <typename S, typename... Args, typename Char = char_t<S>>
format(const std::locale & loc,const S & format_str,Args &&...args)45 inline std::basic_string<Char> format(const std::locale& loc,
46 const S& format_str, Args&&... args) {
47 return internal::vformat(
48 loc, to_string_view(format_str),
49 {internal::make_args_checked<Args...>(format_str, args...)});
50 }
51
52 template <typename S, typename OutputIt, typename... Args,
53 typename Char = enable_if_t<
54 internal::is_output_iterator<OutputIt>::value, char_t<S>>>
vformat_to(OutputIt out,const std::locale & loc,const S & format_str,format_args_t<OutputIt,Char> args)55 inline OutputIt vformat_to(OutputIt out, const std::locale& loc,
56 const S& format_str,
57 format_args_t<OutputIt, Char> args) {
58 using range = internal::output_range<OutputIt, Char>;
59 return vformat_to<arg_formatter<range>>(
60 range(out), to_string_view(format_str), args, internal::locale_ref(loc));
61 }
62
63 template <typename OutputIt, typename S, typename... Args,
64 FMT_ENABLE_IF(internal::is_output_iterator<OutputIt>::value&&
65 internal::is_string<S>::value)>
format_to(OutputIt out,const std::locale & loc,const S & format_str,Args &&...args)66 inline OutputIt format_to(OutputIt out, const std::locale& loc,
67 const S& format_str, Args&&... args) {
68 internal::check_format_string<Args...>(format_str);
69 using context = format_context_t<OutputIt, char_t<S>>;
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_
78