aboutsummaryrefslogtreecommitdiff
path: root/absl/strings/str_cat.h
diff options
context:
space:
mode:
authorAndy Soffer <asoffer@google.com>2022-10-14 11:18:49 -0700
committerCopybara-Service <copybara-worker@google.com>2022-10-14 11:19:40 -0700
commit5631e52ed71b4fe01e0cb9146b6ad10ef216b8b0 (patch)
tree67a8c5fa19c5b6319a693fbf0a403e370e78c5ec /absl/strings/str_cat.h
parentf073fe8ee5dcb0aa18c893198747062f2f51ab59 (diff)
downloadabseil-5631e52ed71b4fe01e0cb9146b6ad10ef216b8b0.tar.gz
abseil-5631e52ed71b4fe01e0cb9146b6ad10ef216b8b0.tar.bz2
abseil-5631e52ed71b4fe01e0cb9146b6ad10ef216b8b0.zip
Support stringification of user-defined types in AbslStringify in absl::Substitute.
We are also moving some internals into an internal header. `HasAbslStringify` was not previously in an internal namespace but was intended to be and has now been moved to an internal namespace. This is in adherence to our compatibility guidelines which wave requirements for APIs within their first 30 days of public release (See https://abseil.io/about/compatibility for details). PiperOrigin-RevId: 481190705 Change-Id: I4c0c348f269ea8d76ea3d4bd5a2c41cce475dc04
Diffstat (limited to 'absl/strings/str_cat.h')
-rw-r--r--absl/strings/str_cat.h103
1 files changed, 33 insertions, 70 deletions
diff --git a/absl/strings/str_cat.h b/absl/strings/str_cat.h
index 8a63be0d..1a37faae 100644
--- a/absl/strings/str_cat.h
+++ b/absl/strings/str_cat.h
@@ -48,53 +48,22 @@
// `StrCat()` or `StrAppend()`. You may specify a minimum hex field width using
// a `PadSpec` enum.
//
-// -----------------------------------------------------------------------------
-
-#ifndef ABSL_STRINGS_STR_CAT_H_
-#define ABSL_STRINGS_STR_CAT_H_
-
-#include <array>
-#include <cstdint>
-#include <string>
-#include <type_traits>
-#include <utility>
-#include <vector>
-
-#include "absl/base/port.h"
-#include "absl/strings/numbers.h"
-#include "absl/strings/string_view.h"
-
-namespace absl {
-ABSL_NAMESPACE_BEGIN
-
-namespace strings_internal {
-// AlphaNumBuffer allows a way to pass a string to StrCat without having to do
-// memory allocation. It is simply a pair of a fixed-size character array, and
-// a size. Please don't use outside of absl, yet.
-template <size_t max_size>
-struct AlphaNumBuffer {
- std::array<char, max_size> data;
- size_t size;
-};
-
-//------------------------------------------------------------------------------
-// StrCat Extension
-//------------------------------------------------------------------------------
-//
-// AbslStringify()
+// User-defined types can be formatted with the `AbslStringify()` customization
+// point. The API relies on detecting an overload in the user-defined type's
+// namespace of a free (non-member) `AbslStringify()` function as a definition
+// (typically declared as a friend and implemented in-line.
+// with the following signature:
//
-// A simple customization API for formatting user-defined types using
-// absl::StrCat(). The API relies on detecting an overload in the
-// user-defined type's namespace of a free (non-member) `AbslStringify()`
-// function as a friend definition with the following signature:
+// class MyClass { ... };
//
// template <typename Sink>
-// void AbslStringify(Sink& sink, const X& value);
+// void AbslStringify(Sink& sink, const MyClass& value);
//
// An `AbslStringify()` overload for a type should only be declared in the same
// file and namespace as said type.
//
-// Note that AbslStringify() also supports use with absl::StrFormat().
+// Note that `AbslStringify()` also supports use with `absl::StrFormat()` and
+// `absl::Substitute()`.
//
// Example:
//
@@ -113,33 +82,36 @@ struct AlphaNumBuffer {
// int x;
// int y;
// };
+// -----------------------------------------------------------------------------
-class StringifySink {
- public:
- void Append(size_t count, char ch);
-
- void Append(string_view v);
+#ifndef ABSL_STRINGS_STR_CAT_H_
+#define ABSL_STRINGS_STR_CAT_H_
- bool PutPaddedString(string_view v, int width, int precision, bool left);
+#include <array>
+#include <cstdint>
+#include <string>
+#include <type_traits>
+#include <utility>
+#include <vector>
- // Support `absl::Format(&sink, format, args...)`.
- friend void AbslFormatFlush(StringifySink* sink, absl::string_view v) {
- sink->Append(v);
- }
+#include "absl/base/port.h"
+#include "absl/strings/internal/stringify_sink.h"
+#include "absl/strings/numbers.h"
+#include "absl/strings/string_view.h"
- template <typename T>
- friend string_view ExtractStringification(StringifySink& sink, const T& v);
+namespace absl {
+ABSL_NAMESPACE_BEGIN
- private:
- std::string buffer_;
+namespace strings_internal {
+// AlphaNumBuffer allows a way to pass a string to StrCat without having to do
+// memory allocation. It is simply a pair of a fixed-size character array, and
+// a size. Please don't use outside of absl, yet.
+template <size_t max_size>
+struct AlphaNumBuffer {
+ std::array<char, max_size> data;
+ size_t size;
};
-template <typename T>
-string_view ExtractStringification(StringifySink& sink, const T& v) {
- AbslStringify(sink, v);
- return sink.buffer_;
-}
-
} // namespace strings_internal
// Enum that specifies the number of significant digits to return in a `Hex` or
@@ -272,15 +244,6 @@ struct Dec {
// `StrAppend()`, providing efficient conversion of numeric, boolean, and
// hexadecimal values (through the `Hex` type) into strings.
-template <typename T, typename = void>
-struct HasAbslStringify : std::false_type {};
-
-template <typename T>
-struct HasAbslStringify<T, std::enable_if_t<std::is_void<decltype(AbslStringify(
- std::declval<strings_internal::StringifySink&>(),
- std::declval<const T&>()))>::value>>
- : std::true_type {};
-
class AlphaNum {
public:
// No bool ctor -- bools convert to an integral type.
@@ -329,7 +292,7 @@ class AlphaNum {
AlphaNum(absl::string_view pc) : piece_(pc) {} // NOLINT(runtime/explicit)
template <typename T, typename = typename std::enable_if<
- HasAbslStringify<T>::value>::type>
+ strings_internal::HasAbslStringify<T>::value>::type>
AlphaNum( // NOLINT(runtime/explicit)
const T& v, // NOLINT(runtime/explicit)
strings_internal::StringifySink&& sink = {}) // NOLINT(runtime/explicit)