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"
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());
180 const char *_mStr = nullptr;
183 static inline const char *format_as(const CStringView& str)
185 return str ? *str : "(null)";
190 template <typename StrT>
191 const char *asConstCharPtr(StrT&& val) noexcept
196 inline const char *asConstCharPtr(const char * const val) noexcept
201 template <typename StrT>
202 using ComparableWithCStringView =
203 IsOneOf<typename std::decay<StrT>::type, CStringView, std::string, const char *>;
205 } /* namespace internal */
208 * Returns true if `lhs` is equal to `rhs`.
210 * `LhsT` and `RhsT` may be any of:
216 * Both `lhs` and `rhs` must not have an underlying `nullptr` raw data.
219 typename LhsT, typename RhsT,
220 typename = typename std::enable_if<internal::ComparableWithCStringView<LhsT>::value>::type,
221 typename = typename std::enable_if<internal::ComparableWithCStringView<RhsT>::value>::type>
222 bool operator==(LhsT&& lhs, RhsT&& rhs) noexcept
224 const auto rawLhs = internal::asConstCharPtr(lhs);
225 const auto rawRhs = internal::asConstCharPtr(rhs);
227 BT_ASSERT_DBG(rawLhs);
228 BT_ASSERT_DBG(rawRhs);
229 return std::strcmp(rawLhs, rawRhs) == 0;
233 * Returns true if `lhs` is not equal to `rhs`.
235 * `LhsT` and `RhsT` may be any of:
241 * Both `lhs` and `rhs` must not have an underlying `nullptr` raw data.
243 template <typename LhsT, typename RhsT>
244 bool operator!=(LhsT&& lhs, RhsT&& rhs) noexcept
246 return !(std::forward<LhsT>(lhs) == std::forward<RhsT>(rhs));
249 } /* namespace bt2c */
251 #endif /* BABELTRACE_CPP_COMMON_BT2C_C_STRING_VIEW_HPP */