summaryrefslogtreecommitdiff
path: root/absl/strings/string_view.h
diff options
context:
space:
mode:
authorGravatar Abseil Team <absl-team@google.com>2019-08-06 07:13:35 -0700
committerGravatar CJ Johnson <johnsoncj@google.com>2019-08-06 14:16:08 -0400
commit67222ffc4c83d918ce8395aa61769eeb77df4c4d (patch)
tree6dba7ec72ecc36105c848fc5da5c044a33643e13 /absl/strings/string_view.h
parentc5c4db4f5191fe5e76cbf68dcc71fb28702f7d2b (diff)
Export of internal Abseil changes
-- 5315e7b98905922e779798f3168d98343438c134 by Derek Mauro <dmauro@google.com>: Fix absl::string_view::copy to throw std::out_of_range when pos > size(). Fixes https://github.com/abseil/abseil-cpp/issues/362 PiperOrigin-RevId: 261907364 GitOrigin-RevId: 5315e7b98905922e779798f3168d98343438c134 Change-Id: Ia8ab971c54f287411f6ea4b99f9c666c989c33fd
Diffstat (limited to 'absl/strings/string_view.h')
-rw-r--r--absl/strings/string_view.h13
1 files changed, 12 insertions, 1 deletions
diff --git a/absl/strings/string_view.h b/absl/strings/string_view.h
index 65b1772d..25a4d1ed 100644
--- a/absl/strings/string_view.h
+++ b/absl/strings/string_view.h
@@ -50,6 +50,7 @@ using std::string_view;
#include "absl/base/internal/throw_delegate.h"
#include "absl/base/macros.h"
+#include "absl/base/optimization.h"
#include "absl/base/port.h"
namespace absl {
@@ -334,7 +335,17 @@ class string_view {
//
// Copies the contents of the `string_view` at offset `pos` and length `n`
// into `buf`.
- size_type copy(char* buf, size_type n, size_type pos = 0) const;
+ size_type copy(char* buf, size_type n, size_type pos = 0) const {
+ if (ABSL_PREDICT_FALSE(pos > length_)) {
+ base_internal::ThrowStdOutOfRange("absl::string_view::copy");
+ }
+ size_type rlen = (std::min)(length_ - pos, n);
+ if (rlen > 0) {
+ const char* start = ptr_ + pos;
+ std::copy(start, start + rlen, buf);
+ }
+ return rlen;
+ }
// string_view::substr()
//