Commit | Line | Data |
---|---|---|
339e8bc4 PP |
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" | |
e64addc7 | 16 | #include "cpp-common/vendor/fmt/format.h" |
339e8bc4 PP |
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 | ||
a0ae532f SM |
45 | /* |
46 | * Builds a view of the string `str`. | |
47 | * | |
48 | * Intentionally not explicit. | |
49 | */ | |
50 | CStringView(const std::string& str) noexcept : _mStr {str.c_str()} | |
51 | { | |
52 | } | |
53 | ||
339e8bc4 PP |
54 | /* |
55 | * Makes this view view the C string `str` (may be `nullptr`). | |
56 | * | |
57 | * Intentionally not explicit. | |
58 | */ | |
59 | CStringView& operator=(const char * const str) noexcept | |
60 | { | |
61 | _mStr = str; | |
62 | return *this; | |
63 | } | |
64 | ||
65 | /* | |
66 | * Viewed null-terminated C string (may be `nullptr`). | |
67 | */ | |
68 | const char *data() const noexcept | |
69 | { | |
70 | return _mStr; | |
71 | } | |
72 | ||
73 | /* | |
74 | * Alias of data(). | |
75 | */ | |
76 | operator const char *() const noexcept | |
77 | { | |
78 | return this->data(); | |
79 | } | |
80 | ||
81 | /* | |
82 | * Alias of data(). | |
83 | */ | |
84 | const char *operator*() const noexcept | |
85 | { | |
86 | return this->data(); | |
87 | } | |
88 | ||
89 | /* | |
90 | * Alias of data(). | |
91 | * | |
92 | * data() must not return `nullptr`. | |
93 | */ | |
94 | const char *begin() const noexcept | |
95 | { | |
96 | BT_ASSERT_DBG(_mStr); | |
97 | return this->data(); | |
98 | } | |
99 | ||
100 | /* | |
101 | * Pointer to the null character of the viewed C string. | |
102 | * | |
103 | * data() must not return `nullptr`. | |
104 | */ | |
105 | const char *end() const noexcept | |
106 | { | |
107 | BT_ASSERT_DBG(_mStr); | |
108 | return _mStr + this->len(); | |
109 | } | |
110 | ||
111 | /* | |
112 | * Length of the viewed C string, excluding the null character. | |
113 | * | |
114 | * data() must not return `nullptr`. | |
115 | */ | |
116 | std::size_t len() const noexcept | |
117 | { | |
118 | BT_ASSERT_DBG(_mStr); | |
119 | return std::strlen(_mStr); | |
120 | } | |
121 | ||
122 | /* | |
123 | * Returns an `std::string` instance containing a copy of the viewed | |
124 | * C string. | |
125 | * | |
126 | * data() must not return `nullptr`. | |
127 | */ | |
128 | std::string str() const | |
129 | { | |
130 | BT_ASSERT_DBG(_mStr); | |
131 | return std::string {_mStr}; | |
132 | } | |
133 | ||
134 | /* | |
135 | * Alias of str(). | |
136 | */ | |
137 | operator std::string() const | |
138 | { | |
139 | return this->str(); | |
140 | } | |
141 | ||
142 | /* | |
143 | * Returns a `bt2s::string_view` instance to view the contents, | |
144 | * excluding the null character, of the viewed C string. | |
145 | */ | |
146 | bt2s::string_view strView() const noexcept | |
147 | { | |
148 | if (_mStr) { | |
149 | return bt2s::string_view {this->begin(), this->len()}; | |
150 | } else { | |
151 | return {}; | |
152 | } | |
153 | } | |
154 | ||
155 | /* | |
156 | * Alias of strView(). | |
157 | */ | |
158 | operator bt2s::string_view() const noexcept | |
159 | { | |
160 | return this->strView(); | |
161 | } | |
162 | ||
163 | /* | |
164 | * Returns the character at index `i`. | |
165 | * | |
166 | * `i` must be less than what len() returns. | |
167 | * | |
168 | * data() must not return `nullptr`. | |
169 | */ | |
170 | char operator[](const std::size_t i) const noexcept | |
171 | { | |
172 | BT_ASSERT_DBG(_mStr); | |
173 | BT_ASSERT_DBG(i < this->len()); | |
174 | return _mStr[i]; | |
175 | } | |
176 | ||
177 | private: | |
178 | const char *_mStr = nullptr; | |
179 | }; | |
180 | ||
e64addc7 | 181 | static inline const char *format_as(const CStringView& str) |
339e8bc4 | 182 | { |
e64addc7 SM |
183 | return str ? *str : "(null)"; |
184 | } | |
339e8bc4 | 185 | |
e64addc7 | 186 | } /* namespace bt2c */ |
339e8bc4 PP |
187 | |
188 | #endif /* BABELTRACE_CPP_COMMON_BT2C_C_STRING_VIEW_HPP */ |