VLink  2.3.0
A high-performance communication middleware
format.h 文件参考

Minimal heap-free {} placeholder formatter for the logger hot path. 更多...

#include <climits>
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <string>
#include <string_view>
#include <type_traits>
#include <utility>
#include "./macros.h"
format.h 的引用(Include)关系图:
此图展示该文件直接或间接的被哪些文件引用了:

浏览源代码.

类

struct  vlink::format::detail::IsOutputIteratorImpl< TypeT, typename >
 
struct  vlink::format::detail::IsOutputIteratorImpl< TypeT, std::enable_if_t< std::is_assignable_v< decltype(*std::declval< TypeT & >()++), char > > >
 
struct  vlink::format::detail::IsOutputIteratorImpl< char * >
 
struct  vlink::format::detail::IsOutputIteratorImpl< TypeT[NumT]>
 
struct  vlink::format::detail::TypeConstant< T >
 
struct  vlink::format::detail::TypeConstant< signed char >
 
struct  vlink::format::detail::TypeConstant< unsigned char >
 
struct  vlink::format::detail::TypeConstant< short >
 
struct  vlink::format::detail::TypeConstant< unsigned short >
 
struct  vlink::format::detail::TypeConstant< int >
 
struct  vlink::format::detail::TypeConstant< unsigned >
 
struct  vlink::format::detail::TypeConstant< long >
 
struct  vlink::format::detail::TypeConstant< unsigned long >
 
struct  vlink::format::detail::TypeConstant< long long >
 
struct  vlink::format::detail::TypeConstant< unsigned long long >
 
struct  vlink::format::detail::TypeConstant< bool >
 
struct  vlink::format::detail::TypeConstant< char >
 
struct  vlink::format::detail::TypeConstant< float >
 
struct  vlink::format::detail::TypeConstant< double >
 
struct  vlink::format::detail::TypeConstant< long double >
 
struct  vlink::format::detail::TypeConstant< std::nullptr_t >
 
struct  vlink::format::detail::TypeConstant< const char * >
 
struct  vlink::format::detail::TypeConstant< char * >
 
struct  vlink::format::detail::TypeConstant< std::string_view >
 
struct  vlink::format::detail::TypeConstant< std::string >
 
struct  vlink::format::detail::TypeConstant< char[NumT]>
 
struct  vlink::format::detail::TypeConstant< const char[NumT]>
 
struct  vlink::format::detail::TypeConstant< T * >
 
struct  vlink::format::detail::HasFormatAs< TypeT, typename >
 
struct  vlink::format::detail::HasFormatAs< TypeT, std::void_t< decltype(format_as(std::declval< const TypeT & >()))> >
 
struct  vlink::format::detail::FormatSpec
 
class  vlink::format::detail::StringWriter
 
class  vlink::format::detail::IteratorWriter< OutputItT >
 
class  vlink::format::detail::Value< CharT >
 
class  vlink::format::detail::FormatArg< CharT >
 
struct  vlink::format::detail::FormatArgStore< CharT, ArgsT >
 
class  vlink::format::detail::BasicFormatArgs< CharT >
 
class  vlink::format::detail::FormatWriter< CharT, WriterT >
 
struct  vlink::format::FString< ArgsT >
 
struct  vlink::format::FormatToNResult< OutputItT >
 
struct  vlink::format::FormatToResult
 

命名空间

 
 
 

类型定义

template<typename TypeT >
using vlink::format::detail::RemoveCvref = typename std::remove_cv_t< std::remove_reference_t< TypeT > >
 
using vlink::format::detail::FormatArgs = BasicFormatArgs< char >
 
template<typename... ArgsT>
using vlink::format::format_string = typename FString< ArgsT... >::t
 

枚举

enum class  vlink::format::detail::Type : uint8_t {
  vlink::format::detail::kNone , vlink::format::detail::kInt , vlink::format::detail::kUint , vlink::format::detail::kLongLong ,
  vlink::format::detail::kUlongLong , vlink::format::detail::kBool , vlink::format::detail::kChar , vlink::format::detail::kFloat ,
  vlink::format::detail::kDouble , vlink::format::detail::kLongDouble , vlink::format::detail::kString , vlink::format::detail::kCstring ,
  vlink::format::detail::kPointer
}
 
enum class  vlink::format::detail::Align : uint8_t { vlink::format::detail::kNone , vlink::format::detail::kLeft , vlink::format::detail::kRight , vlink::format::detail::kCenter }
 
enum class  vlink::format::detail::Sign : uint8_t { vlink::format::detail::kMinus , vlink::format::detail::kPlus , vlink::format::detail::kSpace }
 

函数

VLINK_EXPORT size_t vlink::format::detail::format_uint_to (char *buf, unsigned value) noexcept
 
VLINK_EXPORT size_t vlink::format::detail::format_int_to (char *buf, int value) noexcept
 
VLINK_EXPORT size_t vlink::format::detail::format_ulong_long_to (char *buf, unsigned long long value) noexcept
 
VLINK_EXPORT size_t vlink::format::detail::format_long_long_to (char *buf, long long value) noexcept
 
VLINK_EXPORT size_t vlink::format::detail::format_pointer_to (char *buf, const void *ptr) noexcept
 
VLINK_EXPORT size_t vlink::format::detail::format_float_to (char *buf, size_t buflen, float value) noexcept
 
VLINK_EXPORT size_t vlink::format::detail::format_double_to (char *buf, size_t buflen, double value) noexcept
 
VLINK_EXPORT size_t vlink::format::detail::format_long_double_to (char *buf, size_t buflen, long double value) noexcept
 
VLINK_EXPORT size_t vlink::format::detail::format_double_spec_to (char *buf, size_t buflen, double value, char type, int precision, bool alt) noexcept
 
