Skip to content

Commit 86f1586

Browse files
author
Julian LALU
committed
As slice on cstring_view and wstring_view
1 parent 9035859 commit 86f1586

File tree

5 files changed

+110
-6
lines changed

5 files changed

+110
-6
lines changed

interface/core/slice.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,7 @@ namespace hud
5454
/** Move assign the slice. */
5555
HD_FORCEINLINE constexpr slice &operator=(slice &&other) noexcept
5656
{
57-
if (this != &other)
58-
{
57+
if (this != &other) {
5958
begin_ptr = other.begin_ptr;
6059
other.begin_ptr = nullptr;
6160
count_element = other.count_element;
@@ -188,9 +187,9 @@ namespace hud
188187
}
189188

190189
private:
191-
/** Pointer to the first element */
190+
/** Pointer to the first element. */
192191
type_t *begin_ptr = nullptr;
193-
/** Count of elements */
192+
/** Count of elements. */
194193
usize count_element = 0u;
195194
};
196195

interface/core/string/cstring_view.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,10 +232,21 @@ namespace hud
232232
* @return A slice representing all characters of this string.
233233
*/
234234
[[nodiscard]]
235-
constexpr hud::slice<char_type> slice() const noexcept
235+
constexpr const hud::slice<char_type> as_slice() const noexcept
236236
{
237237
return hud::slice<char_type> {ptr_, length()};
238238
}
239+
240+
/**
241+
* Returns a slice view of the string.
242+
* @return A slice representing all characters of this string.
243+
*/
244+
[[nodiscard]]
245+
constexpr hud::slice<char_type> as_slice() noexcept
246+
{
247+
return hud::slice<char_type> {ptr_, length()};
248+
}
249+
239250
/**
240251
* Provides read-only access to a character at a given index.
241252
* @param i Index of the character to access.

interface/core/string/wstring_view.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,17 @@ namespace hud
250250
* @return A slice representing all characters of this string.
251251
*/
252252
[[nodiscard]]
253-
constexpr hud::slice<char_type> slice() const noexcept
253+
constexpr const hud::slice<char_type> as_slice() const noexcept
254+
{
255+
return hud::slice<char_type> {ptr_, length()};
256+
}
257+
258+
/**
259+
* Returns a slice view of the string.
260+
* @return A slice representing all characters of this string.
261+
*/
262+
[[nodiscard]]
263+
constexpr hud::slice<char_type> as_slice() noexcept
254264
{
255265
return hud::slice<char_type> {ptr_, length()};
256266
}

test/string/cstring_view.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,3 +563,45 @@ GTEST_TEST(cstring_view, to_lowercase_partial)
563563
v.to_lowercase_partial(2);
564564
hud_assert_true(txt[0] == 'a' && txt[1] == 'b' && txt[2] == 'C' && txt[3] == '1' && txt[4] == '2' && txt[5] == '3' && txt[6] == ',' && txt[7] == ';' && txt[8] == ':' && txt[9] == '!' && txt[10] == '\0');
565565
}
566+
567+
GTEST_TEST(cstring_view, slice)
568+
{
569+
hud::cstring_view const_v {"ABC"};
570+
auto const_s = const_v.as_slice();
571+
auto const_begin = const_s.begin();
572+
hud_assert_true(*const_begin == 'A');
573+
const_begin++;
574+
hud_assert_true(*const_begin == 'B');
575+
const_begin++;
576+
hud_assert_true(*const_begin == 'C');
577+
const_begin++;
578+
hud_assert_true(const_begin == const_s.end());
579+
580+
ansichar txt[] = "ABC";
581+
hud::cstring_view v {txt};
582+
auto s = v.as_slice();
583+
auto begin = s.begin();
584+
hud_assert_true(*begin == 'A');
585+
begin++;
586+
hud_assert_true(*begin == 'B');
587+
begin++;
588+
hud_assert_true(*begin == 'C');
589+
begin++;
590+
hud_assert_true(begin == s.end());
591+
592+
auto const_s_1 = v.as_slice().sub_slice(0, 2);
593+
auto const_begin_1 = const_s_1.begin();
594+
hud_assert_true(*const_begin_1 == 'A');
595+
const_begin_1++;
596+
hud_assert_true(*const_begin_1 == 'B');
597+
const_begin_1++;
598+
hud_assert_true(const_begin_1 == const_s_1.end());
599+
600+
s = v.as_slice().sub_slice(0, 2);
601+
begin = s.begin();
602+
hud_assert_true(*begin == 'A');
603+
begin++;
604+
hud_assert_true(*begin == 'B');
605+
begin++;
606+
hud_assert_true(begin == s.end());
607+
}

test/string/wstring_view.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,4 +562,46 @@ GTEST_TEST(wstring_view, to_lowercase_partial)
562562
hud::wstring_view v(txt);
563563
v.to_lowercase_partial(2);
564564
hud_assert_true(txt[0] == L'a' && txt[1] == L'b' && txt[2] == L'C' && txt[3] == L'1' && txt[4] == L'2' && txt[5] == L'3' && txt[6] == L',' && txt[7] == L';' && txt[8] == L':' && txt[9] == L'!' && txt[10] == '\0');
565+
}
566+
567+
GTEST_TEST(wstring_view, slice)
568+
{
569+
hud::wstring_view const_v {L"ABC"};
570+
auto const_s = const_v.as_slice();
571+
auto const_begin = const_s.begin();
572+
hud_assert_true(*const_begin == L'A');
573+
const_begin++;
574+
hud_assert_true(*const_begin == L'B');
575+
const_begin++;
576+
hud_assert_true(*const_begin == L'C');
577+
const_begin++;
578+
hud_assert_true(const_begin == const_s.end());
579+
580+
wchar txt[] = L"ABC";
581+
hud::wstring_view v {txt};
582+
auto s = v.as_slice();
583+
auto begin = s.begin();
584+
hud_assert_true(*begin == L'A');
585+
begin++;
586+
hud_assert_true(*begin == L'B');
587+
begin++;
588+
hud_assert_true(*begin == L'C');
589+
begin++;
590+
hud_assert_true(begin == s.end());
591+
592+
auto const_s_1 = v.as_slice().sub_slice(0, 2);
593+
auto const_begin_1 = const_s_1.begin();
594+
hud_assert_true(*const_begin_1 == L'A');
595+
const_begin_1++;
596+
hud_assert_true(*const_begin_1 == L'B');
597+
const_begin_1++;
598+
hud_assert_true(const_begin_1 == const_s_1.end());
599+
600+
s = v.as_slice().sub_slice(0, 2);
601+
begin = s.begin();
602+
hud_assert_true(*begin == L'A');
603+
begin++;
604+
hud_assert_true(*begin == L'B');
605+
begin++;
606+
hud_assert_true(begin == s.end());
565607
}

0 commit comments

Comments
 (0)