Skip to content

Commit 7d9f957

Browse files
committed
string_format.h: create the StringifySink at the correct scope and pass it along so that its lifetime gets extended to the full-expression that uses FormatArg, as would have happened if AlphaNum was used as intended (i.e. as an argument to StrCat and StrAppend).
1 parent ab5bc69 commit 7d9f957

File tree

1 file changed

+11
-12
lines changed

1 file changed

+11
-12
lines changed

Firestore/core/src/util/string_format.h

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ struct FormatChoice<5> {};
6565
class FormatArg : public absl::AlphaNum {
6666
public:
6767
template <typename T>
68-
FormatArg(T&& value) // NOLINT(runtime/explicit)
69-
: FormatArg{std::forward<T>(value), internal::FormatChoice<0>{}} {
68+
FormatArg(T&& value, absl::strings_internal::StringifySink&& sink = {}) // NOLINT(runtime/explicit)
69+
: FormatArg{std::forward<T>(value), std::move(sink), internal::FormatChoice<0>{}} {
7070
}
7171

7272
private:
@@ -79,7 +79,7 @@ class FormatArg : public absl::AlphaNum {
7979
*/
8080
template <typename T,
8181
typename = typename std::enable_if<std::is_same<bool, T>{}>::type>
82-
FormatArg(T bool_value, internal::FormatChoice<0>)
82+
FormatArg(T bool_value, absl::strings_internal::StringifySink&&, internal::FormatChoice<0>)
8383
: AlphaNum(bool_value ? "true" : "false") {
8484
}
8585

@@ -90,15 +90,15 @@ class FormatArg : public absl::AlphaNum {
9090
template <
9191
typename T,
9292
typename = typename std::enable_if<objc::is_objc_pointer<T>{}>::type>
93-
FormatArg(T object, internal::FormatChoice<1>)
93+
FormatArg(T object, absl::strings_internal::StringifySink&&, internal::FormatChoice<1>)
9494
: AlphaNum(MakeStringView([object description])) {
9595
}
9696

9797
/**
9898
* Creates a FormatArg from any Objective-C Class type. Objective-C Class
9999
* types are a special struct that aren't of a type derived from NSObject.
100100
*/
101-
FormatArg(Class object, internal::FormatChoice<1>)
101+
FormatArg(Class object, absl::strings_internal::StringifySink&&, internal::FormatChoice<1>)
102102
: AlphaNum(MakeStringView(NSStringFromClass(object))) {
103103
}
104104
#endif
@@ -108,15 +108,15 @@ class FormatArg : public absl::AlphaNum {
108108
* handled specially to avoid ambiguity with generic pointers, which are
109109
* handled differently.
110110
*/
111-
FormatArg(std::nullptr_t, internal::FormatChoice<2>) : AlphaNum("null") {
111+
FormatArg(std::nullptr_t, absl::strings_internal::StringifySink&&, internal::FormatChoice<2>) : AlphaNum("null") {
112112
}
113113

114114
/**
115115
* Creates a FormatArg from a character string literal. This is
116116
* handled specially to avoid ambiguity with generic pointers, which are
117117
* handled differently.
118118
*/
119-
FormatArg(const char* string_value, internal::FormatChoice<3>)
119+
FormatArg(const char* string_value, absl::strings_internal::StringifySink&&, internal::FormatChoice<3>)
120120
: AlphaNum(string_value == nullptr ? "null" : string_value) {
121121
}
122122

@@ -125,16 +125,16 @@ class FormatArg : public absl::AlphaNum {
125125
* hexadecimal integer literal.
126126
*/
127127
template <typename T>
128-
FormatArg(T* pointer_value, internal::FormatChoice<4>)
129-
: AlphaNum(absl::Hex(reinterpret_cast<uintptr_t>(pointer_value))) {
128+
FormatArg(T* pointer_value, absl::strings_internal::StringifySink&& sink, internal::FormatChoice<4>)
129+
: AlphaNum(absl::Hex(reinterpret_cast<uintptr_t>(pointer_value)), std::move(sink)) {
130130
}
131131

132132
/**
133133
* As a final fallback, creates a FormatArg from any type of value that
134134
* absl::AlphaNum accepts.
135135
*/
136136
template <typename T>
137-
FormatArg(T&& value, internal::FormatChoice<5>)
137+
FormatArg(T&& value, absl::strings_internal::StringifySink&&, internal::FormatChoice<5>)
138138
: AlphaNum(std::forward<T>(value)) {
139139
}
140140
};
@@ -157,8 +157,7 @@ class FormatArg : public absl::AlphaNum {
157157
*/
158158
template <typename... FA>
159159
std::string StringFormat(const char* format, const FA&... args) {
160-
return internal::StringFormatPieces(
161-
format, {static_cast<const FormatArg&>(args).Piece()...});
160+
return internal::StringFormatPieces(format, {static_cast<const FormatArg&>(args).Piece()...});
162161
}
163162

164163
inline std::string StringFormat() {

0 commit comments

Comments
 (0)