36#ifndef vtkStringFormatter_h
37#define vtkStringFormatter_h
39#include "vtkCharConvCompatibility.h"
40#include "vtkCommonCoreModule.h"
45#include VTK_FMT(fmt/args.h)
46#include VTK_FMT(fmt/chrono.h)
47#include VTK_FMT(fmt/compile.h)
48#include VTK_FMT(fmt/format.h)
49#include VTK_FMT(fmt/ranges.h)
57VTK_ABI_NAMESPACE_BEGIN
67 typename = std::enable_if_t<std::is_integral_v<T> && !std::is_same_v<T, bool>>>
68VTK_ALWAYS_INLINE
auto to_chars(
char* first,
char* last,
const T& value,
int base = 10)
69 -> std::to_chars_result
71 const std::size_t buffer_size = std::distance(first, last);
74 return { first, std::errc::value_too_large };
76 const std::size_t buffer_size_1 = buffer_size - 1;
77 fmt::format_to_n_result<char*> result;
81 result = fmt::format_to_n(first, buffer_size_1, FMT_COMPILE(
"{:b}"), value);
84 result = fmt::format_to_n(first, buffer_size_1, FMT_COMPILE(
"{:o}"), value);
87 result = fmt::format_to_n(first, buffer_size_1, FMT_COMPILE(
"{:x}"), value);
91 result = fmt::format_to_n(first, buffer_size_1, FMT_COMPILE(
"{:d}"), value);
93 if (result.size >= buffer_size_1)
95 return { first + buffer_size_1, std::errc::value_too_large };
98 return { result.out, std::errc{} };
100template <
typename T,
typename = std::enable_if_t<std::is_
floating_po
int_v<T>>>
101VTK_ALWAYS_INLINE
auto to_chars(
char* first,
char* last,
const T& value, std::chars_format format)
102 -> std::to_chars_result
104 const std::size_t buffer_size = std::distance(first, last);
105 if (buffer_size == 0)
107 return { first, std::errc::value_too_large };
109 const std::size_t buffer_size_1 = buffer_size - 1;
110 fmt::format_to_n_result<char*> result;
113 case std::chars_format::scientific:
114 result = fmt::format_to_n(first, buffer_size_1, FMT_COMPILE(
"{:e}"), value);
116 case std::chars_format::fixed:
117 result = fmt::format_to_n(first, buffer_size_1, FMT_COMPILE(
"{:f}"), value);
119 case std::chars_format::hex:
120 result = fmt::format_to_n(first, buffer_size_1, FMT_COMPILE(
"{:a}"), value);
122 case std::chars_format::general:
124 result = fmt::format_to_n(first, buffer_size_1, FMT_COMPILE(
"{:g}"), value);
126 if (result.size >= buffer_size_1)
128 return { first + buffer_size_1, std::errc::value_too_large };
131 return { result.out, std::errc{} };
133template <
typename T,
typename = std::enable_if_t<std::is_
floating_po
int_v<T>>>
134VTK_ALWAYS_INLINE
auto to_chars(
char* first,
char* last,
const T& value, std::chars_format format,
135 int precision) -> std::to_chars_result
137 const std::size_t buffer_size = std::distance(first, last);
138 if (buffer_size == 0)
140 return { first, std::errc::value_too_large };
142 const std::size_t buffer_size_1 = buffer_size - 1;
143 fmt::format_to_n_result<char*> result;
146 case std::chars_format::scientific:
147 result = fmt::format_to_n(first, buffer_size_1, FMT_COMPILE(
"{:.{}e}"), value, precision);
149 case std::chars_format::fixed:
150 result = fmt::format_to_n(first, buffer_size_1, FMT_COMPILE(
"{:.{}f}"), value, precision);
152 case std::chars_format::hex:
153 result = fmt::format_to_n(first, buffer_size_1, FMT_COMPILE(
"{:.{}a}"), value, precision);
155 case std::chars_format::general:
157 result = fmt::format_to_n(first, buffer_size_1, FMT_COMPILE(
"{:.{}g}"), value, precision);
159 if (result.size >= buffer_size_1)
161 return { first + buffer_size_1, std::errc::value_too_large };
164 return { result.out, std::errc{} };
169#define VTK_TO_CHARS_RESULT_IF_ERROR_COMMAND(to_chars_result, value, command) \
170 switch (to_chars_result.ec) \
172 case std::errc::invalid_argument: \
174 vtkLogF(ERROR, "The given argument was invalid, failed to get the converted " #value "."); \
177 case std::errc::value_too_large: \
179 vtkLogF(ERROR, "The given buffer was too small, failed to get the converted " #value "."); \
187#define VTK_TO_CHARS_RESULT_IF_ERROR_BREAK(to_chars_result, value) \
188 VTK_TO_CHARS_RESULT_IF_ERROR_COMMAND(to_chars_result, value, break)
190#define VTK_TO_CHARS_RESULT_IF_ERROR_RETURN(to_chars_result, value, returnValue) \
191 VTK_TO_CHARS_RESULT_IF_ERROR_COMMAND(to_chars_result, value, return returnValue)
205using fmt::formatted_size;
217using fmt::format_to_result;
230using fmt::format_to_n_result;
239using fmt::format_to_n;
Specialization of tuple ranges and iterators for vtkAOSDataArrayTemplate.
VTK_ALWAYS_INLINE auto to_chars(char *first, char *last, const T &value, int base=10) -> std::to_chars_result
Given a number, convert it to a string within char* first and char* last, and return a to_chars_resul...
VTKCOMMONCORE_EXPORT std::string printf_to_std_format(const std::string &printf_format)
Convert a printf style format to a std::format style format.
VTKCOMMONCORE_EXPORT bool is_printf_format(const std::string &format)
Check if the given string is a printf style format.