qxLib
format.h
Go to the documentation of this file.
1 /**
2 
3  @file format.h
4  @author Khrapov
5  @date 27.03.2026
6  @copyright (c) Nick Khrapov, 2026. All right reserved.
7 
8 **/
9 #pragma once
10 
12 
13 namespace qx
14 {
15 
16 /**
17  @brief std::format / fmt::format wrapper that returns qx::string
18  @tparam args_t - format arguments types
19  @param sFormat - format string
20  @param args - template parameter pack
21  @retval - formatted string
22 **/
23 template<class... args_t>
24  requires format_acceptable_args_c<char, args_t...>
25 // Passing by reference showed no consistent benefit in optimized benchmarks.
27  const QX_FMT_NS::format_string<std::type_identity_t<args_t>...> sFormat, //-V801
28  args_t&&... args) noexcept
29 {
30  // doesn't work for fmt for some reason
31 #if QX_CONF_FMT_LIB == QX_FMT_LIB_STD
32  return cstring::static_format(sFormat, std::forward<args_t>(args)...);
33 #else
35  cstring_view(sFormat.get().data(), sFormat.get().size()),
36  std::forward<args_t>(args)...);
37 
38 #endif
39 }
40 
41 /**
42  @brief std::format / fmt::format wrapper that returns qx::wstring
43  @tparam args_t - format arguments types
44  @param sFormat - format string
45  @param args - template parameter pack
46  @retval - formatted string
47 **/
48 template<class... args_t>
49  requires format_acceptable_args_c<wchar_t, args_t...>
50 // Passing by reference showed no consistent benefit in optimized benchmarks.
52  const QX_FMT_NS::wformat_string<std::type_identity_t<args_t>...> sFormat, //-V801
53  args_t&&... args) noexcept
54 {
55  return wstring::static_format(sFormat, std::forward<args_t>(args)...);
56 }
57 
58 /**
59  @brief Converts any type that has a QX_FMT_NS::formatter overload to qx::basic_string
60  @tparam T - object type
61  @tparam char_t - char type (char, wchar_t, etc)
62  @tparam traits_t - char traits. \see string_traits.h
63  @param value - object to convert
64  @retval - qx::basic_string
65 **/
66 template<class T, class char_t = char_type, class traits_t = string_traits::default_traits<char_t>>
68 {
70 }
71 
72 } // namespace qx
requires static format_acceptable_args_c< char, args_t... > basic_string static_format(const format_string_type< std::type_identity_t< args_t >... > sFormat, args_t &&... args) noexcept
Create a string by formatting it with the format string and the args.
requires static format_acceptable_args_c< char, args_t... > basic_string static_vformat(string_view svFormat, args_t &&... args)
Create a string by formatting it with the format string and the args.
requires(same_variadic_args_v< args_t... >) const expr auto coalesce(args_t &&... args)
Coalesce function, C# a ?? b analogue.
Definition: coalesce.inl:57
requires format_acceptable_args_c< char, args_t... > cstring format(const QX_FMT_NS::format_string< std::type_identity_t< args_t >... > sFormat, args_t &&... args) noexcept
std::format / fmt::format wrapper that returns qx::string
Definition: format.h:26
basic_string< char_t, traits_t > convert_to_string(const T &value) noexcept
Converts any type that has a QX_FMT_NS::formatter overload to qx::basic_string.
Definition: format.h:67
#define QX_STR_PREFIX(value_t, str)
Chose witch of prefixes add to string : L or none.
Definition: string_utils.h:120