summaryrefslogtreecommitdiff
path: root/lib/nes/common/containers/string-view.hh
diff options
context:
space:
mode:
authorHannes Schulze <haschu0103@outlook.de>2025-02-06 19:54:38 +0100
committerHannes Schulze <haschu0103@outlook.de>2025-02-06 19:54:38 +0100
commit64ebfde8bcd6408e3b6925624e8680c314db6efc (patch)
treeb969314f909f7f16ef5acc63f90dca317a40850a /lib/nes/common/containers/string-view.hh
parentd060e3a50c987d10799ccc739bdbf9d7d7a13c10 (diff)
get rid of nasty stdlib
Diffstat (limited to 'lib/nes/common/containers/string-view.hh')
-rw-r--r--lib/nes/common/containers/string-view.hh40
1 files changed, 40 insertions, 0 deletions
diff --git a/lib/nes/common/containers/string-view.hh b/lib/nes/common/containers/string-view.hh
new file mode 100644
index 0000000..180a688
--- /dev/null
+++ b/lib/nes/common/containers/string-view.hh
@@ -0,0 +1,40 @@
+#pragma once
+
+#include "nes/common/containers/span.hh"
+#include "nes/common/types.hh"
+
+namespace nes
+{
+ /// Non-owning view into a (not necessarily null-terminated) string.
+ class string_view
+ {
+ span<char const> data_;
+
+ public:
+ explicit string_view() = default;
+ explicit string_view(span<char const>);
+ explicit string_view(char const* data, u32 length);
+ string_view(char const* c_string);
+
+ using iterator = span<char const>::iterator;
+
+ auto operator[](u32 const index) const -> char { return data_[index]; }
+ auto get_span() const -> span<char const> { return data_; }
+ auto get_data() const -> char const* { return data_.get_data(); }
+ auto get_length() const -> u32 { return data_.get_length(); }
+ auto is_empty() const -> bool { return get_length() == 0; }
+ auto begin() const -> iterator { return data_.begin(); }
+ auto end() const -> iterator { return data_.end(); }
+
+ // String helpers
+
+ auto get_first() const -> char;
+ auto get_last() const -> char;
+ auto contains(char) const -> bool;
+ auto substring(u32 first) const -> string_view;
+ auto substring(u32 first, u32 length) const -> string_view;
+ };
+
+ auto operator==(string_view, string_view) -> bool;
+ auto operator!=(string_view, string_view) -> bool;
+} // namespace nes \ No newline at end of file