mirror of
https://github.com/RPCS3/llvm-mirror.git
synced 2025-01-31 20:51:52 +01:00
[ADT] Implicitly convert between StringRef and std::string_view when we have C++17
This makes the types almost seamlessly interchangeable in C++17 codebases. Eventually we want to replace StringRef with the standard type, but that requires C++17 being the default and a huge refactoring job as StringRef has a lot more functionality.
This commit is contained in:
parent
5a4cadf22e
commit
cad5b9a068
@ -18,6 +18,9 @@
|
|||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <limits>
|
#include <limits>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#if __cplusplus > 201402L
|
||||||
|
#include <string_view>
|
||||||
|
#endif
|
||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
|
|
||||||
@ -110,6 +113,12 @@ namespace llvm {
|
|||||||
/*implicit*/ StringRef(const std::string &Str)
|
/*implicit*/ StringRef(const std::string &Str)
|
||||||
: Data(Str.data()), Length(Str.length()) {}
|
: Data(Str.data()), Length(Str.length()) {}
|
||||||
|
|
||||||
|
#if __cplusplus > 201402L
|
||||||
|
/// Construct a string ref from an std::string_view.
|
||||||
|
/*implicit*/ constexpr StringRef(std::string_view Str)
|
||||||
|
: Data(Str.data()), Length(Str.size()) {}
|
||||||
|
#endif
|
||||||
|
|
||||||
static StringRef withNullAsEmpty(const char *data) {
|
static StringRef withNullAsEmpty(const char *data) {
|
||||||
return StringRef(data ? data : "");
|
return StringRef(data ? data : "");
|
||||||
}
|
}
|
||||||
@ -267,6 +276,12 @@ namespace llvm {
|
|||||||
return str();
|
return str();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if __cplusplus > 201402L
|
||||||
|
operator std::string_view() const {
|
||||||
|
return std::string_view(data(), size());
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
/// @}
|
/// @}
|
||||||
/// @name String Predicates
|
/// @name String Predicates
|
||||||
/// @{
|
/// @{
|
||||||
|
@ -59,6 +59,16 @@ TEST(StringRefTest, Construction) {
|
|||||||
EXPECT_EQ("hello", StringRef("hello"));
|
EXPECT_EQ("hello", StringRef("hello"));
|
||||||
EXPECT_EQ("hello", StringRef("hello world", 5));
|
EXPECT_EQ("hello", StringRef("hello world", 5));
|
||||||
EXPECT_EQ("hello", StringRef(std::string("hello")));
|
EXPECT_EQ("hello", StringRef(std::string("hello")));
|
||||||
|
#if __cplusplus > 201402L
|
||||||
|
EXPECT_EQ("hello", StringRef(std::string_view("hello")));
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(StringRefTest, Conversion) {
|
||||||
|
EXPECT_EQ("hello", std::string(StringRef("hello")));
|
||||||
|
#if __cplusplus > 201402L
|
||||||
|
EXPECT_EQ("hello", std::string_view(StringRef("hello")));
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(StringRefTest, EmptyInitializerList) {
|
TEST(StringRefTest, EmptyInitializerList) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user