matrix_thing/include/raii/string_base.tpp

221 lines
7.0 KiB
C++

/**
This file is a part of rexy's matrix client
Copyright (C) 2019 rexy712
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef RAII_STRING_BASE_TPP
#define RAII_STRING_BASE_TPP
#include <utility> //forward, move, swap, etc
#include <cstdlib> //memcpy
#include <cstring> //strlen, strcpy
namespace raii{
template<class Allocator>
string_intermediary<Allocator>::string_intermediary(char* data, size_t len):
string_base(data, len){}
template<class Allocator>
string_intermediary<Allocator>::string_intermediary(const char* data, size_t len):
string_base(reinterpret_cast<char*>(len ? Allocator::copy(data, len+1) : nullptr), len)
{
m_data[len] = 0;
}
template<class Allocator>
string_intermediary<Allocator>::string_intermediary(const char* data):
string_base(data ? strlen(data) : 0)
{
if(m_length)
m_data = reinterpret_cast<char*>(Allocator::copy(data, m_length+1));
}
template<class Allocator>
string_intermediary<Allocator>::string_intermediary(size_t len):
string_base(reinterpret_cast<char*>(len ? Allocator::allocate(len+1) : nullptr), len)
{
m_data[len] = 0;
}
//normal copy and move ctors
template<class Allocator>
string_intermediary<Allocator>::string_intermediary(const string_intermediary& b):
string_base(reinterpret_cast<char*>(b.m_length ? Allocator::copy(b.m_data, b.m_length+1) : nullptr), b.m_length){}
template<class Allocator>
string_intermediary<Allocator>::string_intermediary(string_intermediary&& s):
string_base(std::exchange(s.m_data, nullptr), s.m_length){}
template<class Allocator>
string_intermediary<Allocator>::string_intermediary(const string_base& b):
string_base(reinterpret_cast<char*>(b.length() ? Allocator::copy(b.get(), b.length()+1) : nullptr), b.length()){}
//dtor
template<class Allocator>
string_intermediary<Allocator>::~string_intermediary(void){
Allocator::free(m_data);
}
template<class Allocator>
string_intermediary<Allocator>& string_intermediary<Allocator>::operator=(const string_intermediary& s){
string_intermediary tmp(s);
return (*this = std::move(tmp));
}
template<class Allocator>
string_intermediary<Allocator>& string_intermediary<Allocator>::operator=(string_intermediary&& s){
std::swap(m_data, s.m_data);
m_length = s.m_length;
return *this;
}
//Copy from c string
template<class Allocator>
string_intermediary<Allocator>& string_intermediary<Allocator>::operator=(const char* c){
return _copy_string(c, strlen(c));
}
//Copy from other string_base
template<class Allocator>
string_intermediary<Allocator>& string_intermediary<Allocator>::operator=(const string_base& s){
return _copy_string(s.get(), s.length());
}
//Replace managed pointer. Frees existing value
template<class Allocator>
void string_intermediary<Allocator>::reset(char* val){
Allocator::free(m_data);
m_data = val;
m_length = val ? strlen(val) : 0;
}
template<class Allocator>
void string_intermediary<Allocator>::reset(char* val, size_t len){
Allocator::free(m_data);
m_data = val;
m_length = len;
}
template<class Allocator>
bool string_intermediary<Allocator>::resize(size_t newsize){
if(newsize < m_length)
return false;
string_intermediary tmp(newsize);
memcpy(tmp.get(), m_data, m_length);
tmp[m_length] = 0;
*this = std::move(tmp);
return true;
}
template<class Allocator>
void string_intermediary<Allocator>::append(const char* data, size_t len){
string_intermediary tmp(m_length + len);
memcpy(tmp.m_data, m_data, m_length);
memcpy(tmp.m_data+m_length, data, len);
tmp[m_length+len] = 0;
*this = std::move(tmp);
}
template<class Allocator>
void string_intermediary<Allocator>::append(const char* data){
size_t len = strlen(data);
resize(m_length+len);
memcpy(m_data+m_length, data, len);
m_length += len;
m_data[m_length] = 0;
}
template<class Allocator>
void string_intermediary<Allocator>::append(const string_base& s){
append(s.get());
}
template<class Allocator>
string_intermediary<Allocator>& string_intermediary<Allocator>::_copy_string(const char* s, size_t len){
if(!len){
Allocator::free(m_data);
m_length = 0;
return *this;
}
if(len <= m_length){
strcpy(m_data, s);
}else{
Allocator::free(m_data);
m_data = reinterpret_cast<char*>(Allocator::copy(s, len+1));
if(!m_data){
m_length = 0;
return *this;
}
}
m_length = len;
return *this;
}
template<class Left, class Right>
template<class T, class U>
constexpr string_cat_expr<Left,Right>::string_cat_expr(T&& l, U&& r):
m_l(std::forward<T>(l)),
m_r(std::forward<U>(r)){}
template<class Left, class Right>
constexpr string_cat_expr<Left,Right>::string_cat_expr(const string_cat_expr& s):
m_l(std::forward<Left>(s.m_l)),
m_r(std::forward<Right>(s.m_r)){}
template<class Left, class Right>
constexpr string_cat_expr<Left,Right>::string_cat_expr(string_cat_expr&& s):
m_l(std::forward<Left>(s.m_l)),
m_r(std::forward<Right>(s.m_r)){}
template<class Left, class Right>
constexpr size_t string_cat_expr<Left,Right>::length(void)const{
return m_l.length() + m_r.length();
}
template<class Left, class Right>
template<class Alloc>
string_cat_expr<Left,Right>::operator string_intermediary<Alloc>(void){
string_intermediary<Alloc> ret(length());
for(size_t i = 0;i < length();++i)
ret[i] = 'n';
appender<string_intermediary<Alloc>> append(ret);
append(*this);
return ret;
}
template<class Left, class Right>
constexpr const Left& string_cat_expr<Left,Right>::left(void)const{
return m_l;
}
template<class Left, class Right>
constexpr const Right& string_cat_expr<Left,Right>::right(void)const{
return m_r;
}
template<class Targ>
appender<Targ>::appender(Targ& t): m_targ(t){}
template<class Targ>
template<class L, class R>
void appender<Targ>::operator()(const string_cat_expr<L,R>& str){
(*this)(str.left());
(*this)(str.right());
}
template<class Targ>
void appender<Targ>::operator()(const string_base& str){
memcpy(m_targ.get()+m_pos, str.get(), str.length());
m_pos += str.length();
}
template<size_t N>
constexpr static_string::static_string(const char(&str)[N]):
string_base(const_cast<char*>(str), N){}
constexpr static_string::static_string(const char* str, size_t len):
string_base(const_cast<char*>(str), len){}
constexpr static_string::static_string(const static_string& s):
string_base(s.m_data, s.m_length){}
constexpr static_string::static_string(static_string&& s):
string_base(s.m_data, s.m_length){}
} //namespace raii
#endif