wired
libc-free QUIC/HTTP3 SDK in C for x86_64-linux
Loading...
Searching...
No Matches
fmt.h File Reference

libc-free printf-compatible formatter. More...

Go to the source code of this file.

Macros

#define wired_va_start(ap, last)
 Begin reading variadic arguments after last.
#define wired_va_arg(ap, type)
 Fetch the next variadic argument as type.
#define wired_va_end(ap)
 End variadic argument reading.

Typedefs

typedef __builtin_va_list wired_va_list
 Variadic argument list (compiler builtin, not ISA specific).

Functions

usz wired_vsnprintf (char *out, usz cap, const char *fmt, wired_va_list ap)
 Format into out like C99 vsnprintf.
usz wired_snprintf (char *out, usz cap, const char *fmt,...)
 Format into out like C99 snprintf; see wired_vsnprintf.
usz wired_obuf_printf (wired_obuf *b, const char *fmt,...)
 Append formatted text to b at b->len, like snprintf into the free space.
ssz wired_dprintf (i64 fd, const char *fmt,...)
 Format and write to a file descriptor via write(2).

Detailed Description

libc-free printf-compatible formatter.

Conversions: d i u x X p s c %% f; length modifiers hh h l ll z; flags - 0 + space #; width, precision, and * for both.

Deviations from C99 (deliberate, to stay small):

  • p prints "0x" + lowercase hex, and NULL prints "0x0".
  • s with a NULL pointer prints "(null)" regardless of precision.
  • f rounds half-to-even on the exact binary value (as glibc does). Magnitudes at or above 2^64 saturate the integer part to 18446744073709551615; precision above 40 is clamped to 40; the fraction is exact to 2^-60.
  • An unknown conversion is copied through verbatim ("%" + the char).

Macro Definition Documentation

◆ wired_va_arg

#define wired_va_arg ( ap,
type )
Value:
__builtin_va_arg(ap, type)

Fetch the next variadic argument as type.

◆ wired_va_end

#define wired_va_end ( ap)
Value:
__builtin_va_end(ap)

End variadic argument reading.

◆ wired_va_start

#define wired_va_start ( ap,
last )
Value:
__builtin_va_start(ap, last)

Begin reading variadic arguments after last.

Function Documentation

◆ wired_dprintf()

ssz wired_dprintf ( i64 fd,
const char * fmt,
... )

Format and write to a file descriptor via write(2).

The output is formatted into a 512-byte buffer, so anything beyond 511 bytes is truncated; short writes are retried.

Parameters
fdfile descriptor
fmtprintf format string
Returns
bytes written, or the negative error from write(2)

◆ wired_obuf_printf()

usz wired_obuf_printf ( wired_obuf * b,
const char * fmt,
... )

Append formatted text to b at b->len, like snprintf into the free space.

At most cap - len - 1 bytes are appended (vsnprintf's NUL needs the last byte; it is not counted in len), so output that does not fit is cut.

Parameters
bbuffer to append to (e.g. an HTTP response body)
fmtprintf format string
Returns
bytes appended (b->len advanced by the same amount)

◆ wired_snprintf()

usz wired_snprintf ( char * out,
usz cap,
const char * fmt,
... )

Format into out like C99 snprintf; see wired_vsnprintf.

Parameters
outdestination
capcapacity of out in bytes, including the NUL
fmtprintf format string
Returns
the would-be length, excluding the NUL

◆ wired_vsnprintf()

usz wired_vsnprintf ( char * out,
usz cap,
const char * fmt,
wired_va_list ap )

Format into out like C99 vsnprintf.

Parameters
outdestination (may be NULL when cap is 0)
capcapacity of out in bytes, including the NUL
fmtprintf format string
aparguments
Returns
the length the full output would have (excluding the NUL); output is truncated and NUL-terminated when this is >= cap