Skip to content

Commit 962e6cf

Browse files
committed
core: make StaticString a valid iterator
1 parent 9f5f2cf commit 962e6cf

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

src/libs/core/helper/static_string.hpp

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#pragma once
22

33
#include "./types.hpp"
4+
#include "./utils.hpp"
45

56
#include <algorithm>
67
#include <array>
@@ -29,19 +30,29 @@ struct StaticString {
2930
return size();
3031
}
3132

32-
[[nodiscard]] constexpr char* begin() {
33+
// iterator trait
34+
using iterator = char * ; //NOLINT(readability-identifier-naming)
35+
using const_iterator = const char*; //NOLINT(readability-identifier-naming)
36+
using difference_type = std::ptrdiff_t; //NOLINT(readability-identifier-naming)
37+
using value_type = char; //NOLINT(readability-identifier-naming)
38+
using pointer = char * ; //NOLINT(readability-identifier-naming)
39+
using reference = char&; //NOLINT(readability-identifier-naming)
40+
using iterator_category = std::bidirectional_iterator_tag; //NOLINT(readability-identifier-naming)
41+
42+
43+
[[nodiscard]] constexpr iterator begin() {
3344
return m_data.data();
3445
}
3546

36-
[[nodiscard]] constexpr char* end() {
37-
return begin() + length();
47+
[[nodiscard]] constexpr const_iterator begin() const {
48+
return m_data.data();
3849
}
3950

40-
[[nodiscard]] constexpr const char* begin() const {
41-
return m_data.data();
51+
[[nodiscard]] constexpr iterator end() {
52+
return begin() + length();
4253
}
4354

44-
[[nodiscard]] constexpr const char* end() const {
55+
[[nodiscard]] constexpr const_iterator end() const {
4556
return m_data.data() + size();
4657
}
4758

@@ -132,3 +143,5 @@ struct StaticString {
132143
template<usize other_data_size>
133144
friend struct StaticString;
134145
};
146+
147+
STATIC_ASSERT_WITH_MESSAGE(utils::IsIterator<StaticString<3>>::value, "StaticString<T> has to be an iterator");

0 commit comments

Comments
 (0)