Commit 3ed4ca1f by Tom Manshreck Committed by Copybara-Service

Updated documentation on use of %v

Also updated documentation around FormatSink and PutPaddedString

PiperOrigin-RevId: 488651398
Change-Id: Ic6c586dbb8bea61df841a142f12d22c7e5b03f43
parent edbf6628
...@@ -191,7 +191,7 @@ class FormatCountCapture { ...@@ -191,7 +191,7 @@ class FormatCountCapture {
// absl::StrFormat(formatString, "TheVillage", 6); // absl::StrFormat(formatString, "TheVillage", 6);
// //
// A format string generally follows the POSIX syntax as used within the POSIX // A format string generally follows the POSIX syntax as used within the POSIX
// `printf` specification. // `printf` specification. (Exceptions are noted below.)
// //
// (See http://pubs.opengroup.org/onlinepubs/9699919799/functions/fprintf.html.) // (See http://pubs.opengroup.org/onlinepubs/9699919799/functions/fprintf.html.)
// //
...@@ -211,6 +211,10 @@ class FormatCountCapture { ...@@ -211,6 +211,10 @@ class FormatCountCapture {
// * `n` for the special case of writing out the number of characters // * `n` for the special case of writing out the number of characters
// written to this point. The resulting value must be captured within an // written to this point. The resulting value must be captured within an
// `absl::FormatCountCapture` type. // `absl::FormatCountCapture` type.
// * `v` for values using the default format for a deduced type. These deduced
// types include many of the primitive types denoted here as well as
// user-defined types containing the proper extensions. (See below for more
// information.)
// //
// Implementation-defined behavior: // Implementation-defined behavior:
// * A null pointer provided to "%s" or "%p" is output as "(nil)". // * A null pointer provided to "%s" or "%p" is output as "(nil)".
...@@ -239,6 +243,15 @@ class FormatCountCapture { ...@@ -239,6 +243,15 @@ class FormatCountCapture {
// "%s%d%n", "hello", 123, absl::FormatCountCapture(&n)); // "%s%d%n", "hello", 123, absl::FormatCountCapture(&n));
// EXPECT_EQ(8, n); // EXPECT_EQ(8, n);
// //
// NOTE: the `v` specifier (for "value") is a type specifier not present in the
// POSIX specification. %v will format values according to their deduced type.
// `v` uses `d` for signed integer values, `u` for unsigned integer values, `g`
// for floating point values, and formats boolean values as "true"/"false"
// (instead of 1 or 0 for booleans formatted using d). `const char*` is not
// supported; please use `std:string` and `string_view`. `char` is also not
// supported due to ambiguity of the type. This specifier does not support
// modifiers.
//
// The `FormatSpec` intrinsically supports all of these fundamental C++ types: // The `FormatSpec` intrinsically supports all of these fundamental C++ types:
// //
// * Characters: `char`, `signed char`, `unsigned char` // * Characters: `char`, `signed char`, `unsigned char`
...@@ -807,17 +820,25 @@ enum class FormatConversionCharSet : uint64_t { ...@@ -807,17 +820,25 @@ enum class FormatConversionCharSet : uint64_t {
// FormatSink // FormatSink
// //
// An abstraction to which conversions write their string data. // A format sink is a generic abstraction to which conversions may write their
// formatted string data. `absl::FormatConvert()` uses this sink to write its
// formatted string.
// //
class FormatSink { class FormatSink {
public: public:
// Appends `count` copies of `ch`. // FormatSink::Append()
//
// Appends `count` copies of `ch` to the format sink.
void Append(size_t count, char ch) { sink_->Append(count, ch); } void Append(size_t count, char ch) { sink_->Append(count, ch); }
// Overload of FormatSink::Append() for appending the characters of a string
// view to a format sink.
void Append(string_view v) { sink_->Append(v); } void Append(string_view v) { sink_->Append(v); }
// Appends the first `precision` bytes of `v`. If this is less than // FormatSink::PutPaddedString()
// `width`, spaces will be appended first (if `left` is false), or //
// Appends `precision` number of bytes of `v` to the format sink. If this is
// less than `width`, spaces will be appended first (if `left` is false), or
// after (if `left` is true) to ensure the total amount appended is // after (if `left` is true) to ensure the total amount appended is
// at least `width`. // at least `width`.
bool PutPaddedString(string_view v, int width, int precision, bool left) { bool PutPaddedString(string_view v, int width, int precision, bool left) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment