246 lines
8.0 KiB
C++
246 lines
8.0 KiB
C++
#pragma once
|
|
|
|
#include <cstddef>
|
|
#include <cstring>
|
|
#include <iterator>
|
|
|
|
#include "utility.hpp"
|
|
#include "string_view.hpp"
|
|
|
|
namespace mtl {
|
|
|
|
/**
|
|
* \brief Generic string interface
|
|
*
|
|
* Interface for mtl::string and mtl::string_ext.
|
|
* Implements most functions from std::string, see std::string documentation
|
|
* for function descriptions. Some operations are unimplemented due to the
|
|
* lack of dynamic memory usage.
|
|
*/
|
|
class istring {
|
|
protected:
|
|
char* m_str;
|
|
size_t m_size;
|
|
size_t m_capacity;
|
|
|
|
public:
|
|
typedef char* iterator;
|
|
typedef const char* const_iterator;
|
|
typedef std::reverse_iterator<iterator> reverse_iterator;
|
|
typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
|
|
|
|
static constexpr const size_t npos = -1;
|
|
|
|
istring(char* _str, size_t _capacity, size_t _size);
|
|
|
|
// Data functions
|
|
|
|
// If the new string does not fit, it will be truncated to the capacity
|
|
istring& assign(const istring& str);
|
|
istring& assign(const istring& str, size_t pos, size_t count = npos);
|
|
istring& assign(const string_view& str);
|
|
istring& assign(const string_view& str, size_t pos, size_t count = npos);
|
|
istring& assign(size_t count, char ch);
|
|
template <typename T>
|
|
istring& assign(T first, T last) {
|
|
m_size = 0;
|
|
T it = first;
|
|
while (it != last && m_size < m_capacity) {
|
|
m_str[m_size] = *it;
|
|
++m_size;
|
|
++it;
|
|
}
|
|
|
|
return *this;
|
|
}
|
|
|
|
istring& operator=(const istring& rhs);
|
|
istring& operator=(const string_view& rhs);
|
|
istring& operator=(char rhs);
|
|
|
|
// Element access functions
|
|
|
|
char& at(size_t i);
|
|
const char& at(size_t i) const;
|
|
|
|
char& operator[](size_t i);
|
|
const char& operator[](size_t i) const;
|
|
|
|
char& front();
|
|
const char& front() const;
|
|
|
|
char& back();
|
|
const char& back() const;
|
|
|
|
char* data();
|
|
const char* data() const;
|
|
const char* c_str() const;
|
|
|
|
// Iterator functions
|
|
|
|
iterator begin();
|
|
const_iterator begin() const;
|
|
const_iterator cbegin() const;
|
|
|
|
iterator end();
|
|
const_iterator end() const;
|
|
const_iterator cend() const;
|
|
|
|
reverse_iterator rbegin();
|
|
const_reverse_iterator rbegin() const;
|
|
const_reverse_iterator crbegin() const;
|
|
|
|
reverse_iterator rend();
|
|
const_reverse_iterator rend() const;
|
|
const_reverse_iterator crend() const;
|
|
|
|
// Capacity
|
|
|
|
bool empty() const;
|
|
|
|
size_t size() const;
|
|
size_t length() const;
|
|
|
|
size_t capacity() const;
|
|
|
|
// Modifiers
|
|
|
|
void clear();
|
|
|
|
// Insert from self only works if index is after input range
|
|
istring& insert(size_t index, const istring& str);
|
|
istring& insert(size_t index, const istring& str, size_t s_index, size_t count = npos);
|
|
istring& insert(size_t index, const string_view& str);
|
|
istring& insert(size_t index, const string_view& str, size_t str_index, size_t count = npos);
|
|
istring& insert(size_t index, size_t count, char ch);
|
|
iterator insert(const_iterator pos, char ch);
|
|
iterator insert(const_iterator pos, size_t count, char ch);
|
|
template <typename T>
|
|
iterator insert(const_iterator pos, T first, T last) {
|
|
}
|
|
|
|
istring& erase(size_t index = 0, size_t count = npos);
|
|
iterator erase(const_iterator pos);
|
|
iterator erase(const_iterator first, const_iterator last);
|
|
|
|
void push_back(char ch);
|
|
void pop_back();
|
|
|
|
istring& append(const istring& str);
|
|
istring& append(const istring& str, size_t s_index, size_t count = npos);
|
|
istring& append(const string_view& str);
|
|
istring& append(const string_view& str, size_t str_index, size_t count = npos);
|
|
istring& append(size_t count, char ch);
|
|
template <typename T>
|
|
istring& append(T first, T last) {
|
|
}
|
|
|
|
istring& operator+=(char ch);
|
|
istring& operator+=(const string_view& str);
|
|
istring& operator+=(const istring& str);
|
|
|
|
istring& replace(size_t pos, size_t count, const istring& str);
|
|
istring& replace(const_iterator first, const_iterator last, const istring& str);
|
|
istring& replace(size_t pos, size_t count, const istring& str, size_t pos2, size_t count2 = npos);
|
|
istring& replace(size_t pos, size_t count, const string_view& str, size_t pos2, size_t count2 = npos);
|
|
istring& replace(const_iterator first, const_iterator last, const string_view& str, size_t pos2, size_t count2 = npos);
|
|
istring& replace(size_t pos, size_t count, const string_view& str);
|
|
istring& replace(const_iterator first, const_iterator last, const string_view& str);
|
|
istring& replace(size_t pos, size_t count, size_t count2, char ch);
|
|
istring& replace(const_iterator first, const_iterator last, size_t count2, char ch);
|
|
template <typename T>
|
|
istring& replace(const_iterator first, const_iterator last, T first2, T last2);
|
|
|
|
/**
|
|
* \brief Copy the string to a buffer
|
|
*
|
|
* Copies the string to the buffer pointed to by dest. Will always
|
|
* copy the string from front to back, never in reverse. The strings
|
|
* must not overlap.
|
|
*/
|
|
size_t copy(char* dest, size_t count = npos, size_t pos = 0) const;
|
|
|
|
void resize(size_t count);
|
|
void resize(size_t count, char ch);
|
|
|
|
// Search
|
|
|
|
size_t find(const istring& str, size_t pos = 0) const;
|
|
size_t find(const string_view& str, size_t pos = 0) const;
|
|
size_t find(char ch, size_t pos = 0) const;
|
|
|
|
size_t rfind(const istring& str, size_t pos = npos) const;
|
|
size_t rfind(const string_view& str, size_t pos = npos) const;
|
|
size_t rfind(char ch, size_t pos = npos) const;
|
|
|
|
size_t find_first_of(const istring& str, size_t pos = 0) const;
|
|
size_t find_first_of(const string_view& str, size_t pos = 0) const;
|
|
size_t find_first_of(char ch, size_t pos = 0) const;
|
|
|
|
size_t find_first_not_of(const istring& str, size_t pos = 0) const;
|
|
size_t find_first_not_of(const string_view& str, size_t pos = 0) const;
|
|
size_t find_first_not_of(char ch, size_t pos = 0) const;
|
|
|
|
size_t find_last_of(const istring& str, size_t pos = npos) const;
|
|
size_t find_last_of(const string_view& str, size_t pos = npos) const;
|
|
size_t find_last_of(char ch, size_t pos = npos) const;
|
|
|
|
size_t find_last_not_of(const istring& str, size_t pos = npos) const;
|
|
size_t find_last_not_of(const string_view& str, size_t pos = npos) const;
|
|
size_t find_last_not_of(char ch, size_t pos = npos) const;
|
|
|
|
// Comparison
|
|
|
|
int32_t compare(const istring& str) const;
|
|
int32_t compare(size_t pos, size_t count, const istring& str) const;
|
|
int32_t compare(size_t pos1, size_t count1, istring& str, size_t pos2, size_t count2 = npos) const;
|
|
int32_t compare(const string_view& str) const;
|
|
int32_t compare(size_t pos, size_t count, const string_view& str) const;
|
|
int32_t compare(size_t pos1, size_t count1, const string_view& str, size_t pos2, size_t count2 = npos) const;
|
|
|
|
friend bool operator==(const istring& lhs, const istring& rhs);
|
|
friend bool operator!=(const istring& lhs, const istring& rhs);
|
|
friend bool operator<(const istring& lhs, const istring& rhs);
|
|
friend bool operator<=(const istring& lhs, const istring& rhs);
|
|
friend bool operator>(const istring& lhs, const istring& rhs);
|
|
friend bool operator>=(const istring& lhs, const istring& rhs);
|
|
|
|
friend bool operator==(const istring& lhs, const string_view& rhs);
|
|
friend bool operator==(const string_view& lhs, const istring& rhs);
|
|
friend bool operator!=(const istring& lhs, const string_view& rhs);
|
|
friend bool operator!=(const string_view& lhs, const istring& rhs);
|
|
friend bool operator<(const istring& lhs, const string_view& rhs);
|
|
friend bool operator<(const string_view& lhs, const istring& rhs);
|
|
friend bool operator<=(const istring& lhs, const string_view& rhs);
|
|
friend bool operator<=(const string_view& lhs, const istring& rhs);
|
|
friend bool operator>(const istring& lhs, const string_view& rhs);
|
|
friend bool operator>(const string_view& lhs, const istring& rhs);
|
|
friend bool operator>=(const istring& lhs, const string_view& rhs);
|
|
friend bool operator>=(const string_view& lhs, const istring& rhs);
|
|
};
|
|
|
|
template <size_t C>
|
|
class string : public istring {
|
|
private:
|
|
char m_buf[C + 1];
|
|
|
|
public:
|
|
string() : istring(m_buf, C, 0) {}
|
|
string(const string_view& str) : istring(m_buf, C, 0) {
|
|
m_size = str.length();
|
|
if (m_size > C) { m_size = m_capacity; }
|
|
|
|
mtl::memcpy(m_buf, str.c_str(), m_size);
|
|
m_str[m_size] = '\0';
|
|
}
|
|
};
|
|
|
|
class string_ext : public istring {
|
|
public:
|
|
string_ext(char* buf, size_t buf_size);
|
|
string_ext(const char&) = delete;
|
|
|
|
using istring::operator=;
|
|
};
|
|
} // namespace ml
|