C++ Essence Library 0.1.0
A Utility Library for Modern C++ Programming
Loading...
Searching...
No Matches
format_remediation.hpp
1/*
2 * Copyright (c) 2024 The RefValue Project
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 * THE SOFTWARE.
21 */
22
23#pragma once
24
25#include "basic_string.hpp"
26#include "globalization/globalized_arg.hpp"
27#include "zstring_view.hpp"
28
29#include <iterator>
30#include <locale>
31#include <string>
32#include <string_view>
33#include <utility>
34
35#if __has_include(<version> )
36#include <version>
37#endif
38
39#ifdef CPP_ESSENCE_USE_STD_FORMAT
40#define ES_FMT_NS std
41#include <format>
42#else
43#define ES_FMT_NS fmt
44#include <fmt/format.h>
45#include <fmt/xchar.h>
46#endif
47
48namespace essence {
49 using ES_FMT_NS::format;
50 using ES_FMT_NS::format_string;
51 using ES_FMT_NS::format_to;
52 using ES_FMT_NS::make_format_args;
53 using ES_FMT_NS::make_wformat_args;
54 using ES_FMT_NS::vformat;
55 using ES_FMT_NS::wformat_string;
56
66 template <typename... Args>
67 std::string gformat(
68 const std::locale& locale, format_string<globalization::globalized_arg_t<Args>...> fmt, Args&&... args) {
69 return format(locale, fmt, globalization::make_globalized_arg(locale, std::forward<Args>(args))...);
70 }
71
80 template <std_basic_string S = std::string, typename... Args>
81 S format_as(format_string<Args...> fmt, Args&&... args)
82 requires requires(S str) { format_to(std::back_inserter(str), fmt, std::forward<Args>(args)...); }
83 {
84 S result;
85
86 format_to(std::back_inserter(result), fmt, std::forward<Args>(args)...);
87 result.shrink_to_fit();
88
89 return result;
90 }
91
101 template <std_basic_string S = std::string, typename... Args>
102 S format_as(const std::locale& locale, format_string<Args...> fmt, Args&&... args)
103 requires requires(S str) { format_to(std::back_inserter(str), fmt, std::forward<Args>(args)...); }
104 {
105 S result;
106
107 format_to(std::back_inserter(result), locale, fmt, std::forward<Args>(args)...);
108 result.shrink_to_fit();
109
110 return result;
111 }
112
113
124 template <std_basic_string S = std::string, typename... Args>
125 S gformat_as(const std::locale& locale, format_string<globalization::globalized_arg_t<Args>...> fmt, Args&&... args)
126 requires requires(S str) {
127 format_to(
128 std::back_inserter(str), fmt, globalization::make_globalized_arg(locale, std::forward<Args>(args))...);
129 }
130 {
131 return format_as<S>(locale, fmt, globalization::make_globalized_arg(locale, std::forward<Args>(args))...);
132 }
133} // namespace essence
134
135template <typename CharT, typename Traits>
136struct ES_FMT_NS::formatter<essence::basic_zstring_view<CharT, Traits>, CharT>
137 : formatter<std::basic_string_view<CharT, Traits>, CharT> {
138 template <typename FormatContext>
139 auto format(essence::basic_zstring_view<CharT, Traits> str, FormatContext& ctx) const {
140 return formatter<std::basic_string_view<CharT, Traits>, CharT>::format(
141 std::basic_string_view<CharT, Traits>{str}, ctx);
142 }
143};
144
145#undef ES_FMT_NS
Definition zstring_view.hpp:34