Add member typedefs to cx::string
This commit is contained in:
parent
1648d96232
commit
8703d0089f
@ -20,13 +20,14 @@
|
||||
#define REXY_CX_STRING_HPP
|
||||
|
||||
namespace rexy::cx{
|
||||
template<size_t N>
|
||||
template<size_t N, class Char>
|
||||
class string;
|
||||
}
|
||||
|
||||
#include "../string_base.hpp"
|
||||
#include "utility.hpp"
|
||||
#include <type_traits>
|
||||
#include <cstddef> //ptrdiff_t
|
||||
|
||||
//This is different from rexy::static_string in that this doesn't hold a pointer to a constant string array.
|
||||
//This holds a mutable array of data which can be modified during compile time. static_string is
|
||||
@ -34,45 +35,56 @@ namespace rexy::cx{
|
||||
|
||||
namespace rexy::cx{
|
||||
|
||||
template<size_t N>
|
||||
template<size_t N, class Char = char>
|
||||
class string
|
||||
{
|
||||
public:
|
||||
using value_type = Char;
|
||||
using size_type = size_t;
|
||||
using difference_type = ptrdiff_t;
|
||||
using pointer = value_type*;
|
||||
using const_pointer = const value_type*;
|
||||
using reference = value_type&;
|
||||
using const_reference = const value_type&;
|
||||
using iterator = pointer;
|
||||
using const_iterator = const_pointer;
|
||||
|
||||
public:
|
||||
static constexpr size_t max_size = N;
|
||||
private:
|
||||
char m_data[N] = {};
|
||||
size_t m_length = 0;
|
||||
value_type m_data[N] = {};
|
||||
size_type m_length = 0;
|
||||
|
||||
public:
|
||||
constexpr string(void) = default;
|
||||
template<size_t M>
|
||||
template<size_type M>
|
||||
constexpr string(const char(&data)[M])noexcept:
|
||||
m_length(M)
|
||||
{
|
||||
static_assert(M <= N);
|
||||
for(size_t i = 0;i < M;++i){
|
||||
for(size_type i = 0;i < M;++i){
|
||||
m_data[i] = data[i];
|
||||
}
|
||||
}
|
||||
constexpr string(const char* data)noexcept:
|
||||
constexpr string(const_pointer data)noexcept:
|
||||
m_length(cx::strlen(data))
|
||||
{
|
||||
for(size_t i = 0;i < m_length;++i){
|
||||
for(size_type i = 0;i < m_length;++i){
|
||||
m_data[i] = data[i];
|
||||
}
|
||||
}
|
||||
constexpr string(const char* data, size_t len)noexcept:
|
||||
constexpr string(const_pointer data, size_type len)noexcept:
|
||||
m_length(len)
|
||||
{
|
||||
for(size_t i = 0;i < m_length;++i){
|
||||
for(size_type i = 0;i < m_length;++i){
|
||||
m_data[i] = data[i];
|
||||
}
|
||||
}
|
||||
|
||||
constexpr string(const rexy::static_string<char>& str)noexcept:
|
||||
constexpr string(const rexy::static_string<value_type>& str)noexcept:
|
||||
m_length(str.length())
|
||||
{
|
||||
for(size_t i = 0;i < m_length;++i){
|
||||
for(size_type i = 0;i < m_length;++i){
|
||||
m_data[i] = str[i];
|
||||
}
|
||||
}
|
||||
@ -80,7 +92,7 @@ namespace rexy::cx{
|
||||
constexpr string(const rexy::string_cat_expr<Left,Right>& expr)
|
||||
noexcept(std::is_nothrow_invocable<rexy::detail::string_appender<cx::string<N>>, decltype(expr)>::value)
|
||||
{
|
||||
rexy::detail::string_appender<cx::string<N>> append(*this);
|
||||
rexy::detail::string_appender<cx::string<N,value_type>> append(*this);
|
||||
append(expr);
|
||||
}
|
||||
|
||||
@ -88,9 +100,9 @@ namespace rexy::cx{
|
||||
constexpr string(string&&)noexcept = default;
|
||||
~string(void)noexcept = default;
|
||||
|
||||
constexpr string& operator=(const char* c)noexcept{
|
||||
constexpr string& operator=(const_pointer c)noexcept{
|
||||
m_length = cx::strlen(c);
|
||||
for(size_t i = 0;i < m_length;++i){
|
||||
for(size_type i = 0;i < m_length;++i){
|
||||
m_data[i] = c[i];
|
||||
}
|
||||
return *this;
|
||||
@ -99,42 +111,42 @@ namespace rexy::cx{
|
||||
constexpr string& operator=(string&&)noexcept = default;
|
||||
|
||||
|
||||
constexpr size_t length(void)const noexcept{
|
||||
constexpr size_type length(void)const noexcept{
|
||||
return m_length;
|
||||
}
|
||||
constexpr size_t capacity(void)const noexcept{
|
||||
constexpr size_type capacity(void)const noexcept{
|
||||
return max_size;
|
||||
}
|
||||
constexpr char* c_str(void)noexcept{
|
||||
constexpr pointer c_str(void)noexcept{
|
||||
return m_data;
|
||||
}
|
||||
constexpr const char* c_str(void)const noexcept{
|
||||
constexpr const_pointer c_str(void)const noexcept{
|
||||
return m_data;
|
||||
}
|
||||
constexpr char* get(void)noexcept{
|
||||
constexpr pointer get(void)noexcept{
|
||||
return m_data;
|
||||
}
|
||||
constexpr const char* get(void)const noexcept{
|
||||
constexpr const_pointer get(void)const noexcept{
|
||||
return m_data;
|
||||
}
|
||||
constexpr operator char*(void)noexcept{
|
||||
constexpr operator pointer(void)noexcept{
|
||||
return m_data;
|
||||
}
|
||||
constexpr operator const char*(void)const noexcept{
|
||||
constexpr operator const_pointer(void)const noexcept{
|
||||
return m_data;
|
||||
}
|
||||
|
||||
constexpr bool valid(void)const noexcept{
|
||||
return m_length > 0;
|
||||
}
|
||||
constexpr char& operator[](size_t i)noexcept{
|
||||
constexpr reference operator[](size_type i)noexcept{
|
||||
return m_data[i];
|
||||
}
|
||||
constexpr const char& operator[](size_t i)const noexcept{
|
||||
constexpr const_reference operator[](size_type i)const noexcept{
|
||||
return m_data[i];
|
||||
}
|
||||
|
||||
constexpr bool resize(size_t i)noexcept{
|
||||
constexpr bool resize(size_type i)noexcept{
|
||||
if(i >= capacity())
|
||||
return false;
|
||||
m_length = i;
|
||||
@ -142,18 +154,18 @@ namespace rexy::cx{
|
||||
return true;
|
||||
}
|
||||
|
||||
constexpr void append(const char* data, size_t len)noexcept{
|
||||
for(size_t i = 0;i < len;++i){
|
||||
constexpr void append(const_pointer data, size_type len)noexcept{
|
||||
for(size_type i = 0;i < len;++i){
|
||||
m_data[m_length++] = data[i];
|
||||
}
|
||||
}
|
||||
constexpr void append(const char* data)noexcept{
|
||||
constexpr void append(const_pointer data)noexcept{
|
||||
append(data, cx::strlen(data));
|
||||
}
|
||||
constexpr void append(const string& s)noexcept{
|
||||
append(s.get(), s.length());
|
||||
}
|
||||
constexpr void append(const rexy::static_string<char>& s)noexcept{
|
||||
constexpr void append(const rexy::static_string<value_type>& s)noexcept{
|
||||
append(s.get(), s.length());
|
||||
}
|
||||
};
|
||||
@ -163,8 +175,8 @@ namespace rexy::cx{
|
||||
struct is_cx_string_helper{
|
||||
static constexpr bool value = false;
|
||||
};
|
||||
template<size_t N>
|
||||
struct is_cx_string_helper<cx::string<N>>{
|
||||
template<size_t N, class Char>
|
||||
struct is_cx_string_helper<cx::string<N,Char>>{
|
||||
static constexpr bool value = true;
|
||||
};
|
||||
template<class... Ts>
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user