tvl-depot/absl/strings/string_view.h

566 lines
20 KiB
C
Raw Normal View History

2017-09-19 22:54:40 +02:00
//
// Copyright 2017 The Abseil Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// -----------------------------------------------------------------------------
// File: string_view.h
// -----------------------------------------------------------------------------
//
// This file contains the definition of the `absl::string_view` class. A
// `string_view` points to a contiguous span of characters, often part or all of
// another `std::string`, double-quoted string literal, character array, or even
2017-09-19 22:54:40 +02:00
// another `string_view`.
//
// This `absl::string_view` abstraction is designed to be a drop-in
// replacement for the C++17 `std::string_view` abstraction.
#ifndef ABSL_STRINGS_STRING_VIEW_H_
#define ABSL_STRINGS_STRING_VIEW_H_
#include <algorithm>
#include "absl/base/config.h"
#ifdef ABSL_HAVE_STD_STRING_VIEW
#include <string_view>
namespace absl {
using std::string_view;
} // namespace absl
2017-09-19 22:54:40 +02:00
#else // ABSL_HAVE_STD_STRING_VIEW
#include <cassert>
#include <cstddef>
#include <cstring>
#include <iosfwd>
#include <iterator>
#include <limits>
#include <string>
#include "absl/base/internal/throw_delegate.h"
#include "absl/base/macros.h"
#include "absl/base/port.h"
namespace absl {
// absl::string_view
//
// A `string_view` provides a lightweight view into the string data provided by
// a `std::string`, double-quoted string literal, character array, or even
// another `string_view`. A `string_view` does *not* own the string to which it
2017-09-19 22:54:40 +02:00
// points, and that data cannot be modified through the view.
//
// You can use `string_view` as a function or method parameter anywhere a
// parameter can receive a double-quoted string literal, `const char*`,
2017-09-19 22:54:40 +02:00
// `std::string`, or another `absl::string_view` argument with no need to copy
// the string data. Systematic use of `string_view` within function arguments
2017-09-19 22:54:40 +02:00
// reduces data copies and `strlen()` calls.
//
// Because of its small size, prefer passing `string_view` by value:
//
// void MyFunction(absl::string_view arg);
//
// If circumstances require, you may also pass one by const reference:
//
// void MyFunction(const absl::string_view& arg); // not preferred
//
// Passing by value generates slightly smaller code for many architectures.
//
// In either case, the source data of the `string_view` must outlive the
// `string_view` itself.
//
// A `string_view` is also suitable for local variables if you know that the
// lifetime of the underlying object is longer than the lifetime of your
// `string_view` variable. However, beware of binding a `string_view` to a
// temporary value:
//
// // BAD use of string_view: lifetime problem
// absl::string_view sv = obj.ReturnAString();
//
// // GOOD use of string_view: str outlives sv
// std::string str = obj.ReturnAString();
// absl::string_view sv = str;
//
// Due to lifetime issues, a `string_view` is sometimes a poor choice for a
// return value and usually a poor choice for a data member. If you do use a
// `string_view` this way, it is your responsibility to ensure that the object
// pointed to by the `string_view` outlives the `string_view`.
//
// A `string_view` may represent a whole string or just part of a string. For
// example, when splitting a string, `std::vector<absl::string_view>` is a
2017-09-19 22:54:40 +02:00
// natural data type for the output.
//
//
// When constructed from a source which is nul-terminated, the `string_view`
// itself will not include the nul-terminator unless a specific size (including
// the nul) is passed to the constructor. As a result, common idioms that work
// on nul-terminated strings do not work on `string_view` objects. If you write
// code that scans a `string_view`, you must check its length rather than test
// for nul, for example. Note, however, that nuls may still be embedded within
// a `string_view` explicitly.
//
// You may create a null `string_view` in two ways:
//
// absl::string_view sv();
// absl::string_view sv(nullptr, 0);
//
// For the above, `sv.data() == nullptr`, `sv.length() == 0`, and
// `sv.empty() == true`. Also, if you create a `string_view` with a non-null
// pointer then `sv.data() != nullptr`. Thus, you can use `string_view()` to
// signal an undefined value that is different from other `string_view` values
// in a similar fashion to how `const char* p1 = nullptr;` is different from
// `const char* p2 = "";`. However, in practice, it is not recommended to rely
// on this behavior.
//
// Be careful not to confuse a null `string_view` with an empty one. A null
// `string_view` is an empty `string_view`, but some empty `string_view`s are
// not null. Prefer checking for emptiness over checking for null.
//
// There are many ways to create an empty string_view:
//
// const char* nullcp = nullptr;
// // string_view.size() will return 0 in all cases.
// absl::string_view();
// absl::string_view(nullcp, 0);
// absl::string_view("");
// absl::string_view("", 0);
// absl::string_view("abcdef", 0);
// absl::string_view("abcdef" + 6, 0);
//
// All empty `string_view` objects whether null or not, are equal:
//
// absl::string_view() == absl::string_view("", 0)
// absl::string_view(nullptr, 0) == absl::string_view("abcdef"+6, 0)
2017-09-19 22:54:40 +02:00
class string_view {
public:
using traits_type = std::char_traits<char>;
using value_type = char;
using pointer = char*;
using const_pointer = const char*;
using reference = char&;
using const_reference = const char&;
using const_iterator = const char*;
using iterator = const_iterator;
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
using reverse_iterator = const_reverse_iterator;
using size_type = size_t;
using difference_type = std::ptrdiff_t;
static constexpr size_type npos = static_cast<size_type>(-1);
// Null `string_view` constructor
constexpr string_view() noexcept : ptr_(nullptr), length_(0) {}
// Implicit constructors
template <typename Allocator>
string_view( // NOLINT(runtime/explicit)
const std::basic_string<char, std::char_traits<char>, Allocator>&
str) noexcept
2018-04-18 14:56:39 +02:00
: ptr_(str.data()), length_(CheckLengthInternal(str.size())) {}
2017-09-19 22:54:40 +02:00
// Implicit constructor of a `string_view` from nul-terminated `str`. When
// accepting possibly null strings, use `absl::NullSafeStringView(str)`
// instead (see below).
Export of internal Abseil changes. -- eca34da4ccb7bb6a580f1364dff9ca053418fa3b by Abseil Team <absl-team@google.com>: Internal change. PiperOrigin-RevId: 214305433 -- 35393bdd21a87c4286f945fd34dda93afc4e0cd6 by Abseil Team <absl-team@google.com>: Move some implementation details of string_view around to facilitate compiling on NVCC. Abseil does not officially support NVCC as a reminder. PiperOrigin-RevId: 214184876 -- 61846cab9ab9476a4676ecade7173f68978cd038 by Jorg Brown <jorg@google.com>: Move the initialization values for constants back to their declaration. PiperOrigin-RevId: 214135927 -- 7ac7df6c5f78f2faf419268c04618b936cb26065 by Abseil Team <absl-team@google.com>: Performance improvements on format parser. PiperOrigin-RevId: 214032366 -- 90b4c0cf20e9feaa257a7ece40adaf7db40a60a7 by Xiaoyi Zhang <zhangxy@google.com>: Add static_assert check to absl::visit to make sure all overloads of the visitor return the same type, as required by the C++ standard. PiperOrigin-RevId: 213677001 -- 787995342101b4c181291cde9ecea3048536e4bd by Abseil Team <absl-team@google.com>: Update comment to indicate finite durations are less than InfiniteDuration. PiperOrigin-RevId: 213660328 -- d78f0dce7cc31218807e96d93b9e8513b6c80b24 by Jon Cohen <cohenjon@google.com>: s/invariant/contract in the exceptions safety testing framework. This is a better term as these can be type invariants or function post conditions. They also are very similar ground as to what is covered by c++20 Contracts (and could even be replaced by them. PiperOrigin-RevId: 213631019 -- 0b3ff1a640de9a7391a6c233568802cf86245b0e by Abseil Team <absl-team@google.com>: Add noinline attribute for GetStackTrace/GetStackFrames/... so the skipped frames will not change because of inlining difference. PiperOrigin-RevId: 213009637 GitOrigin-RevId: eca34da4ccb7bb6a580f1364dff9ca053418fa3b Change-Id: Iff1022fd24e440fcbdf3c4ab2a915ca8954daa31
2018-09-24 20:57:52 +02:00
#if ABSL_HAVE_BUILTIN(__builtin_strlen) || \
(defined(__GNUC__) && !defined(__clang__))
// GCC has __builtin_strlen according to
// https://gcc.gnu.org/onlinedocs/gcc-4.7.0/gcc/Other-Builtins.html, but
// ABSL_HAVE_BUILTIN doesn't detect that, so we use the extra checks above.
// __builtin_strlen is constexpr.
constexpr string_view(const char* str) // NOLINT(runtime/explicit)
: ptr_(str),
length_(CheckLengthInternal(str ? __builtin_strlen(str) : 0)) {}
#else
2017-09-19 22:54:40 +02:00
constexpr string_view(const char* str) // NOLINT(runtime/explicit)
Export of internal Abseil changes. -- eca34da4ccb7bb6a580f1364dff9ca053418fa3b by Abseil Team <absl-team@google.com>: Internal change. PiperOrigin-RevId: 214305433 -- 35393bdd21a87c4286f945fd34dda93afc4e0cd6 by Abseil Team <absl-team@google.com>: Move some implementation details of string_view around to facilitate compiling on NVCC. Abseil does not officially support NVCC as a reminder. PiperOrigin-RevId: 214184876 -- 61846cab9ab9476a4676ecade7173f68978cd038 by Jorg Brown <jorg@google.com>: Move the initialization values for constants back to their declaration. PiperOrigin-RevId: 214135927 -- 7ac7df6c5f78f2faf419268c04618b936cb26065 by Abseil Team <absl-team@google.com>: Performance improvements on format parser. PiperOrigin-RevId: 214032366 -- 90b4c0cf20e9feaa257a7ece40adaf7db40a60a7 by Xiaoyi Zhang <zhangxy@google.com>: Add static_assert check to absl::visit to make sure all overloads of the visitor return the same type, as required by the C++ standard. PiperOrigin-RevId: 213677001 -- 787995342101b4c181291cde9ecea3048536e4bd by Abseil Team <absl-team@google.com>: Update comment to indicate finite durations are less than InfiniteDuration. PiperOrigin-RevId: 213660328 -- d78f0dce7cc31218807e96d93b9e8513b6c80b24 by Jon Cohen <cohenjon@google.com>: s/invariant/contract in the exceptions safety testing framework. This is a better term as these can be type invariants or function post conditions. They also are very similar ground as to what is covered by c++20 Contracts (and could even be replaced by them. PiperOrigin-RevId: 213631019 -- 0b3ff1a640de9a7391a6c233568802cf86245b0e by Abseil Team <absl-team@google.com>: Add noinline attribute for GetStackTrace/GetStackFrames/... so the skipped frames will not change because of inlining difference. PiperOrigin-RevId: 213009637 GitOrigin-RevId: eca34da4ccb7bb6a580f1364dff9ca053418fa3b Change-Id: Iff1022fd24e440fcbdf3c4ab2a915ca8954daa31
2018-09-24 20:57:52 +02:00
: ptr_(str), length_(CheckLengthInternal(str ? strlen(str) : 0)) {}
#endif
2017-09-19 22:54:40 +02:00
// Implicit constructor of a `string_view` from a `const char*` and length.
2017-09-19 22:54:40 +02:00
constexpr string_view(const char* data, size_type len)
: ptr_(data), length_(CheckLengthInternal(len)) {}
Changes imported from Abseil "staging" branch: - b527a3e4b36b644ac424e3c525b1cd393f6f6c40 Fix some typos in the usage examples by Jorg Brown <jorg@google.com> - 82be4a9adf3bb0ddafc0d46274969c99afffe870 Fix typo in optional.h comment. by Abseil Team <absl-team@google.com> - d6ee63bf8fc51fba074c23b33cebc28c808d7f07 Remove internal-only identifiers from code. by Daniel Katz <katzdm@google.com> - f9c3ad2f0d73f53b21603638af8b4bed636e79f4 Use easier understandable names for absl::StartsWith and ... by Abseil Team <absl-team@google.com> - 7c16c14fefee89c927b8789d6043c4691bcffc9b Add -Wno-missing-prototypes back to the LLVM copts. by Derek Mauro <dmauro@google.com> - 2f4b7d2e50c7023240242f1e15db60ccd7e8768d IWYU | absl/strings by Juemin Yang <jueminyang@google.com> - a99cbcc1daa34a2d6a2bb26de275e05173cc77e9 IWYU | absl/type by Juemin Yang <jueminyang@google.com> - 12e1146d0fc76c071d7e0ebaabb62f0a984fae66 Use LLVM_FLAGS and LLVM_TEST_FLAGS when --compiler=llvm. by Derek Mauro <dmauro@google.com> - cd6bea616abda558d0bace5bd77455662a233688 IWYU | absl/debugging by Juemin Yang <jueminyang@google.com> - d9a7382e59d46a8581b6b7a31cd5a48bb89326e9 IWYU | absl/synchronization by Juemin Yang <jueminyang@google.com> - 07ec7d6d5a4a666f4183c5d0ed9c342baa7b24bc IWYU | absl/numeric by Juemin Yang <jueminyang@google.com> - 12bfe40051f4270f8707e191af5652f83f2f750c Remove the RoundTrip{Float,Double}ToBuffer routines from ... by Jorg Brown <jorg@google.com> - eeb4fd67c9d97f66cb9475c3c5e51ab132f1c810 Adds conversion functions for converting between absl/tim... by Greg Miller <jgm@google.com> - 59a2108d05d4ea85dc5cc11e49b2cd2335d4295a Change Substitute to use %.6g formatting rather than 15/1... by Jorg Brown <jorg@google.com> - 394becb48e0fcd161642cdaac5120d32567e0ef8 IWYU | absl/meta by Juemin Yang <jueminyang@google.com> - 1e5da6e8da336699b2469dcf6dda025b9b0ec4c9 Rewrite atomic_hook.h to not use std::atomic<T*> under Wi... by Greg Falcon <gfalcon@google.com> GitOrigin-RevId: b527a3e4b36b644ac424e3c525b1cd393f6f6c40 Change-Id: I14e331d91c956ef045ac7927091a9f179716de0c
2017-09-24 17:20:48 +02:00
// NOTE: Harmlessly omitted to work around gdb bug.
// constexpr string_view(const string_view&) noexcept = default;
// string_view& operator=(const string_view&) noexcept = default;
2017-09-19 22:54:40 +02:00
// Iterators
// string_view::begin()
//
// Returns an iterator pointing to the first character at the beginning of the
// `string_view`, or `end()` if the `string_view` is empty.
constexpr const_iterator begin() const noexcept { return ptr_; }
// string_view::end()
//
// Returns an iterator pointing just beyond the last character at the end of
// the `string_view`. This iterator acts as a placeholder; attempting to
// access it results in undefined behavior.
constexpr const_iterator end() const noexcept { return ptr_ + length_; }
// string_view::cbegin()
//
// Returns a const iterator pointing to the first character at the beginning
// of the `string_view`, or `end()` if the `string_view` is empty.
constexpr const_iterator cbegin() const noexcept { return begin(); }
// string_view::cend()
//
// Returns a const iterator pointing just beyond the last character at the end
// of the `string_view`. This pointer acts as a placeholder; attempting to
// access its element results in undefined behavior.
constexpr const_iterator cend() const noexcept { return end(); }
// string_view::rbegin()
//
// Returns a reverse iterator pointing to the last character at the end of the
// `string_view`, or `rend()` if the `string_view` is empty.
const_reverse_iterator rbegin() const noexcept {
return const_reverse_iterator(end());
}
// string_view::rend()
//
// Returns a reverse iterator pointing just before the first character at the
// beginning of the `string_view`. This pointer acts as a placeholder;
// attempting to access its element results in undefined behavior.
const_reverse_iterator rend() const noexcept {
return const_reverse_iterator(begin());
}
// string_view::crbegin()
//
// Returns a const reverse iterator pointing to the last character at the end
// of the `string_view`, or `crend()` if the `string_view` is empty.
const_reverse_iterator crbegin() const noexcept { return rbegin(); }
// string_view::crend()
//
// Returns a const reverse iterator pointing just before the first character
// at the beginning of the `string_view`. This pointer acts as a placeholder;
// attempting to access its element results in undefined behavior.
const_reverse_iterator crend() const noexcept { return rend(); }
// Capacity Utilities
// string_view::size()
//
// Returns the number of characters in the `string_view`.
constexpr size_type size() const noexcept {
return length_;
}
// string_view::length()
//
// Returns the number of characters in the `string_view`. Alias for `size()`.
constexpr size_type length() const noexcept { return size(); }
// string_view::max_size()
//
// Returns the maximum number of characters the `string_view` can hold.
constexpr size_type max_size() const noexcept { return kMaxSize; }
// string_view::empty()
//
// Checks if the `string_view` is empty (refers to no characters).
constexpr bool empty() const noexcept { return length_ == 0; }
// string_view::operator[]
2017-09-19 22:54:40 +02:00
//
// Returns the ith element of an `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::front()
//
// Returns the first element of a `string_view`.
constexpr const_reference front() const { return ptr_[0]; }
// string_view::back()
//
// Returns the last element of a `string_view`.
constexpr const_reference back() const { return ptr_[size() - 1]; }
// string_view::data()
//
// Returns a pointer to the underlying character array (which is of course
// stored elsewhere). Note that `string_view::data()` may contain embedded nul
// characters, but the returned buffer may or may not be nul-terminated;
// therefore, do not pass `data()` to a routine that expects a nul-terminated
// std::string.
constexpr const_pointer data() const noexcept { return ptr_; }
// Modifiers
// string_view::remove_prefix()
//
// Removes the first `n` characters from the `string_view`. Note that the
// underlying std::string is not changed, only the view.
2017-09-19 22:54:40 +02:00
void remove_prefix(size_type n) {
assert(n <= length_);
ptr_ += n;
length_ -= n;
}
// string_view::remove_suffix()
//
// Removes the last `n` characters from the `string_view`. Note that the
// underlying std::string is not changed, only the view.
void remove_suffix(size_type n) {
assert(n <= length_);
length_ -= n;
}
// string_view::swap()
//
// Swaps this `string_view` with another `string_view`.
void swap(string_view& s) noexcept {
auto t = *this;
*this = s;
s = t;
}
// Explicit conversion operators
// Converts to `std::basic_string`.
2017-09-19 22:54:40 +02:00
template <typename A>
explicit operator std::basic_string<char, traits_type, A>() const {
if (!data()) return {};
return std::basic_string<char, traits_type, A>(data(), size());
}
// string_view::copy()
//
// 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;
// string_view::substr()
//
Changes imported from Abseil "staging" branch: - 43853019b439efb32c79d5d50e24508588e1bbe0 Undo the not applying qualifications to absl types in enc... by Derek Mauro <dmauro@google.com> - 06d62a10621c9864279ee57097069cfe3cb7b42a fix capitalization by Abseil Team <absl-team@google.com> - 22adbfee340bb452ba38b68975ade6f072859c4a Fix indices in str_split.h comments. by Derek Mauro <dmauro@google.com> - ae5143a559ad8633a78cd76620e30a781006d088 Fix the inconsistent licenses directives in the BUILD fil... by Derek Mauro <dmauro@google.com> - 0a76a3653b2ecfdad433d3e2f5b651c4ecdcf74b Remove strip.cc, fastmem.h, and fastmem_test.cc from the ... by Derek Mauro <dmauro@google.com> - 77908cfce5927aabca1f8d62481106f22cfc1936 Internal change. by Derek Mauro <dmauro@google.com> - d3277b4171f37e22ab346becb5e295c36c7a0219 Be consistent in (not) applying qualifications for enclos... by Abseil Team <absl-team@google.com> - 9ec7f8164e7d6a5f64288a7360a346628393cc50 Add std:: qualification to isnan and isinf in duration_te... by Derek Mauro <dmauro@google.com> - 9f7c87d7764ddba05286fabca1f4f15285f3250a Fix typos in string_view comments. by Abseil Team <absl-team@google.com> - 281860804f8053143d969b99876e3dbc6deb1236 Fix typo in container.h docs. by Abseil Team <absl-team@google.com> - 0b0a9388c7a9d7f72349d44b5b46132f45bde56c Add bazel-* symlinks to gitignore. by Michael Pratt <mpratt@google.com> GitOrigin-RevId: 43853019b439efb32c79d5d50e24508588e1bbe0 Change-Id: I9e74a5430816a34ecf1acb86486ed3b0bd12a1d6
2017-09-27 19:50:48 +02:00
// 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`.
2017-09-19 22:54:40 +02:00
string_view substr(size_type pos, size_type n = npos) const {
if (ABSL_PREDICT_FALSE(pos > length_))
base_internal::ThrowStdOutOfRange("absl::string_view::substr");
Export of internal Abseil changes. -- 22fa219d17b2281c0695642830c4300711bd65ea by CJ Johnson <johnsoncj@google.com>: Rearrange the private method declarations in InlinedVector PiperOrigin-RevId: 224202447 -- eed3c9f488f23b521bee41d3683eb6cc22517ded by Derek Mauro <dmauro@google.com>: Fix leak_check target (it was always a no-op when LSAN isn't available). Fixes https://github.com/abseil/abseil-cpp/issues/232 PiperOrigin-RevId: 224201634 -- fc08039e175204b14a9561f618fcfc0234586801 by Greg Falcon <gfalcon@google.com>: Add parens around more invocations of min() and max() missed in my prior CL. PiperOrigin-RevId: 224162430 -- 0ec5476a8293c7796cd84928a1a558b14f14f222 by Abseil Team <absl-team@google.com>: Update absl/numeric/CMakeLists.txt to use new functions i.e. absl_cc_(library|test) PiperOrigin-RevId: 224139165 -- 2b46aa6fabb20c589661f8bbc84030ecf39ce394 by Abseil Team <absl-team@google.com>: Update absl/meta/CMakeLists.txt to use new functions i.e. absl_cc_(library|test) PiperOrigin-RevId: 224117258 -- 6c951c798f8c6903bd8793a8a4b5f69244be8aa9 by Abseil Team <absl-team@google.com>: Fix 2 Unused C++ BUILD Dependencies PiperOrigin-RevId: 224070093 -- 0ee7bd191708708f91fc5209c197fd93f6e4a8b3 by Greg Falcon <gfalcon@google.com>: Inside Abseil headers, wrap most invocations of methods and functions named `min` and `max` in parentheses, for better interoperability with Windows toolchains. CCTZ fixes will appear in a follow-up CL. PiperOrigin-RevId: 224051960 -- f562f56577b84a8bc07e5873775c01d068531bca by Jon Cohen <cohenjon@google.com>: Generate Abseil compile options. The single source of truth is now absl/copts/copts.py The way this works goes something like this: copts.py acts as the configuration file. We use python because unlike JSON it allows comments. It has two maps in it: one from names to external flags, and one from names to internal flags. generate_copts.py imports the maps and loops through them to write GENERATED_copts.bzl and GENERATED_AbseilCopts.cmake AbseilConfigureCopts.cmake and configure_copts.bzl import their respective copts args and set the platform-appropriate copts into ABSL_DEFAULT_COPTS, ABSL_TEST_COPTS, ABSL_EXCEPTIONS_FLAG, and ABSL_EXCEPTIONS_LINKOPTS For Bazel, each BUILD file load()s configure_copts.bzl For CMake, AbseilHelpers.cmake include()s AbseilConfigureCopts.cmake to get the final copts and both inserts them as needed into legacy target rules and also makes them available to the rest of our CMakeLists.txt file. We may instead want to include() AbseilConfigureCopts.cmake directly into each CMakeLists.txt file for consistency, but I'm not sure what the deal is with cmake and include guards, or if they are even needed. That's also not as idiomatic -- CMake tends to use directory scope where globals set at a higher level CMakeLists.txt file are used in the subdirectory CMakeLists.txt files. PiperOrigin-RevId: 224039419 -- f7402f6bb65037e668a7355f0a003f5c05a3b6a7 by Abseil Team <absl-team@google.com>: Import of CCTZ from GitHub. PiperOrigin-RevId: 224036622 GitOrigin-RevId: 22fa219d17b2281c0695642830c4300711bd65ea Change-Id: I6b505360539ff2aef8aa30c51a5f7d55db1c75cf
2018-12-05 21:37:41 +01:00
n = (std::min)(n, length_ - pos);
2017-09-19 22:54:40 +02:00
return string_view(ptr_ + pos, n);
}
// string_view::compare()
//
// Performs a lexicographical comparison between the `string_view` and
// another `absl::string_view`, returning -1 if `this` is less than, 0 if
2017-09-19 22:54:40 +02:00
// `this` is equal to, and 1 if `this` is greater than the passed std::string
// view. Note that in the case of data equality, a further comparison is made
// on the respective sizes of the two `string_view`s to determine which is
// smaller, equal, or greater.
int compare(string_view x) const noexcept {
Export of internal Abseil changes. -- 22fa219d17b2281c0695642830c4300711bd65ea by CJ Johnson <johnsoncj@google.com>: Rearrange the private method declarations in InlinedVector PiperOrigin-RevId: 224202447 -- eed3c9f488f23b521bee41d3683eb6cc22517ded by Derek Mauro <dmauro@google.com>: Fix leak_check target (it was always a no-op when LSAN isn't available). Fixes https://github.com/abseil/abseil-cpp/issues/232 PiperOrigin-RevId: 224201634 -- fc08039e175204b14a9561f618fcfc0234586801 by Greg Falcon <gfalcon@google.com>: Add parens around more invocations of min() and max() missed in my prior CL. PiperOrigin-RevId: 224162430 -- 0ec5476a8293c7796cd84928a1a558b14f14f222 by Abseil Team <absl-team@google.com>: Update absl/numeric/CMakeLists.txt to use new functions i.e. absl_cc_(library|test) PiperOrigin-RevId: 224139165 -- 2b46aa6fabb20c589661f8bbc84030ecf39ce394 by Abseil Team <absl-team@google.com>: Update absl/meta/CMakeLists.txt to use new functions i.e. absl_cc_(library|test) PiperOrigin-RevId: 224117258 -- 6c951c798f8c6903bd8793a8a4b5f69244be8aa9 by Abseil Team <absl-team@google.com>: Fix 2 Unused C++ BUILD Dependencies PiperOrigin-RevId: 224070093 -- 0ee7bd191708708f91fc5209c197fd93f6e4a8b3 by Greg Falcon <gfalcon@google.com>: Inside Abseil headers, wrap most invocations of methods and functions named `min` and `max` in parentheses, for better interoperability with Windows toolchains. CCTZ fixes will appear in a follow-up CL. PiperOrigin-RevId: 224051960 -- f562f56577b84a8bc07e5873775c01d068531bca by Jon Cohen <cohenjon@google.com>: Generate Abseil compile options. The single source of truth is now absl/copts/copts.py The way this works goes something like this: copts.py acts as the configuration file. We use python because unlike JSON it allows comments. It has two maps in it: one from names to external flags, and one from names to internal flags. generate_copts.py imports the maps and loops through them to write GENERATED_copts.bzl and GENERATED_AbseilCopts.cmake AbseilConfigureCopts.cmake and configure_copts.bzl import their respective copts args and set the platform-appropriate copts into ABSL_DEFAULT_COPTS, ABSL_TEST_COPTS, ABSL_EXCEPTIONS_FLAG, and ABSL_EXCEPTIONS_LINKOPTS For Bazel, each BUILD file load()s configure_copts.bzl For CMake, AbseilHelpers.cmake include()s AbseilConfigureCopts.cmake to get the final copts and both inserts them as needed into legacy target rules and also makes them available to the rest of our CMakeLists.txt file. We may instead want to include() AbseilConfigureCopts.cmake directly into each CMakeLists.txt file for consistency, but I'm not sure what the deal is with cmake and include guards, or if they are even needed. That's also not as idiomatic -- CMake tends to use directory scope where globals set at a higher level CMakeLists.txt file are used in the subdirectory CMakeLists.txt files. PiperOrigin-RevId: 224039419 -- f7402f6bb65037e668a7355f0a003f5c05a3b6a7 by Abseil Team <absl-team@google.com>: Import of CCTZ from GitHub. PiperOrigin-RevId: 224036622 GitOrigin-RevId: 22fa219d17b2281c0695642830c4300711bd65ea Change-Id: I6b505360539ff2aef8aa30c51a5f7d55db1c75cf
2018-12-05 21:37:41 +01:00
auto min_length = (std::min)(length_, x.length_);
2017-09-19 22:54:40 +02:00
if (min_length > 0) {
int r = memcmp(ptr_, x.ptr_, min_length);
if (r < 0) return -1;
if (r > 0) return 1;
}
if (length_ < x.length_) return -1;
if (length_ > x.length_) return 1;
return 0;
}
// Overload of `string_view::compare()` for comparing a substring of the
// 'string_view` and another `absl::string_view`.
int compare(size_type pos1, size_type count1, string_view v) const {
return substr(pos1, count1).compare(v);
}
// Overload of `string_view::compare()` for comparing a substring of the
// `string_view` and a substring of another `absl::string_view`.
int compare(size_type pos1, size_type count1, string_view v, size_type pos2,
size_type count2) const {
return substr(pos1, count1).compare(v.substr(pos2, count2));
}
// Overload of `string_view::compare()` for comparing a `string_view` and a
// a different C-style std::string `s`.
int compare(const char* s) const { return compare(string_view(s)); }
// Overload of `string_view::compare()` for comparing a substring of the
// `string_view` and a different std::string C-style std::string `s`.
int compare(size_type pos1, size_type count1, const char* s) const {
return substr(pos1, count1).compare(string_view(s));
}
// Overload of `string_view::compare()` for comparing a substring of the
// `string_view` and a substring of a different C-style std::string `s`.
int compare(size_type pos1, size_type count1, const char* s,
size_type count2) const {
return substr(pos1, count1).compare(string_view(s, count2));
}
// Find Utilities
// string_view::find()
//
// Finds the first occurrence of the substring `s` within the `string_view`,
// returning the position of the first character's match, or `npos` if no
// match was found.
size_type find(string_view s, size_type pos = 0) const noexcept;
// Overload of `string_view::find()` for finding the given character `c`
// within the `string_view`.
size_type find(char c, size_type pos = 0) const noexcept;
// string_view::rfind()
//
// Finds the last occurrence of a substring `s` within the `string_view`,
// returning the position of the first character's match, or `npos` if no
// match was found.
size_type rfind(string_view s, size_type pos = npos) const
noexcept;
// Overload of `string_view::rfind()` for finding the last given character `c`
2017-09-19 22:54:40 +02:00
// within the `string_view`.
size_type rfind(char c, size_type pos = npos) const noexcept;
// string_view::find_first_of()
//
// Finds the first occurrence of any of the characters in `s` within the
// `string_view`, returning the start position of the match, or `npos` if no
// match was found.
size_type find_first_of(string_view s, size_type pos = 0) const
noexcept;
// Overload of `string_view::find_first_of()` for finding a character `c`
// within the `string_view`.
size_type find_first_of(char c, size_type pos = 0) const
noexcept {
return find(c, pos);
}
// string_view::find_last_of()
//
// Finds the last occurrence of any of the characters in `s` within the
// `string_view`, returning the start position of the match, or `npos` if no
// match was found.
size_type find_last_of(string_view s, size_type pos = npos) const
noexcept;
// Overload of `string_view::find_last_of()` for finding a character `c`
// within the `string_view`.
size_type find_last_of(char c, size_type pos = npos) const
noexcept {
return rfind(c, pos);
}
// string_view::find_first_not_of()
//
// Finds the first occurrence of any of the characters not in `s` within the
// `string_view`, returning the start position of the first non-match, or
// `npos` if no non-match was found.
size_type find_first_not_of(string_view s, size_type pos = 0) const noexcept;
// Overload of `string_view::find_first_not_of()` for finding a character
// that is not `c` within the `string_view`.
size_type find_first_not_of(char c, size_type pos = 0) const noexcept;
// string_view::find_last_not_of()
//
// Finds the last occurrence of any of the characters not in `s` within the
// `string_view`, returning the start position of the last non-match, or
// `npos` if no non-match was found.
size_type find_last_not_of(string_view s,
size_type pos = npos) const noexcept;
// Overload of `string_view::find_last_not_of()` for finding a character
// that is not `c` within the `string_view`.
size_type find_last_not_of(char c, size_type pos = npos) const
noexcept;
private:
static constexpr size_type kMaxSize =
Export of internal Abseil changes. -- ba4dd47492748bd630462eb68b7959037fc6a11a by Abseil Team <absl-team@google.com>: Work around nvcc 9.0 compiler bug for open-source Tensorflow build. With the current implementation, when I (unintentionally and transitively) include absl/types/optional.h in a CUDA compilation unit, I get the following nvcc error message: INFO: From Compiling tensorflow/core/kernels/crop_and_resize_op_gpu.cu.cc: external/com_google_absl/absl/types/optional.h: In member function 'void absl::optional_internal::optional_data_dtor_base<T, <anonymous> >::destruct()': external/com_google_absl/absl/types/optional.h:185:50: error: '__T0' was not declared in this scope data_.~T(); I've also seen similar compilation failures online, for flat_hash_map: https://devtalk.nvidia.com/default/topic/1042599/nvcc-preprocessor-bug-causes-compilation-failure/ The bug is always around unnamed template parameters. Therefore, the workaround is to make them named. PiperOrigin-RevId: 219208288 -- dad2f40cb2e8d5017660985ef6fb57f3c3cdcc80 by CJ Johnson <johnsoncj@google.com>: Adds internal macros for catching and throwing unknown exception types PiperOrigin-RevId: 219207362 -- 0a9840328d2d86e8420b853435fdbf1f7a19d931 by Abseil Team <absl-team@google.com>: Fix typo in mutex.h comments. PiperOrigin-RevId: 219199397 -- 0d576dc7597564210bfdf91518075064756f0bf4 by Matt Calabrese <calabrese@google.com>: Internal change. PiperOrigin-RevId: 219185475 -- 66be156095571959fb19a76da8ad0b53ec37658e by Abseil Team <absl-team@google.com>: Fix alignment conformance for VS 2017 >= 15.8 (fix #193) PiperOrigin-RevId: 219129894 -- a6e1825a12587945f8194677ccfdcaba6f7aad1d by Abseil Team <absl-team@google.com>: Reapply PR #173 PiperOrigin-RevId: 219129361 -- cf72ade4881b25acc6ccaea468f69793a0fdce32 by Abseil Team <absl-team@google.com>: Update .gitignore PiperOrigin-RevId: 219127495 -- 0537490c6348a2cb489abe15638928ac5aa6982a by Jon Cohen <cohenjon@google.com>: Small refactor and reformat of error messages from the exception safety test framework. PiperOrigin-RevId: 218927773 -- 4c556ca45fa25698ad12002a00c713aeceefab73 by CJ Johnson <johnsoncj@google.com>: Updates the inlined vector swap tests to check for number of moves that took place if available PiperOrigin-RevId: 218900777 -- dcbfda0021a1e6dfa9586986b1269c06ec394053 by Mark Barolak <mbar@google.com>: Add parens around calls to std::numeric_limits<>::min and std::numeric_limits<>::max to prevent compilation errors on Windows platforms where min and max are defined as macros. PiperOrigin-RevId: 218888700 GitOrigin-RevId: ba4dd47492748bd630462eb68b7959037fc6a11a Change-Id: I0e393958eb8cb501b85f6114979f6d4d86ed996c
2018-10-29 23:53:34 +01:00
(std::numeric_limits<difference_type>::max)();
2017-09-19 22:54:40 +02:00
static constexpr size_type CheckLengthInternal(size_type len) {
return ABSL_ASSERT(len <= kMaxSize), len;
}
const char* ptr_;
size_type length_;
};
// This large function is defined inline so that in a fairly common case where
// one of the arguments is a literal, the compiler can elide a lot of the
// following comparisons.
inline bool operator==(string_view x, string_view y) noexcept {
auto len = x.size();
if (len != y.size()) {
return false;
}
return x.data() == y.data() || len <= 0 ||
memcmp(x.data(), y.data(), len) == 0;
}
inline bool operator!=(string_view x, string_view y) noexcept {
return !(x == y);
}
inline bool operator<(string_view x, string_view y) noexcept {
Export of internal Abseil changes. -- 22fa219d17b2281c0695642830c4300711bd65ea by CJ Johnson <johnsoncj@google.com>: Rearrange the private method declarations in InlinedVector PiperOrigin-RevId: 224202447 -- eed3c9f488f23b521bee41d3683eb6cc22517ded by Derek Mauro <dmauro@google.com>: Fix leak_check target (it was always a no-op when LSAN isn't available). Fixes https://github.com/abseil/abseil-cpp/issues/232 PiperOrigin-RevId: 224201634 -- fc08039e175204b14a9561f618fcfc0234586801 by Greg Falcon <gfalcon@google.com>: Add parens around more invocations of min() and max() missed in my prior CL. PiperOrigin-RevId: 224162430 -- 0ec5476a8293c7796cd84928a1a558b14f14f222 by Abseil Team <absl-team@google.com>: Update absl/numeric/CMakeLists.txt to use new functions i.e. absl_cc_(library|test) PiperOrigin-RevId: 224139165 -- 2b46aa6fabb20c589661f8bbc84030ecf39ce394 by Abseil Team <absl-team@google.com>: Update absl/meta/CMakeLists.txt to use new functions i.e. absl_cc_(library|test) PiperOrigin-RevId: 224117258 -- 6c951c798f8c6903bd8793a8a4b5f69244be8aa9 by Abseil Team <absl-team@google.com>: Fix 2 Unused C++ BUILD Dependencies PiperOrigin-RevId: 224070093 -- 0ee7bd191708708f91fc5209c197fd93f6e4a8b3 by Greg Falcon <gfalcon@google.com>: Inside Abseil headers, wrap most invocations of methods and functions named `min` and `max` in parentheses, for better interoperability with Windows toolchains. CCTZ fixes will appear in a follow-up CL. PiperOrigin-RevId: 224051960 -- f562f56577b84a8bc07e5873775c01d068531bca by Jon Cohen <cohenjon@google.com>: Generate Abseil compile options. The single source of truth is now absl/copts/copts.py The way this works goes something like this: copts.py acts as the configuration file. We use python because unlike JSON it allows comments. It has two maps in it: one from names to external flags, and one from names to internal flags. generate_copts.py imports the maps and loops through them to write GENERATED_copts.bzl and GENERATED_AbseilCopts.cmake AbseilConfigureCopts.cmake and configure_copts.bzl import their respective copts args and set the platform-appropriate copts into ABSL_DEFAULT_COPTS, ABSL_TEST_COPTS, ABSL_EXCEPTIONS_FLAG, and ABSL_EXCEPTIONS_LINKOPTS For Bazel, each BUILD file load()s configure_copts.bzl For CMake, AbseilHelpers.cmake include()s AbseilConfigureCopts.cmake to get the final copts and both inserts them as needed into legacy target rules and also makes them available to the rest of our CMakeLists.txt file. We may instead want to include() AbseilConfigureCopts.cmake directly into each CMakeLists.txt file for consistency, but I'm not sure what the deal is with cmake and include guards, or if they are even needed. That's also not as idiomatic -- CMake tends to use directory scope where globals set at a higher level CMakeLists.txt file are used in the subdirectory CMakeLists.txt files. PiperOrigin-RevId: 224039419 -- f7402f6bb65037e668a7355f0a003f5c05a3b6a7 by Abseil Team <absl-team@google.com>: Import of CCTZ from GitHub. PiperOrigin-RevId: 224036622 GitOrigin-RevId: 22fa219d17b2281c0695642830c4300711bd65ea Change-Id: I6b505360539ff2aef8aa30c51a5f7d55db1c75cf
2018-12-05 21:37:41 +01:00
auto min_size = (std::min)(x.size(), y.size());
2017-09-19 22:54:40 +02:00
const int r = min_size == 0 ? 0 : memcmp(x.data(), y.data(), min_size);
return (r < 0) || (r == 0 && x.size() < y.size());
}
inline bool operator>(string_view x, string_view y) noexcept { return y < x; }
inline bool operator<=(string_view x, string_view y) noexcept {
return !(y < x);
}
inline bool operator>=(string_view x, string_view y) noexcept {
return !(x < y);
}
// IO Insertion Operator
std::ostream& operator<<(std::ostream& o, string_view piece);
} // namespace absl
#endif // ABSL_HAVE_STD_STRING_VIEW
namespace absl {
// ClippedSubstr()
//
// Like `s.substr(pos, n)`, but clips `pos` to an upper bound of `s.size()`.
Changes imported from Abseil "staging" branch: - b527a3e4b36b644ac424e3c525b1cd393f6f6c40 Fix some typos in the usage examples by Jorg Brown <jorg@google.com> - 82be4a9adf3bb0ddafc0d46274969c99afffe870 Fix typo in optional.h comment. by Abseil Team <absl-team@google.com> - d6ee63bf8fc51fba074c23b33cebc28c808d7f07 Remove internal-only identifiers from code. by Daniel Katz <katzdm@google.com> - f9c3ad2f0d73f53b21603638af8b4bed636e79f4 Use easier understandable names for absl::StartsWith and ... by Abseil Team <absl-team@google.com> - 7c16c14fefee89c927b8789d6043c4691bcffc9b Add -Wno-missing-prototypes back to the LLVM copts. by Derek Mauro <dmauro@google.com> - 2f4b7d2e50c7023240242f1e15db60ccd7e8768d IWYU | absl/strings by Juemin Yang <jueminyang@google.com> - a99cbcc1daa34a2d6a2bb26de275e05173cc77e9 IWYU | absl/type by Juemin Yang <jueminyang@google.com> - 12e1146d0fc76c071d7e0ebaabb62f0a984fae66 Use LLVM_FLAGS and LLVM_TEST_FLAGS when --compiler=llvm. by Derek Mauro <dmauro@google.com> - cd6bea616abda558d0bace5bd77455662a233688 IWYU | absl/debugging by Juemin Yang <jueminyang@google.com> - d9a7382e59d46a8581b6b7a31cd5a48bb89326e9 IWYU | absl/synchronization by Juemin Yang <jueminyang@google.com> - 07ec7d6d5a4a666f4183c5d0ed9c342baa7b24bc IWYU | absl/numeric by Juemin Yang <jueminyang@google.com> - 12bfe40051f4270f8707e191af5652f83f2f750c Remove the RoundTrip{Float,Double}ToBuffer routines from ... by Jorg Brown <jorg@google.com> - eeb4fd67c9d97f66cb9475c3c5e51ab132f1c810 Adds conversion functions for converting between absl/tim... by Greg Miller <jgm@google.com> - 59a2108d05d4ea85dc5cc11e49b2cd2335d4295a Change Substitute to use %.6g formatting rather than 15/1... by Jorg Brown <jorg@google.com> - 394becb48e0fcd161642cdaac5120d32567e0ef8 IWYU | absl/meta by Juemin Yang <jueminyang@google.com> - 1e5da6e8da336699b2469dcf6dda025b9b0ec4c9 Rewrite atomic_hook.h to not use std::atomic<T*> under Wi... by Greg Falcon <gfalcon@google.com> GitOrigin-RevId: b527a3e4b36b644ac424e3c525b1cd393f6f6c40 Change-Id: I14e331d91c956ef045ac7927091a9f179716de0c
2017-09-24 17:20:48 +02:00
// Provided because std::string_view::substr throws if `pos > size()`
2017-09-19 22:54:40 +02:00
inline string_view ClippedSubstr(string_view s, size_t pos,
size_t n = string_view::npos) {
Export of internal Abseil changes. -- 22fa219d17b2281c0695642830c4300711bd65ea by CJ Johnson <johnsoncj@google.com>: Rearrange the private method declarations in InlinedVector PiperOrigin-RevId: 224202447 -- eed3c9f488f23b521bee41d3683eb6cc22517ded by Derek Mauro <dmauro@google.com>: Fix leak_check target (it was always a no-op when LSAN isn't available). Fixes https://github.com/abseil/abseil-cpp/issues/232 PiperOrigin-RevId: 224201634 -- fc08039e175204b14a9561f618fcfc0234586801 by Greg Falcon <gfalcon@google.com>: Add parens around more invocations of min() and max() missed in my prior CL. PiperOrigin-RevId: 224162430 -- 0ec5476a8293c7796cd84928a1a558b14f14f222 by Abseil Team <absl-team@google.com>: Update absl/numeric/CMakeLists.txt to use new functions i.e. absl_cc_(library|test) PiperOrigin-RevId: 224139165 -- 2b46aa6fabb20c589661f8bbc84030ecf39ce394 by Abseil Team <absl-team@google.com>: Update absl/meta/CMakeLists.txt to use new functions i.e. absl_cc_(library|test) PiperOrigin-RevId: 224117258 -- 6c951c798f8c6903bd8793a8a4b5f69244be8aa9 by Abseil Team <absl-team@google.com>: Fix 2 Unused C++ BUILD Dependencies PiperOrigin-RevId: 224070093 -- 0ee7bd191708708f91fc5209c197fd93f6e4a8b3 by Greg Falcon <gfalcon@google.com>: Inside Abseil headers, wrap most invocations of methods and functions named `min` and `max` in parentheses, for better interoperability with Windows toolchains. CCTZ fixes will appear in a follow-up CL. PiperOrigin-RevId: 224051960 -- f562f56577b84a8bc07e5873775c01d068531bca by Jon Cohen <cohenjon@google.com>: Generate Abseil compile options. The single source of truth is now absl/copts/copts.py The way this works goes something like this: copts.py acts as the configuration file. We use python because unlike JSON it allows comments. It has two maps in it: one from names to external flags, and one from names to internal flags. generate_copts.py imports the maps and loops through them to write GENERATED_copts.bzl and GENERATED_AbseilCopts.cmake AbseilConfigureCopts.cmake and configure_copts.bzl import their respective copts args and set the platform-appropriate copts into ABSL_DEFAULT_COPTS, ABSL_TEST_COPTS, ABSL_EXCEPTIONS_FLAG, and ABSL_EXCEPTIONS_LINKOPTS For Bazel, each BUILD file load()s configure_copts.bzl For CMake, AbseilHelpers.cmake include()s AbseilConfigureCopts.cmake to get the final copts and both inserts them as needed into legacy target rules and also makes them available to the rest of our CMakeLists.txt file. We may instead want to include() AbseilConfigureCopts.cmake directly into each CMakeLists.txt file for consistency, but I'm not sure what the deal is with cmake and include guards, or if they are even needed. That's also not as idiomatic -- CMake tends to use directory scope where globals set at a higher level CMakeLists.txt file are used in the subdirectory CMakeLists.txt files. PiperOrigin-RevId: 224039419 -- f7402f6bb65037e668a7355f0a003f5c05a3b6a7 by Abseil Team <absl-team@google.com>: Import of CCTZ from GitHub. PiperOrigin-RevId: 224036622 GitOrigin-RevId: 22fa219d17b2281c0695642830c4300711bd65ea Change-Id: I6b505360539ff2aef8aa30c51a5f7d55db1c75cf
2018-12-05 21:37:41 +01:00
pos = (std::min)(pos, static_cast<size_t>(s.size()));
2017-09-19 22:54:40 +02:00
return s.substr(pos, n);
}
// NullSafeStringView()
//
// Creates an `absl::string_view` from a pointer `p` even if it's null-valued.
// This function should be used where an `absl::string_view` can be created from
// a possibly-null pointer.
inline string_view NullSafeStringView(const char* p) {
return p ? string_view(p) : string_view();
}
} // namespace absl
#endif // ABSL_STRINGS_STRING_VIEW_H_