diff options
author | Abseil Team <absl-team@google.com> | 2022-09-20 14:20:55 -0700 |
---|---|---|
committer | Copybara-Service <copybara-worker@google.com> | 2022-09-20 14:21:52 -0700 |
commit | 9398fa76a3436bfb89f7a6ec5c74a21f1e4d6029 (patch) | |
tree | f25eb0625f95a73d57d1f960383bf3a40eeb96ef /absl/strings/internal | |
parent | d54fd91c4e2cf8d9c843e320fd1de4eb1723af3c (diff) |
Adds support for "%v" in absl::StrFormat and related functions for bool values. Note that %v prints bool values as "true" and "false" rather than "1" and "0".
PiperOrigin-RevId: 475651689
Change-Id: I24b3749d85b5bb7cbc18653d629271a8b2b07d08
Diffstat (limited to 'absl/strings/internal')
-rw-r--r-- | absl/strings/internal/str_format/arg.cc | 11 | ||||
-rw-r--r-- | absl/strings/internal/str_format/arg.h | 9 |
2 files changed, 19 insertions, 1 deletions
diff --git a/absl/strings/internal/str_format/arg.cc b/absl/strings/internal/str_format/arg.cc index b0e06bfb..967fe9ca 100644 --- a/absl/strings/internal/str_format/arg.cc +++ b/absl/strings/internal/str_format/arg.cc @@ -344,7 +344,7 @@ bool ConvertIntArg(T v, FormatConversionSpecImpl conv, FormatSinkImpl *sink) { return ConvertFloatImpl(static_cast<double>(v), conv, sink); default: - ABSL_ASSUME(false); + ABSL_ASSUME(false); } if (conv.is_basic()) { @@ -376,6 +376,15 @@ inline bool ConvertStringArg(string_view v, const FormatConversionSpecImpl conv, } // namespace +bool ConvertBoolArg(bool v, FormatSinkImpl *sink) { + if (v) { + sink->Append("true"); + } else { + sink->Append("false"); + } + return true; +} + // ==================== Strings ==================== StringConvertResult FormatConvertImpl(const std::string &v, const FormatConversionSpecImpl conv, diff --git a/absl/strings/internal/str_format/arg.h b/absl/strings/internal/str_format/arg.h index 3c2bb3e6..2dfbf728 100644 --- a/absl/strings/internal/str_format/arg.h +++ b/absl/strings/internal/str_format/arg.h @@ -186,6 +186,8 @@ using CharConvertResult = ArgConvertResult<FormatConversionCharSetUnion( FormatConversionCharSetInternal::kNumeric, FormatConversionCharSetInternal::kStar)>; +bool ConvertBoolArg(bool v, FormatSinkImpl* sink); + // Floats. FloatingConvertResult FormatConvertImpl(float v, FormatConversionSpecImpl conv, FormatSinkImpl* sink); @@ -234,9 +236,16 @@ IntegralConvertResult FormatConvertImpl(int128 v, FormatConversionSpecImpl conv, IntegralConvertResult FormatConvertImpl(uint128 v, FormatConversionSpecImpl conv, FormatSinkImpl* sink); + +// This function needs to be a template due to ambiguity regarding type +// conversions. template <typename T, enable_if_t<std::is_same<T, bool>::value, int> = 0> IntegralConvertResult FormatConvertImpl(T v, FormatConversionSpecImpl conv, FormatSinkImpl* sink) { + if (conv.conversion_char() == FormatConversionCharInternal::v) { + return {ConvertBoolArg(v, sink)}; + } + return FormatConvertImpl(static_cast<int>(v), conv, sink); } |