NVIDIA DeepStream SDK API Reference

9.2 Release
9.2/includes/opentelemetry/nostd/string_view.h
Go to the documentation of this file.
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3 
4 #pragma once
5 
6 #if defined(OPENTELEMETRY_STL_VERSION)
7 # if OPENTELEMETRY_STL_VERSION >= 2017
8 # include "opentelemetry/std/string_view.h"
9 # define OPENTELEMETRY_HAVE_STD_STRING_VIEW
10 # endif
11 #endif
12 
13 #if !defined(OPENTELEMETRY_HAVE_STD_STRING_VIEW)
14 # include <algorithm>
15 # include <cstring>
16 # include <functional>
17 # include <ostream>
18 # include <stdexcept>
19 # include <string>
20 # include <type_traits>
21 # include <utility>
22 
23 # include "opentelemetry/version.h"
24 
26 namespace nostd
27 {
28 
29 using Traits = std::char_traits<char>;
30 
37 class string_view
38 {
39 public:
40  typedef std::size_t size_type;
41 
42  static constexpr size_type npos = static_cast<size_type>(-1);
43 
44  string_view() noexcept : length_(0), data_(nullptr) {}
45 
46  string_view(const char *str) noexcept : length_(std::strlen(str)), data_(str) {}
47 
48  string_view(const std::basic_string<char> &str OPENTELEMETRY_ATTRIBUTE_LIFETIME_BOUND) noexcept
49  : length_(str.length()), data_(str.c_str())
50  {}
51 
52  string_view(const char *str, size_type len) noexcept : length_(len), data_(str) {}
53 
54  explicit operator std::string() const { return {data_, length_}; }
55 
56  const char *data() const noexcept { return data_; }
57 
58  bool empty() const noexcept { return length_ == 0; }
59 
60  size_type length() const noexcept { return length_; }
61 
62  size_type size() const noexcept { return length_; }
63 
64  const char *begin() const noexcept { return data(); }
65 
66  const char *end() const noexcept { return data() + length(); }
67 
68  const char &operator[](size_type i) { return *(data() + i); }
69 
71  {
72  if (pos > length_)
73  {
74 # if __EXCEPTIONS
75  throw std::out_of_range{"opentelemetry::nostd::string_view"};
76 # else
78 # endif
79  }
80  n = (std::min)(n, length_ - pos);
81  return string_view(data_ + pos, n);
82  }
83 
84  int compare(string_view v) const noexcept
85  {
86  size_type len = (std::min)(size(), v.size());
87  int result = Traits::compare(data(), v.data(), len);
88  if (result == 0)
89  result = size() == v.size() ? 0 : (size() < v.size() ? -1 : 1);
90  return result;
91  }
92 
93  int compare(size_type pos1, size_type count1, string_view v) const
94  {
95  return substr(pos1, count1).compare(v);
96  }
97 
98  int compare(size_type pos1,
99  size_type count1,
100  string_view v,
101  size_type pos2,
102  size_type count2) const
103  {
104  return substr(pos1, count1).compare(v.substr(pos2, count2));
105  }
106 
107  int compare(const char *s) const { return compare(string_view(s)); }
108 
109  int compare(size_type pos1, size_type count1, const char *s) const
110  {
111  return substr(pos1, count1).compare(string_view(s));
112  }
113 
114  int compare(size_type pos1, size_type count1, const char *s, size_type count2) const
115  {
116  return substr(pos1, count1).compare(string_view(s, count2));
117  }
118 
119  size_type find(char ch, size_type pos = 0) const noexcept
120  {
121  size_type res = npos;
122  if (pos < length())
123  {
124  auto found = Traits::find(data() + pos, length() - pos, ch);
125  if (found)
126  {
127  res = found - data();
128  }
129  }
130  return res;
131  }
132 
133  bool operator<(const string_view v) const noexcept { return compare(v) < 0; }
134 
135  bool operator>(const string_view v) const noexcept { return compare(v) > 0; }
136 
137 private:
138  // Note: uses the same binary layout as libstdc++'s std::string_view
139  // See
140  // https://github.com/gcc-mirror/gcc/blob/e0c554e4da7310df83bb1dcc7b8e6c4c9c5a2a4f/libstdc%2B%2B-v3/include/std/string_view#L466-L467
141  size_type length_;
142  const char *data_;
143 };
144 
145 inline bool operator==(string_view lhs, string_view rhs) noexcept
146 {
147  return lhs.length() == rhs.length() &&
148 # if defined(_MSC_VER)
149 # if _MSC_VER >= 1900 && _MSC_VER <= 1911
150  // Avoid SCL error in Visual Studio 2015, VS2017 update 1 to update 4
151  (std::memcmp(lhs.data(), rhs.data(), lhs.length()) == 0);
152 # else
153  std::equal(lhs.data(), lhs.data() + lhs.length(), rhs.data());
154 # endif
155 # else
156  std::equal(lhs.data(), lhs.data() + lhs.length(), rhs.data());
157 # endif
158 }
159 
160 inline bool operator==(string_view lhs, const std::string &rhs) noexcept
161 {
162  return lhs == string_view(rhs);
163 }
164 
165 inline bool operator==(const std::string &lhs, string_view rhs) noexcept
166 {
167  return string_view(lhs) == rhs;
168 }
169 
170 inline bool operator==(string_view lhs, const char *rhs) noexcept
171 {
172  return lhs == string_view(rhs);
173 }
174 
175 inline bool operator==(const char *lhs, string_view rhs) noexcept
176 {
177  return string_view(lhs) == rhs;
178 }
179 
180 inline bool operator!=(string_view lhs, string_view rhs) noexcept
181 {
182  return !(lhs == rhs);
183 }
184 
185 inline bool operator!=(string_view lhs, const std::string &rhs) noexcept
186 {
187  return !(lhs == rhs);
188 }
189 
190 inline bool operator!=(const std::string &lhs, string_view rhs) noexcept
191 {
192  return !(lhs == rhs);
193 }
194 
195 inline bool operator!=(string_view lhs, const char *rhs) noexcept
196 {
197  return !(lhs == rhs);
198 }
199 
200 inline bool operator!=(const char *lhs, string_view rhs) noexcept
201 {
202  return !(lhs == rhs);
203 }
204 
205 inline std::ostream &operator<<(std::ostream &os, string_view s)
206 {
207  return os.write(s.data(), static_cast<std::streamsize>(s.length()));
208 }
209 } // namespace nostd
211 
212 namespace std
213 {
214 template <>
216 {
218  {
219  // TODO: for C++17 that has native support for std::basic_string_view it would
220  // be more performance-efficient to provide a zero-copy hash.
221  auto s = std::string(k.data(), k.size());
222  return std::hash<std::string>{}(s);
223  }
224 };
225 } // namespace std
226 #endif /* OPENTELEMETRY_HAVE_STD_STRING_VIEW */
std::hash< OPENTELEMETRY_NAMESPACE::nostd::string_view >::operator()
std::size_t operator()(const OPENTELEMETRY_NAMESPACE::nostd::string_view &k) const
Definition: 9.2/includes/opentelemetry/nostd/string_view.h:217
pydeepstreamdoc::node::NodeDoc::find
constexpr const char * find
Definition: service-maker/sources/python/src/bindings/docstrings/nodedoc.h:38
nostd
Definition: includes/opentelemetry/nostd/function_ref.h:14
nostd::string_view::empty
bool empty() const noexcept
Definition: 9.2/includes/opentelemetry/nostd/string_view.h:58
nostd::string_view::compare
int compare(size_type pos1, size_type count1, const char *s, size_type count2) const
Definition: 9.2/includes/opentelemetry/nostd/string_view.h:114
nostd::string_view::operator>
bool operator>(const string_view v) const noexcept
Definition: 9.2/includes/opentelemetry/nostd/string_view.h:135
nostd::operator<<
std::ostream & operator<<(std::ostream &os, string_view s)
Definition: includes/opentelemetry/nostd/string_view.h:205
nostd::string_view::compare
int compare(size_type pos1, size_type count1, string_view v) const
Definition: 9.2/includes/opentelemetry/nostd/string_view.h:93
nostd::operator==
bool operator==(const shared_ptr< T1 > &lhs, const shared_ptr< T2 > &rhs) noexcept
Definition: includes/opentelemetry/nostd/shared_ptr.h:180
nostd::operator!=
bool operator!=(const shared_ptr< T1 > &lhs, const shared_ptr< T2 > &rhs) noexcept
Definition: includes/opentelemetry/nostd/shared_ptr.h:174
nostd::string_view::begin
const char * begin() const noexcept
Definition: 9.2/includes/opentelemetry/nostd/string_view.h:64
OPENTELEMETRY_BEGIN_NAMESPACE
#define OPENTELEMETRY_BEGIN_NAMESPACE
Definition: includes/opentelemetry/version.h:21
nostd::string_view::substr
string_view substr(size_type pos, size_type n=npos) const
Definition: 9.2/includes/opentelemetry/nostd/string_view.h:70
nostd::string_view::string_view
string_view(const char *str, size_type len) noexcept
Definition: 9.2/includes/opentelemetry/nostd/string_view.h:52
nostd::string_view::compare
int compare(string_view v) const noexcept
Definition: 9.2/includes/opentelemetry/nostd/string_view.h:84
nostd::string_view::operator[]
const char & operator[](size_type i)
Definition: 9.2/includes/opentelemetry/nostd/string_view.h:68
OPENTELEMETRY_ATTRIBUTE_LIFETIME_BOUND
#define OPENTELEMETRY_ATTRIBUTE_LIFETIME_BOUND
Definition: includes/opentelemetry/common/macros.h:443
OPENTELEMETRY_END_NAMESPACE
#define OPENTELEMETRY_END_NAMESPACE
Definition: includes/opentelemetry/version.h:24
nostd::string_view::size
size_type size() const noexcept
Definition: 9.2/includes/opentelemetry/nostd/string_view.h:62
OPENTELEMETRY_NAMESPACE
#define OPENTELEMETRY_NAMESPACE
Definition: includes/opentelemetry/version.h:27
nostd::string_view::size_type
std::size_t size_type
Definition: 9.2/includes/opentelemetry/nostd/string_view.h:40
nostd::string_view::operator<
bool operator<(const string_view v) const noexcept
Definition: 9.2/includes/opentelemetry/nostd/string_view.h:133
nostd::string_view::compare
int compare(size_type pos1, size_type count1, string_view v, size_type pos2, size_type count2) const
Definition: 9.2/includes/opentelemetry/nostd/string_view.h:98
nostd::string_view::string_view
string_view() noexcept
Definition: 9.2/includes/opentelemetry/nostd/string_view.h:44
nostd::string_view::end
const char * end() const noexcept
Definition: 9.2/includes/opentelemetry/nostd/string_view.h:66
nostd::string_view::length
size_type length() const noexcept
Definition: 9.2/includes/opentelemetry/nostd/string_view.h:60
pydeepstreamdoc::signalemitter::SourceManagerDoc::terminate
constexpr const char * terminate
Definition: service-maker/sources/python/src/bindings/docstrings/signalemitterdoc.h:47
nostd::string_view::compare
int compare(const char *s) const
Definition: 9.2/includes/opentelemetry/nostd/string_view.h:107
min
int min(int a, int b)
Compute the component-wise minimum.
Definition: sources/libs/nvmultiobjecttracker/include/modules/cuDCFv2/helper_math.h:70
std
Extensions to the C++ standard library.
Definition: includes/opentelemetry/nostd/string_view.h:212
nostd::string_view::string_view
string_view(const char *str) noexcept
Definition: 9.2/includes/opentelemetry/nostd/string_view.h:46
nostd::Traits
std::char_traits< char > Traits
Definition: includes/opentelemetry/nostd/string_view.h:29
nostd::string_view::data
const char * data() const noexcept
Definition: 9.2/includes/opentelemetry/nostd/string_view.h:56
nostd::string_view::find
size_type find(char ch, size_type pos=0) const noexcept
Definition: 9.2/includes/opentelemetry/nostd/string_view.h:119
nostd::string_view::compare
int compare(size_type pos1, size_type count1, const char *s) const
Definition: 9.2/includes/opentelemetry/nostd/string_view.h:109
nostd::string_view::npos
static constexpr size_type npos
Definition: includes/opentelemetry/nostd/string_view.h:42
nostd::string_view
std::string_view string_view
Definition: includes/opentelemetry/std/string_view.h:18
nostd::string_view::string_view
string_view(const std::basic_string< char > &str OPENTELEMETRY_ATTRIBUTE_LIFETIME_BOUND) noexcept
Definition: 9.2/includes/opentelemetry/nostd/string_view.h:48
nostd::string_view
Back port of std::string_view to work with pre-cpp-17 compilers.
Definition: includes/opentelemetry/nostd/string_view.h:37