about summary refs log tree commit diff
path: root/absl/strings
diff options
context:
space:
mode:
authorAbseil Team <absl-team@google.com>2019-08-30T19·03-0400
committerGennadiy Civil <misterg@google.com>2019-08-30T19·38-0400
commit1948f6f967e34db9793cfa8b4bcbaf370d039fd8 (patch)
treeb29287f3d7f35dca5042dfea5a3dac857303b841 /absl/strings
parenta0d1e098c2f99694fa399b175a7ccf920762030e (diff)
Export of internal Abseil changes
--
bc74316103bbda92541896f588f71c9d45bea768 by Gennadiy Civil <misterg@google.com>:

Manually fixing the BUILD.bazel files

--
d41bf9ea916a0dc8c69e6ba77f58f9d55649880e by Shaindel Schwartz <shaindel@google.com>:

Minor cleanup to miscellaneous BUILD files.

PiperOrigin-RevId: 266420157

--
08a8dc2cbd48d27e1115809f9ca8d178551cd66e by Gennadiy Civil <misterg@google.com>:

Internal Change
BEGIN_PUBLIC
Internal Change
END_PUBLIC

--
8617d58fde1ece40e4aa79eaa5e250b42d19835f by Shaindel Schwartz <shaindel@google.com>:

Internal Change
BEGIN_PUBLIC
Internal Change
END_PUBLIC

--
3a0fc7d48bdc70c4f7dffc219578693dea84eb2d by Derek Mauro <dmauro@google.com>:

Implement absl::string_view::at()

PiperOrigin-RevId: 266024644

--
ba53a9da8ede8fe7b8971eaab6b3a1fa34763ff6 by Andy Soffer <asoffer@google.com>:

Remove forcing of optimization levels in MSVC.

PiperOrigin-RevId: 265927588

--
df86f2046b54bba7da2e345040806d43470de5c0 by Shaindel Schwartz <shaindel@google.com>:

Internal change

PiperOrigin-RevId: 265811077

--
e2e3a6e8194363e7c6377672560c806d638a7c74 by Derek Mauro <dmauro@google.com>:

Remove ABI unsafe mixed exceptions mode compilation.
Testing will now be done on CI with the exceptions flag set globally.

PiperOrigin-RevId: 265796079
GitOrigin-RevId: bc74316103bbda92541896f588f71c9d45bea768
Change-Id: Ibccd00f4829520454aa55c4f55c7cb2dc9c6b65a
Diffstat (limited to 'absl/strings')
-rw-r--r--absl/strings/BUILD.bazel5
-rw-r--r--absl/strings/CMakeLists.txt3
-rw-r--r--absl/strings/string_view.h14
-rw-r--r--absl/strings/string_view_test.cc20
4 files changed, 31 insertions, 11 deletions
diff --git a/absl/strings/BUILD.bazel b/absl/strings/BUILD.bazel
index cb80841694eb..eae75d85c499 100644
--- a/absl/strings/BUILD.bazel
+++ b/absl/strings/BUILD.bazel
@@ -17,8 +17,6 @@ load("@rules_cc//cc:defs.bzl", "cc_library", "cc_test")
 load(
     "//absl:copts/configure_copts.bzl",
     "ABSL_DEFAULT_COPTS",
-    "ABSL_EXCEPTIONS_FLAG",
-    "ABSL_EXCEPTIONS_FLAG_LINKOPTS",
     "ABSL_TEST_COPTS",
 )
 
