Bifrost SDK
Bifrost SDK documentation
StringView.h
Go to the documentation of this file.
1//-
2// =============================================================================
3// Copyright 2025 Autodesk, Inc. All rights reserved.
4//
5// Use of this software is subject to the terms of the Autodesk license
6// agreement provided at the time of installation or download, or which
7// otherwise accompanies this software in either electronic or hard copy form.
8// =============================================================================
9
10//+
11
17
18#ifndef AMINO_CORE_STRING_VIEW_H
19#define AMINO_CORE_STRING_VIEW_H
20
21#include "internal/ConfigMacros.h"
22
23#include <cassert>
24#include <cstddef>
25#include <string_view>
26#include <utility>
27#ifndef AMINO_REMOVE_TRANSITIVE_HEADERS
28#include <string>
29#endif
30
31//==============================================================================
32// NAMESPACE Amino
33//==============================================================================
34
35namespace Amino {
36
37//==============================================================================
38// CLASS StringView
39//==============================================================================
40
43private:
44 template <typename R>
45 static inline constexpr bool is_raw_cstring =
46 (std::is_same_v<std::decay_t<R>, char const*> ||
47 std::is_same_v<std::decay_t<R>, char*>)&& //
48 !std::is_array_v<std::remove_reference_t<R>>;
49
50 template <typename R>
51 static inline constexpr bool is_sview_convertible_v =
52 !is_raw_cstring<R> && std::is_convertible_v<R, std::string_view>;
53
54 template <typename R>
55 static inline constexpr bool is_sview_constructible_v =
56 !is_sview_convertible_v<R> &&
57 std::is_constructible_v<std::string_view, R>;
58
59 // Allow StringView to be implicitly constructed from any type that is
60 // implicitly convertible to std::string_view (except char const* is made
61 // explicit).
62 template <typename R>
63 using enable_if_implicitly_constructible =
64 std::enable_if_t<is_sview_convertible_v<R>>;
65
66 template <typename R>
67 using enable_if_explicitly_constructible =
68 std::enable_if_t<is_sview_constructible_v<R>>;
69
70public:
71 /*----- member functions -----*/
72
74 constexpr StringView() noexcept = default;
75
77 // NOLINTNEXTLINE(google-explicit-constructor)
78 StringView(std::nullptr_t) = delete;
79
81 constexpr StringView(char const* data, size_t size) noexcept
82 : m_data(data), m_size(size) {}
83
86 template <typename R, enable_if_explicitly_constructible<R>* = nullptr>
87 AMINO_INTERNAL_FORCEINLINE constexpr explicit StringView(R&& r)
88 : StringView(Private{}, std::string_view{std::forward<R>(r)}) {}
89
96 template <typename R, enable_if_implicitly_constructible<R>* = nullptr>
97 // NOLINTNEXTLINE(google-explicit-constructor)
98 AMINO_INTERNAL_FORCEINLINE constexpr /*implicit*/ StringView(R&& r)
99 : StringView(Private{}, std::forward<R>(r)) {}
100
102 // NOLINTNEXTLINE(google-explicit-constructor)
103 AMINO_INTERNAL_FORCEINLINE constexpr operator std::string_view()
104 const noexcept {
105 return {data(), size()}; // LCOV_EXCL_BR_LINE
106 }
107
109 constexpr char const* data() const noexcept { return m_data; }
110
114 constexpr size_t size() const noexcept { return m_size; }
115
117 constexpr size_t length() const noexcept { return size(); }
118
120 constexpr bool empty() const noexcept { return m_size == 0; }
121
124 constexpr char const* cbegin() const noexcept { return m_data; }
125 constexpr char const* begin() const noexcept { return cbegin(); }
127
130 constexpr char const* cend() const noexcept { return m_data + m_size; }
131 constexpr char const* end() const noexcept { return cend(); }
133
136 constexpr char front() const noexcept { return deref(0); }
137
140 constexpr char back() const noexcept { return deref(m_size - 1); }
141
144 constexpr char operator[](size_t idx) const noexcept { return deref(idx); }
145
148 constexpr bool operator==(StringView o) const noexcept {
149 return size() == o.size() && compare(o) == 0;
150 }
151 constexpr bool operator<(StringView o) const noexcept {
152 return compare(o) < 0;
153 }
154 constexpr bool operator>(StringView o) const noexcept {
155 return compare(o) > 0;
156 }
157 constexpr bool operator!=(StringView o) const noexcept {
158 return !(*this == o);
159 }
160 constexpr bool operator<=(StringView o) const noexcept {
161 return !(*this > o);
162 }
163 constexpr bool operator>=(StringView o) const noexcept {
164 return !(*this < o);
165 }
167
168private:
169 struct Private {};
170
171 /*----- member functions -----*/
172
174 AMINO_INTERNAL_FORCEINLINE constexpr StringView(
175 Private, std::string_view s) noexcept
176 : StringView(s.data(), s.size()) {}
177
179 constexpr char deref(size_t idx) const noexcept {
180 assert(m_data);
181 assert(!empty());
182 assert(idx < m_size);
183 return m_data[idx];
184 }
185
194 constexpr int compare(StringView o) const noexcept {
195 size_t size = m_size < o.m_size ? m_size : o.m_size;
196 for (size_t i = 0; i < size; ++i) {
197 if (m_data[i] < o.m_data[i]) return -1;
198 if (m_data[i] > o.m_data[i]) return +1;
199 }
200 if (m_size < o.m_size) return -1;
201 if (m_size > o.m_size) return +1;
202 return 0;
203 }
204
205 /*----- data members -----*/
206
208 char const* m_data = nullptr;
209
211 size_t m_size = 0;
212};
213
214//==============================================================================
215// CLASS StringLiteral
216//==============================================================================
217
218class StringLiteral;
219
220namespace StringViewLiterals {
221constexpr StringLiteral operator""_asv(char const* data, size_t size);
222} // namespace StringViewLiterals
223
224class StringLiteral final : public StringView {
225private:
226 friend constexpr StringLiteral StringViewLiterals::operator""_asv(
227 char const*, size_t);
228 constexpr StringLiteral(char const* name, size_t size)
229 : StringView{name, size} {}
230};
231
232//==============================================================================
233// NAMESPACE StringViewLiterals
234//==============================================================================
235
236namespace StringViewLiterals {
237
245constexpr StringLiteral operator""_asv(char const* data, size_t size) {
246 return StringLiteral{data, size};
247}
248
249} // namespace StringViewLiterals
250
251} // namespace Amino
252
253#endif
Definition: HostData.h:33
Definition: Ptr.h:2080
String view class (similar to std::string_view).
Definition: StringView.h:42
constexpr bool operator<(StringView o) const noexcept
Comparison operators.
Definition: StringView.h:151
constexpr bool operator>=(StringView o) const noexcept
Comparison operators.
Definition: StringView.h:163
constexpr char const * data() const noexcept
Get the string view data.
Definition: StringView.h:109
constexpr bool empty() const noexcept
Returns whether the string view is empty or not.
Definition: StringView.h:120
constexpr bool operator<=(StringView o) const noexcept
Comparison operators.
Definition: StringView.h:160
constexpr char const * begin() const noexcept
Get an iterator to the beginning of the string view.
Definition: StringView.h:125
constexpr bool operator>(StringView o) const noexcept
Comparison operators.
Definition: StringView.h:154
constexpr char const * end() const noexcept
Get an iterator to the end of the string view.
Definition: StringView.h:131
constexpr char operator[](size_t idx) const noexcept
Get the character at the given index in the string view.
Definition: StringView.h:144
constexpr char const * cbegin() const noexcept
Get an iterator to the beginning of the string view.
Definition: StringView.h:124
AMINO_INTERNAL_FORCEINLINE constexpr StringView(R &&r)
StringView can be explicitly constructed from types from which std::string_view can explicitly be con...
Definition: StringView.h:87
constexpr bool operator!=(StringView o) const noexcept
Comparison operators.
Definition: StringView.h:157
constexpr char front() const noexcept
Get the first character in the string view.
Definition: StringView.h:136
constexpr size_t length() const noexcept
Get the size of the string view.
Definition: StringView.h:117
constexpr bool operator==(StringView o) const noexcept
Comparison operators.
Definition: StringView.h:148
constexpr size_t size() const noexcept
Get the size of the string view.
Definition: StringView.h:114
constexpr char const * cend() const noexcept
Get an iterator to the end of the string view.
Definition: StringView.h:130
constexpr StringView() noexcept=default
Default constructor (empty string view)
constexpr char back() const noexcept
Get the last character in the string view.
Definition: StringView.h:140