clean-up cstring

This commit is contained in:
Neargye 2018-08-28 21:45:26 +05:00
parent 5cddcb0099
commit 9e2272f109

View file

@ -33,6 +33,7 @@
#include <cstddef> #include <cstddef>
#include <type_traits> #include <type_traits>
#include <limits> #include <limits>
#include <stdexcept>
#include <string> #include <string>
#include <ostream> #include <ostream>
@ -148,38 +149,21 @@ class cstring final {
constexpr const char* data() const noexcept { return str_; } constexpr const char* data() const noexcept { return str_; }
constexpr cstring remove_prefix(std::size_t n) const { constexpr cstring remove_prefix(std::size_t n) const { return {str_ + n, size_ - n}; }
return {str_ + n, size_ - n};
}
constexpr cstring add_prefix(std::size_t n) const { constexpr cstring add_prefix(std::size_t n) const { return {str_ - n, size_ + n}; }
return {str_ - n, size_ + n};
}
constexpr cstring remove_suffix(std::size_t n) const { constexpr cstring remove_suffix(std::size_t n) const { return {str_, size_ - n}; }
return {str_, size_ - n};
}
constexpr cstring add_suffix(std::size_t n) const { constexpr cstring add_suffix(std::size_t n) const { return {str_, size_ + n}; }
return {str_, size_ + n};
}
constexpr cstring substr(std::size_t pos, std::size_t n) const { constexpr cstring substr(std::size_t pos, std::size_t n) const { return {str_ + pos, n}; }
return {str_ + pos, n};
}
int compare(cstring s) const { int compare(cstring other) const {
auto result = std::char_traits<char>::compare(str_, s.str_, s.size_ < size_ ? s.size_ : size_); if (const auto result = std::char_traits<char>::compare(str_, other.str_, other.size_ < size_ ? other.size_ : size_))
if (result != 0) {
return result; return result;
}
if (size_ < s.size_) { return size_ == other.size_ ? 0 : size_ < other.size_ ? -1 : 1;
return -1;
}
if (size_ > s.size_) {
return 1;
}
return 0;
} }
friend constexpr bool operator==(cstring lhs, cstring rhs) noexcept { friend constexpr bool operator==(cstring lhs, cstring rhs) noexcept {