diff options
author | Tom Manshreck <shreck@google.com> | 2022-11-15 07:21:28 -0800 |
---|---|---|
committer | Copybara-Service <copybara-worker@google.com> | 2022-11-15 07:22:04 -0800 |
commit | 3ed4ca1f7f6a32cbfaabb095dcd6a2b81c5a3155 (patch) | |
tree | afc274c75fe5c8fe13b79f69fce1e33b995ddad2 | |
parent | edbf66288b7e21c33b39512affc593ff7c3e18fd (diff) |
Updated documentation on use of %v
Also updated documentation around FormatSink and PutPaddedString
PiperOrigin-RevId: 488651398
Change-Id: Ic6c586dbb8bea61df841a142f12d22c7e5b03f43
-rw-r--r-- | absl/strings/str_format.h | 31 |
1 files changed, 26 insertions, 5 deletions
diff --git a/absl/strings/str_format.h b/absl/strings/str_format.h index 43d86186..f4c98f41 100644 --- a/absl/strings/str_format.h +++ b/absl/strings/str_format.h @@ -191,7 +191,7 @@ class FormatCountCapture { // absl::StrFormat(formatString, "TheVillage", 6); // // 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.) // @@ -211,6 +211,10 @@ class FormatCountCapture { // * `n` for the special case of writing out the number of characters // written to this point. The resulting value must be captured within an // `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: // * A null pointer provided to "%s" or "%p" is output as "(nil)". @@ -239,6 +243,15 @@ class FormatCountCapture { // "%s%d%n", "hello", 123, absl::FormatCountCapture(&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: // // * Characters: `char`, `signed char`, `unsigned char` @@ -807,17 +820,25 @@ enum class FormatConversionCharSet : uint64_t { // 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 { 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); } + // Overload of FormatSink::Append() for appending the characters of a string + // view to a format sink. void Append(string_view v) { sink_->Append(v); } - // Appends the first `precision` bytes of `v`. If this is less than - // `width`, spaces will be appended first (if `left` is false), or + // FormatSink::PutPaddedString() + // + // 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 // at least `width`. bool PutPaddedString(string_view v, int width, int precision, bool left) { |