Skip to content

Commit 1d8457b

Browse files
author
Julian LALU
committed
Update cstring_view
1 parent defa3bf commit 1d8457b

File tree

9 files changed

+472
-421
lines changed

9 files changed

+472
-421
lines changed

interface/core/character/character.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ namespace hud
1010
static constexpr wchar WIDE_NULL_CHARACTER = L'\0';
1111

1212
/** Check whether the character is a pure ansi character. */
13-
static HD_FORCEINLINE constexpr bool is_pure_ansi(const ansichar character) noexcept
13+
static HD_FORCEINLINE constexpr bool is_pure_ascii(const ansichar character) noexcept
1414
{
1515
return (character & 0x80) == 0;
1616
}
1717

1818
/** Check whether the character is a pure ansi character. */
19-
static HD_FORCEINLINE constexpr bool is_pure_ansi(const wchar character) noexcept
19+
static HD_FORCEINLINE constexpr bool is_pure_ascii(const wchar character) noexcept
2020
{
2121
return (character & ~0x7F) == 0;
2222
}

interface/core/string/cstring.h

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#define HD_INC_CORE_STRING_CSTRING_H
33
#include "../character.h"
44

5-
// For is_pure_ansi check : https://quick-bench.com/q/P_adhBeQdvHLTBB8EZCtLyrPRsM
5+
// For is_pure_ascii check : https://quick-bench.com/q/P_adhBeQdvHLTBB8EZCtLyrPRsM
66
namespace hud
77
{
88

@@ -28,14 +28,14 @@ namespace hud
2828
* @param string The null-terminated string
2929
* @return Always return true
3030
*/
31-
[[nodiscard]] static HD_FORCEINLINE bool is_pure_ansi(const ansichar *string) noexcept
31+
[[nodiscard]] static HD_FORCEINLINE bool is_pure_ascii(const ansichar *string) noexcept
3232
{
3333
if (string == nullptr) {
3434
return false;
3535
}
3636

3737
while (!character::is_null(*string)) {
38-
if (!character::is_pure_ansi(*string)) {
38+
if (!character::is_pure_ascii(*string)) {
3939
return false;
4040
}
4141
string++;
@@ -48,14 +48,14 @@ namespace hud
4848
* @param string The null-terminated string
4949
* @return true if the string contains only ansichar, false otherwise
5050
*/
51-
[[nodiscard]] static bool is_pure_ansi(const wchar *string) noexcept
51+
[[nodiscard]] static bool is_pure_ascii(const wchar *string) noexcept
5252
{
5353
if (string == nullptr) {
5454
return false;
5555
}
5656

5757
while (!character::is_null(*string)) {
58-
if (!character::is_pure_ansi(*string)) {
58+
if (!character::is_pure_ascii(*string)) {
5959
return false;
6060
}
6161
string++;
@@ -70,7 +70,7 @@ namespace hud
7070
* @return true if the string contains only ansichar and reach null-terminator character or the string_size character.
7171
* false if the string contains non ansichar character
7272
*/
73-
[[nodiscard]] static HD_FORCEINLINE bool is_pure_ansi_safe(const ansichar *string, usize string_size) noexcept
73+
[[nodiscard]] static HD_FORCEINLINE bool is_pure_ascii_safe(const ansichar *string, usize string_size) noexcept
7474
{
7575
return string != nullptr;
7676
}
@@ -82,7 +82,7 @@ namespace hud
8282
* @return true if the string contains only ansichar and reach null-terminator character or the string_size character.
8383
* false if the string contains non ansichar character
8484
*/
85-
[[nodiscard]] static bool is_pure_ansi_safe(const wchar *string, usize string_size) noexcept
85+
[[nodiscard]] static bool is_pure_ascii_safe(const wchar *string, usize string_size) noexcept
8686
{
8787
if (string == nullptr) {
8888
return false;
@@ -93,7 +93,7 @@ namespace hud
9393
if (character::is_null(cur)) {
9494
return true;
9595
}
96-
if (!character::is_pure_ansi(cur)) {
96+
if (!character::is_pure_ascii(cur)) {
9797
return false;
9898
}
9999
string++;
@@ -849,9 +849,9 @@ namespace hud
849849
const bool max_length_is_zero = max_length == 0;
850850
const bool max_length_is_greater_than_RSIZE_MAX_STR = max_length > RSIZE_MAX_STR;
851851
if constexpr (hud::compilation::is_assertion_enabled()) {
852-
hud::debugger::break_here_if(string_is_nullptr);
853-
hud::debugger::break_here_if(max_length_is_zero);
854-
hud::debugger::break_here_if(max_length_is_greater_than_RSIZE_MAX_STR);
852+
// hud::debugger::break_here_if(string_is_nullptr);
853+
// hud::debugger::break_here_if(max_length_is_zero);
854+
// hud::debugger::break_here_if(max_length_is_greater_than_RSIZE_MAX_STR);
855855
}
856856
// We do not add the max_length_is_greater_than_RSIZE_MAX_STR to condition,
857857
// the value will be clamped to RSIZE_MAX_STR

interface/core/string/cstring_view.h

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
namespace hud
66
{
77
/**
8-
* A view of a C-compatible, null-terminated string with no null-terminated bytes in the middle.
8+
* An immutable view of a C-compatible, null-terminated string with no null-terminated bytes in the middle.
99
*/
1010
struct cstring_view
1111
{
@@ -36,6 +36,12 @@ namespace hud
3636
return ptr_;
3737
}
3838

39+
[[nodiscard]]
40+
constexpr bool is_empty() const noexcept
41+
{
42+
return len_ == 0;
43+
}
44+
3945
[[nodiscard]]
4046
constexpr bool is_ascii() const noexcept
4147
{
@@ -64,9 +70,63 @@ namespace hud
6470
return true;
6571
}
6672

73+
[[nodiscard]]
74+
constexpr bool equals(const cstring_view &v) const noexcept
75+
{
76+
if (len_ != v.len_) {
77+
return false;
78+
}
79+
usize i {0};
80+
#if HD_SSE2
81+
if consteval {
82+
}
83+
else {
84+
// DO SSE2 comparison of 16 bytes if we have 16 bytes
85+
for (; i + 16 <= len_; i += 16) {
86+
__m128i chunk_this = _mm_loadu_si128(reinterpret_cast<const __m128i *>(ptr_ + i));
87+
__m128i chunk_v = _mm_loadu_si128(reinterpret_cast<const __m128i *>(v.ptr_ + i));
88+
if (_mm_movemask_epi8(_mm_cmpeq_epi8(chunk_this, chunk_v)) != 0xFFFF)
89+
return false;
90+
}
91+
}
92+
#endif
93+
for (; i < len_; ++i) {
94+
if (ptr_[i] != v.ptr_[i]) {
95+
return false;
96+
}
97+
}
98+
return true;
99+
}
100+
101+
[[nodiscard]]
102+
constexpr bool equals_partial(const cstring_view &v, const usize n) const noexcept
103+
{
104+
usize len_to_compare = (n < len_) ? n : len_;
105+
len_to_compare = (len_to_compare > v.len_) ? v.len_ : len_to_compare;
106+
usize i {0};
107+
#if HD_SSE2
108+
if consteval {
109+
}
110+
else {
111+
// DO SSE2 comparison of 16 bytes if we have 16 bytes
112+
for (; i + 16 <= len_to_compare; i += 16) {
113+
__m128i chunk_this = _mm_loadu_si128(reinterpret_cast<const __m128i *>(ptr_ + i));
114+
__m128i chunk_v = _mm_loadu_si128(reinterpret_cast<const __m128i *>(v.ptr_ + i));
115+
if (_mm_movemask_epi8(_mm_cmpeq_epi8(chunk_this, chunk_v)) != 0xFFFF)
116+
return false;
117+
}
118+
}
119+
#endif
120+
for (; i < len_to_compare; ++i) {
121+
if (ptr_[i] != v.ptr_[i]) {
122+
return false;
123+
}
124+
}
125+
return true;
126+
}
127+
67128
private:
68129
const ansichar *ptr_;
69-
70130
usize len_;
71131
};
72132

test/CMakeLists.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,7 @@ set( src
6868
compressed_tuple/compressed_tuple_misc.cpp
6969
compressed_tuple/compressed_tuple_size.cpp
7070
compressed_tuple/compressed_tuple_swap.cpp
71-
cstring_view/constructors.cpp
72-
cstring_view/misc.cpp
71+
cstring_view/cstring_view.cpp
7372
hash/city_hash.cpp
7473
hash/crc32.cpp
7574
hash/hash_32.cpp

0 commit comments

Comments
 (0)