Overhaul string_cat_expr -> string_intermediary conversion. No longer need to convert fat_strings.cpp to c style string allocation
This commit is contained in:
parent
aa06b2d540
commit
7f4ffbf2ed
1
doc/TODO
1
doc/TODO
@ -4,7 +4,6 @@ general/other:
|
|||||||
1:cross platform matrix-send
|
1:cross platform matrix-send
|
||||||
|
|
||||||
matrix:
|
matrix:
|
||||||
8:fix fat_strings.cpp; use C style string allocation/catenation
|
|
||||||
4:string constant lookup tables
|
4:string constant lookup tables
|
||||||
session:
|
session:
|
||||||
7:server level queries in matrix::session
|
7:server level queries in matrix::session
|
||||||
|
|||||||
@ -20,7 +20,7 @@
|
|||||||
#define MATRIX_EVENT_HPP
|
#define MATRIX_EVENT_HPP
|
||||||
|
|
||||||
#include "raii/rjp_ptr.hpp"
|
#include "raii/rjp_ptr.hpp"
|
||||||
#include "raii/static_string.hpp"
|
#include "raii/string_base.hpp"
|
||||||
#include "raii/rjp_string.hpp"
|
#include "raii/rjp_string.hpp"
|
||||||
|
|
||||||
namespace matrix::sync{
|
namespace matrix::sync{
|
||||||
|
|||||||
@ -20,8 +20,8 @@
|
|||||||
#define MATRIX_ITERABLE_HPP
|
#define MATRIX_ITERABLE_HPP
|
||||||
|
|
||||||
#include "matrix/events.hpp"
|
#include "matrix/events.hpp"
|
||||||
|
#include "raii/string_base.hpp"
|
||||||
#include "raii/rjp_string.hpp"
|
#include "raii/rjp_string.hpp"
|
||||||
#include "raii/static_string.hpp"
|
|
||||||
#include "raii/rjp_iterator.hpp"
|
#include "raii/rjp_iterator.hpp"
|
||||||
|
|
||||||
namespace matrix::sync{
|
namespace matrix::sync{
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
#ifndef MATRIX_JSON_TARGETS_HPP
|
#ifndef MATRIX_JSON_TARGETS_HPP
|
||||||
#define MATRIX_JSON_TARGETS_HPP
|
#define MATRIX_JSON_TARGETS_HPP
|
||||||
|
|
||||||
#include "raii/static_string.hpp"
|
#include "raii/string_base.hpp"
|
||||||
|
|
||||||
namespace matrix::json{
|
namespace matrix::json{
|
||||||
|
|
||||||
|
|||||||
@ -19,8 +19,8 @@
|
|||||||
#ifndef SYNC_RESPONSE_HPP
|
#ifndef SYNC_RESPONSE_HPP
|
||||||
#define SYNC_RESPONSE_HPP
|
#define SYNC_RESPONSE_HPP
|
||||||
|
|
||||||
|
#include "raii/string_base.hpp"
|
||||||
#include "raii/rjp_string.hpp"
|
#include "raii/rjp_string.hpp"
|
||||||
#include "raii/static_string.hpp"
|
|
||||||
#include "raii/rjp_iterator.hpp"
|
#include "raii/rjp_iterator.hpp"
|
||||||
#include "raii/rjp_ptr.hpp"
|
#include "raii/rjp_ptr.hpp"
|
||||||
#include "matrix/events.hpp"
|
#include "matrix/events.hpp"
|
||||||
|
|||||||
@ -1,63 +0,0 @@
|
|||||||
/**
|
|
||||||
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_STATIC_STRING_HPP
|
|
||||||
#define RAII_STATIC_STRING_HPP
|
|
||||||
|
|
||||||
#include "raii/string_base.hpp"
|
|
||||||
|
|
||||||
namespace raii{
|
|
||||||
|
|
||||||
class static_string : public string_base
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
constexpr static_string(void) = default;
|
|
||||||
template<size_t N>
|
|
||||||
constexpr static_string(const char(&str)[N]):
|
|
||||||
string_base(const_cast<char*>(str), N){}
|
|
||||||
constexpr static_string(const char* str, size_t len):
|
|
||||||
string_base(const_cast<char*>(str), len){}
|
|
||||||
static_string(const char* c):
|
|
||||||
string_base(const_cast<char*>(c), strlen(c)){}
|
|
||||||
constexpr static_string(const static_string& s):
|
|
||||||
string_base(s.m_data, s.m_length){}
|
|
||||||
constexpr static_string(static_string&& s):
|
|
||||||
string_base(s.m_data, s.m_length){}
|
|
||||||
~static_string(void) = default;
|
|
||||||
|
|
||||||
static_string& operator=(const char* c){
|
|
||||||
m_data = const_cast<char*>(c);
|
|
||||||
m_length = strlen(c);
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
static_string& operator=(const static_string& s){
|
|
||||||
m_data = s.m_data;
|
|
||||||
m_length = s.m_length;
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
static_string& operator=(static_string&&) = delete;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
namespace{
|
|
||||||
constexpr inline raii::static_string operator"" _ss(const char* str, size_t len){
|
|
||||||
return raii::static_string(str, len);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif
|
|
||||||
@ -19,12 +19,9 @@
|
|||||||
#ifndef RAII_STRING_BASE_HPP
|
#ifndef RAII_STRING_BASE_HPP
|
||||||
#define RAII_STRING_BASE_HPP
|
#define RAII_STRING_BASE_HPP
|
||||||
|
|
||||||
#include <cstddef> //size_t
|
|
||||||
#include <cstring> //strlen, strcpy
|
|
||||||
#include <cstdlib> //memcpy
|
|
||||||
#include <type_traits> //is_same, integral_contant, enable_if, etc
|
#include <type_traits> //is_same, integral_contant, enable_if, etc
|
||||||
#include <utility> //forward
|
#include <utility> //forward
|
||||||
#include <tuple>
|
#include <cstdlib> //size_t
|
||||||
|
|
||||||
namespace raii{
|
namespace raii{
|
||||||
|
|
||||||
@ -51,6 +48,20 @@ namespace raii{
|
|||||||
struct is_tuple{
|
struct is_tuple{
|
||||||
static constexpr bool value = std::is_same<std::true_type,decltype(is_tuple_helper(std::declval<T>()))>::value;
|
static constexpr bool value = std::is_same<std::true_type,decltype(is_tuple_helper(std::declval<T>()))>::value;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
//check for member function 'length'
|
||||||
|
template<class T>
|
||||||
|
struct has_len{
|
||||||
|
template<class U, class V>
|
||||||
|
struct check;
|
||||||
|
|
||||||
|
template<class U>
|
||||||
|
static std::true_type test(check<U,decltype(&U::length)>*);
|
||||||
|
template<class U>
|
||||||
|
static std::false_type test(...);
|
||||||
|
|
||||||
|
static constexpr bool value = std::is_same<std::true_type,decltype(test<T>(0))>::value;
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -92,7 +103,6 @@ namespace raii{
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//Supplies all functions that string_base can't implement
|
//Supplies all functions that string_base can't implement
|
||||||
template<class Allocator>
|
template<class Allocator>
|
||||||
class string_intermediary : public string_base
|
class string_intermediary : public string_base
|
||||||
@ -102,183 +112,39 @@ namespace raii{
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
string_intermediary(void) = default;
|
string_intermediary(void) = default;
|
||||||
string_intermediary(char* data, size_t len):
|
string_intermediary(char* data, size_t len);
|
||||||
string_base(data, len){}
|
string_intermediary(const char* data, size_t len);
|
||||||
string_intermediary(const char* data, size_t len):
|
string_intermediary(const char* data);
|
||||||
string_base(reinterpret_cast<char*>(len ? Allocator::copy(data, len+1) : nullptr), len){}
|
string_intermediary(size_t len);
|
||||||
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));
|
|
||||||
}
|
|
||||||
string_intermediary(size_t len):
|
|
||||||
string_base(reinterpret_cast<char*>(len ? Allocator::allocate(len+1) : nullptr), len){}
|
|
||||||
|
|
||||||
//normal copy and move ctors
|
//normal copy and move ctors
|
||||||
string_intermediary(const string_intermediary& b):
|
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){}
|
string_intermediary(string_intermediary&& s);
|
||||||
string_intermediary(string_intermediary&& s):
|
|
||||||
string_base(std::exchange(s.m_data, nullptr), s.m_length){}
|
|
||||||
|
|
||||||
string_intermediary(const string_base& b):
|
string_intermediary(const string_base& b);
|
||||||
string_base(reinterpret_cast<char*>(b.length() ? Allocator::copy(b.get(), b.length()+1) : nullptr), b.length()){}
|
|
||||||
|
|
||||||
//copy from string expression
|
|
||||||
template<class T, typename std::enable_if<detail::is_string<T>::value && !detail::is_concrete_string<T>::value,void>::type* = nullptr>
|
|
||||||
string_intermediary(T&& t){
|
|
||||||
size_t len = t.length();
|
|
||||||
if(!len){
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
char* tmp = reinterpret_cast<char*>(Allocator::allocate(len+1));
|
|
||||||
_assign(tmp, t.get(), 0);
|
|
||||||
m_data = tmp;
|
|
||||||
m_data[len] = 0;
|
|
||||||
m_length = len;
|
|
||||||
}
|
|
||||||
|
|
||||||
//dtor
|
//dtor
|
||||||
~string_intermediary(void){
|
~string_intermediary(void);
|
||||||
Allocator::free(m_data);
|
|
||||||
}
|
|
||||||
|
|
||||||
string_intermediary& operator=(const string_intermediary& s){
|
string_intermediary& operator=(const string_intermediary& s);
|
||||||
string_intermediary tmp(s);
|
string_intermediary& operator=(string_intermediary&& s);
|
||||||
return (*this = std::move(tmp));
|
|
||||||
}
|
|
||||||
string_intermediary& operator=(string_intermediary&& s){
|
|
||||||
std::swap(m_data, s.m_data);
|
|
||||||
m_length = s.m_length;
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
//Copy from c string
|
//Copy from c string
|
||||||
string_intermediary& operator=(const char* c){
|
string_intermediary& operator=(const char* c);
|
||||||
return _copy_string(c, strlen(c));
|
|
||||||
}
|
|
||||||
//Copy from other string_base
|
//Copy from other string_base
|
||||||
string_intermediary& operator=(const string_base& s){
|
string_intermediary& operator=(const string_base& s);
|
||||||
return _copy_string(s.get(), s.length());
|
|
||||||
}
|
|
||||||
//Move from other string base
|
|
||||||
template<class T, typename std::enable_if<detail::is_string<T>::value && !detail::is_concrete_string<T>::value,void>::type* = nullptr>
|
|
||||||
string_base& operator=(T&& t){
|
|
||||||
size_t len = t.length();
|
|
||||||
char* tmp;
|
|
||||||
if(len > m_length){
|
|
||||||
tmp = reinterpret_cast<char*>(Allocator::allocate(len+1));
|
|
||||||
_assign(tmp, t.get(), 0);
|
|
||||||
Allocator::free(m_data);
|
|
||||||
}else{
|
|
||||||
tmp = m_data;
|
|
||||||
_assign(tmp, t.get(), 0);
|
|
||||||
}
|
|
||||||
m_data = tmp;
|
|
||||||
m_data[len] = 0;
|
|
||||||
m_length = len;
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
string_intermediary operator+(const string_base& s)const{
|
|
||||||
string_intermediary tmp(reinterpret_cast<char*>(Allocator::allocate(m_length + s.length() + 1)), m_length+s.length());
|
|
||||||
memcpy(tmp.get(), m_data, m_length);
|
|
||||||
strcpy(tmp.get()+m_length, s.get());
|
|
||||||
return tmp;
|
|
||||||
}
|
|
||||||
|
|
||||||
string_intermediary operator+(const char* c)const{
|
|
||||||
size_t len = strlen(c);
|
|
||||||
string_intermediary tmp(reinterpret_cast<char*>(Allocator::allocate(m_length + len + 1)), m_length+len);
|
|
||||||
memcpy(tmp.get(), m_data, m_length);
|
|
||||||
strcpy(tmp.get()+m_length, c);
|
|
||||||
return tmp;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
//Replace managed pointer. Frees existing value
|
//Replace managed pointer. Frees existing value
|
||||||
void reset(char* val = nullptr){
|
void reset(char* val = nullptr);
|
||||||
Allocator::free(m_data);
|
void reset(char* val, size_t len);
|
||||||
m_data = val;
|
bool resize(size_t newsize);
|
||||||
m_length = val ? strlen(val) : 0;
|
void append(const char* data, size_t len);
|
||||||
}
|
void append(const char* data);
|
||||||
void reset(char* val, size_t len){
|
void append(const string_base& s);
|
||||||
Allocator::free(m_data);
|
|
||||||
m_data = val;
|
|
||||||
m_length = len;
|
|
||||||
}
|
|
||||||
bool 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;
|
|
||||||
}
|
|
||||||
void 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);
|
|
||||||
}
|
|
||||||
void append(const char* data){
|
|
||||||
*this += data;
|
|
||||||
}
|
|
||||||
void append(const string_expr& s){
|
|
||||||
*this += s;
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
string_intermediary& _copy_string(const char* s, size_t len){
|
string_intermediary& _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 Tup, size_t I = 0>
|
|
||||||
static void _assign(char* dest, Tup&& t, size_t offset){
|
|
||||||
memcpy(dest+offset, std::get<I>(t), std::get<I+1>(t));
|
|
||||||
if constexpr(I+2 < std::tuple_size<Tup>::value){
|
|
||||||
_assign<Tup,I+2>(dest, std::forward<Tup>(t), offset+std::get<I+1>(t));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//check for member function 'length'
|
|
||||||
namespace detail{
|
|
||||||
template<class T>
|
|
||||||
struct has_len{
|
|
||||||
template<class U, class V>
|
|
||||||
struct check;
|
|
||||||
|
|
||||||
template<class U>
|
|
||||||
static std::true_type test(check<U,decltype(&U::length)>*);
|
|
||||||
template<class U>
|
|
||||||
static std::false_type test(...);
|
|
||||||
|
|
||||||
static constexpr bool value = std::is_same<std::true_type,decltype(test<T>(0))>::value;
|
|
||||||
};
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//Like an expression template but not really
|
//Like an expression template but not really
|
||||||
template<class Left, class Right>
|
template<class Left, class Right>
|
||||||
class string_cat_expr : public string_expr
|
class string_cat_expr : public string_expr
|
||||||
@ -289,66 +155,56 @@ namespace raii{
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
template<class T, class U>
|
template<class T, class U>
|
||||||
constexpr string_cat_expr(T&& l, U&& r):
|
constexpr string_cat_expr(T&& l, U&& r);
|
||||||
m_l(std::forward<T>(l)),
|
constexpr string_cat_expr(const string_cat_expr& s);
|
||||||
m_r(std::forward<U>(r)){}
|
constexpr string_cat_expr(string_cat_expr&& s);
|
||||||
constexpr string_cat_expr(const string_cat_expr& s):
|
|
||||||
m_l(std::forward<Left>(s.m_l)),
|
|
||||||
m_r(std::forward<Right>(s.m_r)){}
|
|
||||||
constexpr string_cat_expr(string_cat_expr&& s):
|
|
||||||
m_l(std::forward<Left>(s.m_l)),
|
|
||||||
m_r(std::forward<Right>(s.m_r)){}
|
|
||||||
|
|
||||||
constexpr size_t length(void)const{
|
constexpr size_t length(void)const;
|
||||||
return _llen() + _rlen();
|
template<class Alloc>
|
||||||
}
|
operator string_intermediary<Alloc>(void);
|
||||||
constexpr auto get(void){
|
constexpr const Left& left(void)const;
|
||||||
return std::tuple_cat(_lget(), _rget());
|
constexpr const Right& right(void)const;
|
||||||
}
|
};
|
||||||
|
|
||||||
|
template<class Targ>
|
||||||
|
struct appender
|
||||||
|
{
|
||||||
private:
|
private:
|
||||||
constexpr auto _lget(void){
|
Targ& m_targ;
|
||||||
if constexpr(detail::is_string<Left>::value){
|
size_t m_pos = 0;
|
||||||
if constexpr(detail::is_tuple<decltype(m_l.get())>::value){
|
public:
|
||||||
//string_cat_expr
|
appender(Targ& t);
|
||||||
return m_l.get();
|
template<class L, class R>
|
||||||
}else{
|
void operator()(const string_cat_expr<L,R>& str);
|
||||||
//string_base
|
void operator()(const string_base& str);
|
||||||
return std::make_tuple(m_l.get(), m_l.length());
|
};
|
||||||
}
|
|
||||||
}else{
|
class static_string : public string_base
|
||||||
//c string
|
{
|
||||||
return std::make_tuple(m_l, strlen(m_l));
|
public:
|
||||||
}
|
constexpr static_string(void) = default;
|
||||||
}
|
template<size_t N>
|
||||||
constexpr auto _rget(void){
|
constexpr static_string(const char(&str)[N]);
|
||||||
if constexpr(detail::is_string<Right>::value){
|
constexpr static_string(const char* str, size_t len);
|
||||||
if constexpr(detail::is_tuple<decltype(m_r.get())>::value){
|
static_string(const char* c);
|
||||||
return m_r.get();
|
constexpr static_string(const static_string& s);
|
||||||
}else{
|
constexpr static_string(static_string&& s);
|
||||||
return std::make_tuple(m_r.get(), m_r.length());
|
~static_string(void) = default;
|
||||||
}
|
|
||||||
}else{
|
static_string& operator=(const char* c);
|
||||||
return std::make_tuple(m_r, strlen(m_r));
|
static_string& operator=(const static_string& s);
|
||||||
}
|
static_string& operator=(static_string&&) = delete;
|
||||||
}
|
|
||||||
constexpr size_t _llen(void)const{
|
|
||||||
if constexpr(detail::has_len<typename std::remove_reference<Left>::type>::value){
|
|
||||||
return m_l.length();
|
|
||||||
}else{
|
|
||||||
return strlen(m_l);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
constexpr size_t _rlen(void)const{
|
|
||||||
if constexpr(detail::has_len<typename std::remove_reference<Right>::type>::value){
|
|
||||||
return m_r.length();
|
|
||||||
}else{
|
|
||||||
return strlen(m_r);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#include "raii/string_base.tpp"
|
||||||
|
|
||||||
|
namespace{
|
||||||
|
constexpr inline raii::static_string operator"" _ss(const char* str, size_t len){
|
||||||
|
return raii::static_string(str, len);
|
||||||
|
}
|
||||||
|
}
|
||||||
template<class Str1, class Str2, typename std::enable_if<raii::detail::is_concrete_string<Str1>::value&&raii::detail::is_concrete_string<Str2>::value,void>::type* = nullptr>
|
template<class Str1, class Str2, typename std::enable_if<raii::detail::is_concrete_string<Str1>::value&&raii::detail::is_concrete_string<Str2>::value,void>::type* = nullptr>
|
||||||
bool operator==(Str1&& left, Str2&& right){
|
bool operator==(Str1&& left, Str2&& right){
|
||||||
return left && right && left.length() == right.length() && !strcmp(left.get(), right.get());
|
return left && right && left.length() == right.length() && !strcmp(left.get(), right.get());
|
||||||
@ -360,20 +216,20 @@ bool operator!=(Str1&& left, Str2&& right){
|
|||||||
|
|
||||||
template<class Right, typename std::enable_if<raii::detail::is_string<Right>::value && std::is_rvalue_reference<Right&&>::value,void>::type* = nullptr>
|
template<class Right, typename std::enable_if<raii::detail::is_string<Right>::value && std::is_rvalue_reference<Right&&>::value,void>::type* = nullptr>
|
||||||
constexpr auto operator+(const char* left, Right&& right){
|
constexpr auto operator+(const char* left, Right&& right){
|
||||||
return raii::string_cat_expr<const char*,typename std::remove_reference<Right>::type>(left, std::move(right));
|
return raii::string_cat_expr<raii::static_string,typename std::remove_reference<Right>::type>(raii::static_string(left), std::move(right));
|
||||||
}
|
}
|
||||||
template<class Right, typename std::enable_if<raii::detail::is_string<Right>::value && !std::is_rvalue_reference<Right&&>::value,void>::type* = nullptr>
|
template<class Right, typename std::enable_if<raii::detail::is_string<Right>::value && !std::is_rvalue_reference<Right&&>::value,void>::type* = nullptr>
|
||||||
constexpr auto operator+(const char* left, Right&& right){
|
constexpr auto operator+(const char* left, Right&& right){
|
||||||
return raii::string_cat_expr<const char*,decltype(std::forward<Right>(right))>(left, std::forward<Right>(right));
|
return raii::string_cat_expr<raii::static_string,decltype(std::forward<Right>(right))>(raii::static_string(left), std::forward<Right>(right));
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class Left, typename std::enable_if<raii::detail::is_string<Left>::value && std::is_rvalue_reference<Left&&>::value,void>::type* = nullptr>
|
template<class Left, typename std::enable_if<raii::detail::is_string<Left>::value && std::is_rvalue_reference<Left&&>::value,void>::type* = nullptr>
|
||||||
constexpr auto operator+(Left&& left, const char* right){
|
constexpr auto operator+(Left&& left, const char* right){
|
||||||
return raii::string_cat_expr<typename std::remove_reference<Left>::type,const char*>(std::move(left), right);
|
return raii::string_cat_expr<typename std::remove_reference<Left>::type,raii::static_string>(std::move(left), raii::static_string(right));
|
||||||
}
|
}
|
||||||
template<class Left, typename std::enable_if<raii::detail::is_string<Left>::value && !std::is_rvalue_reference<Left&&>::value,void>::type* = nullptr>
|
template<class Left, typename std::enable_if<raii::detail::is_string<Left>::value && !std::is_rvalue_reference<Left&&>::value,void>::type* = nullptr>
|
||||||
constexpr auto operator+(Left&& left, const char* right){
|
constexpr auto operator+(Left&& left, const char* right){
|
||||||
return raii::string_cat_expr<decltype(std::forward<Left>(left)),const char*>(std::forward<Left>(left), right);
|
return raii::string_cat_expr<decltype(std::forward<Left>(left)),raii::static_string>(std::forward<Left>(left), raii::static_string(right));
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class Left,
|
template<class Left,
|
||||||
|
|||||||
220
include/raii/string_base.tpp
Normal file
220
include/raii/string_base.tpp
Normal file
@ -0,0 +1,220 @@
|
|||||||
|
/**
|
||||||
|
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
|
||||||
12
makefile
12
makefile
@ -60,13 +60,15 @@ else
|
|||||||
endif
|
endif
|
||||||
|
|
||||||
ifeq ($(RELEASE),1)
|
ifeq ($(RELEASE),1)
|
||||||
CXXFLAGS+=-O2
|
#a lot of false strict aliasing warnings from gcc 9
|
||||||
|
CXXFLAGS+=-O2 -Wno-strict-aliasing
|
||||||
|
else ifeq ($(MEMCHK),1)
|
||||||
|
LDFLAGS+=-fsanitize=address -fno-omit-frame-pointer -fno-optimize-sibling-calls
|
||||||
|
CXXFLAGS+=-O0 -fsanitize=address -fno-omit-frame-pointer -fno-optimize-sibling-calls
|
||||||
else
|
else
|
||||||
CXXFLAGS+=-O0 -g
|
CXXFLAGS+=-O0 -g
|
||||||
endif
|
endif
|
||||||
|
|
||||||
memchk: LDFLAGS+=-fsanitize=address -fno-omit-frame-pointer -fno-optimize-sibling-calls
|
|
||||||
memchk: CXXFLAGS+=-O0 -fsanitize=address -fno-omit-frame-pointer -fno-optimize-sibling-calls
|
|
||||||
|
|
||||||
ifeq ($(OS),Windows_NT)
|
ifeq ($(OS),Windows_NT)
|
||||||
mkdir=mkdir $(subst /,\,$(1)) > NUL 2>&1
|
mkdir=mkdir $(subst /,\,$(1)) > NUL 2>&1
|
||||||
@ -115,10 +117,10 @@ memchk: all
|
|||||||
.PHONY: utils
|
.PHONY: utils
|
||||||
utils: matrix-send
|
utils: matrix-send
|
||||||
tester: all $(OBJDIR)/src.test.cpp.o
|
tester: all $(OBJDIR)/src.test.cpp.o
|
||||||
$(CXX) -L. -o "$@" $(OBJDIR)/src.test.cpp.o -l$(MAIN_LIBRARY) $(LDLIBS) -lpthread -Iinclude
|
$(CXX) -L. -o "$@" $(OBJDIR)/src.test.cpp.o -l$(MAIN_LIBRARY) $(LDFLAGS) $(LDLIBS) -lpthread -Iinclude
|
||||||
|
|
||||||
matrix-send: all $(OBJDIR)/util.matrix-send.cpp.o
|
matrix-send: all $(OBJDIR)/util.matrix-send.cpp.o
|
||||||
$(CXX) -L. -o "$@" $(OBJDIR)/util.matrix-send.cpp.o -l$(MAIN_LIBRARY) $(LDLIBS) -Iinclude
|
$(CXX) -L. -o "$@" $(OBJDIR)/util.matrix-send.cpp.o -l$(MAIN_LIBRARY) $(LDFLAGS) $(LDLIBS) -Iinclude
|
||||||
|
|
||||||
|
|
||||||
define GENERATE_OBJECTS
|
define GENERATE_OBJECTS
|
||||||
|
|||||||
@ -20,7 +20,7 @@
|
|||||||
#include "matrix/fat_strings.hpp"
|
#include "matrix/fat_strings.hpp"
|
||||||
#include "matrix/json_targets.hpp"
|
#include "matrix/json_targets.hpp"
|
||||||
#include "raii/filerd.hpp"
|
#include "raii/filerd.hpp"
|
||||||
#include "raii/static_string.hpp"
|
#include "raii/string_base.hpp"
|
||||||
#include "raii/rjp_ptr.hpp"
|
#include "raii/rjp_ptr.hpp"
|
||||||
#include "raii/util.hpp"
|
#include "raii/util.hpp"
|
||||||
|
|
||||||
|
|||||||
@ -18,7 +18,7 @@
|
|||||||
|
|
||||||
#include "matrix/connection.hpp"
|
#include "matrix/connection.hpp"
|
||||||
#include "raii/rjp_ptr.hpp"
|
#include "raii/rjp_ptr.hpp"
|
||||||
#include "raii/static_string.hpp"
|
#include "raii/string_base.hpp"
|
||||||
#include <cstdlib> //size_t
|
#include <cstdlib> //size_t
|
||||||
#include <algorithm> //min, max
|
#include <algorithm> //min, max
|
||||||
|
|
||||||
|
|||||||
@ -18,7 +18,7 @@
|
|||||||
|
|
||||||
#include "matrix/fat_strings.hpp"
|
#include "matrix/fat_strings.hpp"
|
||||||
#include "matrix/upload_info.hpp"
|
#include "matrix/upload_info.hpp"
|
||||||
#include "raii/static_string.hpp"
|
#include "raii/string_base.hpp"
|
||||||
#include "matrix/json_targets.hpp"
|
#include "matrix/json_targets.hpp"
|
||||||
#include "raii/util.hpp"
|
#include "raii/util.hpp"
|
||||||
|
|
||||||
|
|||||||
@ -17,7 +17,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include "matrix/roomcxn.hpp"
|
#include "matrix/roomcxn.hpp"
|
||||||
#include "raii/static_string.hpp"
|
#include "raii/string_base.hpp"
|
||||||
#include "matrix/fat_strings.hpp"
|
#include "matrix/fat_strings.hpp"
|
||||||
#include "matrix/json_targets.hpp"
|
#include "matrix/json_targets.hpp"
|
||||||
#include "raii/util.hpp"
|
#include "raii/util.hpp"
|
||||||
|
|||||||
@ -20,7 +20,7 @@
|
|||||||
#include "raii/rjp_ptr.hpp"
|
#include "raii/rjp_ptr.hpp"
|
||||||
#include "raii/curler.hpp"
|
#include "raii/curler.hpp"
|
||||||
#include "raii/util.hpp"
|
#include "raii/util.hpp"
|
||||||
#include "raii/static_string.hpp"
|
#include "raii/string_base.hpp"
|
||||||
#include "matrix/fat_strings.hpp"
|
#include "matrix/fat_strings.hpp"
|
||||||
#include "matrix/json_targets.hpp"
|
#include "matrix/json_targets.hpp"
|
||||||
|
|
||||||
|
|||||||
@ -17,7 +17,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include "matrix/syncer.hpp"
|
#include "matrix/syncer.hpp"
|
||||||
#include "raii/static_string.hpp"
|
#include "raii/string_base.hpp"
|
||||||
#include "raii/rjp_ptr.hpp"
|
#include "raii/rjp_ptr.hpp"
|
||||||
#include "raii/util.hpp"
|
#include "raii/util.hpp"
|
||||||
|
|
||||||
|
|||||||
@ -17,7 +17,6 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include "raii/string_base.hpp"
|
#include "raii/string_base.hpp"
|
||||||
#include "raii/string.hpp"
|
|
||||||
|
|
||||||
#include <utility> //exchange, swap
|
#include <utility> //exchange, swap
|
||||||
#include <cstdlib> //memcpy
|
#include <cstdlib> //memcpy
|
||||||
@ -34,4 +33,17 @@ namespace raii{
|
|||||||
return m_data[i];
|
return m_data[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static_string::static_string(const char* c):
|
||||||
|
string_base(const_cast<char*>(c), strlen(c)){}
|
||||||
|
static_string& static_string::operator=(const char* c){
|
||||||
|
m_data = const_cast<char*>(c);
|
||||||
|
m_length = strlen(c);
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
static_string& static_string::operator=(const static_string& s){
|
||||||
|
m_data = s.m_data;
|
||||||
|
m_length = s.m_length;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -19,8 +19,9 @@
|
|||||||
//example of a client which responds to commands
|
//example of a client which responds to commands
|
||||||
|
|
||||||
#include "matrix/matrix.hpp"
|
#include "matrix/matrix.hpp"
|
||||||
#include "raii/static_string.hpp"
|
#include "raii/string_base.hpp"
|
||||||
#include "matrix/sync_response.hpp"
|
#include "matrix/sync_response.hpp"
|
||||||
|
#include "raii/util.hpp"
|
||||||
|
|
||||||
#include <atomic>
|
#include <atomic>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
@ -112,7 +113,7 @@ int main(){
|
|||||||
auto room = client.spawn_room("!IaZlRZWhZiNMMsiZRl:rexy712.chickenkiller.com"_ss);
|
auto room = client.spawn_room("!IaZlRZWhZiNMMsiZRl:rexy712.chickenkiller.com"_ss);
|
||||||
|
|
||||||
syn.sync(0);
|
syn.sync(0);
|
||||||
auto status = room.send_message("gay"_ss);
|
auto status = room.send_message("test"_ss);
|
||||||
if(!status.ok())
|
if(!status.ok())
|
||||||
fprintf(stderr, "http error: %d\n", status.httpstatus());
|
fprintf(stderr, "http error: %d\n", status.httpstatus());
|
||||||
if(status.mxerror())
|
if(status.mxerror())
|
||||||
|
|||||||
@ -17,7 +17,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include "matrix/matrix.hpp"
|
#include "matrix/matrix.hpp"
|
||||||
#include "raii/static_string.hpp"
|
#include "raii/string_base.hpp"
|
||||||
|
|
||||||
#include <cstdlib> //exit
|
#include <cstdlib> //exit
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user