blob: 7301bf92a2432975cd759de8b60aa1e61fe36607 [file] [log] [blame]
Martin Stjernholm4fb51112021-04-30 11:53:52 +01001// 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
13#include "format.h"
14
15FMT_BEGIN_NAMESPACE
16
17namespace detail {
18template <typename Char>
19std::basic_string<Char> vformat(
20 const std::locale& loc, basic_string_view<Char> format_str,
21 basic_format_args<buffer_context<type_identity_t<Char>>> args) {
22 basic_memory_buffer<Char> buffer;
23 detail::vformat_to(buffer, format_str, args, detail::locale_ref(loc));
24 return fmt::to_string(buffer);
25}
26} // namespace detail
27
28template <typename S, typename Char = char_t<S>>
29inline std::basic_string<Char> vformat(
30 const std::locale& loc, const S& format_str,
31 basic_format_args<buffer_context<type_identity_t<Char>>> args) {
32 return detail::vformat(loc, to_string_view(format_str), args);
33}
34
35template <typename S, typename... Args, typename Char = char_t<S>>
36inline std::basic_string<Char> format(const std::locale& loc,
37 const S& format_str, Args&&... args) {
38 return detail::vformat(loc, to_string_view(format_str),
39 fmt::make_args_checked<Args...>(format_str, args...));
40}
41
42template <typename S, typename OutputIt, typename... Args,
43 typename Char = char_t<S>,
44 FMT_ENABLE_IF(detail::is_output_iterator<OutputIt, Char>::value)>
45inline OutputIt vformat_to(
46 OutputIt out, const std::locale& loc, const S& format_str,
47 basic_format_args<buffer_context<type_identity_t<Char>>> args) {
48 decltype(detail::get_buffer<Char>(out)) buf(detail::get_buffer_init(out));
49 vformat_to(buf, to_string_view(format_str), args, detail::locale_ref(loc));
50 return detail::get_iterator(buf);
51}
52
53template <typename OutputIt, typename S, typename... Args,
54 bool enable = detail::is_output_iterator<OutputIt, char_t<S>>::value>
55inline auto format_to(OutputIt out, const std::locale& loc,
56 const S& format_str, Args&&... args) ->
57 typename std::enable_if<enable, OutputIt>::type {
58 const auto& vargs = fmt::make_args_checked<Args...>(format_str, args...);
59 return vformat_to(out, loc, to_string_view(format_str), vargs);
60}
61
62FMT_END_NAMESPACE
63
64#endif // FMT_LOCALE_H_