11#ifndef HD_INC_CORE_STRING_CSTRING_VIEW_H
22#define HD_INC_CORE_STRING_CSTRING_VIEW_H
33#include " cstring.h"
4-
4+ # include " ../slice.h "
55namespace hud
66{
77 /* *
8- * An immutable view of a C-compatible, null-terminated string with no null-terminated bytes in the middle.
8+ * An immutable view of a C-style, null-terminated string (`ansichar`).
9+ * This view does not own the underlying string and does not allow modification
10+ * of its length. It provides utility functions for querying and accessing
11+ * the string content.
912 */
13+ template <typename char_t >
1014 struct cstring_view
1115 {
16+ /* * Type of the underlying character. */
17+ using char_type = char_t ;
18+
19+ static_assert (hud::is_same_v<hud::remove_cv_t <char_type>, ansichar>);
20+
1221 /* *
1322 * Constructs a cstring_view from a C-style string pointer.
1423 * @param str Pointer to a null-terminated string. Must not be null.
1524 */
16- constexpr cstring_view (const ansichar *str) noexcept
25+ constexpr cstring_view (char_type *str) noexcept
1726 : ptr_(str)
1827 {
1928 HUD_CHECK (ptr_ != nullptr && " Invalid null pointer" );
@@ -34,7 +43,7 @@ namespace hud
3443 * @return A pointer to the string's first character (null-terminated).
3544 */
3645 [[nodiscard]]
37- constexpr const ansichar *data () const noexcept
46+ constexpr char_type *data () const noexcept
3847 {
3948 return ptr_;
4049 }
@@ -83,73 +92,89 @@ namespace hud
8392 }
8493
8594 /* *
86- * Checks if this string is equal to another string .
87- * @param v Another cstring_view to compare with .
95+ * Checks if this string is equal to another cstring_view .
96+ * @param v Another cstring_view to compare.
8897 * @return true if both strings are identical, false otherwise.
8998 */
90-
99+ template < typename uchar_t >
91100 [[nodiscard]]
92- constexpr bool equals (const cstring_view &v) const noexcept
101+ constexpr bool equals (const cstring_view< uchar_t > &v) const noexcept
93102 {
94103 return hud::cstring::equals (ptr_, v.ptr_ );
95104 }
96105
97106 /* *
98- * Checks if the first n characters of this string match those of another string .
99- * @param v Another cstring_view to compare with .
107+ * Checks if the first n characters of this string match another cstring_view .
108+ * @param v Another cstring_view to compare.
100109 * @param n Number of characters to compare.
101110 * @return true if the first n characters match, false otherwise.
102111 */
112+ template <typename uchar_t >
103113 [[nodiscard]]
104- constexpr bool equals_partial (const cstring_view &v, const usize n) const noexcept
114+ constexpr bool equals_partial (const cstring_view< uchar_t > &v, const usize n) const noexcept
105115 {
106116 return hud::cstring::equals_partial (ptr_, v.ptr_ , n);
107117 }
108118
109119 /* *
110- * Finds the first occurrence of a substring.
111- * @param to_find The substring to search for.
120+ * Finds the first occurrence of a substring given as a C-style string .
121+ * @param to_find_ptr Pointer to a null-terminated string to search for.
112122 * @return Index of the first occurrence, or -1 if not found.
113123 */
114124 [[nodiscard]]
115- constexpr isize find_first (const cstring_view &to_find ) const noexcept
125+ constexpr isize find_first (const ansichar *to_find_ptr ) const noexcept
116126 {
117- const ansichar *result = hud::cstring::find_string (ptr_, to_find. ptr_ );
127+ const ansichar *result = hud::cstring::find_string (ptr_, to_find_ptr );
118128 return result == nullptr ? -1 : result - ptr_;
119129 }
120130
131+ /* *
132+ * Finds the first occurrence of a substring given as another cstring_view.
133+ * @param to_find Substring to search for.
134+ * @return Index of the first occurrence, or -1 if not found.
135+ */
136+ template <typename uchar_t >
137+ [[nodiscard]]
138+ constexpr isize find_first (const cstring_view<uchar_t > &to_find) const noexcept
139+ {
140+ return find_first (to_find.data ());
141+ }
142+
121143 /* *
122144 * Finds the first occurrence of a character.
123145 * @param character_to_find The character to search for.
124146 * @return Index of the first occurrence, or -1 if not found.
125147 */
126148 [[nodiscard]]
127- constexpr isize find_first_character (const ansichar character_to_find) const noexcept
149+ constexpr isize find_first_character (char_type character_to_find) const noexcept
128150 {
129- const ansichar *result = hud::cstring::find_character (ptr_, character_to_find);
151+ const char_type *result = hud::cstring::find_character (ptr_, character_to_find);
130152 return result == nullptr ? -1 : result - ptr_;
131153 }
132154
133155 /* *
134- * Provides read-only access to a character at a given index .
135- * @param i Index of the character to access .
136- * @return A const reference to the character at position i .
156+ * Checks if this string contains a given substring (C-style string) .
157+ * @param to_find_str Substring to search for .
158+ * @return true if the substring is found, false otherwise .
137159 */
138160 [[nodiscard]]
139- constexpr const ansichar & operator [] (const usize i ) const noexcept
161+ constexpr bool contains (const ansichar *to_find_str ) const noexcept
140162 {
141- return ptr_[i] ;
163+ return hud::cstring::find_string ( ptr_, to_find_str) != nullptr ;
142164 }
165+
143166 /* *
144- * Checks if this string contains a given substring.
145- * @param to_find The substring to search for.
167+ * Checks if this string contains a given substring (cstring_view) .
168+ * @param to_find Substring to search for.
146169 * @return true if the substring is found, false otherwise.
147170 */
171+ template <typename uchar_t >
148172 [[nodiscard]]
149- constexpr bool contains (const cstring_view &to_find) const noexcept
173+ constexpr bool contains (const cstring_view< uchar_t > &to_find) const noexcept
150174 {
151- return hud::cstring::find_string (ptr_, to_find.ptr_ ) != nullptr ;
175+ return contains ( to_find.data ()) ;
152176 }
177+
153178 /* *
154179 * Checks if this string contains a given character.
155180 * @param character_to_find The character to search for.
@@ -161,9 +186,70 @@ namespace hud
161186 return hud::cstring::find_character (ptr_, character_to_find) != nullptr ;
162187 }
163188
189+ /* *
190+ * Convert string to uppercase.
191+ * @param string The string buffer to capitalize
192+ * @return string pointer
193+ */
194+ constexpr void to_uppercase () noexcept
195+ {
196+ hud::cstring::to_uppercase (ptr_);
197+ }
198+
199+ /* *
200+ * Convert string to uppercase.
201+ * @param string The string buffer to capitalize
202+ * @param count Number of character to capitalize
203+ * @return string pointer
204+ */
205+ constexpr void to_uppercase_partial (usize count) noexcept
206+ {
207+ hud::cstring::to_uppercase_partial (ptr_, count);
208+ }
209+
210+ /* *
211+ * Convert string to lowercase.
212+ * @param string The string buffer to minimize
213+ * @return string pointer
214+ */
215+ constexpr void to_lowercase () noexcept
216+ {
217+ hud::cstring::to_lowercase (ptr_);
218+ }
219+
220+ /* *
221+ * Convert string to lowercase.
222+ * @param string The string buffer to minimize
223+ * @return string pointer
224+ */
225+ constexpr void to_lowercase_partial (usize count) noexcept
226+ {
227+ hud::cstring::to_lowercase_partial (ptr_, count);
228+ }
229+
230+ /* *
231+ * Returns a slice view of the string.
232+ * @return A slice representing all characters of this string.
233+ */
234+ [[nodiscard]]
235+ constexpr hud::slice<char_type> slice () const noexcept
236+ {
237+ return hud::slice<char_type> {ptr_, length ()};
238+ }
239+ /* *
240+ * Provides read-only access to a character at a given index.
241+ * @param i Index of the character to access.
242+ * @return A const reference to the character at position i.
243+ */
244+ [[nodiscard]]
245+ constexpr char_type &operator [](const usize i) const noexcept
246+ {
247+ return ptr_[i];
248+ }
249+
164250 private:
165251 /* *Pointer to the null-terminated C-style string. */
166- const ansichar *ptr_;
252+ char_type *ptr_;
167253 };
168254
169255} // namespace hud
0 commit comments