APT::StringView is supposed to be a temporary measure, until support
for the standardized string_view is widely available. Introducing
additional unstandardized features just makes porting to the
standard version harder.
The constexpr constructor also won't have any real effect on most
systems, as the compiler will happily optimise the strlen() call
away for constant strings.
Gbp-Dch: ignore
const char *data_;
size_t size_;
const char *data_;
size_t size_;
- // without this little stunt the "char const *" overload is always preferred
- // over char[], but if we got a char[] we can deduct the length at compile time
- #define APT_T_IS_CHARPOINTER template<class T, typename std::enable_if<std::is_pointer<T>{} && !std::is_array<T>{}>::type* = nullptr>
-
public:
static constexpr size_t npos = static_cast<size_t>(-1);
static_assert(APT::StringView::npos == std::string::npos, "npos values are different");
public:
static constexpr size_t npos = static_cast<size_t>(-1);
static_assert(APT::StringView::npos == std::string::npos, "npos values are different");
constexpr StringView() : data_(""), size_(0) {}
constexpr StringView(const char *data, size_t size) : data_(data), size_(size) {}
constexpr StringView() : data_(""), size_(0) {}
constexpr StringView(const char *data, size_t size) : data_(data), size_(size) {}
- template<size_t N> constexpr StringView(char const (&data)[N]) : StringView(data, N-1) {}
- APT_T_IS_CHARPOINTER StringView(T data) : data_(data), size_(strlen(data)) {}
-
+ StringView(const char *data) : data_(data), size_(strlen(data)) {}
StringView(std::string const & str): data_(str.data()), size_(str.size()) {}
StringView(std::string const & str): data_(str.data()), size_(str.size()) {}
/* Viewers */
constexpr StringView substr(size_t pos, size_t n = npos) const {
return StringView(data_ + pos, n > (size_ - pos) ? (size_ - pos) : n);
/* Viewers */
constexpr StringView substr(size_t pos, size_t n = npos) const {
return StringView(data_ + pos, n > (size_ - pos) ? (size_ - pos) : n);
-template<size_t N> inline bool operator ==(char const (&other)[N], APT::StringView that) { return that.operator==(other); }
-APT_T_IS_CHARPOINTER inline bool operator ==(T other, APT::StringView that) { return that.operator==(other); }
+inline bool operator ==(const char *other, APT::StringView that);
+inline bool operator ==(const char *other, APT::StringView that) { return that.operator==(other); }
-#undef APT_T_IS_CHARPOINTER
APT::StringView charp2String{charp};
EXPECT_EQ(0, strString.length());
APT::StringView charp2String{charp};
EXPECT_EQ(0, strString.length());
- constexpr APT::StringView charaString{""};
- static_assert( 0 == charaString.length(), "chara right size");
+ const APT::StringView charaString{""};
+ EXPECT_EQ(0, charaString.length());
EXPECT_TRUE(APT::StringView("") == "");
EXPECT_FALSE(APT::StringView("") != "");
EXPECT_TRUE(APT::StringView("") == "");
EXPECT_FALSE(APT::StringView("") != "");
constexpr char const * const charp = "foo";
constexpr APT::StringView charpString{charp, 3};
constexpr char const * const charp = "foo";
constexpr APT::StringView charpString{charp, 3};
- static_assert( 3 == charpString.length(), "charp right size");
+ EXPECT_EQ( 3, charpString.length());
EXPECT_EQ(charpString.to_string(), charpString.data());
APT::StringView charp2String{charp};
EXPECT_EQ(3, charp2String.length());
EXPECT_EQ(charp2String.to_string(), charp2String.data());
EXPECT_EQ(charpString.to_string(), charpString.data());
APT::StringView charp2String{charp};
EXPECT_EQ(3, charp2String.length());
EXPECT_EQ(charp2String.to_string(), charp2String.data());
- constexpr APT::StringView charaString{"foo"};
- static_assert( 3 == charaString.length(), "chara right size");
+ const APT::StringView charaString{"foo"};
+ EXPECT_EQ(3, charaString.length());
EXPECT_EQ(charaString.to_string(), charaString.data());
EXPECT_TRUE(APT::StringView("foo") == "foo");
EXPECT_EQ(charaString.to_string(), charaString.data());
EXPECT_TRUE(APT::StringView("foo") == "foo");
TEST(StringViewTest,SubStr)
{
TEST(StringViewTest,SubStr)
{
- constexpr APT::StringView defString("Hello World!");
+ const APT::StringView defString("Hello World!");
EXPECT_EQ(defString.to_string().substr(6), defString.substr(6).to_string());
EXPECT_EQ(defString.to_string().substr(0,5), defString.substr(0,5).to_string());
EXPECT_EQ(defString.to_string().substr(6,5), defString.substr(6,5).to_string());
EXPECT_EQ(defString.to_string().substr(6), defString.substr(6).to_string());
EXPECT_EQ(defString.to_string().substr(0,5), defString.substr(0,5).to_string());
EXPECT_EQ(defString.to_string().substr(6,5), defString.substr(6,5).to_string());
TEST(StringViewTest,Find)
{
TEST(StringViewTest,Find)
{
- constexpr APT::StringView defString("Hello World!");
+ const APT::StringView defString("Hello World!");
EXPECT_EQ(defString.to_string().find('l'), defString.find('l'));
EXPECT_EQ(defString.to_string().find('X'), defString.find('X'));
EXPECT_EQ(defString.to_string().find('e',3), defString.find('e',3));
EXPECT_EQ(defString.to_string().find('l'), defString.find('l'));
EXPECT_EQ(defString.to_string().find('X'), defString.find('X'));
EXPECT_EQ(defString.to_string().find('e',3), defString.find('e',3));
TEST(StringViewTest,RFind)
{
TEST(StringViewTest,RFind)
{
- constexpr APT::StringView defString("Hello World!");
+ const APT::StringView defString("Hello World!");
EXPECT_EQ(defString.to_string().rfind('l'), defString.rfind('l'));
EXPECT_EQ(defString.to_string().rfind('X'), defString.rfind('X'));
EXPECT_EQ(defString.to_string().rfind('e',3), defString.rfind('e',3));
EXPECT_EQ(defString.to_string().rfind('l'), defString.rfind('l'));
EXPECT_EQ(defString.to_string().rfind('X'), defString.rfind('X'));
EXPECT_EQ(defString.to_string().rfind('e',3), defString.rfind('e',3));