/src/sentencepiece/third_party/absl/strings/string_view.h
Line | Count | Source (jump to first uncovered line) |
1 | | // |
2 | | // Copyright 2017 The Abseil Authors. |
3 | | // |
4 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
5 | | // you may not use this file except in compliance with the License. |
6 | | // You may obtain a copy of the License at |
7 | | // |
8 | | // http://www.apache.org/licenses/LICENSE-2.0 |
9 | | // |
10 | | // Unless required by applicable law or agreed to in writing, software |
11 | | // distributed under the License is distributed on an "AS IS" BASIS, |
12 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | | // See the License for the specific language governing permissions and |
14 | | // limitations under the License. |
15 | | // |
16 | | // ----------------------------------------------------------------------------- |
17 | | // File: string_view.h |
18 | | // ----------------------------------------------------------------------------- |
19 | | // |
20 | | // This file contains the definition of the `absl::string_view` class. A |
21 | | // `string_view` points to a contiguous span of characters, often part or all of |
22 | | // another `std::string`, double-quoted std::string literal, character array, or |
23 | | // even another `string_view`. |
24 | | // |
25 | | // This `absl::string_view` abstraction is designed to be a drop-in |
26 | | // replacement for the C++17 `std::string_view` abstraction. |
27 | | #ifndef ABSL_STRINGS_STRING_VIEW_H_ |
28 | | #define ABSL_STRINGS_STRING_VIEW_H_ |
29 | | |
30 | | #include <algorithm> |
31 | | #include <string_view> |
32 | | |
33 | | namespace absl { |
34 | | using std::string_view; |
35 | | |
36 | | // ClippedSubstr() |
37 | | // |
38 | | // Like `s.substr(pos, n)`, but clips `pos` to an upper bound of `s.size()`. |
39 | | // Provided because std::string_view::substr throws if `pos > size()` |
40 | | inline string_view ClippedSubstr(string_view s, size_t pos, |
41 | 0 | size_t n = string_view::npos) { |
42 | 0 | pos = std::min(pos, static_cast<size_t>(s.size())); |
43 | 0 | return s.substr(pos, n); |
44 | 0 | } |
45 | | |
46 | | // NullSafeStringView() |
47 | | // |
48 | | // Creates an `absl::string_view` from a pointer `p` even if it's null-valued. |
49 | | // This function should be used where an `absl::string_view` can be created from |
50 | | // a possibly-null pointer. |
51 | 0 | inline string_view NullSafeStringView(const char* p) { |
52 | 0 | return p ? string_view(p) : string_view(); |
53 | 0 | } |
54 | | |
55 | | } // namespace absl |
56 | | |
57 | | #endif // ABSL_STRINGS_STRING_VIEW_H_ |