/** 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" 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, strlen(c)){} template constexpr basic_string_view& basic_string_view::operator=(const_pointer c)noexcept{ m_data = c; m_length = strlen(c); return *this; } template constexpr auto basic_string_view::search(const basic_string_view& s)const -> const_iterator{ 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(const basic_string_view& s, const Searcher& searcher)const -> const_iterator{ 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 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{ if(start >= m_length){ return npos; } for(auto it = begin() + start;it != end();++it){ if(*it == v){ return it - begin(); } } return npos; } template constexpr auto basic_string_view::find_first_of(const_pointer c, size_type start)const -> size_type{ return find_first_of(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{ 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 constexpr auto basic_string_view::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 constexpr auto basic_string_view::find_last_of(const_pointer c, size_type start)const -> size_type{ return find_last_of(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{ 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