VLINK_EXPORT size_t vlink::format::detail::format_long_double_spec_to (char *buf, size_t buflen, long double value, char type, int precision, bool alt) noexcept
 
VLINK_EXPORT const char * vlink::format::detail::parse_spec_ref (const char *p, const char *end, int &ref) noexcept
 
VLINK_EXPORT const char * vlink::format::detail::parse_format_spec (const char *p, const char *end, FormatSpec &spec) noexcept
 
VLINK_EXPORT size_t vlink::format::detail::format_base_digits_to (char(&buf)[64], unsigned long long value, unsigned base_bits, bool upper) noexcept
 
template<typename... ArgsT>
detail::FormatArgStore< char, detail::RemoveCvref< ArgsT >... > vlink::format::make_format_args (const ArgsT &... args)
 Public API 更多...
 
template<typename... ArgsT>
FormatToNResult< char * > vlink::format::format_to_n (char *out, size_t n, format_string< ArgsT... > fmt, const ArgsT &... args)
 Formats args into out, writing at most n characters. 更多...
 
template<size_t NumT, typename... ArgsT>
FormatToResult vlink::format::format_to (char(&out)[NumT], format_string< ArgsT... > fmt, const ArgsT &... args)
 Formats args into a fixed-size character array; equivalent to format_to_n. 更多...
 
template<typename OutputItT , typename... ArgsT, std::enable_if_t< detail::kIsOutputIterator< detail::RemoveCvref< OutputItT >> &&!std::is_array_v< std::remove_reference_t< OutputItT >>, int > = 0>
detail::RemoveCvref< OutputItT > vlink::format::format_to (OutputItT &&out, format_string< ArgsT... > fmt, const ArgsT &... args)
 Formats args through an output iterator. 更多...
 
template<typename... ArgsT>
std::string vlink::format::format (format_string< ArgsT... > fmt, const ArgsT &... args)
 Formats args into a newly allocated std::string. 更多...
 
template<typename UIntT >
int vlink::format::detail::count_digits (UIntT n) noexcept
 
template<typename UIntT >
void vlink::format::detail::write_int_digits (char *buf, UIntT value, int num_digits) noexcept
 

变量

template<typename TypeT >
constexpr bool vlink::format::detail::kIsOutputIterator = IsOutputIteratorImpl<TypeT>::value
 
constexpr int vlink::format::detail::kSpecRefNone = -1
 
constexpr int vlink::format::detail::kSpecRefAuto = -2
 

详细描述

Minimal heap-free {} placeholder formatter for the logger hot path.

VLink targets C++17 first; std::format is unavailable on that baseline and far heavier on older standards. This header provides a focused subset of std::format-style formatting that writes through a stack-allocated buffer or a user-supplied output iterator, never allocates, and dispatches via a compile-time type tag rather than a virtual call chain.

Supported argument types
C++ type Example output
all integral types incl. char and bool 42 / A / true
float / double / long double 3.14
const char* / std::string / std::string_view hello
T* (any pointer) and nullptr 0x7ffe1234 / 0x0
enum underlying integer
any type with an ADL format_as(T) mapped result
Placeholder syntax
Token Meaning
{} Consume the next argument in order
{0}, {1}, ... Explicit positional index
{:spec}, {0:spec} Apply a std::format-style format spec
{{ / }} Literal opening / closing brace
Format specification

The std::format grammar: [[fill]align][sign][#][0][width][.precision][type]

  • fill + align: any byte except braces before < (left), > (right), ^ (center).
  • sign: + / - / space for arithmetic presentations.
  • #: 0b / 0B / 0 / 0x / 0X prefix; floats keep the decimal point and g / G keep trailing zeros.
  • 0: zero padding after sign/prefix; ignored with an explicit align.
  • width / precision: literal digits, or dynamic {} / {n} consuming an integral argument; precision also truncates strings.
  • type: b B c d o x X integral; a A e E f F g G floating; s string/bool; ? escaped debug string/char; p pointer.
Public API
Function Output target Allocates
format_to_n(out, n, fmt, ...) char* buffer with cap n no
format_to(out[N], fmt, ...) Fixed-size array no
format_to(it, fmt, ...) Output iterator no
format(fmt, ...) Returned std::string yes
Example
char buf[128];
auto result = vlink::format::format_to_n(buf, sizeof(buf) - 1, "x={} y={:.1f}", 3, 4.5);
buf[result.size] = '\0';
// buf == "x=3 y=4.5"
注解
Deviations from std::format: locale (L) is not supported, the fill byte is single-byte, and invalid or inapplicable spec fields are ignored instead of throwing, so formatting never fails at runtime. A dynamic width/precision referencing a missing, non-integral or negative argument degrades to "not specified". Width and precision count bytes, so multi-byte UTF-8 sequences may be split by a string precision. Floating precision is clamped to 340 digits. A c presentation outside the char range and a c presentation of bool fall back to the decimal form. Without a spec (and for {:#} without type and precision), floating values use the "%g" 6 significant digit representation. Platforms without floating-point std::to_chars format floats through snprintf with printf semantics, including the a representation. The ? presentation escapes ASCII control bytes, quotes and backslashes; string bytes at or above 0x80 pass through unescaped while a lone char code unit above 0x7F is escaped as \x{..}. Unsupported argument types trigger a compile-time static_assert.
The allocating entry point shares its name with this namespace; call it fully qualified as vlink::format::format(...). vlink::format(...) resolves to the namespace and does not compile.
Custom types opt in by defining format_as(const T&) in the namespace of T, returning a directly formattable value; specs then apply to the mapped result. Class-type results must be returned by reference or as std::string_view.