VTK  9.5.20250816
Namespaces | Macros | Functions
vtkStringFormatter.h File Reference

Optimized C++ utilities for formatting values to strings and files. More...

#include "vtkCharConvCompatibility.h"
#include "vtkCommonCoreModule.h"
#include "vtkLogger.h"
#include "vtk_fmt.h"
#include <VTK_FMT(fmt/args.h)>
#include <VTK_FMT(fmt/chrono.h)>
#include <VTK_FMT(fmt/compile.h)>
#include <VTK_FMT(fmt/format.h)>
#include <VTK_FMT(fmt/ranges.h)>
#include <string>
#include <string_view>
Include dependency graph for vtkStringFormatter.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Namespaces

namespace  vtk
 Specialization of tuple ranges and iterators for vtkAOSDataArrayTemplate.
 

Macros

#define VTK_TO_CHARS_RESULT_IF_ERROR_COMMAND(to_chars_result, value, command)
 
#define VTK_TO_CHARS_RESULT_IF_ERROR_BREAK(to_chars_result, value)    VTK_TO_CHARS_RESULT_IF_ERROR_COMMAND(to_chars_result, value, break)
 
#define VTK_TO_CHARS_RESULT_IF_ERROR_RETURN(to_chars_result, value, returnValue)    VTK_TO_CHARS_RESULT_IF_ERROR_COMMAND(to_chars_result, value, return returnValue)
 

Functions

VTKCOMMONCORE_EXPORT bool vtk::is_printf_format (const std::string &format)
 Check if the given string is a printf style format.
 
VTKCOMMONCORE_EXPORT std::string vtk::printf_to_std_format (const std::string &printf_format)
 Convert a printf style format to a std::format style format.
 
template<typename T , typename = std::enable_if_t<std::is_integral_v<T> && !std::is_same_v<T, bool>>>
VTK_ALWAYS_INLINE auto vtk::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_result;.
 
template<typename T , typename = std::enable_if_t<std::is_floating_point_v<T>>>
VTK_ALWAYS_INLINE auto vtk::to_chars (char *first, char *last, const T &value, std::chars_format format) -> std::to_chars_result
 Given a number, convert it to a string within char* first and char* last, and return a to_chars_result;.
 
template<typename T , typename = std::enable_if_t<std::is_floating_point_v<T>>>
VTK_ALWAYS_INLINE auto vtk::to_chars (char *first, char *last, const T &value, std::chars_format format, int precision) -> std::to_chars_result
 Given a number, convert it to a string within char* first and char* last, and return a to_chars_result;.
 

Detailed Description

Optimized C++ utilities for formatting values to strings and files.

This header provides efficient, alternatives to common C/C++ string handling functions such as printf, strtol etc.

It includes utilities for converting strings to numbers and scanning values from strings and files.

Refer to the documentation for guidance on replacing standard C functions with their modern, type-safe counterparts provided here.

  1. C/C++ has the following functions to convert one/many numbers to a char* or string.
    1. itoa/_itoa, ltoa/_ltoa, lltoa/_i64toa, ultoa/_ultoa, ulltoa/_ulltoa/_ui64toa
    2. sprintf, sprintf_s, vsprintf, vsprintf_s,
    3. snprintf, snprintf_s, vsnprintf, vsnprintf_s,
    4. strftime
    5. std::to_chars, std::to_string
    6. std::put_time These functions should be replaced by:
    1. vtk::to_chars or vtk::to_string, if one number needs to be converted
    2. vtk::format, vtk::format_to, or vtk::format_to_n, if one/many numbers need to be converted with a specific format
  2. C/C++ has the following functions to print one/many numbers to stdout/file.
    1. printf, printf_s, vprintf, vprintf_s,
    2. fprintf, fprintf_s, vfprintf, vfprintf_s,
    3. std::cout, std::ofstream, vtksys::ofstream These functions should be replaced by:
    1. vtk::print, vtk::println

Definition in file vtkStringFormatter.h.

Macro Definition Documentation

◆ VTK_TO_CHARS_RESULT_IF_ERROR_COMMAND

#define VTK_TO_CHARS_RESULT_IF_ERROR_COMMAND (   to_chars_result,
  value,
  command 
)
Value:
switch (to_chars_result.ec) \
{ \
case std::errc::invalid_argument: \
{ \
vtkLogF(ERROR, "The given argument was invalid, failed to get the converted " #value "."); \
command; \
} \
case std::errc::value_too_large: \
{ \
vtkLogF(ERROR, "The given buffer was too small, failed to get the converted " #value "."); \
command; \
} \
default: \
{ \
} \
}

Definition at line 169 of file vtkStringFormatter.h.

◆ VTK_TO_CHARS_RESULT_IF_ERROR_BREAK

#define VTK_TO_CHARS_RESULT_IF_ERROR_BREAK (   to_chars_result,
  value 
)     VTK_TO_CHARS_RESULT_IF_ERROR_COMMAND(to_chars_result, value, break)

Definition at line 187 of file vtkStringFormatter.h.

◆ VTK_TO_CHARS_RESULT_IF_ERROR_RETURN

#define VTK_TO_CHARS_RESULT_IF_ERROR_RETURN (   to_chars_result,
  value,
  returnValue 
)     VTK_TO_CHARS_RESULT_IF_ERROR_COMMAND(to_chars_result, value, return returnValue)

Definition at line 190 of file vtkStringFormatter.h.