summaryrefslogtreecommitdiff
path: root/lib/nes/common/containers/path-view.hh
diff options
context:
space:
mode:
authorHannes Schulze <haschu0103@outlook.de>2025-02-06 16:34:23 +0100
committerHannes Schulze <haschu0103@outlook.de>2025-02-06 16:34:23 +0100
commitd1b61b02b1a6bdaa337ff060eda3d3d5fb67827d (patch)
treede3b1d7f7907e9fc786a1b7491b9bca9b9d296b0 /lib/nes/common/containers/path-view.hh
parent30b4364c772114303d9045677186a53855ea5343 (diff)
refactor: move containers into separate directory
Diffstat (limited to 'lib/nes/common/containers/path-view.hh')
-rw-r--r--lib/nes/common/containers/path-view.hh52
1 files changed, 52 insertions, 0 deletions
diff --git a/lib/nes/common/containers/path-view.hh b/lib/nes/common/containers/path-view.hh
new file mode 100644
index 0000000..7741060
--- /dev/null
+++ b/lib/nes/common/containers/path-view.hh
@@ -0,0 +1,52 @@
+#pragma once
+
+#include "nes/common/types.hh"
+#include <string_view>
+
+namespace nes
+{
+ /// Non-owning copy of a path, useful for iterating over its components.
+ class path_view
+ {
+ std::string_view path_;
+
+ public:
+ /// An iterator for the path components.
+ class iterator
+ {
+ friend path_view;
+
+ char const* current_{ nullptr }; // Start of the current component
+ u32 component_length_{ 0 }; // Length of the current component
+ u32 remaining_{ 0 }; // Number of characters remaining in the buffer, including the current component.
+
+ public:
+ auto operator==(iterator const& other) -> bool { return current_ == other.current_; }
+ auto operator!=(iterator const& other) -> bool { return current_ != other.current_; }
+
+ auto operator*() const -> std::string_view;
+ auto operator++() -> iterator&;
+ auto operator++(int) -> iterator;
+
+ private:
+ explicit iterator(char const* current, u32 const component_length, u32 const remaining)
+ : current_{ current }
+ , component_length_{ component_length }
+ , remaining_{ remaining }
+ {
+ }
+ };
+
+ explicit path_view() = default;
+ explicit path_view(std::string_view const path)
+ : path_{ path }
+ {
+ }
+
+ auto get_path() const -> std::string_view { return path_; }
+
+ auto begin() const -> iterator;
+ auto end() const -> iterator;
+ auto is_empty() const -> bool { return begin() == end(); }
+ };
+} // namespace nes::app \ No newline at end of file