/src/abseil-cpp/absl/strings/string_view.h
Line | Count | Source |
1 | | // Copyright 2017 The Abseil Authors. |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | | // you may not use this file except in compliance with the License. |
5 | | // You may obtain a copy of the License at |
6 | | // |
7 | | // https://www.apache.org/licenses/LICENSE-2.0 |
8 | | // |
9 | | // Unless required by applicable law or agreed to in writing, software |
10 | | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | | // See the License for the specific language governing permissions and |
13 | | // limitations under the License. |
14 | | // |
15 | | // ----------------------------------------------------------------------------- |
16 | | // File: string_view.h |
17 | | // ----------------------------------------------------------------------------- |
18 | | // |
19 | | // Historical note: Abseil once provided an implementation of |
20 | | // `absl::string_view` as a polyfill for `std::string_view` prior to C++17. Now |
21 | | // that C++17 is required, `absl::string_view` is an alias for |
22 | | // `std::string_view` |
23 | | |
24 | | #ifndef ABSL_STRINGS_STRING_VIEW_H_ |
25 | | #define ABSL_STRINGS_STRING_VIEW_H_ |
26 | | |
27 | | #include <string_view> |
28 | | |
29 | | #include "absl/base/attributes.h" |
30 | | #include "absl/base/config.h" |
31 | | #include "absl/base/nullability.h" |
32 | | |
33 | | namespace absl { |
34 | | ABSL_NAMESPACE_BEGIN |
35 | | |
36 | | using std::string_view; |
37 | | |
38 | | // ClippedSubstr() |
39 | | // |
40 | | // Like `s.substr(pos, n)`, but clips `pos` to an upper bound of `s.size()`. |
41 | | // Provided because std::string_view::substr throws if `pos > size()` |
42 | | inline string_view ClippedSubstr(string_view s ABSL_ATTRIBUTE_LIFETIME_BOUND, |
43 | 0 | size_t pos, size_t n = string_view::npos) { |
44 | 0 | pos = (std::min)(pos, static_cast<size_t>(s.size())); |
45 | 0 | return s.substr(pos, n); |
46 | 0 | } |
47 | | |
48 | | // NullSafeStringView() |
49 | | // |
50 | | // Creates an `absl::string_view` from a pointer `p` even if it's null-valued. |
51 | | // This function should be used where an `absl::string_view` can be created from |
52 | | // a possibly-null pointer. |
53 | 0 | constexpr string_view NullSafeStringView(const char* absl_nullable p) { |
54 | 0 | return p ? string_view(p) : string_view(); |
55 | 0 | } |
56 | | |
57 | | ABSL_NAMESPACE_END |
58 | | } // namespace absl |
59 | | |
60 | | #endif // ABSL_STRINGS_STRING_VIEW_H_ |