mirror of
https://github.com/fmtlib/fmt.git
synced 2025-07-31 03:07:36 +02:00
Add fprintf and write docs.
This commit is contained in:
@ -6,8 +6,8 @@ Usage
|
|||||||
-----
|
-----
|
||||||
|
|
||||||
To use the C++ Format library, add ``format.h`` and ``format.cc`` from
|
To use the C++ Format library, add ``format.h`` and ``format.cc`` from
|
||||||
a `release archive <https://github.com/cppformat/cppformat/releases/latest>`__
|
a `release archive <https://github.com/cppformat/cppformat/releases/latest>`_
|
||||||
or the `Git repository <https://github.com/cppformat/cppformat>`__ to your project.
|
or the `Git repository <https://github.com/cppformat/cppformat>`_ to your project.
|
||||||
|
|
||||||
If you are using Visual C++ with precompiled headers, you might need to add
|
If you are using Visual C++ with precompiled headers, you might need to add
|
||||||
the line
|
the line
|
||||||
@ -25,6 +25,20 @@ All functions and classes provided by the C++ Format library reside
|
|||||||
in namespace ``fmt`` and macros have prefix ``FMT_``. For brevity the
|
in namespace ``fmt`` and macros have prefix ``FMT_``. For brevity the
|
||||||
namespace is usually omitted in examples.
|
namespace is usually omitted in examples.
|
||||||
|
|
||||||
|
Formatting functions
|
||||||
|
^^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
|
The following functions use `format string syntax`_ similar to the one
|
||||||
|
used by Python's `str.format
|
||||||
|
<http://docs.python.org/3/library/stdtypes.html#str.format>`_ function.
|
||||||
|
They take *format_str* and *args* as arguments.
|
||||||
|
|
||||||
|
*format_str* is a format string that contains literal text and replacement
|
||||||
|
fields surrounded by braces ``{}``. The fields are replaced with formatted
|
||||||
|
arguments in the resulting string.
|
||||||
|
|
||||||
|
*args* is an argument list representing arbitrary arguments.
|
||||||
|
|
||||||
.. doxygenfunction:: fmt::format(StringRef, const ArgList &)
|
.. doxygenfunction:: fmt::format(StringRef, const ArgList &)
|
||||||
|
|
||||||
.. doxygenfunction:: fmt::print(StringRef, const ArgList &)
|
.. doxygenfunction:: fmt::print(StringRef, const ArgList &)
|
||||||
@ -33,20 +47,25 @@ namespace is usually omitted in examples.
|
|||||||
|
|
||||||
.. doxygenfunction:: fmt::print(std::ostream &, StringRef, const ArgList &)
|
.. doxygenfunction:: fmt::print(std::ostream &, StringRef, const ArgList &)
|
||||||
|
|
||||||
.. doxygendefine:: FMT_VARIADIC
|
Printf formatting functions
|
||||||
|
^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
.. doxygenclass:: fmt::BasicWriter
|
The following functions use `printf format string syntax
|
||||||
:members:
|
<http://pubs.opengroup.org/onlinepubs/009695399/functions/fprintf.html>`_ with
|
||||||
|
a POSIX extension for positional arguments.
|
||||||
|
|
||||||
.. doxygenclass:: fmt::ArgList
|
.. doxygenfunction:: fmt::printf(StringRef, const ArgList &)
|
||||||
:members:
|
|
||||||
|
|
||||||
.. doxygenclass:: fmt::BasicStringRef
|
.. doxygenfunction:: fmt::fprintf(std::FILE *, StringRef, const ArgList &)
|
||||||
:members:
|
|
||||||
|
.. doxygenfunction:: fmt::sprintf(StringRef, const ArgList &)
|
||||||
|
|
||||||
Write API
|
Write API
|
||||||
---------
|
---------
|
||||||
|
|
||||||
|
.. doxygenclass:: fmt::BasicWriter
|
||||||
|
:members:
|
||||||
|
|
||||||
.. doxygenfunction:: fmt::bin
|
.. doxygenfunction:: fmt::bin
|
||||||
|
|
||||||
.. doxygenfunction:: fmt::oct
|
.. doxygenfunction:: fmt::oct
|
||||||
@ -59,6 +78,17 @@ Write API
|
|||||||
|
|
||||||
.. _formatstrings:
|
.. _formatstrings:
|
||||||
|
|
||||||
|
Utilities
|
||||||
|
---------
|
||||||
|
|
||||||
|
.. doxygendefine:: FMT_VARIADIC
|
||||||
|
|
||||||
|
.. doxygenclass:: fmt::ArgList
|
||||||
|
:members:
|
||||||
|
|
||||||
|
.. doxygenclass:: fmt::BasicStringRef
|
||||||
|
:members:
|
||||||
|
|
||||||
System Errors
|
System Errors
|
||||||
-------------
|
-------------
|
||||||
|
|
||||||
|
18
format.cc
18
format.cc
@ -1248,21 +1248,15 @@ void fmt::report_windows_error(
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
void fmt::print(StringRef format, const ArgList &args) {
|
void fmt::print(std::FILE *f, StringRef format_str, const ArgList &args) {
|
||||||
Writer w;
|
Writer w;
|
||||||
w.write(format, args);
|
w.write(format_str, args);
|
||||||
std::fwrite(w.data(), 1, w.size(), stdout);
|
|
||||||
}
|
|
||||||
|
|
||||||
void fmt::print(std::FILE *f, StringRef format, const ArgList &args) {
|
|
||||||
Writer w;
|
|
||||||
w.write(format, args);
|
|
||||||
std::fwrite(w.data(), 1, w.size(), f);
|
std::fwrite(w.data(), 1, w.size(), f);
|
||||||
}
|
}
|
||||||
|
|
||||||
void fmt::print(std::ostream &os, StringRef format, const ArgList &args) {
|
void fmt::print(std::ostream &os, StringRef format_str, const ArgList &args) {
|
||||||
Writer w;
|
Writer w;
|
||||||
w.write(format, args);
|
w.write(format_str, args);
|
||||||
os.write(w.data(), w.size());
|
os.write(w.data(), w.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1274,10 +1268,10 @@ void fmt::print_colored(Color c, StringRef format, const ArgList &args) {
|
|||||||
std::fputs(RESET_COLOR, stdout);
|
std::fputs(RESET_COLOR, stdout);
|
||||||
}
|
}
|
||||||
|
|
||||||
void fmt::printf(StringRef format, const ArgList &args) {
|
int fmt::fprintf(std::FILE *f, StringRef format, const ArgList &args) {
|
||||||
Writer w;
|
Writer w;
|
||||||
printf(w, format, args);
|
printf(w, format, args);
|
||||||
std::fwrite(w.data(), 1, w.size(), stdout);
|
return std::fwrite(w.data(), 1, w.size(), f);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Explicit instantiations for char.
|
// Explicit instantiations for char.
|
||||||
|
78
format.h
78
format.h
@ -1745,21 +1745,11 @@ void print_colored(Color c, StringRef format, const ArgList &args);
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
\rst
|
\rst
|
||||||
Formats a string similarly to Python's `str.format
|
Formats arguments and returns the result as a string.
|
||||||
<http://docs.python.org/3/library/stdtypes.html#str.format>`__ function
|
|
||||||
and returns the result as a string.
|
|
||||||
|
|
||||||
*format_str* is a format string that contains literal text and replacement
|
|
||||||
fields surrounded by braces ``{}``. The fields are replaced with formatted
|
|
||||||
arguments in the resulting string.
|
|
||||||
|
|
||||||
*args* is an argument list representing arbitrary arguments.
|
|
||||||
|
|
||||||
**Example**::
|
**Example**::
|
||||||
|
|
||||||
std::string message = format("The answer is {}", 42);
|
std::string message = format("The answer is {}", 42);
|
||||||
|
|
||||||
See also `Format String Syntax`_.
|
|
||||||
\endrst
|
\endrst
|
||||||
*/
|
*/
|
||||||
inline std::string format(StringRef format_str, const ArgList &args) {
|
inline std::string format(StringRef format_str, const ArgList &args) {
|
||||||
@ -1774,6 +1764,17 @@ inline std::wstring format(WStringRef format_str, const ArgList &args) {
|
|||||||
return w.str();
|
return w.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
\rst
|
||||||
|
Prints formatted data to the file *f*.
|
||||||
|
|
||||||
|
**Example**::
|
||||||
|
|
||||||
|
print(stderr, "Don't {}!", "panic");
|
||||||
|
\endrst
|
||||||
|
*/
|
||||||
|
void print(std::FILE *f, StringRef format_str, const ArgList &args);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
\rst
|
\rst
|
||||||
Prints formatted data to ``stdout``.
|
Prints formatted data to ``stdout``.
|
||||||
@ -1783,29 +1784,20 @@ inline std::wstring format(WStringRef format_str, const ArgList &args) {
|
|||||||
print("Elapsed time: {0:.2f} seconds", 1.23);
|
print("Elapsed time: {0:.2f} seconds", 1.23);
|
||||||
\endrst
|
\endrst
|
||||||
*/
|
*/
|
||||||
void print(StringRef format, const ArgList &args);
|
inline void print(StringRef format_str, const ArgList &args) {
|
||||||
|
print(stdout, format_str, args);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
\rst
|
\rst
|
||||||
Prints formatted data to a file.
|
Prints formatted data to the stream *os*.
|
||||||
|
|
||||||
**Example**::
|
|
||||||
|
|
||||||
print(stderr, "Don't {}!", "panic");
|
|
||||||
\endrst
|
|
||||||
*/
|
|
||||||
void print(std::FILE *f, StringRef format, const ArgList &args);
|
|
||||||
|
|
||||||
/**
|
|
||||||
\rst
|
|
||||||
Prints formatted data to a stream.
|
|
||||||
|
|
||||||
**Example**::
|
**Example**::
|
||||||
|
|
||||||
print(cerr, "Don't {}!", "panic");
|
print(cerr, "Don't {}!", "panic");
|
||||||
\endrst
|
\endrst
|
||||||
*/
|
*/
|
||||||
void print(std::ostream &os, StringRef format, const ArgList &args);
|
void print(std::ostream &os, StringRef format_str, const ArgList &args);
|
||||||
|
|
||||||
template <typename Char>
|
template <typename Char>
|
||||||
void printf(BasicWriter<Char> &w,
|
void printf(BasicWriter<Char> &w,
|
||||||
@ -1813,13 +1805,44 @@ void printf(BasicWriter<Char> &w,
|
|||||||
internal::PrintfFormatter<Char>().format(w, format, args);
|
internal::PrintfFormatter<Char>().format(w, format, args);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
\rst
|
||||||
|
Formats arguments and returns the result as a string.
|
||||||
|
|
||||||
|
**Example**::
|
||||||
|
|
||||||
|
std::string message = fmt::sprintf("The answer is %d", 42);
|
||||||
|
\endrst
|
||||||
|
*/
|
||||||
inline std::string sprintf(StringRef format, const ArgList &args) {
|
inline std::string sprintf(StringRef format, const ArgList &args) {
|
||||||
Writer w;
|
Writer w;
|
||||||
printf(w, format, args);
|
printf(w, format, args);
|
||||||
return w.str();
|
return w.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
void printf(StringRef format, const ArgList &args);
|
/**
|
||||||
|
\rst
|
||||||
|
Prints formatted data to the file *f*.
|
||||||
|
|
||||||
|
**Example**::
|
||||||
|
|
||||||
|
fmt::fprintf(stderr, "Don't %s!", "panic");
|
||||||
|
\endrst
|
||||||
|
*/
|
||||||
|
int fprintf(std::FILE *f, StringRef format, const ArgList &args);
|
||||||
|
|
||||||
|
/**
|
||||||
|
\rst
|
||||||
|
Prints formatted data to ``stdout``.
|
||||||
|
|
||||||
|
**Example**::
|
||||||
|
|
||||||
|
fmt::printf("Elapsed time: %.2f seconds", 1.23);
|
||||||
|
\endrst
|
||||||
|
*/
|
||||||
|
inline int printf(StringRef format, const ArgList &args) {
|
||||||
|
return fprintf(stdout, format, args);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
Fast integer formatter.
|
Fast integer formatter.
|
||||||
@ -2032,7 +2055,8 @@ FMT_VARIADIC(void, print, std::FILE *, StringRef)
|
|||||||
FMT_VARIADIC(void, print, std::ostream &, StringRef)
|
FMT_VARIADIC(void, print, std::ostream &, StringRef)
|
||||||
FMT_VARIADIC(void, print_colored, Color, StringRef)
|
FMT_VARIADIC(void, print_colored, Color, StringRef)
|
||||||
FMT_VARIADIC(std::string, sprintf, StringRef)
|
FMT_VARIADIC(std::string, sprintf, StringRef)
|
||||||
FMT_VARIADIC(void, printf, StringRef)
|
FMT_VARIADIC(int, printf, StringRef)
|
||||||
|
FMT_VARIADIC(int, fprintf, std::FILE *, StringRef)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Restore warnings.
|
// Restore warnings.
|
||||||
|
Reference in New Issue
Block a user