VTK  9.7.1
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 
56 namespace vtk
57 {
58 VTK_ABI_NAMESPACE_BEGIN
60 
67 VTK_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 }
81 template <typename T,
82  typename = FASTFLOAT_ENABLE_IF(fast_float::is_supported_integer_type<T>::value)>
83 VTK_ALWAYS_INLINE auto from_chars(
84  const char* first, const char* last, T& value, int base = 10) -> 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 
98 VTK_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 }
103 template <typename T,
104  typename = FASTFLOAT_ENABLE_IF(fast_float::is_supported_integer_type<T>::value)>
105 VTK_ALWAYS_INLINE auto from_chars(
106  const std::string_view str, T& value, int base = 10) -> 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  do \
115  { \
116  switch (from_chars_result.ec) \
117  { \
118  case std::errc::invalid_argument: \
119  { \
120  vtkLogF(ERROR, "The given argument was invalid, failed to get the converted " #value "."); \
121  command; \
122  } \
123  case std::errc::result_out_of_range: \
124  { \
125  vtkLogF(ERROR, "The result is out of range, failed to get the converted " #value "."); \
126  command; \
127  } \
128  default: \
129  { \
130  } \
131  } \
132  } while (0)
133 // Create a macro that evaluates the result of from_chars, checks for errors, and breaks
134 #define VTK_FROM_CHARS_RESULT_IF_ERROR_BREAK(from_chars_result, value) \
135  VTK_FROM_CHARS_RESULT_IF_ERROR_COMMAND(from_chars_result, value, break)
136 // Create a macro that evaluates the result of from_chars, checks for errors, and returns a value
137 #define VTK_FROM_CHARS_RESULT_IF_ERROR_RETURN(from_chars_result, value, returnValue) \
138  VTK_FROM_CHARS_RESULT_IF_ERROR_COMMAND(from_chars_result, value, return returnValue)
139 
140 // Helper for token‐pasting
141 #define VTK_FROM_CHARS_CONCAT_INNER(a, b) a##b
142 #define VTK_FROM_CHARS_CONCAT(a, b) VTK_FROM_CHARS_CONCAT_INNER(a, b)
143 
144 // Create a macro that executes from_chars, checks for errors, and executes a command
145 #define VTK_FROM_CHARS_IF_ERROR_COMMAND(string, value, command) \
146  auto VTK_FROM_CHARS_CONCAT(_from_chars_result_, __LINE__) = vtk::from_chars(string, value); \
147  VTK_FROM_CHARS_RESULT_IF_ERROR_COMMAND( \
148  VTK_FROM_CHARS_CONCAT(_from_chars_result_, __LINE__), value, command)
149 // Create a macro that executes from_chars, checks for errors, and breaks
150 #define VTK_FROM_CHARS_IF_ERROR_BREAK(string, value) \
151  VTK_FROM_CHARS_IF_ERROR_COMMAND(string, value, break)
152 // Create a macro that executes from_chars, checks for errors, and returns a value
153 #define VTK_FROM_CHARS_IF_ERROR_RETURN(string, value, returnValue) \
154  VTK_FROM_CHARS_IF_ERROR_COMMAND(string, value, return returnValue)
155 
156 // Create a macro that executes from_chars with a param, checks for errors, and executes a command
157 #define VTK_FROM_CHARS_WITH_PARAM_IF_ERROR_COMMAND(string, value, param, command) \
158  auto VTK_FROM_CHARS_CONCAT(_from_chars_result_, __LINE__) = \
159  vtk::from_chars(string, value, param); \
160  VTK_FROM_CHARS_RESULT_IF_ERROR_COMMAND( \
161  VTK_FROM_CHARS_CONCAT(_from_chars_result_, __LINE__), value, command)
162 // Create a macro that executes from_chars with param, checks for errors, and breaks
163 #define VTK_FROM_CHARS_WITH_PARAM_IF_ERROR_BREAK(string, value, param) \
164  VTK_FROM_CHARS_WITH_PARAM_IF_ERROR_COMMAND(string, value, param, break)
165 // Create a macro that executes from_chars with param, checks for errors, and returns a value
166 #define VTK_FROM_CHARS_WITH_PARAM_IF_ERROR_RETURN(string, value, param, returnValue) \
167  VTK_FROM_CHARS_WITH_PARAM_IF_ERROR_COMMAND(string, value, param, return returnValue)
168 
172 using scn::scan_result_type;
173 
179 using scn::runtime_format;
180 
182 
187 using scn::scan_int;
188 using scn::scan_value;
190 
197 using scn::scan;
198 
204 using scn::input;
205 
206 VTK_ABI_NAMESPACE_END
207 }
208 
209 #endif // vtkStringScanner_h
210 // 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;.