Coverage Report

Created: 2026-09-14 06:45

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/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 <algorithm>
28
#include <string_view>
29
30
#include "absl/base/attributes.h"
31
#include "absl/base/config.h"
32
#include "absl/base/nullability.h"
33
34
namespace absl {
35
ABSL_NAMESPACE_BEGIN
36
37
using std::string_view;
38
39
// ClippedSubstr()
40
//
41
// Like `s.substr(pos, n)`, but clips `pos` to an upper bound of `s.size()`.
42
// Provided because std::string_view::substr throws if `pos > size()`
43
inline string_view ClippedSubstr(string_view s ABSL_ATTRIBUTE_LIFETIME_BOUND,
44
0
                                 size_t pos, size_t n = string_view::npos) {
45
0
  pos = (std::min)(pos, static_cast<size_t>(s.size()));
46
0
  return s.substr(pos, n);
47
0
}
48
49
// NullSafeStringView()
50
//
51
// Creates an `absl::string_view` from a pointer `p` even if it's null-valued.
52
// This function should be used where an `absl::string_view` can be created from
53
// a possibly-null pointer.
54
0
constexpr string_view NullSafeStringView(const char* absl_nullable p) {
55
0
  return p ? string_view(p) : string_view();
56
0
}
57
58
ABSL_NAMESPACE_END
59
}  // namespace absl
60
61
#endif  // ABSL_STRINGS_STRING_VIEW_H_