diff options
Diffstat (limited to 'lib/nes/common')
| -rw-r--r-- | lib/nes/common/containers/string-view.cc | 56 | ||||
| -rw-r--r-- | lib/nes/common/containers/string-view.hh | 11 | ||||
| -rw-r--r-- | lib/nes/common/status.hh | 5 |
3 files changed, 61 insertions, 11 deletions
diff --git a/lib/nes/common/containers/string-view.cc b/lib/nes/common/containers/string-view.cc index 8265d09..e3470c0 100644 --- a/lib/nes/common/containers/string-view.cc +++ b/lib/nes/common/containers/string-view.cc @@ -15,6 +15,16 @@ namespace nes } return res; } + + auto to_uppercase(char const c) -> char + { + if (c >= 'a' && c <= 'z') + { + return static_cast<char>(c - 'a' + 'A'); + } + + return c; + } } // namespace string_view::string_view(span<char const> const data) @@ -44,6 +54,28 @@ namespace nes return data_[data_.get_length() - 1]; } + auto string_view::compare(string_view const other, case_sensitive const sensitive) const -> bool + { + if (get_length() != other.get_length()) { return false; } + + for (auto i = u32{ 0 }; i < get_length(); ++i) + { + auto const a = (*this)[i]; + auto const b = other[i]; + + if (sensitive == case_sensitive::yes) + { + if (a != b) { return false; } + } + else + { + if (to_uppercase(a) != to_uppercase(b)) { return false; } + } + } + + return true; + } + auto string_view::contains(char const needle) const -> bool { for (const auto c : *this) @@ -63,19 +95,25 @@ namespace nes return string_view{ data_.subspan(first, length) }; } - auto operator==(string_view const lhs, string_view const rhs) -> bool + auto string_view::has_prefix(string_view const prefix, case_sensitive const case_sensitive) const -> bool { - if (lhs.get_length() != rhs.get_length()) { return false; } + if (prefix.get_length() > get_length()) { return false; } + return substring(0, prefix.get_length()).compare(prefix, case_sensitive); + } - for (auto i = u32{ 0 }; i < lhs.get_length(); ++i) - { - if (lhs[i] != rhs[i]) { return false; } - } + auto string_view::has_suffix(string_view const suffix, case_sensitive const case_sensitive) const -> bool + { + if (suffix.get_length() > get_length()) { return false; } + return substring(get_length() - suffix.get_length()).compare(suffix, case_sensitive); + } - return true; + auto operator==(string_view const lhs, string_view const rhs) -> bool + { + return lhs.compare(rhs); } + auto operator!=(string_view const lhs, string_view const rhs) -> bool { - return !(lhs == rhs); + return !lhs.compare(rhs); } -} // namespace nes
\ No newline at end of file +} // namespace nes diff --git a/lib/nes/common/containers/string-view.hh b/lib/nes/common/containers/string-view.hh index 9bd27ae..858a858 100644 --- a/lib/nes/common/containers/string-view.hh +++ b/lib/nes/common/containers/string-view.hh @@ -5,6 +5,12 @@ namespace nes { + enum class case_sensitive + { + yes, + no, + }; + /// Non-owning view into a (not necessarily null-terminated) string. class string_view { @@ -30,11 +36,14 @@ namespace nes auto get_first() const -> char; auto get_last() const -> char; + auto compare(string_view, case_sensitive = case_sensitive::yes) const -> bool; auto contains(char) const -> bool; auto substring(u32 first) const -> string_view; auto substring(u32 first, u32 length) const -> string_view; + auto has_prefix(string_view, case_sensitive = case_sensitive::yes) const -> bool; + auto has_suffix(string_view, case_sensitive = case_sensitive::yes) const -> bool; }; auto operator==(string_view, string_view) -> bool; auto operator!=(string_view, string_view) -> bool; -} // namespace nes
\ No newline at end of file +} // namespace nes diff --git a/lib/nes/common/status.hh b/lib/nes/common/status.hh index 4b23b60..54c210f 100644 --- a/lib/nes/common/status.hh +++ b/lib/nes/common/status.hh @@ -13,6 +13,7 @@ namespace nes error_buffer_overflow, error_invalid_path, error_invalid_format_string, + error_unknown_file_type, }; constexpr auto to_string(status const status) -> char const* @@ -35,8 +36,10 @@ namespace nes return "Invalid path"; case status::error_invalid_format_string: return "Invalid format string"; + case status::error_unknown_file_type: + return "Unknown file type"; } return "(invalid)"; } -} // namespace nes
\ No newline at end of file +} // namespace nes |
