| 1 | /* |
| 2 | * Copyright (c) 2023 Philippe Proulx <pproulx@efficios.com> |
| 3 | * |
| 4 | * SPDX-License-Identifier: MIT |
| 5 | */ |
| 6 | |
| 7 | #ifndef BABELTRACE_CPP_COMMON_BT2C_C_STRING_VIEW_HPP |
| 8 | #define BABELTRACE_CPP_COMMON_BT2C_C_STRING_VIEW_HPP |
| 9 | |
| 10 | #include <cstddef> |
| 11 | #include <cstring> |
| 12 | #include <string> |
| 13 | |
| 14 | #include "common/assert.h" |
| 15 | #include "cpp-common/bt2s/string-view.hpp" |
| 16 | #include "cpp-common/vendor/fmt/core.h" |
| 17 | |
| 18 | namespace bt2c { |
| 19 | |
| 20 | /* |
| 21 | * A view on a constant null-terminated C string. |
| 22 | * |
| 23 | * Similar to `bt2s::string_view`, but len() and end() compute the |
| 24 | * length on demand. |
| 25 | */ |
| 26 | class CStringView final |
| 27 | { |
| 28 | public: |
| 29 | /* |
| 30 | * Builds an empty view (data() returns `nullptr`). |
| 31 | * |
| 32 | * Intentionally not explicit. |
| 33 | */ |
| 34 | CStringView() noexcept = default; |
| 35 | |
| 36 | /* |
| 37 | * Builds a view of the C string `str` (may be `nullptr`). |
| 38 | * |
| 39 | * Intentionally not explicit. |
| 40 | */ |
| 41 | CStringView(const char * const str) noexcept : _mStr {str} |
| 42 | { |
| 43 | } |
| 44 | |
| 45 | /* |
| 46 | * Makes this view view the C string `str` (may be `nullptr`). |
| 47 | * |
| 48 | * Intentionally not explicit. |
| 49 | */ |
| 50 | CStringView& operator=(const char * const str) noexcept |
| 51 | { |
| 52 | _mStr = str; |
| 53 | return *this; |
| 54 | } |
| 55 | |
| 56 | /* |
| 57 | * Viewed null-terminated C string (may be `nullptr`). |
| 58 | */ |
| 59 | const char *data() const noexcept |
| 60 | { |
| 61 | return _mStr; |
| 62 | } |
| 63 | |
| 64 | /* |
| 65 | * Alias of data(). |
| 66 | */ |
| 67 | operator const char *() const noexcept |
| 68 | { |
| 69 | return this->data(); |
| 70 | } |
| 71 | |
| 72 | /* |
| 73 | * Alias of data(). |
| 74 | */ |
| 75 | const char *operator*() const noexcept |
| 76 | { |
| 77 | return this->data(); |
| 78 | } |
| 79 | |
| 80 | /* |
| 81 | * Alias of data(). |
| 82 | * |
| 83 | * data() must not return `nullptr`. |
| 84 | */ |
| 85 | const char *begin() const noexcept |
| 86 | { |
| 87 | BT_ASSERT_DBG(_mStr); |
| 88 | return this->data(); |
| 89 | } |
| 90 | |
| 91 | /* |
| 92 | * Pointer to the null character of the viewed C string. |
| 93 | * |
| 94 | * data() must not return `nullptr`. |
| 95 | */ |
| 96 | const char *end() const noexcept |
| 97 | { |
| 98 | BT_ASSERT_DBG(_mStr); |
| 99 | return _mStr + this->len(); |
| 100 | } |
| 101 | |
| 102 | /* |
| 103 | * Length of the viewed C string, excluding the null character. |
| 104 | * |
| 105 | * data() must not return `nullptr`. |
| 106 | */ |
| 107 | std::size_t len() const noexcept |
| 108 | { |
| 109 | BT_ASSERT_DBG(_mStr); |
| 110 | return std::strlen(_mStr); |
| 111 | } |
| 112 | |
| 113 | /* |
| 114 | * Returns an `std::string` instance containing a copy of the viewed |
| 115 | * C string. |
| 116 | * |
| 117 | * data() must not return `nullptr`. |
| 118 | */ |
| 119 | std::string str() const |
| 120 | { |
| 121 | BT_ASSERT_DBG(_mStr); |
| 122 | return std::string {_mStr}; |
| 123 | } |
| 124 | |
| 125 | /* |
| 126 | * Alias of str(). |
| 127 | */ |
| 128 | operator std::string() const |
| 129 | { |
| 130 | return this->str(); |
| 131 | } |
| 132 | |
| 133 | /* |
| 134 | * Returns a `bt2s::string_view` instance to view the contents, |
| 135 | * excluding the null character, of the viewed C string. |
| 136 | */ |
| 137 | bt2s::string_view strView() const noexcept |
| 138 | { |
| 139 | if (_mStr) { |
| 140 | return bt2s::string_view {this->begin(), this->len()}; |
| 141 | } else { |
| 142 | return {}; |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | /* |
| 147 | * Alias of strView(). |
| 148 | */ |
| 149 | operator bt2s::string_view() const noexcept |
| 150 | { |
| 151 | return this->strView(); |
| 152 | } |
| 153 | |
| 154 | /* |
| 155 | * Returns the character at index `i`. |
| 156 | * |
| 157 | * `i` must be less than what len() returns. |
| 158 | * |
| 159 | * data() must not return `nullptr`. |
| 160 | */ |
| 161 | char operator[](const std::size_t i) const noexcept |
| 162 | { |
| 163 | BT_ASSERT_DBG(_mStr); |
| 164 | BT_ASSERT_DBG(i < this->len()); |
| 165 | return _mStr[i]; |
| 166 | } |
| 167 | |
| 168 | private: |
| 169 | const char *_mStr = nullptr; |
| 170 | }; |
| 171 | |
| 172 | } /* namespace bt2c */ |
| 173 | |
| 174 | namespace fmt { |
| 175 | |
| 176 | template <> |
| 177 | struct formatter<bt2c::CStringView> |
| 178 | { |
| 179 | constexpr auto parse(format_parse_context& ctx) const -> format_parse_context::iterator |
| 180 | { |
| 181 | return ctx.end(); |
| 182 | } |
| 183 | |
| 184 | auto format(const bt2c::CStringView& str, format_context& ctx) const -> format_context::iterator |
| 185 | { |
| 186 | return str ? fmt::format_to(ctx.out(), "{}", *str) : fmt::format_to(ctx.out(), "(null)"); |
| 187 | } |
| 188 | }; |
| 189 | |
| 190 | } /* namespace fmt */ |
| 191 | |
| 192 | #endif /* BABELTRACE_CPP_COMMON_BT2C_C_STRING_VIEW_HPP */ |