mtl/include/mtl/string_view.hpp

110 lines
4.2 KiB
C++

#pragma once
#include <cstddef>
#include <cstring>
#include <string> // std::char_traits
namespace mtl {
class istring;
constexpr size_t strlen(const char* str) {
return *str != '\0' ? strlen(str + 1) + 1 : 0;
}
class string_view {
const char* m_str;
size_t m_size;
friend class istring;
public:
static constexpr size_t npos = -1;
constexpr string_view(const char* str) : m_str(str), m_size(std::char_traits<char>::length(str)) {}
constexpr string_view(const char* str, size_t size) : m_str(str), m_size(size) {}
string_view() : m_str(nullptr), m_size(0) {}
string_view(const istring& str);
string_view(const istring& str, size_t size);
constexpr const char& at(size_t i) const { return m_str[i]; }
constexpr const char& operator[](size_t i) const { return m_str[i]; }
constexpr const char& front() const { return m_str[0]; }
constexpr const char& back() const { return m_str[m_size - 1]; }
constexpr const char* data() const { return m_str; }
constexpr const char* c_str() const { return m_str; }
constexpr bool empty() const { return m_size == 0; }
constexpr size_t size() const { return m_size; }
constexpr size_t length() const { return m_size; }
/**
* \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;
// 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&, 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);
};
} // namespace mtl