Skip to content

Commit 90bec26

Browse files
committed
Improve string_views
1 parent 929b18c commit 90bec26

2 files changed

Lines changed: 11 additions & 12 deletions

File tree

interface/core/string/cstring_view.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,21 @@
1111

1212
namespace hud
1313
{
14-
template<typename char_t, usize char_size = sizeof(char_t)>
15-
struct cstring_view;
16-
1714
/**
1815
* An immutable view of a C-style, null-terminated string.
1916
* This view does not own the underlying string and does not allow modification
2017
* of its length. It provides utility functions for querying and accessing
2118
* the string content.
2219
*/
2320
template<typename char_t>
24-
struct cstring_view<char_t, 1>
21+
struct cstring_view
2522
{
2623
/** Type of the underlying character. */
2724
using char_type = char_t;
2825

26+
/** CString string view only support 1 byte char_type. */
27+
static_assert(sizeof(char_t) == 1, "cstring_view requires char_t with sizeof(char_t) == 1");
28+
2929
/**
3030
* Constructs a cstring_view from a C-style string pointer.
3131
* @param str Pointer to a null-terminated string. Must not be null.

interface/core/string/utf16_string_view.h

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,15 @@
77

88
namespace hud
99
{
10-
template<typename char_t, usize char_size = sizeof(char_t)>
11-
struct utf16_string_view;
12-
1310
template<typename char_t>
14-
struct utf16_string_view<char_t, 2>
11+
struct utf16_string_view
1512
{
1613

1714
/** Type of the underlying character. */
1815
using char_type = char_t;
1916

17+
/** UTF16 string view only support 2 byte char_type. */
18+
static_assert(sizeof(char_t) == 2, "utf16_string_view requires char_t with sizeof(char_t) == 2 (UTF-16)");
2019
/**
2120
* Returns the length of the string (number of characters before the null terminator).
2221
* @return The length of the string in characters.
@@ -53,7 +52,7 @@ namespace hud
5352
* @return true if both strings are identical, false otherwise.
5453
*/
5554
[[nodiscard]]
56-
constexpr bool equals(const utf16_string_view<char_type, 1> &v) const noexcept
55+
constexpr bool equals(const utf16_string_view<char_type> &v) const noexcept
5756
{
5857
return hud::cstring::equals(ptr_, v.ptr_);
5958
}
@@ -65,7 +64,7 @@ namespace hud
6564
* @return true if the first n characters match, false otherwise.
6665
*/
6766
[[nodiscard]]
68-
constexpr bool equals_partial(const utf16_string_view<char_type, 1> &v, const usize n) const noexcept
67+
constexpr bool equals_partial(const utf16_string_view<char_type> &v, const usize n) const noexcept
6968
{
7069
return hud::cstring::equals_partial(ptr_, v.ptr_, n);
7170
}
@@ -88,7 +87,7 @@ namespace hud
8887
* @return Index of the first occurrence, or -1 if not found.
8988
*/
9089
[[nodiscard]]
91-
constexpr isize find_first(const utf16_string_view<char_type, 1> &to_find) const noexcept
90+
constexpr isize find_first(const utf16_string_view<char_type> &to_find) const noexcept
9291
{
9392
return find_first(to_find.data());
9493
}
@@ -122,7 +121,7 @@ namespace hud
122121
* @return true if the substring is found, false otherwise.
123122
*/
124123
[[nodiscard]]
125-
constexpr bool contains(const utf16_string_view<char_type, 1> &to_find) const noexcept
124+
constexpr bool contains(const utf16_string_view<char_type> &to_find) const noexcept
126125
{
127126
return contains(to_find.data());
128127
}

0 commit comments

Comments
 (0)