From f70899aed10a1f801cec505d051aa3ef55b29b61 Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Thu, 30 Apr 2020 20:47:22 +0200 Subject: [PATCH] Import string_bt from duckhandy Modified to use std::make_index_sequence instead of the equivalent duckhandy utility. --- src/wren/string_bt.hpp | 128 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 128 insertions(+) create mode 100644 src/wren/string_bt.hpp diff --git a/src/wren/string_bt.hpp b/src/wren/string_bt.hpp new file mode 100644 index 0000000..39def96 --- /dev/null +++ b/src/wren/string_bt.hpp @@ -0,0 +1,128 @@ +/* Copyright 2016-2018 Michele Santullo + * This file is part of "duckhandy". + * + * "duckhandy" 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. + * + * "duckhandy" 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 "duckhandy". If not, see . + */ + +#ifndef id170B0E6C34D14EBA9B92A35977BDBFB3 +#define id170B0E6C34D14EBA9B92A35977BDBFB3 + +#include +#include +#include +#include + +namespace dhandy { + namespace bt { + template + class string; + + template + std::basic_ostream& operator<< ( std::basic_ostream& parStream, const string& parString ); + + namespace implem { + template constexpr bool eq (const string& l, const string& r); + } //namespace implem + + template + class string { + friend std::ostream& operator<< <>( std::ostream& parStream, const string& parString ); + friend constexpr bool implem::eq (const string&, const string&); + public: + using value_type = Ch; + constexpr string ( const value_type* parString ); + + constexpr std::size_t size ( void ) const { return S - 1; } + template + constexpr string operator+ ( const string& parOther ) const; + constexpr value_type operator[] ( std::size_t parIndex ) const; + constexpr bool operator== (const string& other) const; + template constexpr bool operator== (const string&) const { return false; } + + template + constexpr string ( Args... ); + + constexpr const value_type (&data_arr() const)[S] { return m_data; } + constexpr const value_type* data() const { return m_data; } + + private: + template + constexpr string ( std::index_sequence, const value_type* parString ); + + const value_type m_data[S]; + }; + + namespace implem { + template + constexpr string concat ( std::index_sequence, const string& parLeft, const string& parRight ) { + return string( + (I < S - 1 ? parLeft[I] : (I < S + S2 - 2 ? parRight[I - (S - 1)] : '\0'))... + ); + } + + template <> constexpr inline bool eq (const string<1>& l, const string<1>& r) { return l[0] == r[0]; } + template + constexpr inline bool eq (const string& l, const string& r) { return l[0] == r[0] and eq(string(l.m_data+1), string(r.m_data+1)); } + } //namespace implem + + template + template + constexpr string::string (std::index_sequence, const value_type* parString) : + m_data{parString[I]...} + { + } + + template + inline constexpr string::string (const value_type* parString) : + string(std::make_index_sequence(), parString) + { + } + + template + template + inline constexpr string::string (Args... parArgs) : + m_data{parArgs...} + { + } + + template + template + constexpr inline string string::operator+ (const string& parOther) const { + return implem::concat(std::make_index_sequence(), string(m_data), parOther); + } + + template + inline std::ostream& operator<< (std::ostream& parStream, const string& parString) { + parStream << parString.m_data; + return parStream; + } + + template + constexpr auto string::operator[] (std::size_t parIndex) const -> value_type { + return (parIndex < S ? m_data[parIndex] : throw std::out_of_range("")); + } + + template + constexpr string make_string (const Ch (&parData)[S]) { + return string(parData); + } + + template + constexpr bool string::operator== (const string& other) const { + return implem::eq(*this, other); + } + } //namespace bt +} //namespace dhandy + +#endif