Check __cpp_lib_string_view, not __cplusplus.
While I'm here, facilitate conversion to std::basic_string_view.
Fixes #361.
Change-Id: I155f7487bdfd9ed65096764a953ac965d8e45f7c
Reviewed-on: https://code-review.googlesource.com/c/re2/+/59770
Reviewed-by: Perry Lorier <perryl@google.com>
Reviewed-by: Paul Wankadia <junyer@google.com>
diff --git a/re2/stringpiece.h b/re2/stringpiece.h
index 1d9c2d3..b9d6661 100644
--- a/re2/stringpiece.h
+++ b/re2/stringpiece.h
@@ -19,18 +19,13 @@
//
// Arghh! I wish C++ literals were "string".
-// Doing this simplifies the logic below.
-#ifndef __has_include
-#define __has_include(x) 0
-#endif
-
#include <stddef.h>
#include <string.h>
#include <algorithm>
#include <iosfwd>
#include <iterator>
#include <string>
-#if __has_include(<string_view>) && __cplusplus >= 201703L
+#ifdef __cpp_lib_string_view
#include <string_view>
#endif
@@ -57,7 +52,7 @@
// expected.
StringPiece()
: data_(NULL), size_(0) {}
-#if __has_include(<string_view>) && __cplusplus >= 201703L
+#ifdef __cpp_lib_string_view
StringPiece(const std::string_view& str)
: data_(str.data()), size_(str.size()) {}
#endif
@@ -103,6 +98,14 @@
size_ = len;
}
+#ifdef __cpp_lib_string_view
+ // Converts to `std::basic_string_view`.
+ operator std::basic_string_view<char, traits_type>() const {
+ if (!data_) return {};
+ return std::basic_string_view<char, traits_type>(data_, size_);
+ }
+#endif
+
// Converts to `std::basic_string`.
template <typename A>
explicit operator std::basic_string<char, traits_type, A>() const {