[ox/std] Add begin and end methods to BasicString

This commit is contained in:
Gary Talent 2021-08-03 00:41:03 -05:00
parent 38596ac941
commit 853f8c25ea
2 changed files with 75 additions and 29 deletions

View File

@ -38,6 +38,46 @@ class BasicString {
constexpr BasicString(BasicString&&) noexcept;
[[nodiscard]]
constexpr auto begin() noexcept {
return m_buff.begin();
}
[[nodiscard]]
constexpr auto end() noexcept {
return m_buff.end();
}
[[nodiscard]]
constexpr auto cbegin() const noexcept {
return m_buff.cbegin();
}
[[nodiscard]]
constexpr auto cend() const noexcept {
return m_buff.cend();
}
[[nodiscard]]
constexpr auto rbegin() noexcept {
return m_buff.rbegin();
}
[[nodiscard]]
constexpr auto rend() noexcept {
return m_buff.rend();
}
[[nodiscard]]
constexpr auto crbegin() const noexcept {
return m_buff.crbegin();
}
[[nodiscard]]
constexpr auto crend() const noexcept {
return m_buff.crend();
}
BasicString &operator=(const char *str) noexcept;
BasicString &operator=(char *str) noexcept;
@ -210,37 +250,43 @@ BasicString<SmallStringSize> &BasicString<SmallStringSize>::operator=(const char
template<std::size_t SmallStringSize>
BasicString<SmallStringSize> &BasicString<SmallStringSize>::operator=(char *str) noexcept {
return *this = const_cast<const char*>(str);
*this = const_cast<const char*>(str);
return *this;
}
template<std::size_t SmallStringSize>
BasicString<SmallStringSize> &BasicString<SmallStringSize>::operator=(char c) noexcept {
char str[] = {c, 0};
return this->operator=(str);
this->operator=(str);
return *this;
}
template<std::size_t SmallStringSize>
BasicString<SmallStringSize> &BasicString<SmallStringSize>::operator=(int i) noexcept {
return this->operator=(static_cast<int64_t>(i));
this->operator=(static_cast<int64_t>(i));
return *this;
}
template<std::size_t SmallStringSize>
BasicString<SmallStringSize> &BasicString<SmallStringSize>::operator=(int64_t i) noexcept {
char str[65] = {};
ox_itoa(i, str);
return this->operator=(str);
this->operator=(str);
return *this;
}
template<std::size_t SmallStringSize>
BasicString<SmallStringSize> &BasicString<SmallStringSize>::operator=(uint64_t i) noexcept {
char str[65] = {};
ox_itoa(i, str);
return this->operator=(str);
this->operator=(str);
return *this;
}
template<std::size_t SmallStringSize>
BasicString<SmallStringSize> &BasicString<SmallStringSize>::operator=(const BasicString &src) noexcept {
return *this = src.c_str();
*this = src.c_str();
return *this;
}
template<std::size_t SmallStringSize>

View File

@ -102,15 +102,15 @@ class Vector: detail::SmallVector<T, SmallVectorSize> {
using value_type = T;
using size_type = std::size_t;
template<typename RefType, bool reverse = false>
template<typename RefType = T&, typename PtrType = T*, bool reverse = false>
struct iterator: public std::iterator<std::bidirectional_iterator_tag, T> {
private:
T *m_t = nullptr;
PtrType m_t = nullptr;
size_type m_offset = 0;
size_type m_max = 0;
public:
constexpr iterator(T *t, size_type offset, size_type max) {
constexpr iterator(PtrType t, size_type offset, size_type max) {
m_t = t;
m_offset = offset;
m_max = max;
@ -223,36 +223,36 @@ class Vector: detail::SmallVector<T, SmallVectorSize> {
~Vector();
constexpr iterator<T&> begin() const noexcept {
return iterator<T&>(m_items, 0, m_size);
constexpr iterator<> begin() const noexcept {
return iterator<>(m_items, 0, m_size);
}
constexpr iterator<T&> end() const noexcept {
return iterator<T&>(m_items, m_size, m_size);
constexpr iterator<> end() const noexcept {
return iterator<>(m_items, m_size, m_size);
}
constexpr iterator<const T&> cbegin() const noexcept {
return iterator<const T&>(m_items, 0, m_size);
constexpr iterator<const T&, const T*> cbegin() const noexcept {
return iterator<const T&, const T*>(m_items, 0, m_size);
}
constexpr iterator<const T&> cend() const noexcept {
return iterator<const T&>(m_items, m_size, m_size);
constexpr iterator<const T&, const T*> cend() const noexcept {
return iterator<const T&, const T*>(m_items, m_size, m_size);
}
constexpr iterator<T&, true> rbegin() const noexcept {
return iterator<T&, true>(m_items, 0, m_size);
constexpr iterator<T&, T*, true> rbegin() const noexcept {
return iterator<T&, T*, true>(m_items, m_size, m_size);
}
constexpr iterator<T&, true> rend() const noexcept {
return iterator<T&, true>(m_items, m_size, m_size);
constexpr iterator<T&, T*, true> rend() const noexcept {
return iterator<T&, T*, true>(m_items, 0, m_size);
}
constexpr iterator<const T&, true> crbegin() const noexcept {
return iterator<const T&, true>(m_items, 0, m_size);
constexpr iterator<const T&, const T*, true> crbegin() const noexcept {
return iterator<const T&, const T*, true>(m_items, m_size, m_size);
}
constexpr iterator<const T&, true> crend() const noexcept {
return iterator<const T&, true>(m_items, m_size, m_size);
constexpr iterator<const T&, const T*, true> crend() const noexcept {
return iterator<const T&, const T*, true>(m_items, 0, m_size);
}
bool operator==(const Vector &other) const;
@ -310,8 +310,8 @@ class Vector: detail::SmallVector<T, SmallVectorSize> {
* @param pos iterator at the point to remove
* @return Error if index is out of bounds
*/
template<typename RefType, bool reverse>
Error erase(const iterator<RefType, reverse> &pos);
template<typename RefType, typename RefPtr, bool reverse>
Error erase(const iterator<RefType, RefPtr, reverse> &pos);
/**
* Removes an item from the Vector.
@ -564,8 +564,8 @@ void Vector<T, SmallVectorSize>::pop_back() {
}
template<typename T, std::size_t SmallVectorSize>
template<typename RefType, bool reverse>
Error Vector<T, SmallVectorSize>::erase(const iterator<RefType, reverse> &pos) {
template<typename RefType, typename RefPtr, bool reverse>
Error Vector<T, SmallVectorSize>::erase(const iterator<RefType, RefPtr, reverse> &pos) {
return erase(pos.offset());
}