2 * Copyright (c) 2023 Philippe Proulx <pproulx@efficios.com>
4 * SPDX-License-Identifier: MIT
7 #ifndef BABELTRACE_CPP_COMMON_BT2C_C_STRING_VIEW_HPP
8 #define BABELTRACE_CPP_COMMON_BT2C_C_STRING_VIEW_HPP
14 #include "common/assert.h"
15 #include "cpp-common/bt2s/string-view.hpp"
16 #include "cpp-common/vendor/fmt/format.h" /* IWYU pragma: keep */
18 #include "type-traits.hpp"
23 * A view on a constant null-terminated C string.
25 * Similar to `bt2s::string_view`, but len() and end() compute the
28 class CStringView final
32 * Builds an empty view (data() returns `nullptr`).
34 * Intentionally not explicit.
36 constexpr CStringView() noexcept = default;
39 * Builds a view of the C string `str` (may be `nullptr`).
41 * Intentionally not explicit.
43 constexpr CStringView(const char * const str) noexcept : _mStr {str}
48 * Builds a view of the string `str`.
50 * Intentionally not explicit.
52 CStringView(const std::string& str) noexcept : _mStr {str.c_str()}
57 * Makes this view view the C string `str` (may be `nullptr`).
59 * Intentionally not explicit.
61 CStringView& operator=(const char * const str) noexcept
68 * Viewed null-terminated C string (may be `nullptr`).
70 const char *data() const noexcept
78 operator const char *() const noexcept
86 const char *operator*() const noexcept
94 * data() must not return `nullptr`.
96 const char *begin() const noexcept
103 * Pointer to the null character of the viewed C string.
105 * data() must not return `nullptr`.
107 const char *end() const noexcept
109 BT_ASSERT_DBG(_mStr);
110 return _mStr + this->len();
114 * Length of the viewed C string, excluding the null character.
116 * data() must not return `nullptr`.
118 std::size_t len() const noexcept
120 BT_ASSERT_DBG(_mStr);
121 return std::strlen(_mStr);
125 * Returns an `std::string` instance containing a copy of the viewed
128 * data() must not return `nullptr`.
130 std::string str() const
132 BT_ASSERT_DBG(_mStr);
133 return std::string {_mStr};
139 operator std::string() const
145 * Returns a `bt2s::string_view` instance to view the contents,
146 * excluding the null character, of the viewed C string.
148 bt2s::string_view strView() const noexcept
151 return bt2s::string_view {this->begin(), this->len()};
158 * Alias of strView().
160 operator bt2s::string_view() const noexcept
162 return this->strView();
166 * Returns the character at index `i`.
168 * `i` must be less than what len() returns.
170 * data() must not return `nullptr`.
172 char operator[](const std::size_t i) const noexcept
174 BT_ASSERT_DBG(_mStr);
175 BT_ASSERT_DBG(i < this->len());
179 bool startsWith(const CStringView prefix) const noexcept
181 BT_ASSERT_DBG(_mStr);
182 BT_ASSERT_DBG(prefix);
183 return std::strncmp(_mStr, prefix, prefix.len()) == 0;
187 const char *_mStr = nullptr;
190 inline const char *format_as(const CStringView& str)
192 return str ? *str : "(null)";
197 template <typename StrT>
198 const char *asConstCharPtr(StrT&& val) noexcept
203 inline const char *asConstCharPtr(const char * const val) noexcept
208 template <typename StrT>
209 using ComparableWithCStringView =
210 IsOneOf<typename std::decay<StrT>::type, CStringView, std::string, const char *>;
212 } /* namespace internal */
215 * Returns true if `lhs` is equal to `rhs`.
217 * `LhsT` and `RhsT` may be any of:
223 * Both `lhs` and `rhs` must not have an underlying `nullptr` raw data.
226 typename LhsT, typename RhsT,
227 typename = typename std::enable_if<internal::ComparableWithCStringView<LhsT>::value>::type,
228 typename = typename std::enable_if<internal::ComparableWithCStringView<RhsT>::value>::type>
229 bool operator==(LhsT&& lhs, RhsT&& rhs) noexcept
231 const auto rawLhs = internal::asConstCharPtr(lhs);
232 const auto rawRhs = internal::asConstCharPtr(rhs);
234 BT_ASSERT_DBG(rawLhs);
235 BT_ASSERT_DBG(rawRhs);
236 return std::strcmp(rawLhs, rawRhs) == 0;
240 * Returns true if `lhs` is not equal to `rhs`.
242 * `LhsT` and `RhsT` may be any of:
248 * Both `lhs` and `rhs` must not have an underlying `nullptr` raw data.
251 typename LhsT, typename RhsT,
252 typename = typename std::enable_if<internal::ComparableWithCStringView<LhsT>::value>::type,
253 typename = typename std::enable_if<internal::ComparableWithCStringView<RhsT>::value>::type>
254 bool operator!=(LhsT&& lhs, RhsT&& rhs) noexcept
256 return !(std::forward<LhsT>(lhs) == std::forward<RhsT>(rhs));
259 } /* namespace bt2c */
262 * Appends `rhs` to `lhs`.
264 inline void operator+=(std::string& lhs, bt2c::CStringView rhs)
269 #endif /* BABELTRACE_CPP_COMMON_BT2C_C_STRING_VIEW_HPP */