VTK  9.5.20250824
vtkStringScanner.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
2// SPDX-License-Identifier: BSD-3-Clause
38#ifndef vtkStringScanner_h
39#define vtkStringScanner_h
40
41#include "vtkCharConvCompatibility.h" // For std::chars_format, std::from_chars_result
42#include "vtkCommonCoreModule.h" // For export macro
43#include "vtkLogger.h" // For vtkLogF
44
45#include "vtkfast_float.h" // For fast_float
46
47#include "vtk_scn.h" // For scn
48// clang-format off
49#include VTK_SCN(scn/chrono.h) // For scn's chrono
50#include VTK_SCN(scn/scan.h) // For scn's scan
51// clang-format on
52
53#include <array> // For std::array
54#include <string_view> // For std::string_view
55
56namespace vtk
57{
58VTK_ABI_NAMESPACE_BEGIN
60
66template <typename T, typename = FASTFLOAT_ENABLE_IF(fast_float::is_supported_float_type<T>::value)>
67VTK_ALWAYS_INLINE auto from_chars(const char* first, const char* last, T& value,
68 std::chars_format format = std::chars_format::general) -> std::from_chars_result
69{
70 static constexpr std::array<fast_float::chars_format, 5> std_to_fast_float_chars_format = { {
71 fast_float::chars_format::general, // 0: Default general
72 fast_float::chars_format::scientific, // 1: scientific
73 fast_float::chars_format::fixed, // 2: fixed
74 fast_float::chars_format::general, // 3: general
75 fast_float::chars_format::hex, // 4: hex
76 } };
77 auto result = fast_float::from_chars<T>(first, last, value,
78 std_to_fast_float_chars_format[static_cast<std::underlying_type_t<std::chars_format>>(format)]);
79 return { result.ptr, result.ec };
80}
81template <typename T,
82 typename = FASTFLOAT_ENABLE_IF(fast_float::is_supported_integer_type<T>::value)>
83VTK_ALWAYS_INLINE auto from_chars(const char* first, const char* last, T& value, int base = 10)
84 -> std::from_chars_result
85{
86 auto result = fast_float::from_chars<T>(first, last, value, base);
87 return { result.ptr, result.ec };
88}
90
92
97template <typename T, typename = FASTFLOAT_ENABLE_IF(fast_float::is_supported_float_type<T>::value)>
98VTK_ALWAYS_INLINE auto from_chars(const std::string_view str, T& value,
99 std::chars_format format = std::chars_format::general) -> std::from_chars_result
100{
101 return vtk::from_chars<T>(str.data(), str.data() + str.size(), value, format);
102}
103template <typename T,
104 typename = FASTFLOAT_ENABLE_IF(fast_float::is_supported_integer_type<T>::value)>
105VTK_ALWAYS_INLINE auto from_chars(const std::string_view str, T& value, int base = 10)
106 -> std::from_chars_result
107{
108 return vtk::from_chars<T>(str.data(), str.data() + str.size(), value, base);
109}
111
112// Create a macro that evaluates the result of from_chars, checks for errors, and executes a command
113#define VTK_FROM_CHARS_RESULT_IF_ERROR_COMMAND(from_chars_result, value, command) \
114 switch (from_chars_result.ec) \
115 { \
116 case std::errc::invalid_argument: \
117 { \
118 vtkLogF(ERROR, "The given argument was invalid, failed to get the converted " #value "."); \
119 command; \
120 } \
121 case std::errc::result_out_of_range: \
122 { \
123 vtkLogF(ERROR, "The result is out of range, failed to get the converted " #value "."); \
124 command; \
125 } \
126 default: \
127 { \
128 } \
129 }
130// Create a macro that evaluates the result of from_chars, checks for errors, and breaks
131#define VTK_FROM_CHARS_RESULT_IF_ERROR_BREAK(from_chars_result, value) \
132 VTK_FROM_CHARS_RESULT_IF_ERROR_COMMAND(from_chars_result, value, break)
133// Create a macro that evaluates the result of from_chars, checks for errors, and returns a value
134#define VTK_FROM_CHARS_RESULT_IF_ERROR_RETURN(from_chars_result, value, returnValue) \
135 VTK_FROM_CHARS_RESULT_IF_ERROR_COMMAND(from_chars_result, value, return returnValue)
136
137// Helper for token‐pasting
138#define VTK_FROM_CHARS_CONCAT_INNER(a, b) a##b
139#define VTK_FROM_CHARS_CONCAT(a, b) VTK_FROM_CHARS_CONCAT_INNER(a, b)
140
141// Create a macro that executes from_chars, checks for errors, and executes a command
142#define VTK_FROM_CHARS_IF_ERROR_COMMAND(string, value, command) \
143 auto VTK_FROM_CHARS_CONCAT(_from_chars_result_, __LINE__) = vtk::from_chars(string, value); \
144 VTK_FROM_CHARS_RESULT_IF_ERROR_COMMAND( \
145 VTK_FROM_CHARS_CONCAT(_from_chars_result_, __LINE__), value, command)
146// Create a macro that executes from_chars, checks for errors, and breaks
147#define VTK_FROM_CHARS_IF_ERROR_BREAK(string, value) \
148 VTK_FROM_CHARS_IF_ERROR_COMMAND(string, value, break)
149// Create a macro that executes from_chars, checks for errors, and returns a value
150#define VTK_FROM_CHARS_IF_ERROR_RETURN(string, value, returnValue) \
151 VTK_FROM_CHARS_IF_ERROR_COMMAND(string, value, return returnValue)
152
153// Create a macro that executes from_chars with a param, checks for errors, and executes a command
154#define VTK_FROM_CHARS_WITH_PARAM_IF_ERROR_COMMAND(string, value, param, command) \
155 auto VTK_FROM_CHARS_CONCAT(_from_chars_result_, __LINE__) = \
156 vtk::from_chars(string, value, param); \
157 VTK_FROM_CHARS_RESULT_IF_ERROR_COMMAND( \
158 VTK_FROM_CHARS_CONCAT(_from_chars_result_, __LINE__), value, command)
159// Create a macro that executes from_chars with param, checks for errors, and breaks
160#define VTK_FROM_CHARS_WITH_PARAM_IF_ERROR_BREAK(string, value, param) \
161 VTK_FROM_CHARS_WITH_PARAM_IF_ERROR_COMMAND(string, value, param, break)
162// Create a macro that executes from_chars with param, checks for errors, and returns a value
163#define VTK_FROM_CHARS_WITH_PARAM_IF_ERROR_RETURN(string, value, param, returnValue) \
164 VTK_FROM_CHARS_WITH_PARAM_IF_ERROR_COMMAND(string, value, param, return returnValue)
165
169using scn::scan_result_type;
170
172
177using scn::scan_int;
178using scn::scan_value;
180
187using scn::scan;
188
194using scn::input;
195
196VTK_ABI_NAMESPACE_END
197}
198
199#endif // vtkStringScanner_h
200// VTK-HeaderTest-Exclude: vtkStringScanner.h
Specialization of tuple ranges and iterators for vtkAOSDataArrayTemplate.
VTK_ALWAYS_INLINE auto from_chars(const char *first, const char *last, T &value, std::chars_format format=std::chars_format::general) -> std::from_chars_result
Given a char* first and char* last, convert it to a number, and return a from_chars_result;.