rexylib/include/rexy/string_view.tpp

165 lines
4.8 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_VIEW_TPP
#define REXY_STRING_VIEW_TPP
#include "compat/to_address.hpp"
#include "utility.hpp"
#include "string_base.hpp"
namespace rexy{
template<class Char>
constexpr basic_string_view<Char>::basic_string_view(const_pointer str, size_type len)noexcept:
m_data(str), m_length(len){}
template<class Char>
constexpr basic_string_view<Char>::basic_string_view(const string_base<Char>& s)noexcept:
m_data(s.c_str()), m_length(s.length()){}
template<class Char>
template<class InIter>
constexpr basic_string_view<Char>::basic_string_view(InIter start, InIter fin)noexcept:
basic_string_view(compat::to_address(start), fin - start){}
template<class Char>
constexpr basic_string_view<Char>::basic_string_view(const_pointer c)noexcept:
basic_string_view(c, strlen(c)){}
template<class Char>
constexpr basic_string_view<Char>& basic_string_view<Char>::operator=(const_pointer c)noexcept{
m_data = c;
m_length = strlen(c);
return *this;
}
template<class Char>
constexpr auto basic_string_view<Char>::search(const basic_string_view& s)const -> const_iterator{
return two_way_search(cbegin(), cend(), s.cbegin(), s.cend());
}
template<class Char>
constexpr auto basic_string_view<Char>::search(const_pointer c)const -> const_iterator{
basic_string_view tmp(c);
return search(tmp);
}
template<class Char>
template<class Searcher>
constexpr auto basic_string_view<Char>::search(const basic_string_view& s, const Searcher& searcher)const -> const_iterator{
return searcher(cbegin(), cend(), s.cbegin(), s.cend());
}
template<class Char>
template<class Searcher>
constexpr auto basic_string_view<Char>::search(const_pointer c, const Searcher& searcher)const -> const_iterator{
basic_string_view tmp(c);
return search(tmp, searcher);
}
template<class Char>
constexpr basic_string_view<Char> basic_string_view<Char>::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<class Char>
constexpr void basic_string_view<Char>::remove_prefix(size_type i){
if(i > m_length){
m_data = end();
m_length = 0;
}else{
m_data = begin() + i;
m_length -= i;
}
}
template<class Char>
constexpr void basic_string_view<Char>::remove_suffix(size_type i){
if(i > m_length){
m_length = 0;
}else{
m_length -= i;
}
}
template<class Char>
constexpr auto basic_string_view<Char>::find_first_of(value_type v, size_type start)const -> size_type{
if(start >= m_length){
return npos;
}
for(auto it = begin() + start;it != end();++it){
if(*it == v){
return it - begin();
}
}
return npos;
}
template<class Char>
constexpr auto basic_string_view<Char>::find_first_of(const_pointer c, size_type start)const -> size_type{
return find_first_of(c, start, ::rexy::strlen(c));
}
template<class Char>
constexpr auto basic_string_view<Char>::find_first_of(const_pointer c, size_type start, size_type size)const -> size_type{
if(start > m_length){
return npos;
}
for(auto it = begin() + start;it != end();++it){
for(size_type i = 0;i < size;++i){
if(*it == c[i]){
return it - begin();
}
}
}
return npos;
}
template<class Char>
constexpr auto basic_string_view<Char>::find_last_of(value_type v, size_type start)const -> size_type{
if(start >= m_length){
return npos;
}
const auto b = end() - 1;
const auto e = begin() - 1 - start;
for(auto it = b;it != e;--it){
if(*it == v){
return it - begin();
}
}
return npos;
}
template<class Char>
constexpr auto basic_string_view<Char>::find_last_of(const_pointer c, size_type start)const -> size_type{
return find_last_of(c, start, ::rexy::strlen(c));
}
template<class Char>
constexpr auto basic_string_view<Char>::find_last_of(const_pointer c, size_type start, size_type size)const -> size_type{
if(start > m_length){
return npos;
}
const auto b = end() - 1;
const auto e = begin() - 1 - start;
for(auto it = b;it != e;--it){
for(size_type i = 0;i < size;++i){
if(*it == c[i]){
return it - begin();
}
}
}
return npos;
}
}
#endif