Skip to content

Commit 9effa59

Browse files
author
Julian LALU
committed
Improve unicode support
1 parent a035729 commit 9effa59

File tree

11 files changed

+1000
-791
lines changed

11 files changed

+1000
-791
lines changed
Lines changed: 238 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,238 @@
1+
#ifndef HD_INC_CORE_STRING_ASCII_STRING_VIEW_H
2+
#define HD_INC_CORE_STRING_ASCII_STRING_VIEW_H
3+
#include "cstring.h"
4+
#include "../containers/optional.h"
5+
#include "../slice.h"
6+
#include "encoding/ascii.h"
7+
8+
namespace hud
9+
{
10+
/**
11+
* An view of a C-style, ASCII null-terminated string.
12+
* This view does not own the underlying string and does not allow modification
13+
* of its length. It provides utility functions for querying and accessing
14+
* the string content.
15+
*/
16+
template<typename char_t>
17+
struct ascii_string_view
18+
{
19+
static_assert(sizeof(char_t) == 1);
20+
21+
/** Type of the underlying character. */
22+
using char_type = char_t;
23+
24+
/**
25+
* Returns the length of the string (number of characters before the null terminator).
26+
* @return The length of the string in characters.
27+
*/
28+
[[nodiscard]]
29+
constexpr usize length() const noexcept
30+
{
31+
return hud::cstring::length(ptr_);
32+
}
33+
34+
/**
35+
* Returns a pointer to the underlying C-style string.
36+
* @return A pointer to the string's first character (null-terminated).
37+
*/
38+
[[nodiscard]]
39+
constexpr char_t *data() const noexcept
40+
{
41+
return ptr_;
42+
}
43+
44+
/**
45+
* Checks if the string is empty.
46+
* @return true if the string is empty, false otherwise.
47+
*/
48+
[[nodiscard]]
49+
constexpr bool is_empty() const noexcept
50+
{
51+
return hud::character::is_null(ptr_[0]);
52+
}
53+
54+
/**
55+
* Checks if this string is equal to another ascii_string_view.
56+
* @param v Another ascii_string_view to compare.
57+
* @return true if both strings are identical, false otherwise.
58+
*/
59+
[[nodiscard]]
60+
constexpr bool equals(const ascii_string_view<char_t> &v) const noexcept
61+
{
62+
return hud::cstring::equals(ptr_, v.ptr_);
63+
}
64+
65+
/**
66+
* Checks if the first n characters of this string match another ascii_string_view.
67+
* @param v Another ascii_string_view to compare.
68+
* @param n Number of characters to compare.
69+
* @return true if the first n characters match, false otherwise.
70+
*/
71+
[[nodiscard]]
72+
constexpr bool equals_partial(const ascii_string_view<char_t> &v, const usize n) const noexcept
73+
{
74+
return hud::cstring::equals_partial(ptr_, v.ptr_, n);
75+
}
76+
77+
/**
78+
* Finds the first occurrence of a substring given as a C-style string.
79+
* @param to_find_ptr Pointer to a null-terminated string to search for.
80+
* @return Index of the first occurrence, or -1 if not found.
81+
*/
82+
[[nodiscard]]
83+
constexpr isize find_first(const char_t *to_find_ptr) const noexcept
84+
{
85+
const char_t *result = hud::cstring::find_string(ptr_, to_find_ptr);
86+
return result == nullptr ? -1 : result - ptr_;
87+
}
88+
89+
/**
90+
* Finds the first occurrence of a substring given as another ascii_string_view.
91+
* @param to_find Substring to search for.
92+
* @return Index of the first occurrence, or -1 if not found.
93+
*/
94+
[[nodiscard]]
95+
constexpr isize find_first(const ascii_string_view<char_t> &to_find) const noexcept
96+
{
97+
return find_first(to_find.data());
98+
}
99+
100+
/**
101+
* Finds the first occurrence of a character.
102+
* @param character_to_find The character to search for.
103+
* @return Index of the first occurrence, or -1 if not found.
104+
*/
105+
[[nodiscard]]
106+
constexpr isize find_first_character(char_t character_to_find) const noexcept
107+
{
108+
const char_t *result = hud::cstring::find_character(ptr_, character_to_find);
109+
return result == nullptr ? -1 : result - ptr_;
110+
}
111+
112+
/**
113+
* Checks if this string contains a given substring (C-style string).
114+
* @param to_find_str Substring to search for.
115+
* @return true if the substring is found, false otherwise.
116+
*/
117+
[[nodiscard]]
118+
constexpr bool contains(const char_t *to_find_str) const noexcept
119+
{
120+
return hud::cstring::find_string(ptr_, to_find_str) != nullptr;
121+
}
122+
123+
/**
124+
* Checks if this string contains a given substring (ascii_string_view).
125+
* @param to_find Substring to search for.
126+
* @return true if the substring is found, false otherwise.
127+
*/
128+
[[nodiscard]]
129+
constexpr bool contains(const ascii_string_view<char_t> &to_find) const noexcept
130+
{
131+
return contains(to_find.data());
132+
}
133+
134+
/**
135+
* Checks if this string contains a given character.
136+
* @param character_to_find The character to search for.
137+
* @return true if the character is found, false otherwise.
138+
*/
139+
[[nodiscard]]
140+
constexpr bool contains(const char_t character_to_find) const noexcept
141+
{
142+
return hud::cstring::find_character(ptr_, character_to_find) != nullptr;
143+
}
144+
145+
/** Convert string to uppercase. */
146+
constexpr void to_uppercase() noexcept
147+
{
148+
hud::cstring::ascii_to_uppercase(ptr_);
149+
}
150+
151+
/**
152+
* Convert string to uppercase.
153+
* @param count Number of character to capitalize
154+
*/
155+
constexpr void to_uppercase_partial(usize count) noexcept
156+
{
157+
hud::cstring::ascii_to_uppercase_partial(ptr_, count);
158+
}
159+
160+
/** Convert string to lowercase. */
161+
constexpr void to_lowercase() noexcept
162+
{
163+
hud::cstring::ascii_to_lowercase(ptr_);
164+
}
165+
166+
/**
167+
* Convert string to lowercase.
168+
* @param count Number of character to minimize
169+
* @return string pointer
170+
*/
171+
constexpr void to_lowercase_partial(usize count) noexcept
172+
{
173+
hud::cstring::ascii_to_lowercase_partial(ptr_, count);
174+
}
175+
176+
/**
177+
* Returns a slice view of the string.
178+
* @return A slice representing all characters of this string.
179+
*/
180+
[[nodiscard]]
181+
constexpr const hud::slice<char_t> as_slice() const noexcept
182+
{
183+
return hud::slice<char_t> {ptr_, length()};
184+
}
185+
186+
/**
187+
* Returns a slice view of the string.
188+
* @return A slice representing all characters of this string.
189+
*/
190+
[[nodiscard]]
191+
constexpr hud::slice<char_t> as_slice() noexcept
192+
{
193+
return hud::slice<char_t> {ptr_, length()};
194+
}
195+
196+
/**
197+
* Provides read-only access to the code unit at a given index.
198+
* @param i Index of the code unit to access.
199+
* @return A const reference to the code unit at position i.
200+
*/
201+
[[nodiscard]]
202+
constexpr char_t &operator[](const usize i) const noexcept
203+
{
204+
return ptr_[i];
205+
}
206+
207+
/**
208+
* Constructs a ascii_string_view from a C-style string pointer.
209+
* @param str Pointer to a null-terminated string. Must not be null.
210+
*/
211+
constexpr ascii_string_view(char_t *str) noexcept
212+
: ptr_(str)
213+
{
214+
HUD_CHECK(ptr_ != nullptr && "Invalid null pointer");
215+
}
216+
217+
private:
218+
/**Pointer to the ascii null-terminated C-style string. */
219+
char_type *ptr_;
220+
};
221+
222+
/**
223+
* Create a string view over a full ascii string.
224+
* If the given string is not ascii, the function return a nullopt.
225+
*/
226+
template<typename char_t>
227+
requires(hud::is_same_v<hud::remove_cv_t<char_t>, char8>)
228+
constexpr hud::optional<hud::ascii_string_view<char_t>> make_ascii_string_view(char_t *ptr) noexcept
229+
{
230+
if (hud::encoding::is_valid_ascii(hud::slice {ptr, hud::cstring::length(ptr)})) {
231+
return hud::ascii_string_view(ptr);
232+
}
233+
return hud::nullopt;
234+
}
235+
236+
} // namespace hud
237+
238+
#endif // HD_INC_CORE_STRING_ASCII_STRING_VIEW_H

interface/core/string/cstring.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
#include "../character.h"
44
#include "../memory.h"
55
#include <stdarg.h> // va_start, va_end
6-
#include <string.h> // strncpy, wcsncpy, wcscat,
6+
#include <string.h> // strncpy, wcsncpy, wcscat
7+
78
// For is_ascii check : https://quick-bench.com/q/P_adhBeQdvHLTBB8EZCtLyrPRsM
89
namespace hud
910
{

0 commit comments

Comments
 (0)