Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add operator to convert frozen::string to std::string view (with c++17 and more) #180

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 29 additions & 5 deletions include/frozen/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,40 @@ class basic_string {
constexpr basic_string(chr_t const *data, std::size_t size)
: data_(data), size_(size) {}

#ifdef FROZEN_LETITGO_HAS_STRING_VIEW
constexpr basic_string(std::basic_string_view<chr_t> data)
: data_(data.data()), size_(data.size()) {}
#endif

constexpr basic_string(const basic_string &) noexcept = default;
constexpr basic_string &operator=(const basic_string &) noexcept = default;

constexpr std::size_t length() const { return size_; }
constexpr std::size_t size() const { return size_; }

constexpr chr_t operator[](std::size_t i) const { return data_[i]; }

#ifdef FROZEN_LETITGO_HAS_STRING_VIEW
constexpr basic_string(std::basic_string_view<chr_t> data)
: data_(data.data()), size_(data.size()) {}

constexpr operator std::basic_string_view<chr_t>() const {
return std::basic_string_view<chr_t>(data_, size_);
}

constexpr bool operator==(std::basic_string_view<chr_t> other) const {
return (std::basic_string_view<chr_t>(data_, size_) == other);
}

constexpr bool operator<(const std::basic_string_view<chr_t> &other) const {
return (std::basic_string_view<chr_t>(data_, size_) < other);
}

constexpr bool operator==(basic_string other) const {
return (std::basic_string_view<chr_t>(data_, size_) == std::basic_string_view<chr_t>(other.data_, other.size_));
}

constexpr bool operator<(const basic_string &other) const {
return (std::basic_string_view<chr_t>(data_, size_) < std::basic_string_view<chr_t>(other.data_, other.size_));
}

#else

constexpr bool operator==(basic_string other) const {
if (size_ != other.size_)
return false;
Expand All @@ -86,6 +108,8 @@ class basic_string {
return size() < other.size();
}

#endif

friend constexpr bool operator>(const basic_string& lhs, const basic_string& rhs) {
return rhs < lhs;
}
Expand Down