File tree Expand file tree Collapse file tree 2 files changed +25
-2
lines changed Expand file tree Collapse file tree 2 files changed +25
-2
lines changed Original file line number Diff line number Diff line change @@ -167,8 +167,14 @@ class ConstString {
167
167
168
168
// Implicitly convert \class ConstString instances to \class StringRef.
169
169
operator llvm::StringRef () const { return GetStringRef (); }
170
- // Implicitly convert \class ConstString instances to \calss std::string_view.
171
- operator std::string_view () const { return std::string_view (m_string, GetLength ()); }
170
+
171
+ // Implicitly convert \class ConstString instances to \class std::string_view.
172
+ operator std::string_view () const {
173
+ return std::string_view (m_string, GetLength ());
174
+ }
175
+
176
+ // Explicitly convert \class ConstString instances to \class std::string.
177
+ explicit operator std::string () const { return GetString (); }
172
178
173
179
// / Get the string value as a C string.
174
180
// /
@@ -192,6 +198,9 @@ class ConstString {
192
198
return llvm::StringRef (m_string, GetLength ());
193
199
}
194
200
201
+ // / Get the string value as a std::string
202
+ std::string GetString () const { return std::string (m_string, GetLength ()); }
203
+
195
204
// / Get the string value as a C string.
196
205
// /
197
206
// / Get the value of the contained string as a NULL terminated C string
Original file line number Diff line number Diff line change @@ -137,3 +137,17 @@ TEST(ConstStringTest, CompareStringRef) {
137
137
EXPECT_TRUE (null == static_cast <const char *>(nullptr ));
138
138
EXPECT_TRUE (null != " bar" );
139
139
}
140
+
141
+ TEST (ConstStringTest, StringConversions) {
142
+ ConstString foo (" foo" );
143
+
144
+ // Member functions.
145
+ EXPECT_EQ (llvm::StringRef (" foo" ), foo.GetStringRef ());
146
+ EXPECT_EQ (std::string (" foo" ), foo.GetString ());
147
+ EXPECT_STREQ (" foo" , foo.AsCString ());
148
+
149
+ // Conversion operators.
150
+ EXPECT_EQ (llvm::StringRef (" foo" ), llvm::StringRef (foo));
151
+ EXPECT_EQ (std::string (" foo" ), std::string_view (foo));
152
+ EXPECT_EQ (std::string (" foo" ), std::string (foo));
153
+ }
You can’t perform that action at this time.
0 commit comments