403 lines
14 KiB
C++
403 lines
14 KiB
C++
/**
|
|
This file is a part of rexy's general purpose library
|
|
Copyright (C) 2020-2022 rexy712
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU 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 General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#ifndef REXY_STRING_BASE_TPP
|
|
#define REXY_STRING_BASE_TPP
|
|
|
|
#include <utility> //forward, move, swap, etc
|
|
#include <cstdlib> //memcpy
|
|
#include <cstring> //strlen, strcpy
|
|
|
|
#include "utility.hpp" //max
|
|
#include "detail/string_appender.hpp"
|
|
#include "algorithm.hpp"
|
|
#include "compat/to_address.hpp"
|
|
#include "string_view.hpp"
|
|
|
|
#ifdef __cpp_concepts
|
|
#include "compat/cpp20/string_base.hpp"
|
|
#else
|
|
#include "compat/cpp17/string_base.hpp"
|
|
#endif
|
|
|
|
namespace rexy{
|
|
|
|
template<class Char>
|
|
constexpr auto string_base<Char>::search(const string_base& s)const -> const_iterator{
|
|
return two_way_search(cbegin(), cend(), s.begin(), s.end());
|
|
}
|
|
template<class Char>
|
|
constexpr auto string_base<Char>::search(const_pointer c)const -> const_iterator{
|
|
const auto len = strlen(c);
|
|
return two_way_search(cbegin(), cend(), c, c + len);
|
|
}
|
|
template<class Char>
|
|
constexpr auto string_base<Char>::search(const string_base& s) -> iterator{
|
|
return two_way_search(begin(), end(), s.begin(), s.end());
|
|
}
|
|
template<class Char>
|
|
constexpr auto string_base<Char>::search(const_pointer c) -> iterator{
|
|
const auto len = strlen(c);
|
|
return two_way_search(begin(), end(), c, c + len);
|
|
}
|
|
template<class Char>
|
|
template<class Searcher>
|
|
constexpr auto string_base<Char>::search(const string_base& s, const Searcher& searcher)const -> const_iterator{
|
|
return searcher(cbegin(), cend(), s.begin(), s.end());
|
|
}
|
|
template<class Char>
|
|
template<class Searcher>
|
|
constexpr auto string_base<Char>::search(const_pointer c, const Searcher& searcher)const -> const_iterator{
|
|
const auto len = strlen(c);
|
|
return searcher(cbegin(), cend(), c, c + len);
|
|
}
|
|
template<class Char>
|
|
template<class Searcher>
|
|
constexpr auto string_base<Char>::search(const string_base& s, const Searcher& searcher) -> iterator{
|
|
return searcher(begin(), end(), s.begin(), s.end());
|
|
}
|
|
template<class Char>
|
|
template<class Searcher>
|
|
constexpr auto string_base<Char>::search(const_pointer c, const Searcher& searcher) -> iterator{
|
|
const auto len = strlen(c);
|
|
return searcher(begin(), end(), c, c + len);
|
|
}
|
|
|
|
|
|
//allocate string if longer than small string capacity, copy otherwise
|
|
template<class Char, REXY_ALLOCATOR_CONCEPT Alloc>
|
|
REXY_CPP20_CONSTEXPR void basic_string<Char,Alloc>::_copy_construct_string(const_pointer data, size_type len, size_type cap)
|
|
noexcept(noexcept(this->allocate(0)))
|
|
{
|
|
if(cap > this->get_short_capacity()){
|
|
this->set_islong_flag(true);
|
|
pointer raw = this->set_long_ptr(this->allocate(sizeof(value_type)*(cap+1)));
|
|
if(data)
|
|
memcpy(raw, data, sizeof(value_type)*len);
|
|
raw[len] = 0;
|
|
this->set_long_length(len);
|
|
this->set_long_capacity(cap);
|
|
}else{
|
|
this->set_islong_flag(false);
|
|
pointer raw = this->set_short_ptr();
|
|
if(data)
|
|
memcpy(raw, data, sizeof(value_type)*len);
|
|
raw[len] = 0;
|
|
this->set_short_length(len);
|
|
this->set_short_capacity(cap);
|
|
}
|
|
}
|
|
|
|
template<class Char, REXY_ALLOCATOR_CONCEPT Alloc>
|
|
constexpr basic_string<Char,Alloc>::basic_string(void)noexcept{}
|
|
template<class Char, REXY_ALLOCATOR_CONCEPT Alloc>
|
|
constexpr basic_string<Char,Alloc>::basic_string(rexy::steal<pointer> data)noexcept:
|
|
basic_string(data.value(), data.value() ? strlen(data.value()) : 0){}
|
|
template<class Char, REXY_ALLOCATOR_CONCEPT Alloc>
|
|
constexpr basic_string<Char,Alloc>::basic_string(rexy::steal<pointer> data, size_type len)noexcept:
|
|
string_base<Char>(data.value(), len, len){}
|
|
template<class Char, REXY_ALLOCATOR_CONCEPT Alloc>
|
|
constexpr basic_string<Char,Alloc>::basic_string(rexy::steal<pointer> data, size_type len, size_type cap)noexcept:
|
|
string_base<Char>(data.value(), len, cap){}
|
|
template<class Char, REXY_ALLOCATOR_CONCEPT Alloc>
|
|
REXY_CPP20_CONSTEXPR basic_string<Char,Alloc>::basic_string(const_pointer data, size_type len, size_type cap)
|
|
noexcept(noexcept(this->allocate(0)))
|
|
{
|
|
_copy_construct_string(data, len, cap);
|
|
}
|
|
template<class Char, REXY_ALLOCATOR_CONCEPT Alloc>
|
|
REXY_CPP20_CONSTEXPR basic_string<Char,Alloc>::basic_string(const_pointer data, size_type len)
|
|
noexcept(noexcept(this->allocate(0))):
|
|
basic_string(data, len, len){}
|
|
template<class Char, REXY_ALLOCATOR_CONCEPT Alloc>
|
|
REXY_CPP20_CONSTEXPR basic_string<Char,Alloc>::basic_string(const_pointer data)
|
|
noexcept(noexcept(this->allocate(0))):
|
|
basic_string(data, data ? strlen(data) : 0){}
|
|
template<class Char, REXY_ALLOCATOR_CONCEPT Alloc>
|
|
REXY_CPP20_CONSTEXPR basic_string<Char,Alloc>::basic_string(size_type cap)
|
|
noexcept(noexcept(this->allocate(0))):
|
|
basic_string(size_type{0}, cap){}
|
|
template<class Char, REXY_ALLOCATOR_CONCEPT Alloc>
|
|
REXY_CPP20_CONSTEXPR basic_string<Char,Alloc>::basic_string(size_type len, size_type cap)
|
|
noexcept(noexcept(this->allocate(0)))
|
|
{
|
|
_copy_construct_string(nullptr, len, cap);
|
|
}
|
|
template<class Char, REXY_ALLOCATOR_CONCEPT Alloc>
|
|
REXY_CPP20_CONSTEXPR basic_string<Char,Alloc>::basic_string(const basic_string_view<Char>& sv)
|
|
noexcept(noexcept(this->allocate(0)))
|
|
{
|
|
_copy_construct_string(sv.c_str(), sv.length(), sv.length());
|
|
}
|
|
|
|
template<class Char, REXY_ALLOCATOR_CONCEPT Alloc>
|
|
template<class InputIt>
|
|
REXY_CPP20_CONSTEXPR basic_string<Char,Alloc>::basic_string(InputIt start, InputIt fin)
|
|
noexcept(noexcept(this->allocate(0))):
|
|
basic_string(nullptr, size_type{fin - start})
|
|
{
|
|
auto raw = this->get_pointer();
|
|
size_type i = 0;
|
|
for(auto it = start;it != fin;++it,++i){
|
|
raw[i] = *it;
|
|
}
|
|
raw[i] = 0;
|
|
}
|
|
//normal copy and move ctors
|
|
template<class Char, REXY_ALLOCATOR_CONCEPT Alloc>
|
|
REXY_CPP20_CONSTEXPR basic_string<Char,Alloc>::basic_string(const basic_string& b)
|
|
noexcept(noexcept(this->allocate(0))):
|
|
detail::hasallocator<Alloc>(b)
|
|
{
|
|
_copy_construct_string(b.get(), b.length(), b.capacity());
|
|
}
|
|
template<class Char, REXY_ALLOCATOR_CONCEPT Alloc>
|
|
constexpr basic_string<Char,Alloc>::basic_string(basic_string&& s)noexcept:
|
|
detail::hasallocator<Alloc>(std::move(s)),
|
|
string_base<Char>(std::move(s)){}
|
|
template<class Char, REXY_ALLOCATOR_CONCEPT Alloc>
|
|
REXY_CPP20_CONSTEXPR basic_string<Char,Alloc>::basic_string(const string_base<Char>& b)
|
|
noexcept(noexcept(this->allocate(0)))
|
|
{
|
|
_copy_construct_string(b.get(), b.length(), b.capacity());
|
|
}
|
|
|
|
//dtor
|
|
template<class Char, REXY_ALLOCATOR_CONCEPT Alloc>
|
|
REXY_CPP20_CONSTEXPR basic_string<Char,Alloc>::~basic_string(void)
|
|
noexcept(noexcept(this->deallocate(nullptr, 0)))
|
|
{
|
|
if(this->islong()){
|
|
this->deallocate(this->get_pointer(), sizeof(value_type)*(this->get_long_capacity()+1));
|
|
}
|
|
}
|
|
|
|
template<class Char, REXY_ALLOCATOR_CONCEPT Alloc>
|
|
REXY_CPP20_CONSTEXPR basic_string<Char,Alloc>& basic_string<Char,Alloc>::operator=(const basic_string& s)
|
|
noexcept(noexcept(this->allocate(0)) &&
|
|
noexcept(this->deallocate(nullptr, 0)))
|
|
{
|
|
if(s.length() < this->capacity()){
|
|
memcpy(this->get_pointer(), s.get_pointer(), sizeof(value_type)*(s.length()+1));
|
|
this->set_length(s.length());
|
|
return *this;
|
|
}
|
|
basic_string tmp(s);
|
|
return (*this = std::move(tmp));
|
|
}
|
|
template<class Char, REXY_ALLOCATOR_CONCEPT Alloc>
|
|
constexpr basic_string<Char,Alloc>& basic_string<Char,Alloc>::operator=(basic_string&& s)noexcept{
|
|
string_base<Char>::operator=(std::move(s));
|
|
return *this;
|
|
}
|
|
template<class Char, REXY_ALLOCATOR_CONCEPT Alloc>
|
|
REXY_CPP20_CONSTEXPR basic_string<Char,Alloc>& basic_string<Char,Alloc>::operator=(const string_base<Char>& s)
|
|
noexcept(noexcept(this->allocate(0)) &&
|
|
noexcept(this->deallocate(nullptr,0)))
|
|
{
|
|
return (*this = basic_string(s));
|
|
}
|
|
//Copy from c string
|
|
template<class Char, REXY_ALLOCATOR_CONCEPT Alloc>
|
|
REXY_CPP20_CONSTEXPR basic_string<Char,Alloc>& basic_string<Char,Alloc>::operator=(const basic_string_view<Char>& sv)
|
|
noexcept(noexcept(this->allocate(0)) &&
|
|
noexcept(this->deallocate(nullptr,0)))
|
|
{
|
|
return _copy_string(sv.c_str(), sv.length());
|
|
}
|
|
|
|
template<class Char, REXY_ALLOCATOR_CONCEPT Alloc>
|
|
REXY_CPP20_CONSTEXPR basic_string<Char,Alloc>& basic_string<Char,Alloc>::operator=(const_pointer c)
|
|
noexcept(noexcept(this->allocate(0)) &&
|
|
noexcept(this->deallocate(nullptr,0)))
|
|
{
|
|
return _copy_string(c, strlen(c));
|
|
}
|
|
|
|
//Replace managed pointer. Frees existing value
|
|
template<class Char, REXY_ALLOCATOR_CONCEPT Alloc>
|
|
REXY_CPP20_CONSTEXPR void basic_string<Char,Alloc>::reset(pointer val)
|
|
noexcept(noexcept(this->deallocate(nullptr,0)))
|
|
{
|
|
reset(val, val ? strlen(val) : 0);
|
|
}
|
|
template<class Char, REXY_ALLOCATOR_CONCEPT Alloc>
|
|
REXY_CPP20_CONSTEXPR void basic_string<Char,Alloc>::reset(pointer val, size_type len)
|
|
noexcept(noexcept(this->deallocate(nullptr,0)))
|
|
{
|
|
if(this->islong())
|
|
this->deallocate(this->get_long_ptr(),sizeof(value_type)*(this->get_long_capacity()+1));
|
|
this->set_islong_flag(true);
|
|
this->set_long_ptr(val);
|
|
this->set_long_length(len);
|
|
this->set_long_capacity(len);
|
|
}
|
|
template<class Char, REXY_ALLOCATOR_CONCEPT Alloc>
|
|
REXY_CPP20_CONSTEXPR bool basic_string<Char,Alloc>::resize(size_type newsize)
|
|
noexcept(noexcept(this->allocate(0)) &&
|
|
noexcept(this->deallocate(nullptr,0)))
|
|
{
|
|
if(newsize < this->capacity())
|
|
return false;
|
|
if(!this->islong() && newsize < this->get_short_capacity())
|
|
return false;
|
|
return (*this = basic_string(this->get_pointer(), newsize));
|
|
}
|
|
|
|
template<class Char, REXY_ALLOCATOR_CONCEPT Alloc>
|
|
REXY_CPP20_CONSTEXPR void basic_string<Char,Alloc>::push_back(value_type data)
|
|
noexcept(noexcept(this->allocate(0)) &&
|
|
noexcept(this->deallocate(nullptr,0)))
|
|
{
|
|
append(&data, 1);
|
|
}
|
|
|
|
template<class Char, REXY_ALLOCATOR_CONCEPT Alloc>
|
|
REXY_CPP20_CONSTEXPR void basic_string<Char,Alloc>::append(const_pointer data, size_type len)
|
|
noexcept(noexcept(this->allocate(0)) &&
|
|
noexcept(this->deallocate(nullptr,0)))
|
|
{
|
|
size_type mylen = this->length();
|
|
size_type mycap = this->capacity();
|
|
pointer raw = this->get_pointer();
|
|
|
|
if(mylen+len <= mycap){
|
|
memcpy(raw+mylen, data, sizeof(value_type)*len);
|
|
this->set_length(mylen+len);
|
|
raw[mylen+len] = 0;
|
|
}else{
|
|
auto newsize = max(mylen+len, mycap*2);
|
|
basic_string tmp(newsize);
|
|
tmp.append(raw, mylen);
|
|
tmp.append(data, len);
|
|
*this = std::move(tmp);
|
|
}
|
|
}
|
|
template<class Char, REXY_ALLOCATOR_CONCEPT Alloc>
|
|
REXY_CPP20_CONSTEXPR void basic_string<Char,Alloc>::append(const_pointer data)
|
|
noexcept(noexcept(this->allocate(0)) &&
|
|
noexcept(this->deallocate(nullptr,0)))
|
|
{
|
|
if(data)
|
|
append(data, strlen(data));
|
|
}
|
|
template<class Char, REXY_ALLOCATOR_CONCEPT Alloc>
|
|
template<class InputIt>
|
|
REXY_CPP20_CONSTEXPR void basic_string<Char,Alloc>::append(InputIt start, InputIt fin)
|
|
noexcept(noexcept(this->allocate(0)) &&
|
|
noexcept(this->deallocate(nullptr,0)))
|
|
{
|
|
size_type append_len = fin - start;
|
|
|
|
size_type my_len = this->length();
|
|
size_type my_cap = this->capacity();
|
|
pointer raw = this->get_pointer();
|
|
|
|
if(my_len + append_len > my_cap){
|
|
*this = basic_string(raw, my_len, max(my_len+append_len, my_cap*2));
|
|
raw = this->get_pointer();
|
|
my_cap *= 2;
|
|
}
|
|
|
|
size_type i = 0;
|
|
for(auto it = start;it != fin;++it,++i){
|
|
raw[my_len + i] = *it;
|
|
}
|
|
this->set_length(my_len + append_len);
|
|
raw[my_len + i] = 0;
|
|
}
|
|
|
|
|
|
template<class Char, REXY_ALLOCATOR_CONCEPT Alloc>
|
|
template<REXY_ALLOCATOR_CONCEPT A>
|
|
REXY_CPP20_CONSTEXPR auto basic_string<Char,Alloc>::substring(size_type start, size_type end)const -> basic_string<value_type,A>{
|
|
if(start > end || end > this->length())
|
|
return {};
|
|
const size_type newlen = end - start;
|
|
basic_string<value_type,Alloc> tmp(newlen);
|
|
tmp.append(this->get() + start, newlen);
|
|
return tmp;
|
|
}
|
|
template<class Char, REXY_ALLOCATOR_CONCEPT Alloc>
|
|
REXY_CPP20_CONSTEXPR auto basic_string<Char,Alloc>::release(void)noexcept(noexcept(this->allocate(0))) -> pointer{
|
|
if(this->islong()){
|
|
pointer raw = this->get_long_ptr();
|
|
this->set_islong_flag(false);
|
|
this->set_short_ptr();
|
|
this->set_short_length(0);
|
|
return raw;
|
|
}
|
|
size_type len = this->get_short_length();
|
|
pointer raw = this->get_short_ptr();
|
|
pointer retval = this->allocate(sizeof(value_type)*len+1);
|
|
memcpy(retval, raw, sizeof(value_type)*len);
|
|
retval[len] = 0;
|
|
raw[0] = 0;
|
|
this->set_short_length(0);
|
|
return retval;
|
|
}
|
|
template<class Char, REXY_ALLOCATOR_CONCEPT Alloc>
|
|
constexpr auto basic_string<Char,Alloc>::create_view(void)const noexcept -> basic_string_view<value_type>{
|
|
const auto ptr = this->get_pointer();
|
|
return basic_string_view<value_type>(ptr, ptr + this->length());
|
|
}
|
|
template<class Char, REXY_ALLOCATOR_CONCEPT Alloc>
|
|
constexpr auto basic_string<Char,Alloc>::create_view(const_iterator start, const_iterator fin)const noexcept -> basic_string_view<value_type>{
|
|
return basic_string_view<value_type>(start, fin);
|
|
}
|
|
|
|
template<class Char, REXY_ALLOCATOR_CONCEPT Alloc>
|
|
REXY_CPP20_CONSTEXPR basic_string<Char,Alloc>& basic_string<Char,Alloc>::_copy_string(const_pointer s, size_type len)
|
|
noexcept(noexcept(this->allocate(0)) &&
|
|
noexcept(this->deallocate(nullptr,0)))
|
|
{
|
|
if(!s || !len)
|
|
return (*this = basic_string(rexy::steal<pointer>(nullptr), 0, 0));
|
|
if(len <= this->length()){
|
|
this->set_length(len);
|
|
pointer raw = this->get_pointer();
|
|
memcpy(raw, s, sizeof(value_type)*len);
|
|
raw[len] = 0;
|
|
return *this;
|
|
}
|
|
return (*this = basic_string(s, len));
|
|
}
|
|
|
|
|
|
template<class Left, class Right>
|
|
constexpr auto string_cat_expr<Left,Right>::length(void)const noexcept -> size_type{
|
|
return this->m_l.length() + this->m_r.length();
|
|
}
|
|
template<class Left, class Right>
|
|
template<REXY_ALLOCATOR_CONCEPT Alloc>
|
|
REXY_CPP20_CONSTEXPR string_cat_expr<Left,Right>::operator basic_string<value_type,Alloc>(void)
|
|
noexcept(std::is_nothrow_constructible<basic_string<value_type,Alloc>, typename basic_string<value_type,Alloc>::size_type>::value &&
|
|
std::is_nothrow_invocable<detail::string_appender<basic_string<value_type,Alloc>>,decltype(*this)>::value)
|
|
{
|
|
size_type len = length();
|
|
basic_string<value_type,Alloc> ret(len);
|
|
detail::string_appender<basic_string<value_type,Alloc>> append(ret);
|
|
append(*this);
|
|
return ret;
|
|
}
|
|
|
|
|
|
} //namespace rexy
|
|
|
|
#endif
|