summaryrefslogtreecommitdiff
path: root/absl/strings
diff options
context:
space:
mode:
authorGravatar Abseil Team <absl-team@google.com>2018-01-05 07:54:33 -0800
committerGravatar Derek Mauro <dmauro@google.com>2018-01-08 10:38:50 -0500
commit4132ce25956a91e1224e0f205b7f8c326304a995 (patch)
tree724485e8778f91d865db86af663af9e7be3293db /absl/strings
parent0271cd35577599fa99b59202da17d3136956e4c0 (diff)
Changes imported from Abseil "staging" branch:
- 0a519d9a4507158267cc515e0c7c83959d94fc78 Fix missing header include when compiling with _GLIBCXX_D... by Alex Strelnikov <strel@google.com> - d089af70781d92af9a5de2d84c417ddf2c87689a Internal change by Gennadiy Rozental <rogeeff@google.com> - 0d3afc89d3907923ede964d58c6bcca579e8ad65 Test absl::any for exception safety. This test is tempor... by Jon Cohen <cohenjon@google.com> - 29af424b8a3174a7b3e657e478aa30a8a425aee2 Tweak the ABSL type trait library and expand its tests. by Abseil Team <absl-team@google.com> - 99ab42b2ebbe466cc3730fb6b16b5fad848f95af Rollback GLIBCXX_DEBUG fix due to internal breakage. by Alex Strelnikov <strel@google.com> - 1a5bcb93ee16d4dd2170254e54c4b62b38fbf17b Internal change. by Abseil Team <absl-team@google.com> - 46de7d09c7d4aef5b7b5389ce9b4f96b654aac02 absl::string_view::rfind: doc fix. by Abseil Team <absl-team@google.com> - edda4c7ddd2d76fbb5b3fd5226b95082083c57d9 Fix string_view_test with c++17/clang/libc++ to address by Xiaoyi Zhang <zhangxy@google.com> GitOrigin-RevId: 0a519d9a4507158267cc515e0c7c83959d94fc78 Change-Id: Ie27de1be3e79bba011f05e924d34e8fcc62d8de5
Diffstat (limited to 'absl/strings')
-rw-r--r--absl/strings/internal/stl_type_traits.h121
-rw-r--r--absl/strings/string_view.h2
-rw-r--r--absl/strings/string_view_test.cc25
-rw-r--r--absl/strings/strip_test.cc20
4 files changed, 120 insertions, 48 deletions
diff --git a/absl/strings/internal/stl_type_traits.h b/absl/strings/internal/stl_type_traits.h
index 8c3d877e..04c4a532 100644
--- a/absl/strings/internal/stl_type_traits.h
+++ b/absl/strings/internal/stl_type_traits.h
@@ -79,31 +79,48 @@ struct IsSTLContainer
template <typename C, template <typename...> class T, typename = void>
struct IsBaseOfSpecializationImpl : std::false_type {};
-// IsBaseOfSpecializationImpl must have three partial specializations,
-// because we must only compare templates that take the same number of
-// arguments. Otherwise, for example, std::vector can be compared with std::map,
-// and fail to compile because of too few or too many template arguments.
-//
-// We must also SFINAE on the existence of an allocator_type. Otherwise, we may
-// try to compare, for example, a std::pair<std::string, std::string> with a
-// std::vector<std::string, std:std::string>. This would fail to compile, because
-// of expected properties of the type passed in as the allocator.
-template <template <typename, typename> class U,
- template <typename, typename> class T, typename... Args>
+// IsBaseOfSpecializationImpl needs multiple partial specializations to SFINAE
+// on the existence of container dependent types and plug them into the STL
+// template.
+template <typename C, template <typename, typename> class T>
+struct IsBaseOfSpecializationImpl<
+ C, T, absl::void_t<typename C::value_type, typename C::allocator_type>>
+ : std::is_base_of<C,
+ T<typename C::value_type, typename C::allocator_type>> {};
+template <typename C, template <typename, typename, typename> class T>
struct IsBaseOfSpecializationImpl<
- U<Args...>, T, absl::void_t<typename U<Args...>::allocator_type>>
- : std::is_base_of<U<Args...>, T<Args...>> {};
-template <template <typename, typename, typename> class U,
- template <typename, typename, typename> class T, typename... Args>
+ C, T,
+ absl::void_t<typename C::key_type, typename C::key_compare,
+ typename C::allocator_type>>
+ : std::is_base_of<C, T<typename C::key_type, typename C::key_compare,
+ typename C::allocator_type>> {};
+template <typename C, template <typename, typename, typename, typename> class T>
+struct IsBaseOfSpecializationImpl<
+ C, T,
+ absl::void_t<typename C::key_type, typename C::mapped_type,
+ typename C::key_compare, typename C::allocator_type>>
+ : std::is_base_of<C,
+ T<typename C::key_type, typename C::mapped_type,
+ typename C::key_compare, typename C::allocator_type>> {
+};
+template <typename C, template <typename, typename, typename, typename> class T>
struct IsBaseOfSpecializationImpl<
- U<Args...>, T, absl::void_t<typename U<Args...>::allocator_type>>
- : std::is_base_of<U<Args...>, T<Args...>> {};
-template <template <typename, typename, typename, typename> class U,
- template <typename, typename, typename, typename> class T,
- typename... Args>
+ C, T,
+ absl::void_t<typename C::key_type, typename C::hasher,
+ typename C::key_equal, typename C::allocator_type>>
+ : std::is_base_of<C, T<typename C::key_type, typename C::hasher,
+ typename C::key_equal, typename C::allocator_type>> {
+};
+template <typename C,
+ template <typename, typename, typename, typename, typename> class T>
struct IsBaseOfSpecializationImpl<
- U<Args...>, T, absl::void_t<typename U<Args...>::allocator_type>>
- : std::is_base_of<U<Args...>, T<Args...>> {};
+ C, T,
+ absl::void_t<typename C::key_type, typename C::mapped_type,
+ typename C::hasher, typename C::key_equal,
+ typename C::allocator_type>>
+ : std::is_base_of<C, T<typename C::key_type, typename C::mapped_type,
+ typename C::hasher, typename C::key_equal,
+ typename C::allocator_type>> {};
template <typename C, template <typename...> class T>
using IsBaseOfSpecialization = IsBaseOfSpecializationImpl<absl::decay_t<C>, T>;
@@ -140,31 +157,47 @@ struct IsBaseOfSTLContainer
template <typename C, template <typename...> class T, typename = void>
struct IsConvertibleToSpecializationImpl : std::false_type {};
-// IsConvertibleToSpecializationImpl must have three partial specializations,
-// because we must only compare templates that take the same number of
-// arguments. Otherwise, for example, std::vector can be compared with std::map,
-// and fail to compile because of too few or too many template arguments.
-//
-// We must also SFINAE on the existence of an allocator_type. Otherwise, we may
-// try to compare, for example, a std::pair<std::string, std::string> with a
-// std::vector<std::string, std:std::string>. This would fail to compile, because
-// of expected properties of the type passed in as the allocator.
-template <template <typename, typename> class U,
- template <typename, typename> class T, typename... Args>
+// IsConvertibleToSpecializationImpl needs multiple partial specializations to
+// SFINAE on the existence of container dependent types and plug them into the
+// STL template.
+template <typename C, template <typename, typename> class T>
+struct IsConvertibleToSpecializationImpl<
+ C, T, absl::void_t<typename C::value_type, typename C::allocator_type>>
+ : std::is_convertible<
+ C, T<typename C::value_type, typename C::allocator_type>> {};
+template <typename C, template <typename, typename, typename> class T>
+struct IsConvertibleToSpecializationImpl<
+ C, T,
+ absl::void_t<typename C::key_type, typename C::key_compare,
+ typename C::allocator_type>>
+ : std::is_convertible<C, T<typename C::key_type, typename C::key_compare,
+ typename C::allocator_type>> {};
+template <typename C, template <typename, typename, typename, typename> class T>
struct IsConvertibleToSpecializationImpl<
- U<Args...>, T, absl::void_t<typename U<Args...>::allocator_type>>
- : std::is_convertible<U<Args...>, T<Args...>> {};
-template <template <typename, typename, typename> class U,
- template <typename, typename, typename> class T, typename... Args>
+ C, T,
+ absl::void_t<typename C::key_type, typename C::mapped_type,
+ typename C::key_compare, typename C::allocator_type>>
+ : std::is_convertible<
+ C, T<typename C::key_type, typename C::mapped_type,
+ typename C::key_compare, typename C::allocator_type>> {};
+template <typename C, template <typename, typename, typename, typename> class T>
struct IsConvertibleToSpecializationImpl<
- U<Args...>, T, absl::void_t<typename U<Args...>::allocator_type>>
- : std::is_convertible<U<Args...>, T<Args...>> {};
-template <template <typename, typename, typename, typename> class U,
- template <typename, typename, typename, typename> class T,
- typename... Args>
+ C, T,
+ absl::void_t<typename C::key_type, typename C::hasher,
+ typename C::key_equal, typename C::allocator_type>>
+ : std::is_convertible<
+ C, T<typename C::key_type, typename C::hasher, typename C::key_equal,
+ typename C::allocator_type>> {};
+template <typename C,
+ template <typename, typename, typename, typename, typename> class T>
struct IsConvertibleToSpecializationImpl<
- U<Args...>, T, absl::void_t<typename U<Args...>::allocator_type>>
- : std::is_convertible<U<Args...>, T<Args...>> {};
+ C, T,
+ absl::void_t<typename C::key_type, typename C::mapped_type,
+ typename C::hasher, typename C::key_equal,
+ typename C::allocator_type>>
+ : std::is_convertible<C, T<typename C::key_type, typename C::mapped_type,
+ typename C::hasher, typename C::key_equal,
+ typename C::allocator_type>> {};
template <typename C, template <typename...> class T>
using IsConvertibleToSpecialization =
IsConvertibleToSpecializationImpl<absl::decay_t<C>, T>;
diff --git a/absl/strings/string_view.h b/absl/strings/string_view.h
index c3acd729..ddc89341 100644
--- a/absl/strings/string_view.h
+++ b/absl/strings/string_view.h
@@ -419,7 +419,7 @@ class string_view {
size_type rfind(string_view s, size_type pos = npos) const
noexcept;
- // Overload of `string_view::rfind()` for finding the given character `c`
+ // Overload of `string_view::rfind()` for finding the last given character `c`
// within the `string_view`.
size_type rfind(char c, size_type pos = npos) const noexcept;
diff --git a/absl/strings/string_view_test.cc b/absl/strings/string_view_test.cc
index 13fc214b..df307ac7 100644
--- a/absl/strings/string_view_test.cc
+++ b/absl/strings/string_view_test.cc
@@ -796,11 +796,25 @@ TEST(StringViewTest, FrontBackSingleChar) {
EXPECT_EQ(&c, &csp.back());
}
+// `std::string_view::string_view(const char*)` calls
+// `std::char_traits<char>::length(const char*)` to get the std::string length. In
+// libc++, it doesn't allow `nullptr` in the constexpr context, with the error
+// "read of dereferenced null pointer is not allowed in a constant expression".
+// At run time, the behavior of `std::char_traits::length()` on `nullptr` is
+// undefined by the standard and usually results in crash with libc++. This
+// conforms to the standard, but `absl::string_view` implements a different
+// behavior for historical reasons. We work around tests that construct
+// `string_view` from `nullptr` when using libc++.
+#if !defined(ABSL_HAVE_STD_STRING_VIEW) || !defined(_LIBCPP_VERSION)
+#define ABSL_HAVE_STRING_VIEW_FROM_NULLPTR 1
+#endif // !defined(ABSL_HAVE_STD_STRING_VIEW) || !defined(_LIBCPP_VERSION)
+
TEST(StringViewTest, NULLInput) {
absl::string_view s;
EXPECT_EQ(s.data(), nullptr);
EXPECT_EQ(s.size(), 0);
+#ifdef ABSL_HAVE_STRING_VIEW_FROM_NULLPTR
s = absl::string_view(nullptr);
EXPECT_EQ(s.data(), nullptr);
EXPECT_EQ(s.size(), 0);
@@ -808,6 +822,7 @@ TEST(StringViewTest, NULLInput) {
// .ToString() on a absl::string_view with nullptr should produce the empty
// std::string.
EXPECT_EQ("", std::string(s));
+#endif // ABSL_HAVE_STRING_VIEW_FROM_NULLPTR
}
TEST(StringViewTest, Comparisons2) {
@@ -879,7 +894,9 @@ TEST(StringViewTest, NullSafeStringView) {
TEST(StringViewTest, ConstexprCompiles) {
constexpr absl::string_view sp;
+#ifdef ABSL_HAVE_STRING_VIEW_FROM_NULLPTR
constexpr absl::string_view cstr(nullptr);
+#endif
constexpr absl::string_view cstr_len("cstr", 4);
#if defined(ABSL_HAVE_STD_STRING_VIEW)
@@ -923,10 +940,12 @@ TEST(StringViewTest, ConstexprCompiles) {
constexpr absl::string_view::iterator const_end_empty = sp.end();
EXPECT_EQ(const_begin_empty, const_end_empty);
+#ifdef ABSL_HAVE_STRING_VIEW_FROM_NULLPTR
constexpr absl::string_view::iterator const_begin_nullptr = cstr.begin();
constexpr absl::string_view::iterator const_end_nullptr = cstr.end();
EXPECT_EQ(const_begin_nullptr, const_end_nullptr);
-#endif
+#endif // ABSL_HAVE_STRING_VIEW_FROM_NULLPTR
+#endif // !defined(__clang__) || ...
constexpr absl::string_view::iterator const_begin = cstr_len.begin();
constexpr absl::string_view::iterator const_end = cstr_len.end();
@@ -1042,11 +1061,11 @@ TEST(HugeStringView, TwoPointTwoGB) {
}
#endif // THREAD_SANITIZER
-#ifndef NDEBUG
+#if !defined(NDEBUG) && !defined(ABSL_HAVE_STD_STRING_VIEW)
TEST(NonNegativeLenTest, NonNegativeLen) {
EXPECT_DEATH_IF_SUPPORTED(absl::string_view("xyz", -1), "len <= kMaxSize");
}
-#endif // NDEBUG
+#endif // !defined(NDEBUG) && !defined(ABSL_HAVE_STD_STRING_VIEW)
class StringViewStreamTest : public ::testing::Test {
public:
diff --git a/absl/strings/strip_test.cc b/absl/strings/strip_test.cc
index ff0e7f1c..205c160c 100644
--- a/absl/strings/strip_test.cc
+++ b/absl/strings/strip_test.cc
@@ -178,4 +178,24 @@ TEST(String, StripLeadingAsciiWhitespace) {
EXPECT_EQ(absl::string_view(), absl::StripLeadingAsciiWhitespace(orig));
}
+TEST(Strip, StripAsciiWhitespace) {
+ std::string test2 = "\t \f\r\n\vfoo \t\f\r\v\n";
+ absl::StripAsciiWhitespace(&test2);
+ EXPECT_EQ(test2, "foo");
+ std::string test3 = "bar";
+ absl::StripAsciiWhitespace(&test3);
+ EXPECT_EQ(test3, "bar");
+ std::string test4 = "\t \f\r\n\vfoo";
+ absl::StripAsciiWhitespace(&test4);
+ EXPECT_EQ(test4, "foo");
+ std::string test5 = "foo \t\f\r\v\n";
+ absl::StripAsciiWhitespace(&test5);
+ EXPECT_EQ(test5, "foo");
+ absl::string_view test6("\t \f\r\n\vfoo \t\f\r\v\n");
+ test6 = absl::StripAsciiWhitespace(test6);
+ EXPECT_EQ(test6, "foo");
+ test6 = absl::StripAsciiWhitespace(test6);
+ EXPECT_EQ(test6, "foo"); // already stripped
+}
+
} // namespace