@@ -237,8 +235,7 @@ cc_test(
     name = "string_view_test",
     size = "small",
     srcs = ["string_view_test.cc"],
-    copts = ABSL_TEST_COPTS + ABSL_EXCEPTIONS_FLAG,
-    linkopts = ABSL_EXCEPTIONS_FLAG_LINKOPTS,
+    copts = ABSL_TEST_COPTS,
     visibility = ["//visibility:private"],
     deps = [
         ":strings",
diff --git a/absl/strings/CMakeLists.txt b/absl/strings/CMakeLists.txt
index 82a906b7e4b9..ccff44419341 100644
--- a/absl/strings/CMakeLists.txt
+++ b/absl/strings/CMakeLists.txt
@@ -161,9 +161,6 @@ absl_cc_test(
     "string_view_test.cc"
   COPTS
     ${ABSL_TEST_COPTS}
-    ${ABSL_EXCEPTIONS_FLAG}
-  LINKOPTS
-    ${ABSL_EXCEPTIONS_FLAG_LINKOPTS}
   DEPS
     absl::strings
     absl::config
diff --git a/absl/strings/string_view.h b/absl/strings/string_view.h
index a1b5a17b52b3..68b90aa3126f 100644
--- a/absl/strings/string_view.h
+++ b/absl/strings/string_view.h
@@ -269,10 +269,22 @@ class string_view {
 
   // string_view::operator[]
   //
-  // Returns the ith element of an `string_view` using the array operator.
+  // Returns the ith element of the `string_view` using the array operator.
   // Note that this operator does not perform any bounds checking.
   constexpr const_reference operator[](size_type i) const { return ptr_[i]; }
 
+  // string_view::at()
+  //
+  // Returns the ith element of the `string_view`. Bounds checking is performed,
+  // and an exception of type `std::out_of_range` will be thrown on invalid
+  // access.
+  constexpr const_reference at(size_type i) const {
+    return ABSL_PREDICT_TRUE(i < size())
+               ? ptr_[i]
+               : (base_internal::ThrowStdOutOfRange("absl::string_view::at"),
+                  ptr_[i]);
+  }
+
   // string_view::front()
   //
   // Returns the first element of a `string_view`.
diff --git a/absl/strings/string_view_test.cc b/absl/strings/string_view_test.cc
index 4f531226dfe2..eb8b170bbbde 100644
--- a/absl/strings/string_view_test.cc
+++ b/absl/strings/string_view_test.cc
@@ -29,7 +29,8 @@
 #include "absl/base/config.h"
 #include "absl/base/dynamic_annotations.h"
 
-#ifdef __ANDROID__
+#if defined(ABSL_HAVE_STD_STRING_VIEW) || defined(__ANDROID__)
+// We don't control the death messaging when using std::string_view.
 // Android assert messages only go to system log, so death tests cannot inspect
 // the message for matching.
 #define ABSL_EXPECT_DEATH_IF_SUPPORTED(statement, regex) \
@@ -372,7 +373,7 @@ TEST(StringViewTest, STL1) {
 #ifdef ABSL_HAVE_EXCEPTIONS
   EXPECT_THROW(a.copy(buf, 1, 27), std::out_of_range);
 #else
-  EXPECT_DEATH(a.copy(buf, 1, 27), "absl::string_view::copy");
+  ABSL_EXPECT_DEATH_IF_SUPPORTED(a.copy(buf, 1, 27), "absl::string_view::copy");
 #endif
 }
 
@@ -686,7 +687,8 @@ TEST(StringViewTest, STL2Substr) {
 #ifdef ABSL_HAVE_EXCEPTIONS
   EXPECT_THROW((void)a.substr(99, 2), std::out_of_range);
 #else
-  EXPECT_DEATH((void)a.substr(99, 2), "absl::string_view::substr");
+  ABSL_EXPECT_DEATH_IF_SUPPORTED((void)a.substr(99, 2),
+                                 "absl::string_view::substr");
 #endif
 }
 
@@ -894,6 +896,18 @@ TEST(StringViewTest, Comparisons2) {
   EXPECT_LT(digits.compare(0, npos, "0123456789", 3, 5), 0);  // 6
 }
 
+TEST(StringViewTest, At) {
+  absl::string_view abc = "abc";
+  EXPECT_EQ(abc.at(0), 'a');
+  EXPECT_EQ(abc.at(1), 'b');
+  EXPECT_EQ(abc.at(2), 'c');
+#ifdef ABSL_HAVE_EXCEPTIONS
+  EXPECT_THROW(abc.at(3), std::out_of_range);
+#else
+  ABSL_EXPECT_DEATH_IF_SUPPORTED(abc.at(3), "absl::string_view::at");
+#endif
+}
+
 struct MyCharAlloc : std::allocator<char> {};
 
 TEST(StringViewTest, ExplicitConversionOperator) {