/**
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 .
*/
#ifndef REXY_STRING_BASE_TPP
#define REXY_STRING_BASE_TPP
#include //move, etc
#include //is_nothrow_invokable, is_nothrow_constructible
#include //reverse_iterator
#include "utility.hpp" //max, memcpy, strlen
#include "detail/string_appender.hpp"
#include "algorithm.hpp"
#include "compat/to_address.hpp"
#include "string_view.hpp"
#include "compat/string_base.hpp"
namespace rexy{
namespace detail{
template
struct value_iterator_adapter{
T val;
constexpr value_iterator_adapter& operator++(void)noexcept{return *this;}
constexpr value_iterator_adapter operator++(int)noexcept{return *this;}
constexpr T operator*(void)const noexcept{return val;}
constexpr bool operator==(const value_iterator_adapter& other)const{return val == other.val;}
constexpr bool operator!=(const value_iterator_adapter& other)const{return val == other.val;}
};
template
value_iterator_adapter(T) -> value_iterator_adapter;
}
template
constexpr auto string_base::search(basic_string_view sv)const -> const_iterator{
if(sv.length() > length()){
return cend();
}
return two_way_search(cbegin(), cend(), sv.cbegin(), sv.cend());
}
template
constexpr auto string_base::search(basic_string_view sv) -> iterator{
if(sv.length() > length()){
return end();
}
return two_way_search(begin(), end(), sv.cbegin(), sv.cend());
}
template
constexpr auto string_base::search(const_pointer c)const -> const_iterator{
const auto len = rexy::strlen(c);
return two_way_search(cbegin(), cend(), c, c + len);
}
template
constexpr auto string_base::search(const_pointer c) -> iterator{
const auto len = rexy::strlen(c);
return two_way_search(begin(), end(), c, c + len);
}
template
template
constexpr auto string_base::search(const_pointer c, const Searcher& searcher)const -> const_iterator{
const auto len = rexy::strlen(c);
return searcher(cbegin(), cend(), c, c + len);
}
template
template
constexpr auto string_base::search(const_pointer c, const Searcher& searcher) -> iterator{
const auto len = rexy::strlen(c);
return searcher(begin(), end(), c, c + len);
}
template
constexpr auto string_base::rsearch(basic_string_view sv)const -> const_iterator{
if(sv.length() > length()){
return cend();
}
return two_way_search(crbegin(), crend(), sv.crbegin(), sv.crend()).base();
}
template
constexpr auto string_base::rsearch(basic_string_view sv) -> iterator{
if(sv.length() > length()){
return end();
}
return two_way_search(rbegin(), rend(), sv.crbegin(), sv.crend()).base() - sv.length();
}
template
constexpr auto string_base::rsearch(const_pointer c)const -> const_iterator{
const auto len = rexy::strlen(c);
return two_way_search(crbegin(), crend(), std::reverse_iterator(c + len), std::reverse_iterator(c)).base() - len;
}
template
constexpr auto string_base::rsearch(const_pointer c) -> iterator{
const auto len = rexy::strlen(c);
return two_way_search(rbegin(), rend(), std::reverse_iterator(c + len), std::reverse_iterator(c)).base() - len;
}
template
template
constexpr auto string_base::rsearch(const_pointer c, const Searcher& searcher)const -> const_iterator{
const auto len = rexy::strlen(c);
return searcher(crbegin(), crend(), std::reverse_iterator(c + len), std::reverse_iterator(c)).base() - len;
}
template
template
constexpr auto string_base::rsearch(const_pointer c, const Searcher& searcher) -> iterator{
const auto len = rexy::strlen(c);
return searcher(rbegin(), rend(), std::reverse_iterator(c + len), std::reverse_iterator(c)).base() - len;
}
template
constexpr bool string_base::starts_with(basic_string_view sv)const noexcept{
return basic_string_view(data(), length()).starts_with(sv);
}
template
constexpr bool string_base::starts_with(value_type v)const noexcept{
return front() == v;
}
template
constexpr bool string_base::starts_with(const_pointer str)const noexcept{
return starts_with(basic_string_view(str));
}
template
constexpr bool string_base::ends_with(basic_string_view sv)const noexcept{
return basic_string_view(data(), length()).ends_with(sv);
}
template
constexpr bool string_base::ends_with(value_type v)const noexcept{
return back() == v;
}
template
constexpr bool string_base::ends_with(const_pointer str)const noexcept{
return ends_with(basic_string_view(str));
}
template
constexpr bool string_base::contains(basic_string_view sv)const noexcept{
return basic_string_view(data(), length()).contains(sv);
}
template
constexpr bool string_base::contains(value_type v)const noexcept{
return basic_string_view(data(), length()).contains(v);
}
template
constexpr bool string_base::contains(const_pointer str)const noexcept{
return contains(basic_string_view(str));
}
template
constexpr auto string_base::find_first_of(value_type v, size_type start)const -> size_type{
return rexy::find_first_of(*this, &v, start, 1);
}
template
constexpr auto string_base::find_first_of(const_pointer c, size_type pos)const -> size_type{
return rexy::find_first_of(*this, c, pos, rexy::strlen(c));
}
template
constexpr auto string_base::find_first_of(const_pointer c, size_type pos, size_type size)const -> size_type{
return rexy::find_first_of(*this, c, pos, size);
}
template
constexpr auto string_base::find_last_of(value_type v, size_type start)const -> size_type{
return rexy::find_last_of(*this, &v, start, 1);
}
template
constexpr auto string_base::find_last_of(const_pointer c, size_type pos)const -> size_type{
return rexy::find_last_of(*this, c, pos, rexy::strlen(c));
}
template
constexpr auto string_base::find_last_of(const_pointer c, size_type pos, size_type size)const -> size_type{
return rexy::find_last_of(*this, c, pos, size);
}
template
constexpr void string_base::clear(void)noexcept{
set_length(0);
get_pointer()[0] = 0;
}
template
constexpr auto string_base::create_view(void)const noexcept -> basic_string_view{
return basic_string_view(data(), length());
}
template
constexpr auto string_base::create_view(const_iterator start, const_iterator fin)const noexcept -> basic_string_view{
return basic_string_view(start, fin);
}
//allocate string if longer than small string capacity, copy otherwise
template
REXY_CPP20_CONSTEXPR void basic_string::_copy_construct_string(const_pointer data, size_type len, size_type cap)
noexcept(is_nothrow_allocator_v)
{
if constexpr(string_base::uses_sso()){
if(cap > string_base::short_string_size()){
pointer raw = this->set_long_ptr(this->allocate(sizeof(value_type)*(cap+1)));
if(data){
rexy::memcpy(raw, data, sizeof(value_type)*len);
}
raw[len] = 0;
this->set_long_length(len);
this->set_long_capacity(cap);
}else{
pointer raw = this->set_short_ptr();
if(data){
rexy::memcpy(raw, data, sizeof(value_type)*len);
}
raw[len] = 0;
this->set_short_length(len);
this->set_short_capacity(cap);
}
}else{
if(cap == 0){
return;
}
pointer raw = this->set_long_ptr(this->allocate(sizeof(value_type)*(cap+1)));
if(data){
rexy::memcpy(raw, data, sizeof(value_type)*len);
}
raw[len] = 0;
this->set_length(len);
this->set_capacity(cap);
}
}
template
constexpr basic_string::basic_string(void)noexcept{}
template
constexpr basic_string::basic_string(rexy::steal data)noexcept:
basic_string(data.value(), data.value() ? rexy::strlen(data.value()) : 0){}
template
constexpr basic_string::basic_string(rexy::steal data, size_type len)noexcept:
string_base(data.value(), len, len){}
template
constexpr basic_string::basic_string(rexy::steal data, size_type len, size_type cap)noexcept:
string_base(data.value(), len, cap){}
template
REXY_CPP20_CONSTEXPR basic_string::basic_string(const_pointer data, size_type len, size_type cap)
noexcept(is_nothrow_allocator_v)
{
_copy_construct_string(data, len, cap);
}
template
REXY_CPP20_CONSTEXPR basic_string::basic_string(const_pointer data, size_type len)
noexcept(is_nothrow_allocator_v):
basic_string(data, len, len){}
template
REXY_CPP20_CONSTEXPR basic_string::basic_string(const_pointer data)
noexcept(is_nothrow_allocator_v):
basic_string(data, data ? rexy::strlen(data) : 0){}
template
REXY_CPP20_CONSTEXPR basic_string::basic_string(size_type cap)
noexcept(is_nothrow_allocator_v):
basic_string(size_type(0), cap){}
template
REXY_CPP20_CONSTEXPR basic_string::basic_string(size_type len, size_type cap)
noexcept(is_nothrow_allocator_v)
{
_copy_construct_string(nullptr, len, cap);
}
template
REXY_CPP20_CONSTEXPR basic_string::basic_string(const basic_string_view& sv)
noexcept(is_nothrow_allocator_v)
{
_copy_construct_string(sv.c_str(), sv.length(), sv.length());
}
template
template
REXY_CPP20_CONSTEXPR basic_string::basic_string(InputIt start, InputIt fin)
noexcept(is_nothrow_allocator_v):
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
REXY_CPP20_CONSTEXPR basic_string::basic_string(const basic_string& b)
noexcept(is_nothrow_allocator_v):
detail::hasallocator(b)
{
_copy_construct_string(b.data(), b.length(), b.length());
}
template
constexpr basic_string::basic_string(basic_string&& s)noexcept:
detail::hasallocator(std::move(s)),
string_base(std::move(s)){}
template
REXY_CPP20_CONSTEXPR basic_string::basic_string(const string_base& b)
noexcept(is_nothrow_allocator_v)
{
_copy_construct_string(b.data(), b.length(), b.length());
}
//dtor
template
REXY_CPP20_CONSTEXPR basic_string::~basic_string(void)
noexcept(is_nothrow_allocator_v)
{
if(this->islong()){
this->deallocate(this->get_pointer(), sizeof(value_type)*(this->capacity()+1));
}
}
template
REXY_CPP20_CONSTEXPR basic_string& basic_string::operator=(const basic_string& s)
noexcept(is_nothrow_allocator_v)
{
if(s.length() < this->capacity()){
rexy::memcpy(this->get_pointer(), s.get_pointer(), sizeof(value_type)*(s.length()+1));
this->set_length(s.length());
return *this;
}else{
basic_string tmp(s);
return (*this = std::move(tmp));
}
}
template
constexpr basic_string& basic_string::operator=(basic_string&& s)noexcept{
string_base::operator=(std::move(s));
return *this;
}
template
REXY_CPP20_CONSTEXPR basic_string& basic_string::operator=(const string_base& s)
noexcept(is_nothrow_allocator_v)
{
return (*this = basic_string(s));
}
//Copy from c string
template
REXY_CPP20_CONSTEXPR basic_string& basic_string::operator=(const basic_string_view& sv)
noexcept(is_nothrow_allocator_v)
{
return _copy_string(sv.c_str(), sv.length());
}
template
REXY_CPP20_CONSTEXPR basic_string& basic_string::operator=(const_pointer c)
noexcept(is_nothrow_allocator_v)
{
return _copy_string(c, rexy::strlen(c));
}
//Replace managed pointer. Frees existing value
template
REXY_CPP20_CONSTEXPR void basic_string::reset(pointer val)
noexcept(is_nothrow_allocator_v)
{
reset(val, val ? rexy::strlen(val) : 0);
}
template
REXY_CPP20_CONSTEXPR void basic_string::reset(pointer val, size_type len)
noexcept(is_nothrow_allocator_v)
{
if(this->islong())
this->deallocate(this->get_pointer(),sizeof(value_type)*(this->capacity()+1));
this->set_long_ptr(val);
if constexpr(string_base::uses_sso()){
this->set_long_length(len);
this->set_long_capacity(len);
}else{
this->set_length(len);
this->set_capacity(len);
}
}
template
REXY_CPP20_CONSTEXPR bool basic_string::reserve(size_type newsize)noexcept(is_nothrow_allocator_v){
if(newsize < this->capacity())
return false;
if(!this->islong() && newsize < string_base::short_string_size())
return false;
return (*this = basic_string(this->get_pointer(), newsize)).valid();
}
template
REXY_CPP20_CONSTEXPR void basic_string::shrink_to_fit(void)noexcept(is_nothrow_allocator_v){
if(this->length() == this->capacity()){
return;
}
*this = basic_string(this->get_pointer(), this->length(), this->length());
}
template
REXY_CPP20_CONSTEXPR void basic_string::resize(size_type newsize, value_type v)noexcept(is_nothrow_allocator_v){
const auto len = this->length();
const auto copy_count = std::min(newsize, len);
const auto insert_count = newsize > len ? newsize - len : 0;
basic_string newstr(newsize);
auto* ptr = newstr.data();
rexy::memcpy(ptr, this->data(), copy_count * sizeof(value_type));
ptr += copy_count;
for(size_type i = 0;i < insert_count;++i){
*ptr++ = v;
}
*ptr = 0;
newstr.set_length(newsize);
*this = std::move(newstr);
}
template
REXY_CPP20_CONSTEXPR auto basic_string::insert(size_type pos, size_type insert_count, value_type v)noexcept(is_nothrow_allocator_v) -> basic_string&{
return _insert_impl(pos, detail::value_iterator_adapter{v}, insert_count);
}
template
REXY_CPP20_CONSTEXPR auto basic_string::insert(size_type pos, value_type v)noexcept(is_nothrow_allocator_v) -> basic_string&{
return _insert_impl(pos, &v, 1);
}
template
REXY_CPP20_CONSTEXPR auto basic_string::insert(size_type pos, const_pointer str)noexcept(is_nothrow_allocator_v) -> basic_string&{
return _insert_impl(pos, str, rexy::strlen(str));
}
template
REXY_CPP20_CONSTEXPR auto basic_string::insert(size_type pos, const_pointer str, size_type insert_count)noexcept(is_nothrow_allocator_v) -> basic_string&{
return _insert_impl(pos, str, insert_count);
}
template
REXY_CPP20_CONSTEXPR auto basic_string::insert(size_type pos, const basic_string& other)noexcept(is_nothrow_allocator_v) -> basic_string&{
return _insert_impl(pos, other.begin(), other.length());
}
template
REXY_CPP20_CONSTEXPR auto basic_string::insert(size_type pos, const basic_string& other, size_type index_str, size_type count)noexcept(is_nothrow_allocator_v) -> basic_string&{
return _insert_impl(pos, other.begin() + index_str, count);
}
template
REXY_CPP20_CONSTEXPR auto basic_string::insert(const_iterator pos, value_type v)noexcept(is_nothrow_allocator_v) -> basic_string&{
return _insert_impl(pos - this->begin(), &v, 1);
}
template
REXY_CPP20_CONSTEXPR auto basic_string::insert(const_iterator pos, size_type count, value_type v)noexcept(is_nothrow_allocator_v) -> basic_string&{
return insert(pos - this->begin(), count, v);
}
template
template
REXY_CPP20_CONSTEXPR auto basic_string::insert(const_iterator pos, InputIt start, InputIt last)noexcept(is_nothrow_allocator_v) ->
std::enable_if_t,basic_string&>
{
return insert(pos - this->begin(), std::move(start), std::move(last));
}
template
REXY_CPP20_CONSTEXPR auto basic_string::insert(const_iterator pos, std::initializer_list list)noexcept(is_nothrow_allocator_v) -> basic_string&{
return insert(pos, list.begin(), list.end());
}
template
template
REXY_CPP20_CONSTEXPR auto basic_string::insert(size_type pos, const StringView& sv)noexcept(is_nothrow_allocator_v) ->
std::enable_if_t> && !std::is_convertible_v,basic_string&>
{
return insert(pos, sv.begin(), sv.end());
}
template
template
REXY_CPP20_CONSTEXPR auto basic_string::insert(size_type pos, const StringView& sv, size_type index_str, size_type count)noexcept(is_nothrow_allocator_v) ->
std::enable_if_t> && !std::is_convertible_v,basic_string&>
{
return insert(pos, sv.begin() + index_str, sv.begin() + index_str + count);
}
template
template
REXY_CPP20_CONSTEXPR auto basic_string::insert(size_type pos, InputIt start, InputIt last)noexcept(is_nothrow_allocator_v) ->
std::enable_if_t,basic_string&>
{
size_type insert_count = 0;
for(auto it = start;it != last;++it, ++insert_count){}
return _insert_impl(pos, start, insert_count);
}
template
REXY_CPP20_CONSTEXPR auto basic_string::append(const_pointer data, size_type len)noexcept(is_nothrow_allocator_v) -> basic_string&{
return _insert_impl(this->length(), data, len);
}
template
REXY_CPP20_CONSTEXPR auto basic_string::append(const_pointer data)noexcept(is_nothrow_allocator_v) -> basic_string&{
if(data){
append(data, rexy::strlen(data));
}
return *this;
}
template
REXY_CPP20_CONSTEXPR auto basic_string::append(const basic_string& other)noexcept(is_nothrow_allocator_v) -> basic_string&{
return _insert_impl(this->length(), other.data(), other.length());
}
template
template
REXY_CPP20_CONSTEXPR auto basic_string::append(InputIt start, InputIt fin)noexcept(is_nothrow_allocator_v) ->
std::enable_if_t,basic_string&>
{
return insert(this->length(), start, fin);
}
template
REXY_CPP20_CONSTEXPR void basic_string::push_back(value_type data)
noexcept(is_nothrow_allocator_v)
{
append(&data, 1);
}
template
constexpr void basic_string::pop_back(void)noexcept{
erase(this->end() - 1);
}
template
constexpr auto basic_string::replace(size_type pos, size_type count, const basic_string& str) -> basic_string&{
return replace(pos, count, str.create_view());
}
template