/**
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_VIEW_TPP
#define REXY_STRING_VIEW_TPP
#include "compat/to_address.hpp"
#include "utility.hpp"
#include "string_base.hpp"
#include "algorithm.hpp" //two_way_search
#include "compat/string_base.hpp"
namespace rexy{
template
constexpr basic_string_view::basic_string_view(const_pointer str, size_type len)noexcept:
m_data(str), m_length(len){}
template
constexpr basic_string_view::basic_string_view(const string_base& s)noexcept:
m_data(s.c_str()), m_length(s.length()){}
template
template
constexpr basic_string_view::basic_string_view(InIter start, InIter fin)noexcept:
basic_string_view(compat::to_address(start), fin - start){}
template
constexpr basic_string_view::basic_string_view(const_pointer c)noexcept:
basic_string_view(c, rexy::strlen(c)){}
template
constexpr basic_string_view& basic_string_view::operator=(const_pointer c)noexcept{
m_data = c;
m_length = rexy::strlen(c);
return *this;
}
template
constexpr auto basic_string_view::search(basic_string_view s)const -> const_iterator{
if(s.length() > m_length){
return cend();
}
return two_way_search(cbegin(), cend(), s.cbegin(), s.cend());
}
template
constexpr auto basic_string_view::search(const_pointer c)const -> const_iterator{
basic_string_view tmp(c);
return search(tmp);
}
template
template
constexpr auto basic_string_view::search(basic_string_view s, const Searcher& searcher)const -> const_iterator{
if(s.length() > m_length){
return cend();
}
return searcher(cbegin(), cend(), s.cbegin(), s.cend());
}
template
template
constexpr auto basic_string_view::search(const_pointer c, const Searcher& searcher)const -> const_iterator{
basic_string_view tmp(c);
return search(tmp, searcher);
}
template
constexpr bool basic_string_view::starts_with(basic_string_view sv)const{
if(sv.length() > length()){
return false;
}
auto it = two_way_search(begin(), begin() + sv.length(), sv.cbegin(), sv.cend());
if(it == begin()){
return true;
}
return false;
}
template
constexpr bool basic_string_view::starts_with(value_type v)const{
return front() == v;
}
template
constexpr bool basic_string_view::starts_with(const_pointer s)const{
return starts_with(basic_string_view(s));
}
template
constexpr bool basic_string_view::ends_with(basic_string_view sv)const{
if(sv.length() > length()){
return false;
}
const auto start = end() - sv.length();
auto it = two_way_search(start, end(), sv.cbegin(), sv.cend());
if(it == start){
return true;
}
return false;
}
template
constexpr bool basic_string_view::ends_with(value_type v)const{
return back() == v;
}
template
constexpr bool basic_string_view::ends_with(const_pointer s)const{
return ends_with(basic_string_view(s));
}
template
constexpr bool basic_string_view::contains(basic_string_view sv)const{
const auto it = two_way_search(cbegin(), cend(), sv.cbegin(), sv.cend());
if(it != cend()){
return true;
}
return false;
}
template
constexpr bool basic_string_view::contains(value_type v)const{
for(size_type i = 0;i < length();++i){
if(m_data[i] == v){
return true;
}
}
return false;
}
template
constexpr bool basic_string_view::contains(const_pointer str)const{
return contains(basic_string_view(str));
}
template
constexpr basic_string_view basic_string_view::substr(size_type pos, size_type count)const{
const size_type real_count = rexy::min(count, length() - pos);
return basic_string_view{m_data + pos, real_count};
}
template
constexpr void basic_string_view::remove_prefix(size_type i){
if(i > m_length){
m_data = end();
m_length = 0;
}else{
m_data = begin() + i;
m_length -= i;
}
}
template
constexpr void basic_string_view::remove_suffix(size_type i){
if(i > m_length){
m_length = 0;
}else{
m_length -= i;
}
}
template
constexpr auto basic_string_view::find_first_of(value_type v, size_type start)const -> size_type{
return rexy::find_first_of(*this, &v, start, 1);
}
template
constexpr auto basic_string_view::find_first_of(const_pointer c, size_type start)const -> size_type{
return rexy::find_first_of(*this, c, start, rexy::strlen(c));
}
template
constexpr auto basic_string_view::find_first_of(const_pointer c, size_type start, size_type size)const -> size_type{
return rexy::find_first_of(*this, c, start, size);
}
template
constexpr auto basic_string_view::find_last_of(value_type v, size_type start)const -> size_type{
return rexy::find_last_of(*this, &v, start, 1);
}
template
constexpr auto basic_string_view::find_last_of(const_pointer c, size_type start)const -> size_type{
return rexy::find_last_of(*this, c, start, rexy::strlen(c));
}
template
constexpr auto basic_string_view::find_last_of(const_pointer c, size_type start, size_type size)const -> size_type{
return rexy::find_last_of(*this, c, start, size);
}
}
#endif