From dbb4d0d147503f3bacdff87a7dd71d8a12665fab Mon Sep 17 00:00:00 2001 From: Jessica James Date: Tue, 30 Nov 2021 01:42:17 -0600 Subject: [PATCH] Initial removal of Reference_String and Readable_String classes --- src/common/HTTP_Server.cpp | 21 ++-- src/common/IRC_Client.cpp | 26 ++-- src/common/Socket.cpp | 4 - src/include/Jupiter/HTTP_QueryString.h | 1 - src/include/Jupiter/HTTP_Server.h | 10 +- src/include/Jupiter/IRC_Client.h | 2 +- src/include/Jupiter/Readable_String.h | 105 ++-------------- src/include/Jupiter/Readable_String_Imp.h | 137 --------------------- src/include/Jupiter/Reference_String.h | 82 +----------- src/include/Jupiter/Reference_String_Imp.h | 78 ------------ src/include/Jupiter/Shift_String_Imp.h | 8 +- src/include/Jupiter/Socket.h | 1 - src/include/Jupiter/String.hpp | 33 +++-- src/include/Jupiter/String_Imp.h | 20 +-- src/include/Jupiter/String_Type.h | 66 ++++++++-- src/include/Jupiter/String_Type_Imp.h | 19 +-- 16 files changed, 132 insertions(+), 481 deletions(-) delete mode 100644 src/include/Jupiter/Readable_String_Imp.h delete mode 100644 src/include/Jupiter/Reference_String_Imp.h diff --git a/src/common/HTTP_Server.cpp b/src/common/HTTP_Server.cpp index 795bc74..cb31f07 100644 --- a/src/common/HTTP_Server.cpp +++ b/src/common/HTTP_Server.cpp @@ -75,7 +75,7 @@ Jupiter::HTTP::Server::Content::Content(std::string in_name, Jupiter::HTTP::Serv Jupiter::HTTP::Server::Content::name_checksum = calc_checksum(name); // switch to calcChecksumi to make case-insensitive } -Jupiter::ReadableString *Jupiter::HTTP::Server::Content::execute(std::string_view query_string) { +std::string* Jupiter::HTTP::Server::Content::execute(std::string_view query_string) { return Jupiter::HTTP::Server::Content::function(query_string); } @@ -216,7 +216,7 @@ Jupiter::HTTP::Server::Content *Jupiter::HTTP::Server::Directory::find(std::stri return nullptr; // No such directory } -Jupiter::ReadableString* Jupiter::HTTP::Server::Directory::execute(std::string_view in_name, std::string_view query_string) { +std::string* Jupiter::HTTP::Server::Directory::execute(std::string_view in_name, std::string_view query_string) { Jupiter::HTTP::Server::Content *content_ptr = find(in_name); if (content_ptr == nullptr) return nullptr; @@ -273,8 +273,8 @@ struct Jupiter::HTTP::Server::Data { Jupiter::HTTP::Server::Host *find_host(std::string_view name); Content* find(std::string_view name); Content* find(std::string_view hostname, std::string_view name); - Jupiter::ReadableString *execute(std::string_view name, std::string_view query_string); - Jupiter::ReadableString *execute(std::string_view hostname, std::string_view name, std::string_view query_string); + std::string* execute(std::string_view name, std::string_view query_string); + std::string* execute(std::string_view hostname, std::string_view name, std::string_view query_string); int process_request(HTTPSession &session); @@ -391,7 +391,7 @@ Jupiter::HTTP::Server::Content *Jupiter::HTTP::Server::Data::find(std::string_vi return host->find(name); } -Jupiter::ReadableString *Jupiter::HTTP::Server::Data::execute(std::string_view name, std::string_view query_string) { +std::string* Jupiter::HTTP::Server::Data::execute(std::string_view name, std::string_view query_string) { Jupiter::HTTP::Server::Content *content = find(name); if (content == nullptr) return nullptr; @@ -399,7 +399,7 @@ Jupiter::ReadableString *Jupiter::HTTP::Server::Data::execute(std::string_view n return content->execute(query_string); } -Jupiter::ReadableString *Jupiter::HTTP::Server::Data::execute(std::string_view hostname, std::string_view name, std::string_view query_string) { +std::string* Jupiter::HTTP::Server::Data::execute(std::string_view hostname, std::string_view name, std::string_view query_string) { Jupiter::HTTP::Server::Content *content = find(hostname, name); if (content == nullptr) return nullptr; @@ -442,7 +442,8 @@ int Jupiter::HTTP::Server::Data::process_request(HTTPSession &session) { if (!line.empty()) // end of http request { - Jupiter::String result(256); + std::string result; + result.reserve(256); switch (command) { case HTTPCommand::GET: @@ -451,7 +452,7 @@ int Jupiter::HTTP::Server::Data::process_request(HTTPSession &session) { { // 200 (success) // TODO: remove referencestring warpper - Jupiter::ReadableString *content_result = content->execute(Jupiter::ReferenceString{query_string}); + std::string* content_result = content->execute(Jupiter::ReferenceString{query_string}); switch (session.version) { @@ -703,11 +704,11 @@ Jupiter::HTTP::Server::Content *Jupiter::HTTP::Server::find(std::string_view hos return m_data->find(host, name); } -Jupiter::ReadableString *Jupiter::HTTP::Server::execute(std::string_view name, std::string_view query_string) { +std::string* Jupiter::HTTP::Server::execute(std::string_view name, std::string_view query_string) { return m_data->execute(name, query_string); } -Jupiter::ReadableString *Jupiter::HTTP::Server::execute(std::string_view host, std::string_view name, std::string_view query_string) { +std::string* Jupiter::HTTP::Server::execute(std::string_view host, std::string_view name, std::string_view query_string) { return m_data->execute(host, name, query_string); } diff --git a/src/common/IRC_Client.cpp b/src/common/IRC_Client.cpp index a2594ee..d36b0ce 100644 --- a/src/common/IRC_Client.cpp +++ b/src/common/IRC_Client.cpp @@ -445,7 +445,7 @@ int Jupiter::IRC::Client::process_line(std::string_view in_line) { case Error::UNKNOWNCOMMAND: // 421 { Jupiter::ReferenceString command = getLineToken(2); - if (command.equalsi("STARTTLS")) { // Server doesn't support STARTTLS + if (jessilib::equalsi(command, "STARTTLS"sv)) { // Server doesn't support STARTTLS Client::startCAP(); } } @@ -492,7 +492,7 @@ int Jupiter::IRC::Client::process_line(std::string_view in_line) { switch (numeric) { case 0: - if (getLineToken(1).equalsi("CAP")) + if (jessilib::equalsi(getLineToken(1), "CAP"sv)) { Jupiter::ReferenceString w4 = getLineToken(3); if (w4 == "LS"sv) @@ -577,7 +577,7 @@ int Jupiter::IRC::Client::process_line(std::string_view in_line) { m_nickname = configNick; m_nickname += "1"; - m_socket->send("NICK "_jrs + m_nickname + ENDL); + m_socket->send("NICK "s + m_nickname + ENDL); } else if (jessilib::equalsi(m_nickname, configNick)) // The config nick failed { @@ -591,7 +591,7 @@ int Jupiter::IRC::Client::process_line(std::string_view in_line) { else m_nickname = altNick; - m_socket->send("NICK "_jrs + m_nickname + ENDL); + m_socket->send("NICK "s + m_nickname + ENDL); } // Note: Add a series of contains() functions to String_Type. else @@ -604,7 +604,7 @@ int Jupiter::IRC::Client::process_line(std::string_view in_line) { m_nickname = configNick; m_nickname += std::to_string(n + 1); - m_socket->send("NICK "_jrs + m_nickname + ENDL); + m_socket->send("NICK "s + m_nickname + ENDL); } else { @@ -612,7 +612,7 @@ int Jupiter::IRC::Client::process_line(std::string_view in_line) { // This can be somewhat edgy -- this will only trigger if someone rehashes AND the new nickname is shorter. // However, it won't be fatal even if the new nickname's length is >= the old. m_nickname = configNick; - m_socket->send("NICK "_jrs + m_nickname + ENDL); + m_socket->send("NICK "s + m_nickname + ENDL); } } else @@ -817,7 +817,7 @@ int Jupiter::IRC::Client::process_line(std::string_view in_line) { if (!newnick.empty() && newnick[0] == ':') { newnick.remove_prefix(1); } - if (nick.equalsi(m_nickname)) { + if (jessilib::equalsi(nick, m_nickname)) { m_nickname = newnick; } auto user = Client::findUser(nick); @@ -884,7 +884,7 @@ int Jupiter::IRC::Client::process_line(std::string_view in_line) { plugin->OnPart(this, channel_name, nick, reason); } - if (nick.equalsi(m_nickname)) + if (jessilib::equalsi(nick, m_nickname)) Client::delChannel(channel_name); if (user->getChannelCount() == 0) @@ -1092,7 +1092,7 @@ int Jupiter::IRC::Client::process_line(std::string_view in_line) { Jupiter::StringS auth_str = m_nickname + '\0' + m_sasl_account + '\0' + m_sasl_password; char *enc = Jupiter::base64encode(auth_str.data(), auth_str.size()); - m_socket->send("AUTHENTICATE "_jrs + enc + ENDL); + m_socket->send("AUTHENTICATE "s + enc + ENDL); delete[] enc; } m_socket->send("CAP END" ENDL); @@ -1519,8 +1519,12 @@ void Jupiter::IRC::Client::Channel::addUserPrefix(std::string_view in_nickname, void Jupiter::IRC::Client::Channel::delUserPrefix(std::string_view in_nickname, char prefix) { auto user = getUser(in_nickname); - if (user != nullptr) - user->m_prefixes.remove(prefix); + if (user != nullptr) { + size_t prefix_pos = user->m_prefixes.find(prefix); + if (prefix_pos != std::string::npos) { + user->m_prefixes.erase(prefix_pos, 1); + } + } } const Jupiter::ReadableString &Jupiter::IRC::Client::Channel::getName() const { diff --git a/src/common/Socket.cpp b/src/common/Socket.cpp index ed01d1b..217b009 100644 --- a/src/common/Socket.cpp +++ b/src/common/Socket.cpp @@ -501,10 +501,6 @@ int Jupiter::Socket::send(const char *data, size_t datalen) { return ::send(Jupiter::Socket::data_->rawSock, data, datalen, 0); } -int Jupiter::Socket::send(const Jupiter::ReadableString &str) { - return this->send(str.data(), str.size()); -} - int Jupiter::Socket::send(std::string_view str) { return this->send(str.data(), str.size()); } diff --git a/src/include/Jupiter/HTTP_QueryString.h b/src/include/Jupiter/HTTP_QueryString.h index f7a8c02..f068b11 100644 --- a/src/include/Jupiter/HTTP_QueryString.h +++ b/src/include/Jupiter/HTTP_QueryString.h @@ -52,7 +52,6 @@ namespace Jupiter public: HTMLFormResponse() = delete; inline HTMLFormResponse(std::string_view query_string) : HTMLFormResponse(query_string.data(), query_string.size()) {} - inline HTMLFormResponse(const Jupiter::ReadableString &query_string) : HTMLFormResponse(query_string.data(), query_string.size()) {} inline HTMLFormResponse(const char *ptr, size_t str_size); using TableType = std::unordered_map, std::equal_to<>>; #ifdef __cpp_lib_generic_unordered_lookup diff --git a/src/include/Jupiter/HTTP_Server.h b/src/include/Jupiter/HTTP_Server.h index 2255a7a..8687fcd 100644 --- a/src/include/Jupiter/HTTP_Server.h +++ b/src/include/Jupiter/HTTP_Server.h @@ -45,7 +45,7 @@ namespace Jupiter virtual int think(); public: // Server - typedef Jupiter::ReadableString *HTTPFunction(std::string_view query_string); + typedef std::string* HTTPFunction(std::string_view query_string); static const Jupiter::ReadableString &global_namespace; static const Jupiter::ReadableString &server_string; @@ -59,7 +59,7 @@ namespace Jupiter const Jupiter::ReadableString *type = nullptr; // Pointer to a constant (or otherwise managed) string const Jupiter::ReadableString *charset = nullptr; // Pointer to a constant (or otherwise managed) string - virtual Jupiter::ReadableString *execute(std::string_view query_string); + virtual std::string* execute(std::string_view query_string); Content(std::string in_name, Jupiter::HTTP::Server::HTTPFunction in_function); }; @@ -76,7 +76,7 @@ namespace Jupiter virtual bool remove(std::string_view path, std::string_view name); virtual bool has(std::string_view name); virtual Jupiter::HTTP::Server::Content* find(std::string_view name); - virtual Jupiter::ReadableString* execute(std::string_view name, std::string_view query_string); + virtual std::string* execute(std::string_view name, std::string_view query_string); Directory(const Directory&) = delete; Directory& operator=(const Directory&) = delete; @@ -97,8 +97,8 @@ namespace Jupiter bool has(std::string_view host, std::string_view name); Content *find(std::string_view name); Content *find(std::string_view host, std::string_view name); - Jupiter::ReadableString *execute(std::string_view name, std::string_view query_string); - Jupiter::ReadableString *execute(std::string_view host, std::string_view name, std::string_view query_string); + std::string* execute(std::string_view name, std::string_view query_string); + std::string* execute(std::string_view host, std::string_view name, std::string_view query_string); bool bind(std::string_view hostname, uint16_t port = 80); bool tls_bind(std::string_view hostname, uint16_t port = 443); diff --git a/src/include/Jupiter/IRC_Client.h b/src/include/Jupiter/IRC_Client.h index 30eec2f..42e0081 100644 --- a/src/include/Jupiter/IRC_Client.h +++ b/src/include/Jupiter/IRC_Client.h @@ -312,7 +312,7 @@ namespace Jupiter /** Private members */ private: std::shared_ptr m_user; - Jupiter::StringS m_prefixes; + std::string m_prefixes; }; using UserTableType = std::unordered_map, default_hash_function>; diff --git a/src/include/Jupiter/Readable_String.h b/src/include/Jupiter/Readable_String.h index f0aa2b1..4f9e959 100644 --- a/src/include/Jupiter/Readable_String.h +++ b/src/include/Jupiter/Readable_String.h @@ -33,102 +33,13 @@ #include "DataBuffer.h" #include "Functions.h" -namespace Jupiter -{ - - /** - * @brief Provides the basis for String classes by providing implementations for operators, comparative operations, and defining abstract functions. - * Note: This is an abstract type. - * - * @param T Element type which the String will store. Defaults to char. - */ - template class Readable_String - { - public: - - /** - * @brief Returns a pointer to the underlying string of elements. - * - * @return Pointer to the underlying string of elements. - */ - virtual const T *data() const = 0; // KEEP - - /** - * @brief Returns the number of elements in the String. - * - * @return Number of elements in the string. - */ - virtual size_t size() const = 0; // KEEP - - /** - * @brief Checks if the String is empty. - * - * @return True if the String is empty, false otherwise. - */ - bool empty() const { return size() == 0; }; // KEEP - - /** - * @brief Returns the index of the first element in the string with the specified value. - * - * @param value Value of the element to search for. - * @param index Index of the match to return (i.e: 0 returns the first match). - * @return The index of an element if one is found, INVALID_INDEX otherwise. - */ - size_t find(const T &value, size_t index = 0) const; // KEEP - size_t find(const Readable_String &in) const; // KEEP - - /** - * @brief Checks if the strings are equal. - * Note: Case insensitive. Returns false for any type other than char and wchar_t. - * - * @param in String to compare against. - * @return True if the contents of the strings are equal, false otherwise. - */ - bool equalsi(const Readable_String &in) const; // REMOVE - bool equalsi(const std::basic_string &in) const; // REMOVE - bool equalsi(const T *in, size_t len) const; // REMOVE - bool equalsi(const T &in) const; // REMOVE - - /** - * @brief Destructor for the Readable_String class. - */ - virtual ~Readable_String() = default; - - /** Access operator */ - inline const T &operator[](size_t index) const { return this->data()[index]; }; - - /** Comparative operators */ - inline bool operator==(const Readable_String& right)const{ return operator==(std::basic_string_view{right}); } - inline bool operator==(const std::basic_string& right)const{ return operator==(std::basic_string_view{right}); } - inline bool operator==(const std::basic_string_view& right)const{ return std::basic_string_view(data(), size()) == right; } - inline bool operator==(const T right)const{ return this->size() == 1 && this->get(0) == right; } - inline bool operator==(std::nullptr_t) = delete; - inline bool operator!=(const Readable_String &right)const{ return !operator==(right); } - inline bool operator!=(const std::basic_string &right)const{ return !operator==(right); } - inline bool operator!=(const T right)const{ return !operator==(right); } - inline bool operator!=(std::nullptr_t) = delete; - inline bool operator<(const Readable_String &right)const{ return this->compare(right) < 0; } - inline bool operator<(const std::basic_string &right)const{ return this->compare(right) < 0; } - inline bool operator<(const T right)const{ return this->compare(right) < 0; } - inline bool operator>(const Readable_String &right)const{ return this->compare(right) > 0; } - inline bool operator>(const std::basic_string &right)const{ return this->compare(right) > 0; } - inline bool operator>(const T right)const{ return this->compare(right) > 0; } - inline bool operator<=(const Readable_String &right)const{ return !operator>(right); } - inline bool operator<=(const std::basic_string &right)const{ return !operator>(right); } - inline bool operator<=(const T right)const{ return !operator>(right); } - inline bool operator>=(const Readable_String &right)const{ return !operator<(right); } - inline bool operator>=(const std::basic_string &right)const{ return !operator<(right); } - inline bool operator>=(const T right)const{ return !operator<(right); } - - /** Conversion operators */ - explicit inline operator std::basic_string() const { return std::basic_string(this->data(), this->size()); } - inline operator std::basic_string_view() const { return std::basic_string_view(this->data(), this->size()); } - }; - - /** Generic Readable String Type */ - typedef Readable_String ReadableString; - - // TEMP; create some equivalent in jessilib +namespace Jupiter { + /** DEPRECATED: Generic Readable String Type */ + template + using Readable_String = std::basic_string_view; + using ReadableString = std::string_view; + + // these methods will be written in a similar fashion in jessilib, to retain behavior / ease of use template bool asBool(std::basic_string_view in_string) { using namespace std::literals; @@ -248,6 +159,4 @@ namespace Jupiter } } -#include "Readable_String_Imp.h" - #endif // _READABLE_STRING_H_HEADER \ No newline at end of file diff --git a/src/include/Jupiter/Readable_String_Imp.h b/src/include/Jupiter/Readable_String_Imp.h deleted file mode 100644 index 9384d6c..0000000 --- a/src/include/Jupiter/Readable_String_Imp.h +++ /dev/null @@ -1,137 +0,0 @@ -/** - * Copyright (C) 2014-2017 Jessica James. - * - * Permission to use, copy, modify, and/or distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY - * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION - * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN - * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - * - * Written by Jessica James - */ - -#if !defined _READABLE_STRING_IMP_H_HEADER -#define _READABLE_STRING_IMP_H_HEADER - -/** - * @file Readable_String_Imp.h - * @brief Provides the implementations for Readable_String functions. - * Note: Modification of this file is not supported in any way. - */ - -#include "Readable_String.h" - -/** -* IMPLEMENTATION: -* Readable_String -*/ - -// find - -template size_t Jupiter::Readable_String::find(const T &value, size_t index) const -{ - for (size_t i = 0; i != this->size(); i++) - { - if (operator[](i) == value) - { - if (index == 0) return i; - else index--; - } - } - return Jupiter::INVALID_INDEX; -} - -template size_t Jupiter::Readable_String::find(const Jupiter::Readable_String &in) const -{ - if (in.size() > this->size()) - return Jupiter::INVALID_INDEX; - if (in.size() == this->size()) - return (*this == in) ? 0 : Jupiter::INVALID_INDEX; - if (in.empty()) - return 0; - - for (size_t i = 0, j; i != this->size() - in.size() + 1; i++) - { - j = 0; - while (operator[](i + j) == in[j]) - if (++j == in.size()) return i; - } - return Jupiter::INVALID_INDEX; -} - -// equalsi() - -template bool Jupiter::Readable_String::equalsi(const Jupiter::Readable_String &in) const -{ - return this->equalsi(in.data(), in.size()); -} - -template bool Jupiter::Readable_String::equalsi(const std::basic_string &in) const -{ - return this->equalsi(in.data(), in.size()); -} - -template<> bool inline Jupiter::Readable_String::equalsi(const char *in, size_t len) const -{ - if (this->size() != len) - return false; - - in += len; - while (len != 0) - if (toupper(operator[](--len)) != toupper(*(--in))) - return false; - - return true; -} - -template<> bool inline Jupiter::Readable_String::equalsi(const wchar_t *in, size_t len) const -{ - if (this->size() != len) - return false; - - while (len != 0) - if (towupper(operator[](--len)) != towupper(*(--in))) - return false; - - return true; -} - -template bool Jupiter::Readable_String::equalsi(const T *in, size_t len) const -{ - return *this == std::basic_string_view(in); // Concept of "case" not supported for type. -} - -template<> bool inline Jupiter::Readable_String::equalsi(const char &in) const -{ - return this->size() == 1 && toupper(operator[](0)) == toupper(in); -} - -template<> bool inline Jupiter::Readable_String::equalsi(const wchar_t &in) const -{ - return this->size() == 1 && towupper(operator[](0)) == towupper(in); -} - -template bool Jupiter::Readable_String::equalsi(const T &in) const -{ - return *this == in; // Concept of "case" not supported for type. -} - -// Jupiter::DataBuffer specialization - -template<> struct _Jupiter_DataBuffer_partial_specialization_impl -{ - template static void push(Jupiter::DataBuffer *buffer, const Jupiter::Readable_String *data) - { - buffer->secure(sizeof(size_t) + data->size() * sizeof(Y)); - buffer->push(data->size()); - buffer->push(reinterpret_cast(data->data()), data->size() * sizeof(Y)); - }; -}; - -#endif // _READABLE_STRING_IMP_H_HEADER \ No newline at end of file diff --git a/src/include/Jupiter/Reference_String.h b/src/include/Jupiter/Reference_String.h index 4eac17d..a32f68f 100644 --- a/src/include/Jupiter/Reference_String.h +++ b/src/include/Jupiter/Reference_String.h @@ -28,84 +28,12 @@ #include #include "Readable_String.h" -namespace Jupiter -{ - - /** - * @brief Provides the basis for String classes by providing implementations for operators, comparative operations, and defining abstract functions. - * Note: This is an abstract type. - * - * @param T Element type which the String will store. Defaults to char. - */ - template class Reference_String : public Jupiter::Readable_String, public std::basic_string_view { - /** DEPRECATED IN FAVOR OF std::string_view */ - public: - - /** - * @brief Returns the number of elements in the String. - * - * @return Number of elements in the string. - */ - size_t size() const override; - - /** - * @brief Returns a pointer to the underlying string of elements. - * - * @return Pointer to the underlying string of elements. - */ - const T* data() const override; - - /** Mutative operators */ - inline Reference_String& operator=(const Readable_String &right) { std::basic_string_view::operator=({ - right.data(), right.size()}); return *this; }; - inline Reference_String& operator=(const Reference_String &right) = default; - inline Reference_String& operator=(const std::basic_string &right) { std::basic_string_view::operator=(right); return *this; }; - inline Reference_String& operator=(const std::basic_string_view &right) { std::basic_string_view::operator=(right); return *this; }; - inline Reference_String& operator=(const T *right) { std::basic_string_view::operator=(right); return *this; }; - - /** - * @brief Default constructor for the Reference_String class. - */ - Reference_String() = default; - - /** - * @brief Creates a reference to a C-Style string. - * Difference from basic_string_view: checks nullptr - * - * @param in String to get a reference of. - */ - Reference_String(const T *in); - - /** - * @brief Move constructor for the Reference_String class. - */ - Reference_String(Jupiter::Reference_String &&source) = default; - - /** - * @brief Constructor for the Refererence_String class to create a reference to an existing string. - */ - Reference_String(const Jupiter::Readable_String &in); - - /** - * @brief Copy constructor for the Reference_String class. - */ - Reference_String(const Jupiter::Reference_String &in) = default; - Reference_String(const std::basic_string_view&in) : std::basic_string_view(in){} - - // Bring in constructors from basic_string_view - using std::basic_string_view::basic_string_view; - - /** Methods to force disambiguation between bases until this class is removed entirely */ - using std::basic_string_view::find; - using std::basic_string_view::operator[]; - using std::basic_string_view::empty; - }; - - /** Generic Reference String Type */ - typedef Reference_String ReferenceString; +namespace Jupiter { + /** DEPRECATED: Generic Reference String Type */ + using ReferenceString = std::string_view; namespace literals { - /** Reference_String literals */ + /** DEPRECATED: Reference_String literals */ inline Jupiter::ReferenceString operator"" _jrs(const char *str, size_t len) { return Jupiter::ReferenceString(str, len); } } } @@ -114,6 +42,4 @@ namespace Jupiter #define STRING_LITERAL_AS_REFERENCE(str) Jupiter::literals::operator"" _jrs(str, sizeof(str) - 1) #define STRING_LITERAL_AS_NAMED_REFERENCE(name, str) Jupiter::ReferenceString name = STRING_LITERAL_AS_REFERENCE(str) -#include "Reference_String_Imp.h" - #endif // _REFERENCE_STRING_H_HEADER \ No newline at end of file diff --git a/src/include/Jupiter/Reference_String_Imp.h b/src/include/Jupiter/Reference_String_Imp.h deleted file mode 100644 index 861afcc..0000000 --- a/src/include/Jupiter/Reference_String_Imp.h +++ /dev/null @@ -1,78 +0,0 @@ -/** - * Copyright (C) 2014-2017 Jessica James. - * - * Permission to use, copy, modify, and/or distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY - * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION - * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN - * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - * - * Written by Jessica James - */ - -#if !defined _REFERENCE_STRING_IMP_H_HEADER -#define _REFERENCE_STRING_IMP_H_HEADER - -/** - * @file Readable_String_Imp.h - * @brief Provides the implementations for Readable_String functions. - * Note: Modification of this file is not supported in any way. - */ - -#include "Reference_String.h" -#include // std::min according to standard -#include // std::min on MSVC -#include "Functions.h" - -// A hack due to some Windows.h inclusion somewhere, ugh -#ifdef min -#undef min -#endif - -/** -* IMPLEMENTATION: -* Reference_String -*/ - -template Jupiter::Reference_String::Reference_String(const T *in) - : std::basic_string_view{} { - if (in != nullptr) { - *this = in; - } -} - -template Jupiter::Reference_String::Reference_String(const Jupiter::Readable_String& in) - : Reference_String(in.data(), in.size()) { -} - -template size_t Jupiter::Reference_String::size() const { - return std::basic_string_view::size(); -} - -template const T* Jupiter::Reference_String::data() const { - return std::basic_string_view::data(); -} - -// Jupiter::DataBuffer specialization - -template<> struct _Jupiter_DataBuffer_partial_specialization_impl { - template static void push(Jupiter::DataBuffer *buffer, const Jupiter::Reference_String *data) { - _Jupiter_DataBuffer_partial_specialization_impl::push(buffer, data); - }; - - template static Jupiter::Reference_String interpret(uint8_t *&head) { - size_t size_ = *reinterpret_cast(head); - head += sizeof(size_t); - Jupiter::Reference_String r = Jupiter::Reference_String(reinterpret_cast(head), size_); - head += size_; - return r; - } -}; - -#endif // _REFERENCE_STRING_IMP \ No newline at end of file diff --git a/src/include/Jupiter/Shift_String_Imp.h b/src/include/Jupiter/Shift_String_Imp.h index 6f60060..0f89f71 100644 --- a/src/include/Jupiter/Shift_String_Imp.h +++ b/src/include/Jupiter/Shift_String_Imp.h @@ -121,11 +121,9 @@ template bool Jupiter::Shift_String_Type::setBufferSizeNoCopy(siz // Jupiter::DataBuffer specialization -template<> struct _Jupiter_DataBuffer_partial_specialization_impl -{ - template static void push(Jupiter::DataBuffer *buffer, const Jupiter::Shift_String_Type *data) - { - _Jupiter_DataBuffer_partial_specialization_impl::push(buffer, data); +template<> struct _Jupiter_DataBuffer_partial_specialization_impl { + template static void push(Jupiter::DataBuffer *buffer, const Jupiter::Shift_String_Type *data) { + _Jupiter_DataBuffer_partial_specialization_impl::push(buffer, data); }; }; diff --git a/src/include/Jupiter/Socket.h b/src/include/Jupiter/Socket.h index 7e412fa..c3a2dc8 100644 --- a/src/include/Jupiter/Socket.h +++ b/src/include/Jupiter/Socket.h @@ -437,7 +437,6 @@ namespace Jupiter * @return Number of bytes sent on success, SOCKET_ERROR (-1) otherwise. * Note: Any returned value less than or equal to 0 should be treated as an error. */ - int send(const Jupiter::ReadableString &str); int send(std::string_view str); /** diff --git a/src/include/Jupiter/String.hpp b/src/include/Jupiter/String.hpp index 8485acb..f30734e 100644 --- a/src/include/Jupiter/String.hpp +++ b/src/include/Jupiter/String.hpp @@ -133,7 +133,6 @@ namespace Jupiter String_Strict(const String_Strict &in); String_Strict(const Readable_String &in); String_Strict(const std::basic_string &in); - explicit String_Strict(const std::basic_string_view &in) : String_Strict(in.data(), in.size()){}; String_Strict(const T *in, size_t len); String_Strict(const T *in); String_Strict(const Jupiter::DataBuffer &in); @@ -161,11 +160,12 @@ namespace Jupiter }; /** String_Strict Addition Operators */ - template static inline Jupiter::String_Strict operator+(const Jupiter::Readable_String &lhs, const Jupiter::Readable_String &rhs); - template static inline Jupiter::String_Strict operator+(const Jupiter::Readable_String &lhs, const T &rhs); - template static inline Jupiter::String_Strict operator+(const Jupiter::Readable_String &lhs, const Jupiter::Readable_String &rhs); - template static inline Jupiter::String_Strict operator+(const Jupiter::Readable_String &lhs, const std::basic_string &rhs); - template static inline Jupiter::String_Strict operator+(const Jupiter::Readable_String &lhs, const T *rhs); + template static inline Jupiter::String_Strict operator+(const Jupiter::String_Type &lhs, const Jupiter::String_Type &rhs); + template static inline Jupiter::String_Strict operator+(const Jupiter::String_Type &lhs, const T &rhs); + template static inline Jupiter::String_Strict operator+(const Jupiter::String_Type &lhs, const Jupiter::String_Type &rhs); + template static inline Jupiter::String_Strict operator+(const Jupiter::String_Type &lhs, const std::basic_string &rhs); + template static inline Jupiter::String_Strict operator+(const Jupiter::String_Type &lhs, const std::basic_string_view &rhs); + template static inline Jupiter::String_Strict operator+(const Jupiter::String_Type &lhs, const T *rhs); /** * @brief Provides a "loose" String implementation that's more optimized for repeated concatenations. @@ -324,26 +324,16 @@ namespace Jupiter typedef String_Strict StringS; /** Definition of a Loose String. */ - typedef String_Loose StringL; + typedef String_Strict StringL; /** Definition of a String. */ - typedef StringL String; + typedef StringS String; namespace literals { /** String_Strict literals */ inline Jupiter::StringS operator""_jss(const char *str, size_t len) { return Jupiter::StringS(str, len); } } - // Carried over from Hash_Table.h for compatibility - struct default_hash_function { - size_t operator()(const Jupiter::ReadableString& in) const { - if constexpr (sizeof(size_t) >= sizeof(uint64_t)) - return static_cast(Jupiter::fnv1a(in)); - - return static_cast(Jupiter::fnv1a_32(in)); - } - }; - template struct str_hash { using is_transparent = std::true_type; @@ -357,7 +347,14 @@ namespace Jupiter auto operator()(const std::basic_string& in_key) const noexcept { return std::hash>()(in_key); } + + // DEPRECATED: + auto operator()(const String_Type& in_key) const noexcept { + return operator()(static_cast>(in_key)); + } }; + + using default_hash_function = str_hash; } /** Re-enable warning */ diff --git a/src/include/Jupiter/String_Imp.h b/src/include/Jupiter/String_Imp.h index eb81a97..461ae4d 100644 --- a/src/include/Jupiter/String_Imp.h +++ b/src/include/Jupiter/String_Imp.h @@ -313,22 +313,27 @@ template inline Jupiter::String_Strict Jupiter::String_Strict: return Jupiter::operator+(*this, rhs); } -template static inline Jupiter::String_Strict Jupiter::operator+(const Jupiter::Readable_String &lhs, const T &rhs) +template static inline Jupiter::String_Strict Jupiter::operator+(const Jupiter::String_Type &lhs, const T &rhs) { return Jupiter::String_Strict(lhs, rhs); } -template static inline Jupiter::String_Strict Jupiter::operator+(const Jupiter::Readable_String &lhs, const Jupiter::Readable_String &rhs) +template static inline Jupiter::String_Strict Jupiter::operator+(const Jupiter::String_Type &lhs, const Jupiter::String_Type &rhs) { return Jupiter::String_Strict(lhs, rhs); } -template static inline Jupiter::String_Strict Jupiter::operator+(const Jupiter::Readable_String &lhs, const std::basic_string &rhs) +template static inline Jupiter::String_Strict Jupiter::operator+(const Jupiter::String_Type &lhs, const std::basic_string &rhs) { return Jupiter::String_Strict(lhs, rhs); } -template static inline Jupiter::String_Strict Jupiter::operator+(const Jupiter::Readable_String &lhs, const T *rhs) +template static inline Jupiter::String_Strict Jupiter::operator+(const Jupiter::String_Type &lhs, const std::basic_string_view &rhs) +{ + return Jupiter::String_Strict(lhs, rhs); +} + +template static inline Jupiter::String_Strict Jupiter::operator+(const Jupiter::String_Type &lhs, const T *rhs) { return Jupiter::String_Strict(lhs, rhs); } @@ -337,9 +342,8 @@ template static inline Jupiter::String_Strict Jupiter::operator+( template<> struct _Jupiter_DataBuffer_partial_specialization_impl { - template static void push(Jupiter::DataBuffer *buffer, const Jupiter::String_Strict *data) - { - _Jupiter_DataBuffer_partial_specialization_impl::push(buffer, data); + template static void push(Jupiter::DataBuffer *buffer, const Jupiter::String_Strict *data) { + _Jupiter_DataBuffer_partial_specialization_impl::push(buffer, data); }; template static Jupiter::String_Strict interpret(uint8_t *&head) @@ -706,7 +710,7 @@ template<> struct _Jupiter_DataBuffer_partial_specialization_impl static void push(Jupiter::DataBuffer *buffer, const Jupiter::String_Loose *data) { - _Jupiter_DataBuffer_partial_specialization_impl::push(buffer, data); + _Jupiter_DataBuffer_partial_specialization_impl::push(buffer, data); }; template static Jupiter::String_Loose interpret(uint8_t *&head) diff --git a/src/include/Jupiter/String_Type.h b/src/include/Jupiter/String_Type.h index bd25be2..f3de50f 100644 --- a/src/include/Jupiter/String_Type.h +++ b/src/include/Jupiter/String_Type.h @@ -43,7 +43,7 @@ namespace Jupiter * * @param T Element type which the String will store. Defaults to char. */ - template class String_Type : public Jupiter::Readable_String + template class String_Type { public: @@ -52,7 +52,7 @@ namespace Jupiter * * @return Number of elements in the string. */ - size_t size() const override; + size_t size() const; /** * @brief Returns the maximum number of elements the String can contain, @@ -68,7 +68,7 @@ namespace Jupiter * * @return Pointer to the underlying string of elements. */ - const T *data() const override; + const T *data() const; /** * @brief Sets the String's contents based on the format string and input variables. @@ -183,7 +183,6 @@ namespace Jupiter */ virtual size_t replace(size_t index, size_t length, const T &value); virtual size_t replace(size_t index, size_t length, const T *value, size_t valueSize); - virtual size_t replace(size_t index, size_t length, const Jupiter::Readable_String &value); virtual size_t replace(size_t index, size_t length, std::basic_string_view value); /** @@ -197,13 +196,9 @@ namespace Jupiter virtual size_t replace(const T *target, size_t targetSize, const T &value); virtual size_t replace(const Jupiter::Readable_String &target, const T &value); - //virtual size_t replace(const T &target, const T *value, size_t valueSize); - //virtual size_t replace(const T &target, const Jupiter::Readable_String &value); - virtual size_t replace(const T *target, size_t targetSize, const T *value, size_t valueSize); virtual size_t replace(const T *target, size_t targetSize, const Jupiter::Readable_String &value); virtual size_t replace(const Jupiter::Readable_String &target, const T *value, size_t valueSize); - virtual size_t replace(const Jupiter::Readable_String &target, const Jupiter::Readable_String &value); virtual size_t replace(std::basic_string_view target, std::basic_string_view value); /** @@ -262,21 +257,18 @@ namespace Jupiter virtual bool setBufferSizeNoCopy(size_t len) = 0; /** Mutative operators */ - inline String_Type &operator+=(const Readable_String &right) { this->concat(right); return *this; }; inline String_Type &operator+=(const String_Type &right) { this->concat(right); return *this; }; inline String_Type &operator+=(const std::basic_string &right) { this->concat(right); return *this; }; + inline String_Type &operator+=(const std::basic_string_view &right) { this->concat(right); return *this; }; inline String_Type &operator+=(const T *right) { this->concat(right); return *this; }; inline String_Type &operator+=(const T right) { this->concat(right); return *this; }; inline String_Type &operator-=(size_t right) { this->truncate(right); return *this; }; - inline String_Type &operator=(const Readable_String &right) { this->set(right); return *this; }; inline String_Type &operator=(const String_Type &right) { this->set(right); return *this; }; inline String_Type &operator=(const std::basic_string &right) { this->set(right); return *this; }; inline String_Type &operator=(const std::basic_string_view &right) { this->set(right.data(), right.size()); return *this; }; inline String_Type &operator=(const T *right) { this->set(right); return *this; }; inline String_Type &operator=(const T right) { this->set(right); return *this; }; - using Readable_String::operator[]; - /** * @brief Default constructor for the String_Type class. */ @@ -303,11 +295,61 @@ namespace Jupiter */ virtual ~String_Type() = default; + /** + * Necessities when removing Readable_String & Reference_String + */ + bool empty() const { return size() == 0; }; // KEEP + + /** Access operator */ + inline const T &operator[](size_t index) const { return this->data()[index]; }; + + /** Conversion operators */ + explicit inline operator std::basic_string() const { return std::basic_string(this->data(), this->size()); } + inline operator std::basic_string_view() const { return std::basic_string_view(this->data(), this->size()); } + + /** Comparative operators */ + inline bool operator==(const String_Type& right)const{ return operator==(std::basic_string_view{right}); } + inline bool operator==(const std::basic_string& right)const{ return operator==(std::basic_string_view{right}); } + inline bool operator==(const std::basic_string_view& right)const{ return std::basic_string_view(data(), size()) == right; } + inline bool operator==(const T right)const{ return this->size() == 1 && this->get(0) == right; } + inline bool operator==(std::nullptr_t) = delete; + inline bool operator!=(const String_Type &right)const{ return !operator==(right); } + inline bool operator!=(const std::basic_string &right)const{ return !operator==(right); } + inline bool operator!=(const T right)const{ return !operator==(right); } + inline bool operator!=(std::nullptr_t) = delete; + inline bool operator<(const String_Type &right)const{ return this->compare(right) < 0; } + inline bool operator<(const std::basic_string &right)const{ return this->compare(right) < 0; } + inline bool operator<(const T right)const{ return this->compare(right) < 0; } + inline bool operator>(const String_Type &right)const{ return this->compare(right) > 0; } + inline bool operator>(const std::basic_string &right)const{ return this->compare(right) > 0; } + inline bool operator>(const T right)const{ return this->compare(right) > 0; } + inline bool operator<=(const String_Type &right)const{ return !operator>(right); } + inline bool operator<=(const std::basic_string &right)const{ return !operator>(right); } + inline bool operator<=(const T right)const{ return !operator>(right); } + inline bool operator>=(const String_Type &right)const{ return !operator<(right); } + inline bool operator>=(const std::basic_string &right)const{ return !operator<(right); } + inline bool operator>=(const T right)const{ return !operator<(right); } + protected: T *str{}; /** Pointer for the underlying string of elements */ size_t length{}; /** Number of representable elements in the string */ }; + namespace literals { + // Not truly a literal, but I just want this available in places I'm using literals + template + static inline std::basic_string operator+(std::basic_string lhs, std::basic_string_view rhs) { + lhs += rhs; + return lhs; + } + + template + static inline std::basic_string operator+(std::basic_string lhs, const String_Type& rhs) { + lhs += std::basic_string_view{rhs}; + return lhs; + } + } + /** Generic String Type */ typedef String_Type StringType; } diff --git a/src/include/Jupiter/String_Type_Imp.h b/src/include/Jupiter/String_Type_Imp.h index 205358a..d7de48a 100644 --- a/src/include/Jupiter/String_Type_Imp.h +++ b/src/include/Jupiter/String_Type_Imp.h @@ -548,10 +548,6 @@ template size_t Jupiter::String_Type::replace(size_t index, size_ return Jupiter::String_Type::length; } -template size_t Jupiter::String_Type::replace(size_t index, size_t targetSize, const Jupiter::Readable_String &value) { - return this->replace(index, targetSize, value.data(), value.size()); -} - template size_t Jupiter::String_Type::replace(size_t index, size_t targetSize, std::basic_string_view value) { return this->replace(index, targetSize, value.data(), value.size()); } @@ -716,11 +712,6 @@ template size_t Jupiter::String_Type::replace(const Jupiter::Read return this->replace(target.data(), target.size(), value, valueSize); } -template size_t Jupiter::String_Type::replace(const Jupiter::Readable_String &target, const Jupiter::Readable_String &value) -{ - return this->replace(target.data(), target.size(), value.data(), value.size()); -} - template size_t Jupiter::String_Type::replace(std::basic_string_view target, std::basic_string_view value) { return this->replace(target.data(), target.size(), value.data(), value.size()); @@ -801,11 +792,11 @@ template template class R> R Jupiter::String_T // Jupiter::DataBuffer specialization -template<> struct _Jupiter_DataBuffer_partial_specialization_impl -{ - template static void push(Jupiter::DataBuffer *buffer, const Jupiter::String_Type *data) - { - _Jupiter_DataBuffer_partial_specialization_impl::push(buffer, data); +template<> struct _Jupiter_DataBuffer_partial_specialization_impl { + template static void push(Jupiter::DataBuffer *buffer, const Jupiter::String_Type *data) { + buffer->secure(sizeof(size_t) + data->size() * sizeof(Y)); + buffer->push(data->size()); + buffer->push(reinterpret_cast(data->data()), data->size() * sizeof(Y)); }; };