From bcefbdcdf6ad85046ccacee0aeffba5404d3e528 Mon Sep 17 00:00:00 2001 From: Abseil Team Date: Fri, 28 Feb 2020 12:18:28 -0800 Subject: Export of internal Abseil changes -- 09c1e7877210fe85c43631538303af801c233e89 by Gennadiy Rozental : Change CordRep::length to size_t to be compatible with ChunkIterator. PiperOrigin-RevId: 297901255 -- 226d5e79fb4e20fb09d75f034624d5be770f4ece by Derek Mauro : Makes absl::string_view::substr constexpr for std::string_view compatibility Fixes #627 PiperOrigin-RevId: 297872778 -- 851aa24a22d0ba5552098bf7e5735c95e4a8d4f7 by Abseil Team : Reformat one line. PiperOrigin-RevId: 297839574 -- 4f449c462583797455375fa6f1365a6b2cfa7e0a by Benjamin Barenblat : Internal change PiperOrigin-RevId: 297677173 -- 2d41a250e9a8f272946bc3262463e4025d88fba3 by Abseil Team : Internal change PiperOrigin-RevId: 297537076 -- 6e7fbe1e90999a58556d41955bef44033c61876c by Gennadiy Rozental : Internal change PiperOrigin-RevId: 297376994 -- 5acf79338191b31cec158b06cd96ca8dfa3e81fe by Derek Mauro : Add a debug-mode bounds-check on absl::Span::operator[]. PiperOrigin-RevId: 297355826 -- 1c540d06a56c7e92bb07b90f16b4e00b014ef18f by CJ Johnson : Adding new LTS to the list PiperOrigin-RevId: 297235265 -- 696ce48bea6927436ff89f59b887e5869b1b0f38 by Derek Mauro : Fix build on FreeBSD/powerpc (implement UnscaledCycleClock) Merges/Fixes GitHub #616 PiperOrigin-RevId: 297188640 GitOrigin-RevId: 09c1e7877210fe85c43631538303af801c233e89 Change-Id: I5d97b16bb6378792d2fcf7d29080cca18aa7729a --- absl/strings/cord.cc | 2 -- absl/strings/internal/cord_internal.h | 3 +-- absl/strings/internal/str_split_internal.h | 4 ++-- absl/strings/string_view.h | 11 ++++++----- absl/strings/string_view_test.cc | 8 ++++++++ 5 files changed, 17 insertions(+), 11 deletions(-) (limited to 'absl/strings') diff --git a/absl/strings/cord.cc b/absl/strings/cord.cc index d9503ae332d7..5cc68539941d 100644 --- a/absl/strings/cord.cc +++ b/absl/strings/cord.cc @@ -136,8 +136,6 @@ inline const CordRepExternal* CordRep::external() const { static const size_t kFlatOverhead = offsetof(CordRep, data); -static_assert(kFlatOverhead == 13, "Unittests assume kFlatOverhead == 13"); - // Largest and smallest flat node lengths we are willing to allocate // Flat allocation size is stored in tag, which currently can encode sizes up // to 4K, encoded as multiple of either 8 or 32 bytes. diff --git a/absl/strings/internal/cord_internal.h b/absl/strings/internal/cord_internal.h index 5b5d10830891..830ceaf473bd 100644 --- a/absl/strings/internal/cord_internal.h +++ b/absl/strings/internal/cord_internal.h @@ -86,8 +86,7 @@ struct CordRepExternal; struct CordRep { // The following three fields have to be less than 32 bytes since // that is the smallest supported flat node size. - // We use uint64_t for the length even in 32-bit binaries. - uint64_t length; + size_t length; Refcount refcount; // If tag < FLAT, it represents CordRepKind and indicates the type of node. // Otherwise, the node type is CordRepFlat and the tag is the encoded size. diff --git a/absl/strings/internal/str_split_internal.h b/absl/strings/internal/str_split_internal.h index b54f6ebe09e0..6f5bc095fbbe 100644 --- a/absl/strings/internal/str_split_internal.h +++ b/absl/strings/internal/str_split_internal.h @@ -65,8 +65,8 @@ class ConvertibleToStringView { : value_(s) {} // Matches rvalue strings and moves their data to a member. -ConvertibleToStringView(std::string&& s) // NOLINT(runtime/explicit) - : copy_(std::move(s)), value_(copy_) {} + ConvertibleToStringView(std::string&& s) // NOLINT(runtime/explicit) + : copy_(std::move(s)), value_(copy_) {} ConvertibleToStringView(const ConvertibleToStringView& other) : copy_(other.copy_), diff --git a/absl/strings/string_view.h b/absl/strings/string_view.h index 1861ea62a9ea..55e80d62ae80 100644 --- a/absl/strings/string_view.h +++ b/absl/strings/string_view.h @@ -382,11 +382,12 @@ class string_view { // Returns a "substring" of the `string_view` (at offset `pos` and length // `n`) as another string_view. This function throws `std::out_of_bounds` if // `pos > size`. - string_view substr(size_type pos, size_type n = npos) const { - if (ABSL_PREDICT_FALSE(pos > length_)) - base_internal::ThrowStdOutOfRange("absl::string_view::substr"); - n = (std::min)(n, length_ - pos); - return string_view(ptr_ + pos, n); + constexpr string_view substr(size_type pos, size_type n = npos) const { + return ABSL_PREDICT_FALSE(pos > length_) + ? (base_internal::ThrowStdOutOfRange( + "absl::string_view::substr"), + string_view()) + : string_view(ptr_ + pos, Min(n, length_ - pos)); } // string_view::compare() diff --git a/absl/strings/string_view_test.cc b/absl/strings/string_view_test.cc index 7b1d56fac762..cb6a758fea6f 100644 --- a/absl/strings/string_view_test.cc +++ b/absl/strings/string_view_test.cc @@ -1086,6 +1086,14 @@ TEST(StringViewTest, ConstexprCompiles) { EXPECT_EQ(sp_npos, -1); } +TEST(StringViewTest, ConstexprSubstr) { + constexpr absl::string_view foobar("foobar", 6); + constexpr absl::string_view foo = foobar.substr(0, 3); + constexpr absl::string_view bar = foobar.substr(3); + EXPECT_EQ(foo, "foo"); + EXPECT_EQ(bar, "bar"); +} + TEST(StringViewTest, Noexcept) { EXPECT_TRUE((std::is_nothrow_constructible::value)); -- cgit 1.4.1