@@ -59,6 +59,23 @@ bool IsValidIdentifier(const std::string& segment) {
59
59
return true ;
60
60
}
61
61
62
+ /* * A custom formatter to be used with absl::StrJoin(). */
63
+ struct JoinEscaped {
64
+ static std::string escaped_segment (const std::string& segment) {
65
+ auto escaped = absl::StrReplaceAll (segment, {{" \\ " , " \\\\ " }, {" `" , " \\ `" }});
66
+ const bool needs_escaping = !IsValidIdentifier (escaped);
67
+ if (needs_escaping) {
68
+ escaped.insert (escaped.begin (), ' `' );
69
+ escaped.push_back (' `' );
70
+ }
71
+ return escaped;
72
+ }
73
+
74
+ template <typename T>
75
+ void operator ()(T* out, const std::string& segment) {
76
+ out->append (escaped_segment (segment));
77
+ }
78
+ };
62
79
} // namespace
63
80
64
81
FieldPath FieldPath::FromServerFormat (const absl::string_view path) {
@@ -143,20 +160,7 @@ bool FieldPath::IsKeyFieldPath() const {
143
160
}
144
161
145
162
std::string FieldPath::CanonicalString () const {
146
- const auto escaped_segment = [](const std::string& segment) {
147
- auto escaped = absl::StrReplaceAll (segment, {{" \\ " , " \\\\ " }, {" `" , " \\ `" }});
148
- const bool needs_escaping = !IsValidIdentifier (escaped);
149
- if (needs_escaping) {
150
- escaped.insert (escaped.begin (), ' `' );
151
- escaped.push_back (' `' );
152
- }
153
- return escaped;
154
- };
155
- return absl::StrJoin (
156
- begin (), end (), " ." ,
157
- [escaped_segment](std::string* out, const std::string& segment) {
158
- out->append (escaped_segment (segment));
159
- });
163
+ return absl::StrJoin (begin (), end (), " ." , JoinEscaped ());
160
164
}
161
165
162
166
} // namespace model
0 commit comments