From 9db2fd92b8d7316d6b15a11b60c35036a010eaef Mon Sep 17 00:00:00 2001 From: Jessica James Date: Mon, 29 Nov 2021 22:16:33 -0600 Subject: [PATCH] Remove most extraneous functions from ReadableString --- src/common/File.cpp | 44 +- src/common/GenericCommand.cpp | 42 +- src/common/HTTP_Server.cpp | 56 +- src/common/IRC_Client.cpp | 1156 +++++++++----------- src/common/Plugin.cpp | 30 +- src/common/Socket.cpp | 16 +- src/include/Jupiter/Command.h | 2 +- src/include/Jupiter/Config.h | 9 +- src/include/Jupiter/File.h | 14 +- src/include/Jupiter/GenericCommand.h | 7 +- src/include/Jupiter/IRC_Client.h | 90 +- src/include/Jupiter/IRC_Numerics.h | 1 - src/include/Jupiter/Plugin.h | 30 +- src/include/Jupiter/Readable_String.h | 287 +++-- src/include/Jupiter/Readable_String_Imp.h | 787 +------------ src/include/Jupiter/Reference_String.h | 69 +- src/include/Jupiter/Reference_String_Imp.h | 30 - src/include/Jupiter/Socket.h | 1 + src/include/Jupiter/String.hpp | 115 +- src/include/Jupiter/String_Imp.h | 80 -- src/include/Jupiter/String_Type_Imp.h | 12 +- 21 files changed, 838 insertions(+), 2040 deletions(-) diff --git a/src/common/File.cpp b/src/common/File.cpp index d4eafbd..c5b5922 100644 --- a/src/common/File.cpp +++ b/src/common/File.cpp @@ -18,6 +18,7 @@ #include #include +#include "jessilib/word_split.hpp" #include "File.h" #include "String.hpp" @@ -41,7 +42,7 @@ const size_t defaultBufferSize = 8192; struct JUPITER_API Jupiter::File::Data { // TODO: remove pimpl std::string fileName; - std::vector lines; + std::vector lines; Data(); Data(const Data &data); @@ -53,6 +54,8 @@ struct JUPITER_API Jupiter::File::Data { // TODO: remove pimpl #pragma warning(pop) #endif +using namespace std::literals; + Jupiter::File::Data::Data() { } @@ -88,7 +91,7 @@ size_t Jupiter::File::getLineCount() const { return m_data->lines.size(); } -const Jupiter::ReadableString &Jupiter::File::getLine(size_t line) const { +const std::string& Jupiter::File::getLine(size_t line) const { return m_data->lines[line]; } @@ -96,12 +99,14 @@ const std::string &Jupiter::File::getFileName() const { return m_data->fileName; } -bool Jupiter::File::addData(const Jupiter::ReadableString &data) { - unsigned int word_count = data.wordCount(ENDL); - if (word_count == 0) return false; +bool Jupiter::File::addData(std::string_view data) { + auto lines = jessilib::word_split_view(data, "\r\n"sv); + if (lines.empty()) { + return false; + } - for (unsigned int i = 0; i < word_count; i++) { - m_data->lines.emplace_back(std::move(Jupiter::StringS::getWord(data, i, ENDL))); + for (const auto& line : lines) { + m_data->lines.emplace_back(line); } return true; } @@ -121,15 +126,14 @@ bool Jupiter::File::load(const char *file) { return result; } -bool Jupiter::File::load(const Jupiter::ReadableString &file) { - std::string fileName = static_cast(file); - FILE *filePtr = fopen(fileName.c_str(), "rb"); +bool Jupiter::File::load(std::string file) { + FILE *filePtr = fopen(file.c_str(), "rb"); if (filePtr == nullptr) { return false; } if (m_data->fileName.empty()) { - m_data->fileName = fileName; + m_data->fileName = std::move(file); } bool r = load(filePtr); @@ -225,7 +229,7 @@ bool Jupiter::File::reload() { std::string fileName(std::move(m_data->fileName)); unload(); - return load(fileName.c_str()); + return load(fileName); } bool Jupiter::File::reload(const char *file) { @@ -233,9 +237,9 @@ bool Jupiter::File::reload(const char *file) { return load(file); } -bool Jupiter::File::reload(const Jupiter::ReadableString &file) { +bool Jupiter::File::reload(std::string file) { unload(); - return load(file); + return load(std::move(file)); } bool Jupiter::File::reload(FILE *file) { @@ -253,19 +257,23 @@ bool Jupiter::File::sync() { bool Jupiter::File::sync(const char *file) { FILE *filePtr = fopen(file, "wb"); - if (filePtr == nullptr) return false; + if (filePtr == nullptr) { + return false; + } + sync(filePtr); // Always returns true. fclose(filePtr); return true; } -bool Jupiter::File::sync(const Jupiter::ReadableString &file) { - return sync(static_cast(file).c_str()); +bool Jupiter::File::sync(const std::string& file) { + return sync(file.c_str()); } bool Jupiter::File::sync(FILE *file) { for (const auto& line : m_data->lines) { - line.println(file); + fwrite(line.data(), sizeof(char), line.size(), file); + fputs("\r\n", file); } return true; diff --git a/src/common/GenericCommand.cpp b/src/common/GenericCommand.cpp index 07c7ee4..1f3c5f3 100644 --- a/src/common/GenericCommand.cpp +++ b/src/common/GenericCommand.cpp @@ -17,12 +17,14 @@ */ #include "GenericCommand.h" +#include "jessilib/word_split.hpp" #include "Plugin.h" using namespace Jupiter::literals; +using namespace std::literals; -constexpr const char GENERIC_COMMAND_WORD_DELIMITER = ' '; -constexpr const char *GENERIC_COMMAND_WORD_DELIMITER_CS = " "; +// Is there a reason we're not using WHITESPACE_SV? +constexpr std::string_view GENERIC_COMMAND_WORD_DELIMITER_SV = " "sv; Jupiter::GenericCommandNamespace o_generic_commands; Jupiter::GenericCommandNamespace &Jupiter::g_generic_commands = o_generic_commands; @@ -52,17 +54,14 @@ bool Jupiter::GenericCommand::isNamespace() const { } void Jupiter::GenericCommand::setNamespace(const Jupiter::ReadableString &in_namespace) { - if (in_namespace.wordCount(GENERIC_COMMAND_WORD_DELIMITER_CS) == 0) { - return; // We're already here - } - if (Jupiter::GenericCommand::m_parent == nullptr) { return; // We have no parent to start from } Jupiter::GenericCommand *command = Jupiter::GenericCommand::m_parent->getCommand(in_namespace); - if (command != nullptr && command->isNamespace()) - Jupiter::GenericCommand::setNamespace(*static_cast(command)); + if (command != nullptr && command != this && command->isNamespace()) { + Jupiter::GenericCommand::setNamespace(*static_cast(command)); + } } void Jupiter::GenericCommand::setNamespace(Jupiter::GenericCommandNamespace &in_namespace) { @@ -86,6 +85,11 @@ Jupiter::GenericCommand::ResponseLine::ResponseLine(const Jupiter::ReadableStrin type{ in_type } { } +Jupiter::GenericCommand::ResponseLine::ResponseLine(std::string in_response, GenericCommand::DisplayType in_type) + : response{ std::move(in_response) }, + type{ in_type } { +} + Jupiter::GenericCommand::ResponseLine* Jupiter::GenericCommand::ResponseLine::set(const Jupiter::ReadableString &in_response, GenericCommand::DisplayType in_type) { response = in_response; type = in_type; @@ -100,23 +104,26 @@ Jupiter::GenericCommandNamespace::~GenericCommandNamespace() { Jupiter::GenericCommand::ResponseLine* Jupiter::GenericCommandNamespace::trigger(const Jupiter::ReadableString &in_input) { GenericCommand* command; Jupiter::ReferenceString input(in_input); + auto split_input = jessilib::word_split_once_view(input, GENERIC_COMMAND_WORD_DELIMITER_SV); - if (input.wordCount(GENERIC_COMMAND_WORD_DELIMITER_CS) == 0) { // No parameters; list commands + if (split_input.second.empty()) { // No parameters; list commands return new Jupiter::GenericCommand::ResponseLine(m_help,Jupiter::GenericCommand::DisplayType::PrivateSuccess); } - command = Jupiter::GenericCommandNamespace::getCommand(input.getWord(0, GENERIC_COMMAND_WORD_DELIMITER_CS)); + command = Jupiter::GenericCommandNamespace::getCommand(split_input.first); if (command != nullptr) { - return command->trigger(input.gotoWord(1, GENERIC_COMMAND_WORD_DELIMITER_CS)); + return command->trigger(Jupiter::ReferenceString{split_input.second}); } - return new Jupiter::GenericCommand::ResponseLine(Jupiter::ReferenceString::empty, Jupiter::GenericCommand::DisplayType::PrivateError); + return new Jupiter::GenericCommand::ResponseLine(""_jrs, Jupiter::GenericCommand::DisplayType::PrivateError); } const Jupiter::ReadableString &Jupiter::GenericCommandNamespace::getHelp(const Jupiter::ReadableString ¶meters) { static Jupiter::ReferenceString not_found = "Error: Command not found"_jrs; + Jupiter::ReferenceString input(parameters); - if (parameters.wordCount(GENERIC_COMMAND_WORD_DELIMITER_CS) == 0) // No parameters; list commands + auto input_split = jessilib::word_split_once_view(input, GENERIC_COMMAND_WORD_DELIMITER_SV); + if (input_split.second.empty()) // No parameters; list commands { if (Jupiter::GenericCommandNamespace::m_should_update_help) Jupiter::GenericCommandNamespace::updateHelp(); @@ -124,13 +131,12 @@ const Jupiter::ReadableString &Jupiter::GenericCommandNamespace::getHelp(const J return Jupiter::GenericCommandNamespace::m_help; } - Jupiter::ReferenceString input(parameters); GenericCommand *command; // Search for command - command = Jupiter::GenericCommandNamespace::getCommand(input.getWord(0, GENERIC_COMMAND_WORD_DELIMITER_CS)); + command = Jupiter::GenericCommandNamespace::getCommand(input_split.first); if (command != nullptr) { - return command->getHelp(input.gotoWord(1, GENERIC_COMMAND_WORD_DELIMITER_CS)); + return command->getHelp(Jupiter::ReferenceString{input_split.second}); } // Command not found @@ -169,7 +175,7 @@ std::vector Jupiter::GenericCommandNamespace::getComma return result; } -Jupiter::GenericCommand *Jupiter::GenericCommandNamespace::getCommand(const Jupiter::ReadableString &in_command) const { +Jupiter::GenericCommand *Jupiter::GenericCommandNamespace::getCommand(std::string_view in_command) const { /** This is broken into 2 loops in order to insure that exact matches are ALWAYS prioritized over inexact matches */ // Search commands @@ -212,7 +218,7 @@ void Jupiter::GenericCommandNamespace::removeCommand(Jupiter::GenericCommand &in } } -void Jupiter::GenericCommandNamespace::removeCommand(const Jupiter::ReadableString &in_command) +void Jupiter::GenericCommandNamespace::removeCommand(std::string_view in_command) { for (auto itr = m_commands.begin(); itr != m_commands.end(); ++itr) { if ((*itr)->matches(in_command)) { diff --git a/src/common/HTTP_Server.cpp b/src/common/HTTP_Server.cpp index 3dda14c..795bc74 100644 --- a/src/common/HTTP_Server.cpp +++ b/src/common/HTTP_Server.cpp @@ -93,26 +93,26 @@ Jupiter::HTTP::Server::Directory::~Directory() { // .hook("dir/subdir/", content) void Jupiter::HTTP::Server::Directory::hook(std::string_view in_name, std::unique_ptr in_content) { - Jupiter::ReferenceString in_name_ref = in_name; - in_name_ref.shiftRight(in_name_ref.span('/')); + std::string_view in_name_ref = in_name; + size_t in_name_start = in_name_ref.find_first_not_of('/'); - if (in_name_ref.isEmpty()) { // Hook content + if (in_name_start == std::string_view::npos) { // Hook content content.push_back(std::move(in_content)); return; } + in_name_ref.remove_prefix(in_name_start); - size_t index = in_name_ref.find('/'); + size_t in_name_end = in_name_ref.find('/'); std::string_view dir_name; - if (index == Jupiter::INVALID_INDEX) { + if (in_name_end == std::string_view::npos) { dir_name = in_name_ref; } else { - dir_name = in_name_ref.substring(size_t{ 0 }, index); + dir_name = in_name_ref.substr(size_t{ 0 }, in_name_end); } - in_name_ref.shiftRight(dir_name.size()); + in_name_ref.remove_prefix(dir_name.size()); unsigned int dir_name_checksum = calc_checksum(dir_name); - index = directories.size(); for (auto& directory : directories) { if (directory->name_checksum == dir_name_checksum && directory->name == dir_name) { directory->hook(in_name_ref, std::move(in_content)); @@ -124,14 +124,16 @@ void Jupiter::HTTP::Server::Directory::hook(std::string_view in_name, std::uniqu Directory* directory = directories.emplace_back(std::make_unique(static_cast(dir_name))).get(); directory_add_loop: // TODO: for the love of god, why why why - in_name_ref.shiftRight(in_name_ref.span('/')); - if (in_name_ref.isNotEmpty()) { + in_name_start = in_name_ref.find_first_not_of('/'); + if (in_name_start != std::string_view::npos) { + in_name_ref.remove_prefix(in_name_start); + // add directory - index = in_name_ref.find('/'); + size_t index = in_name_ref.find('/'); if (index != Jupiter::INVALID_INDEX) { - directory->directories.push_back(std::make_unique(static_cast(in_name_ref.substring(size_t{ 0 }, index)))); + directory->directories.push_back(std::make_unique(static_cast(in_name_ref.substr(size_t{ 0 }, index)))); directory = directory->directories[directories.size() - 1].get(); - in_name_ref.shiftRight(index + 1); + in_name_ref.remove_prefix(index + 1); goto directory_add_loop; } directory->directories.push_back(std::make_unique(static_cast(in_name_ref))); @@ -143,12 +145,11 @@ directory_add_loop: // TODO: for the love of god, why why why } bool Jupiter::HTTP::Server::Directory::remove(std::string_view path, std::string_view content_name) { - Jupiter::ReferenceString in_name_ref = path; - in_name_ref.shiftRight(in_name_ref.span('/')); - unsigned int checksum; + std::string_view in_name_ref = path; + size_t in_name_start = in_name_ref.find_first_not_of('/'); - if (in_name_ref.isEmpty()) { // Remove content - checksum = calc_checksum(content_name); + if (in_name_start == std::string_view::npos) { // Remove content + unsigned int checksum = calc_checksum(content_name); for (auto itr = content.begin(); itr != content.end(); ++itr) { auto& content_node = *itr; if (content_node->name_checksum == checksum && content_node->name == content_name) { @@ -159,17 +160,18 @@ bool Jupiter::HTTP::Server::Directory::remove(std::string_view path, std::string return false; } + in_name_ref.remove_prefix(in_name_start); // Call remove() on next directory in path size_t index = in_name_ref.find('/'); std::string_view dir_name; - if (index == Jupiter::INVALID_INDEX) + if (index == std::string_view::npos) dir_name = in_name_ref; else - dir_name = in_name_ref.substring(size_t{ 0 }, index); + dir_name = in_name_ref.substr(size_t{ 0 }, index); - in_name_ref.shiftRight(dir_name.size()); - checksum = calc_checksum(dir_name); + in_name_ref.remove_prefix(dir_name.size()); + unsigned int checksum = calc_checksum(dir_name); for (auto& directory : directories) { if (directory->name_checksum == checksum && directory->name == dir_name) { return directory->remove(in_name_ref, content_name); @@ -300,7 +302,6 @@ Jupiter::HTTP::Server::Data::~Data() { // Data functions void Jupiter::HTTP::Server::Data::hook(std::string_view hostname, std::string_view in_path, std::unique_ptr in_content) { - Jupiter::ReferenceString path = in_path; Jupiter::ReferenceString dir_name; Jupiter::HTTP::Server::Host* host = find_host(hostname); @@ -310,12 +311,13 @@ void Jupiter::HTTP::Server::Data::hook(std::string_view hostname, std::string_vi // OPTIMIZE: create directory tree and return. } - path.shiftRight(path.span('/')); - if (path.isEmpty()) { + size_t in_path_start = in_path.find_first_not_of('/'); + if (in_path_start == std::string_view::npos) { host->content.push_back(std::move(in_content)); } else { - host->hook(path, std::move(in_content)); + in_path.remove_prefix(in_path_start); + host->hook(in_path, std::move(in_content)); } } @@ -824,5 +826,5 @@ int Jupiter::HTTP::Server::think() { return 0; } -const Jupiter::ReadableString &Jupiter::HTTP::Server::global_namespace = Jupiter::ReferenceString::empty; +const Jupiter::ReadableString &Jupiter::HTTP::Server::global_namespace = ""_jrs; const Jupiter::ReadableString &Jupiter::HTTP::Server::server_string = "Jupiter"_jrs; \ No newline at end of file diff --git a/src/common/IRC_Client.cpp b/src/common/IRC_Client.cpp index 726d08e..a159231 100644 --- a/src/common/IRC_Client.cpp +++ b/src/common/IRC_Client.cpp @@ -16,14 +16,16 @@ * Written by Jessica James */ +#include "IRC_Client.h" #include #include #include +#include #include "jessilib/split.hpp" +#include "jessilib/word_split.hpp" #include "jessilib/unicode.hpp" #include "Jupiter.h" #include "Functions.h" -#include "IRC_Client.h" #include "TCPSocket.h" #include "String.hpp" #include "Plugin.h" @@ -32,17 +34,10 @@ //#define SHORT_IRC_MACROS #include "IRC_Numerics.h" -#if defined _WIN32 -#include -#else // _WIN32 -#include -#endif // _WIN32 - using namespace Jupiter::literals; using namespace std::literals; -Jupiter::IRC::Client::Client(Jupiter::Config *in_primary_section, Jupiter::Config *in_secondary_section) -{ +Jupiter::IRC::Client::Client(Jupiter::Config *in_primary_section, Jupiter::Config *in_secondary_section) { m_primary_section = in_primary_section; m_secondary_section = in_secondary_section; @@ -117,116 +112,77 @@ Jupiter::IRC::Client::~Client() { fclose(m_log_file); } -void Jupiter::IRC::Client::OnConnect() -{ - return; +void Jupiter::IRC::Client::OnConnect(){ } -void Jupiter::IRC::Client::OnDisconnect() -{ - return; +void Jupiter::IRC::Client::OnDisconnect(){ } -void Jupiter::IRC::Client::OnReconnectAttempt(bool) -{ - return; +void Jupiter::IRC::Client::OnReconnectAttempt(bool){ } -void Jupiter::IRC::Client::OnRaw(const Jupiter::ReadableString &) -{ - return; +void Jupiter::IRC::Client::OnRaw(std::string_view){ } -void Jupiter::IRC::Client::OnNumeric(long int, const Jupiter::ReadableString &) -{ - return; +void Jupiter::IRC::Client::OnNumeric(long int, std::string_view){ } -void Jupiter::IRC::Client::OnError(const Jupiter::ReadableString &) -{ - return; +void Jupiter::IRC::Client::OnError(std::string_view){ } -void Jupiter::IRC::Client::OnChat(const Jupiter::ReadableString &, const Jupiter::ReadableString &, const Jupiter::ReadableString &) -{ - return; +void Jupiter::IRC::Client::OnChat(std::string_view, std::string_view, std::string_view){ } -void Jupiter::IRC::Client::OnNotice(const Jupiter::ReadableString &, const Jupiter::ReadableString &, const Jupiter::ReadableString &) -{ - return; +void Jupiter::IRC::Client::OnNotice(std::string_view, std::string_view, std::string_view){ } -void Jupiter::IRC::Client::OnServerNotice(const Jupiter::ReadableString &, const Jupiter::ReadableString &, const Jupiter::ReadableString &) -{ - return; +void Jupiter::IRC::Client::OnServerNotice(std::string_view, std::string_view, std::string_view){ } -void Jupiter::IRC::Client::OnCTCP(const Jupiter::ReadableString &, const Jupiter::ReadableString &, const Jupiter::ReadableString &, const Jupiter::ReadableString &) -{ - return; +void Jupiter::IRC::Client::OnCTCP(std::string_view, std::string_view, std::string_view, std::string_view) { } -void Jupiter::IRC::Client::OnAction(const Jupiter::ReadableString &, const Jupiter::ReadableString &, const Jupiter::ReadableString &) -{ - return; +void Jupiter::IRC::Client::OnAction(std::string_view, std::string_view, std::string_view) { } -void Jupiter::IRC::Client::OnInvite(const Jupiter::ReadableString &, const Jupiter::ReadableString &, const Jupiter::ReadableString &) -{ - return; +void Jupiter::IRC::Client::OnInvite(std::string_view, std::string_view, std::string_view) { } -void Jupiter::IRC::Client::OnJoin(const Jupiter::ReadableString &, const Jupiter::ReadableString &) -{ - return; +void Jupiter::IRC::Client::OnJoin(std::string_view, std::string_view) { } -void Jupiter::IRC::Client::OnPart(const Jupiter::ReadableString &, const Jupiter::ReadableString &, const Jupiter::ReadableString &) -{ - return; +void Jupiter::IRC::Client::OnPart(std::string_view, std::string_view, std::string_view) { } -void Jupiter::IRC::Client::OnNick(const Jupiter::ReadableString &, const Jupiter::ReadableString &) -{ - return; +void Jupiter::IRC::Client::OnNick(std::string_view, std::string_view) { } -void Jupiter::IRC::Client::OnKick(const Jupiter::ReadableString &, const Jupiter::ReadableString &, const Jupiter::ReadableString &, const Jupiter::ReadableString &) -{ - return; +void Jupiter::IRC::Client::OnKick(std::string_view, std::string_view, std::string_view, std::string_view) { } -void Jupiter::IRC::Client::OnQuit(const Jupiter::ReadableString &, const Jupiter::ReadableString &) -{ - return; +void Jupiter::IRC::Client::OnQuit(std::string_view, std::string_view) { } -void Jupiter::IRC::Client::OnMode(const Jupiter::ReadableString &, const Jupiter::ReadableString &, const Jupiter::ReadableString &) -{ - return; +void Jupiter::IRC::Client::OnMode(std::string_view, std::string_view, std::string_view) { } -const Jupiter::ReadableString &Jupiter::IRC::Client::getConfigSection() const -{ - if (m_primary_section != nullptr) +std::string_view Jupiter::IRC::Client::getConfigSection() const { + if (m_primary_section != nullptr) { return m_primary_section_name; + } - return Jupiter::ReferenceString::empty; + return ""_jrs; } -Jupiter::Config *Jupiter::IRC::Client::getPrimaryConfigSection() const -{ +Jupiter::Config *Jupiter::IRC::Client::getPrimaryConfigSection() const { return m_primary_section; } -Jupiter::Config *Jupiter::IRC::Client::getSecondaryConfigSection() const -{ +Jupiter::Config *Jupiter::IRC::Client::getSecondaryConfigSection() const { return m_secondary_section; } -void Jupiter::IRC::Client::setPrimaryConfigSection(Jupiter::Config *in_primary_section) -{ +void Jupiter::IRC::Client::setPrimaryConfigSection(Jupiter::Config *in_primary_section) { m_primary_section = in_primary_section; if (m_primary_section != nullptr) @@ -235,104 +191,86 @@ void Jupiter::IRC::Client::setPrimaryConfigSection(Jupiter::Config *in_primary_s m_primary_section_name.erase(); } -void Jupiter::IRC::Client::setSecondaryConfigSection(Jupiter::Config *in_secondary_section) -{ +void Jupiter::IRC::Client::setSecondaryConfigSection(Jupiter::Config *in_secondary_section) { m_secondary_section = in_secondary_section; } -const std::string &Jupiter::IRC::Client::getLogFile() const -{ +const std::string &Jupiter::IRC::Client::getLogFile() const { return m_log_file_name; } -const Jupiter::ReadableString &Jupiter::IRC::Client::getPrefixes() const -{ +std::string_view Jupiter::IRC::Client::getPrefixes() const { return m_prefixes; } -const Jupiter::ReadableString &Jupiter::IRC::Client::getPrefixModes() const -{ +std::string_view Jupiter::IRC::Client::getPrefixModes() const { return m_prefix_modes; } -std::string_view Jupiter::IRC::Client::getNickname() const -{ +std::string_view Jupiter::IRC::Client::getNickname() const { return m_nickname; } -std::string_view Jupiter::IRC::Client::getRealname() const -{ +std::string_view Jupiter::IRC::Client::getRealname() const { return m_realname; } -const Jupiter::ReadableString &Jupiter::IRC::Client::getServerName() const -{ +const Jupiter::ReadableString &Jupiter::IRC::Client::getServerName() const { return m_server_name; } -const std::string &Jupiter::IRC::Client::getServerHostname() const -{ +const std::string &Jupiter::IRC::Client::getServerHostname() const { return m_server_hostname; } -unsigned short Jupiter::IRC::Client::getServerPort() const -{ +unsigned short Jupiter::IRC::Client::getServerPort() const { return m_server_port; } -time_t Jupiter::IRC::Client::getReconnectDelay() const -{ +time_t Jupiter::IRC::Client::getReconnectDelay() const { return m_reconnect_delay; } -time_t Jupiter::IRC::Client::getReconnectTime() const -{ +time_t Jupiter::IRC::Client::getReconnectTime() const { return m_reconnect_time; } -int Jupiter::IRC::Client::getReconnectAttempts() const -{ +int Jupiter::IRC::Client::getReconnectAttempts() const { return m_reconnect_attempts; } -int Jupiter::IRC::Client::getMaxReconnectAttempts() const -{ +int Jupiter::IRC::Client::getMaxReconnectAttempts() const { return m_max_reconnect_attempts; } -int Jupiter::IRC::Client::getDefaultChanType() const -{ +int Jupiter::IRC::Client::getDefaultChanType() const { return m_default_chan_type; } -FILE *Jupiter::IRC::Client::getPrintOutput() const -{ +FILE *Jupiter::IRC::Client::getPrintOutput() const { return m_output; } -void Jupiter::IRC::Client::setPrintOutput(FILE *f) -{ +void Jupiter::IRC::Client::setPrintOutput(FILE *f) { m_output = f; } -inline Jupiter::ReferenceString getSender(const Jupiter::ReadableString &line) -{ - return Jupiter::ReferenceString::getWord(line, 0, ":! "); +Jupiter::ReferenceString getSender(std::string_view line) { + return jessilib::word_split_once_view(line, ":! "sv).first; } -int Jupiter::IRC::Client::getAccessLevel(const Channel &in_channel, const Jupiter::ReadableString &in_nickname) const -{ +int Jupiter::IRC::Client::getAccessLevel(const Channel &in_channel, std::string_view in_nickname) const { char prefix = in_channel.getUserPrefix(in_nickname); - if (prefix != 0) + if (prefix != 0) { return static_cast(m_prefixes.size() - m_prefixes.find(prefix)); + } return 0; } -int Jupiter::IRC::Client::getAccessLevel(const Jupiter::ReadableString &in_channel, const Jupiter::ReadableString &in_nickname) const -{ - auto channel = m_channels.find(in_channel); +int Jupiter::IRC::Client::getAccessLevel(std::string_view in_channel, std::string_view in_nickname) const { + auto channel = m_channels.find(Jupiter::ReferenceString{in_channel}); if (channel != m_channels.end()) return this->getAccessLevel(channel->second, in_nickname); @@ -340,27 +278,23 @@ int Jupiter::IRC::Client::getAccessLevel(const Jupiter::ReadableString &in_chann return 0; } -void Jupiter::IRC::Client::send(const Jupiter::ReadableString &rawMessage) -{ +void Jupiter::IRC::Client::send(const Jupiter::ReadableString &rawMessage) { Jupiter::StringS out = rawMessage; out += ENDL; m_socket->send(out); } -const Jupiter::IRC::Client::UserTableType &Jupiter::IRC::Client::getUsers() const -{ +const Jupiter::IRC::Client::UserTableType &Jupiter::IRC::Client::getUsers() const { return m_users; } -size_t Jupiter::IRC::Client::getUserCount() const -{ +size_t Jupiter::IRC::Client::getUserCount() const { return m_users.size(); } -std::shared_ptr Jupiter::IRC::Client::getUser(const Jupiter::ReadableString &in_nickname) const -{ - auto user = m_users.find(in_nickname); +std::shared_ptr Jupiter::IRC::Client::getUser(std::string_view in_nickname) const { + auto user = m_users.find(Jupiter::ReferenceString{in_nickname}); if (user != m_users.end()) { return user->second; } @@ -368,19 +302,17 @@ std::shared_ptr Jupiter::IRC::Client::getUser(const return nullptr; } -const Jupiter::IRC::Client::ChannelTableType &Jupiter::IRC::Client::getChannels() const -{ +const Jupiter::IRC::Client::ChannelTableType &Jupiter::IRC::Client::getChannels() const { return m_channels; } -size_t Jupiter::IRC::Client::getChannelCount() const -{ +size_t Jupiter::IRC::Client::getChannelCount() const { return m_channels.size(); } -Jupiter::IRC::Client::Channel *Jupiter::IRC::Client::getChannel(const Jupiter::ReadableString &in_channel) const -{ - auto channel = m_channels.find(in_channel); +Jupiter::IRC::Client::Channel *Jupiter::IRC::Client::getChannel(std::string_view in_channel) const { + Jupiter::ReferenceString channel_name = in_channel; // TODO: remove this + auto channel = m_channels.find(channel_name); if (channel != m_channels.end()) { return const_cast(&channel->second); } @@ -388,52 +320,45 @@ Jupiter::IRC::Client::Channel *Jupiter::IRC::Client::getChannel(const Jupiter::R return nullptr; } -bool Jupiter::IRC::Client::isAutoReconnect() const -{ +bool Jupiter::IRC::Client::isAutoReconnect() const { return m_max_reconnect_attempts != 0; } -void Jupiter::IRC::Client::setAutoReconnect(int val) -{ +void Jupiter::IRC::Client::setAutoReconnect(int val) { m_max_reconnect_attempts = val; } -void Jupiter::IRC::Client::joinChannel(std::string_view in_channel) -{ +void Jupiter::IRC::Client::joinChannel(std::string_view in_channel) { m_socket->send(Jupiter::StringS::Format("JOIN %.*s" ENDL, in_channel.size(), in_channel.data())); } -void Jupiter::IRC::Client::joinChannel(const Jupiter::ReadableString &in_channel, const Jupiter::ReadableString &in_password) -{ - m_socket->send(Jupiter::StringS::Format("JOIN %.*s %.*s" ENDL, in_channel.size(), in_channel.ptr(), in_password.size(), in_password.ptr())); +void Jupiter::IRC::Client::joinChannel(std::string_view in_channel, std::string_view in_password) { + m_socket->send(Jupiter::StringS::Format("JOIN %.*s %.*s" ENDL, in_channel.size(), in_channel.data(), in_password.size(), in_password.data())); } -void Jupiter::IRC::Client::partChannel(const Jupiter::ReadableString &in_channel) -{ - m_socket->send(Jupiter::StringS::Format("PART %.*s" ENDL, in_channel.size(), in_channel.ptr())); +void Jupiter::IRC::Client::partChannel(std::string_view in_channel) { + m_socket->send(Jupiter::StringS::Format("PART %.*s" ENDL, in_channel.size(), in_channel.data())); - m_channels[in_channel].setType(-2); + Jupiter::ReferenceString channel_name = in_channel; + m_channels[channel_name].setType(-2); } -void Jupiter::IRC::Client::partChannel(const Jupiter::ReadableString &in_channel, std::string_view in_message) -{ - m_socket->send(Jupiter::StringS::Format("PART %.*s :%.*s" ENDL, in_channel.size(), in_channel.ptr(), in_message.size(), in_message.data())); - - m_channels[in_channel].setType(-2); +void Jupiter::IRC::Client::partChannel(std::string_view in_channel, std::string_view in_message) { + m_socket->send(Jupiter::StringS::Format("PART %.*s :%.*s" ENDL, in_channel.size(), in_channel.data(), in_message.size(), in_message.data())); + + Jupiter::ReferenceString channel_name = in_channel; + m_channels[channel_name].setType(-2); } -void Jupiter::IRC::Client::sendMessage(const Jupiter::ReadableString &dest, const Jupiter::ReadableString &message) -{ - m_socket->send(Jupiter::StringS::Format("PRIVMSG %.*s :%.*s" ENDL, dest.size(), dest.ptr(), message.size(), message.ptr())); +void Jupiter::IRC::Client::sendMessage(std::string_view dest, std::string_view message) { + m_socket->send(Jupiter::StringS::Format("PRIVMSG %.*s :%.*s" ENDL, dest.size(), dest.data(), message.size(), message.data())); } -void Jupiter::IRC::Client::sendNotice(const Jupiter::ReadableString &dest, const Jupiter::ReadableString &message) -{ - m_socket->send(Jupiter::StringS::Format("NOTICE %.*s :%.*s" ENDL, dest.size(), dest.ptr(), message.size(), message.ptr())); +void Jupiter::IRC::Client::sendNotice(std::string_view dest, std::string_view message) { + m_socket->send(Jupiter::StringS::Format("NOTICE %.*s :%.*s" ENDL, dest.size(), dest.data(), message.size(), message.data())); } -size_t Jupiter::IRC::Client::messageChannels(int type, const Jupiter::ReadableString &message) -{ +size_t Jupiter::IRC::Client::messageChannels(int type, std::string_view message) { for (auto& channel : m_channels) { if (channel.second.getType() == type) { sendMessage(channel.second.getName(), message); @@ -457,45 +382,53 @@ int Jupiter::IRC::Client::process_line(std::string_view in_line) { if (line.isNotEmpty()) { Jupiter::IRC::Client::writeToLogs(line); - if (m_output != nullptr) - line.println(m_output); + if (m_output != nullptr) { + // TODO: use ostream instead + fwrite(in_line.data(), sizeof(char), in_line.size(), m_output); + fputs("\r\n", m_output); + } - Jupiter::ReferenceString w1 = Jupiter::ReferenceString::getWord(line, 0, WHITESPACE); + auto first_split = jessilib::split_once_view(line, " "sv); + Jupiter::ReferenceString w1 = first_split.first; if (w1.isNotEmpty()) { - Jupiter::ReferenceString w2 = Jupiter::ReferenceString::getWord(line, 1, WHITESPACE); - int numeric = w2.asInt(10); - if (w1[0] == ':') //Messages - { - if (w2.isNotEmpty()) - { + int numeric = asInt(first_split.second); + if (w1[0] == ':') { //Messages + if (!first_split.second.empty()) { + // TODO: This entire method should basically just be a state machine instead of this massive mess + auto line_split = jessilib::split_view(line, " "sv); + auto getLineToken = [&line_split](size_t index) -> Jupiter::ReferenceString { + if (index < line_split.size()) { + return line_split[index]; + } + + return {}; + }; + switch (numeric) // Numerics that don't rely on a specific connectionStatus. { case Reply::BOUNCE: // 010 { - Jupiter::ReferenceString portToken = Jupiter::ReferenceString::getWord(line, 4, " "); - unsigned short port; - if (portToken[0] == '+') // This is most likely not used anywhere. - { - port = (unsigned short)portToken.asUnsignedInt(10); - if (m_ssl == false) - { + std::string_view portToken = getLineToken(4); + unsigned short port{}; + if (!portToken.empty() && portToken[0] == '+') { // This is most likely not used anywhere. + portToken.remove_prefix(1); + if (m_ssl == false) { m_ssl = true; m_socket.reset(new Jupiter::SecureTCPSocket()); } } - else - { - port = (unsigned short)portToken.asUnsignedInt(10); - if (m_ssl == true) - { + else { + if (m_ssl == true) { m_ssl = false; m_socket.reset(new Jupiter::TCPSocket()); } } + + std::from_chars(portToken.data(), portToken.data() + portToken.size(), port, 10); if (port != 0) // Don't default -- could be non-compliant input. { - m_server_hostname = static_cast(Jupiter::ReferenceString::getWord(line, 3, WHITESPACE)); + m_server_hostname = getLineToken(3); m_server_port = port; puts("Reconnecting due to old bounce."); this->reconnect(); @@ -509,48 +442,12 @@ int Jupiter::IRC::Client::process_line(std::string_view in_line) { case 1: // Socket established -- attempting STARTTLS switch (numeric) { - case Reply::BOUNCEOLD: // 005 - if (line.matchi("*:Try server *, port *")) - { - Jupiter::ReferenceString portToken = Jupiter::ReferenceString::getWord(line, 6, " "); - unsigned short bouncePort = (unsigned short)Jupiter::ReferenceString::getWord(line, 6, " ").asInt(10); - - if (portToken[0] == '+') // This is almost certainly not used anywhere. - { - bouncePort = (unsigned short)portToken.asInt(10); - if (m_ssl == false) - { - m_ssl = true; - m_socket.reset(new Jupiter::SecureTCPSocket()); - } - } - else - { - bouncePort = (unsigned short)portToken.asInt(10); - if (m_ssl == true) - { - m_ssl = false; - m_socket.reset(new Jupiter::TCPSocket()); - } - } - if (bouncePort != 0) - { - auto server_hostname = Jupiter::ReferenceString::getWord(line, 4, " "); - server_hostname.truncate(1); // trailing comma - m_server_hostname = static_cast(server_hostname); - m_server_port = bouncePort; - puts("Reconnecting due to old bounce."); - this->reconnect(); - } - else puts("Error: Failed to parse old bounce token."); - } - break; - case Error::UNKNOWNCOMMAND: // 421 { - Jupiter::ReferenceString command = Jupiter::ReferenceString::getWord(line, 2, " "); - if (command.equalsi("STARTTLS")) // Server doesn't support STARTTLS + Jupiter::ReferenceString command = getLineToken(2); + if (command.equalsi("STARTTLS")) { // Server doesn't support STARTTLS Client::startCAP(); + } } break; @@ -595,51 +492,54 @@ int Jupiter::IRC::Client::process_line(std::string_view in_line) { switch (numeric) { case 0: - if (w2.equalsi("CAP")) + if (getLineToken(1).equalsi("CAP")) { - Jupiter::ReferenceString w4 = Jupiter::ReferenceString::getWord(line, 3, WHITESPACE); + Jupiter::ReferenceString w4 = getLineToken(3); if (w4 == "LS"sv) { - Jupiter::ReferenceString listParams = Jupiter::ReferenceString::gotoWord(line, 4, WHITESPACE); - if (listParams[0] == ':') listParams.shiftRight(1); - unsigned int len = listParams.wordCount(WHITESPACE); - Jupiter::ReferenceString curr; - Jupiter::StringL req = "CAP REQ :"; + std::vector cap_list{ line_split.begin() + 4, line_split.end() }; + if (!cap_list.empty() && !cap_list.front().empty() && cap_list.front().front() == ':') { + cap_list.front().remove_prefix(1); + } + std::string caps_request = "CAP REQ :"; bool sasl = false; - for (unsigned int i = 0; i < len; i++) - { - curr = listParams.getWord(i, WHITESPACE); - if (curr.equalsi("multi-prefix")) req += "multi-prefix "; - else if (curr.equalsi("userhost-in-names")) req += "userhost-in-names "; - else if (curr.equalsi("sasl")) - { - if (!m_sasl_password.empty()) - { - req += "sasl "_jrs; + for (const auto& cap : cap_list) { + if (jessilib::equalsi(cap, "multi-prefix"sv)) { + caps_request += cap; + caps_request.push_back(' '); + } + else if (jessilib::equalsi(cap, "userhost-in-names"sv)) { + caps_request += cap; + caps_request.push_back(' '); + } + else if (jessilib::equalsi(cap, "sasl"sv)) { + if (!m_sasl_password.empty()) { + caps_request += cap; + caps_request.push_back(' '); sasl = true; } } // else; // We don't know what this is! } - if (req.size() > 9) + if (caps_request.size() > 9) { - req -= 1; // Trim off the extra space byte. - req += ENDL; - m_socket->send(req); - if (sasl) - m_socket->send("AUTHENTICATE PLAIN"_jrs ENDL); + caps_request.pop_back(); + caps_request += ENDL; + m_socket->send(caps_request); + if (sasl) { + m_socket->send("AUTHENTICATE PLAIN"sv ENDL); + } } if (!sasl) { - m_socket->send("CAP END"_jrs ENDL); + m_socket->send("CAP END"sv ENDL); Client::registerClient(); } } } break; case Error::UNKNOWNCOMMAND: // 421 - if (w2.equalsi("CAP")) // Server doesn't support CAP - { + if (jessilib::equalsi(first_split.second, "CAP"sv)) { // Server doesn't support CAP Client::registerClient(); } break; @@ -655,7 +555,7 @@ int Jupiter::IRC::Client::process_line(std::string_view in_line) { { // We'll take any of these 4, just in-case any of them are missing. In general, this will trigger on 001. case Reply::MYINFO: // 004 - m_server_name = Jupiter::ReferenceString::getWord(line, 3, " "); + m_server_name = getLineToken(3); case Reply::WELCOME: // 001 case Reply::YOURHOST: // 002 case Reply::CREATED: // 003 @@ -731,44 +631,42 @@ int Jupiter::IRC::Client::process_line(std::string_view in_line) { { case Reply::ISUPPORT: // 005 { + // Parse supported user prefixes size_t pos = line.find("PREFIX=("_jrs); if (pos != Jupiter::INVALID_INDEX) { - Jupiter::ReferenceString ref = Jupiter::ReferenceString::substring(line, pos + 8); - m_prefix_modes = Jupiter::ReferenceString::getWord(ref, 0, ")"); - ref.shiftRight(ref.find(')') + 1); - m_prefixes = Jupiter::ReferenceString::getWord(ref, 0, " " ENDL); + std::string_view prefix_line_start = line.substr(pos + 8); + size_t prefix_modes_end = prefix_line_start.find(')'); + if (prefix_modes_end != std::string_view::npos) { + m_prefix_modes = prefix_line_start.substr(0, prefix_modes_end); + prefix_line_start.remove_prefix(m_prefix_modes.size() + 1); + m_prefixes = jessilib::word_split_once_view(prefix_line_start, " "sv).first; + } } + + // Parse supported channel modes pos = line.find("CHANMODES="_jrs); - if (pos != Jupiter::INVALID_INDEX) - { - Jupiter::ReferenceString ref = Jupiter::ReferenceString::substring(line, pos + 10); - ref = ref.getWord(0, " "); - size_t pos2 = ref.find(',', 0); - if (pos != INVALID_INDEX) - { - m_modeA = ref.getWord(0, ", "); - ref.shiftRight(pos + 1); - pos2 = ref.find(',', 0); - if (pos != INVALID_INDEX) - { - m_modeB = ref.getWord(0, ", "); - ref.shiftRight(pos + 1); - pos2 = ref.find(',', 0); - if (pos != INVALID_INDEX) - { - m_modeC = ref.getWord(0, ", "); - ref.shiftRight(pos + 1); - m_modeD = ref.getWord(0, ", "); + if (pos != Jupiter::INVALID_INDEX) { + std::string_view chan_modes_view = line.substr(pos + 10, line.find(' ')); + std::vector chan_modes = jessilib::split_n_view(chan_modes_view, ","sv, 3); // only split 3 times to cover A-D, but server _can_ send more + if (chan_modes.size() > 0) { + m_modeA = chan_modes[0]; + if (chan_modes.size() > 1) { + m_modeB = chan_modes[1]; + if (chan_modes.size() > 2) { + m_modeC = chan_modes[2]; + if (chan_modes.size() > 3) { + m_modeD = chan_modes[3]; + } } } } } + + // Parse supported channel types pos = line.find("CHANTYPES="_jrs); - if (pos != Jupiter::INVALID_INDEX) - { - Jupiter::ReferenceString ref = Jupiter::ReferenceString::substring(line, pos + 10); - m_chan_types = ref.getWord(0, " "); + if (pos != Jupiter::INVALID_INDEX) { + m_chan_types = line.substring(pos + 10, line.find(' ')); } } break; @@ -783,7 +681,7 @@ int Jupiter::IRC::Client::process_line(std::string_view in_line) { { offset = key.aformat("%u", i); value = Jupiter::IRC::Client::readConfigValue(key); - return !value.isEmpty(); + return !value.empty(); }; while (config_loop_condition()) { @@ -817,171 +715,163 @@ int Jupiter::IRC::Client::process_line(std::string_view in_line) { } break; - default: // Post-registration. - if (w2.equalsi("PRIVMSG")) - { - Jupiter::ReferenceString chan = Jupiter::ReferenceString::getWord(line, 2, WHITESPACE); - if (chan.isNotEmpty()) - { - Jupiter::ReferenceString nick = getSender(line); - if (nick.isNotEmpty()) - { - Jupiter::ReferenceString premessage = Jupiter::ReferenceString::substring(line, line.find(':', 1) + 1); - if (premessage[0] == '\001') //CTCP (ACTIONs are included) - { - Jupiter::ReferenceString rawmessage(premessage.ptr() + 1, premessage.size() - 1); - Jupiter::ReferenceString command = rawmessage.getWord(0, WHITESPACE); - if (command[command.size() - 1] == IRC::CTCP) command.truncate(1); - Jupiter::ReferenceString message = rawmessage.substring(rawmessage.find(' ') + 1, rawmessage.find(IRC::CTCP)); - if (message[message.size() - 1] == IRC::CTCP) message.truncate(1); - - if (command == "ACTION"sv) - { - this->OnAction(chan, nick, message); - for (auto& plugin : Jupiter::plugins) { - plugin->OnAction(this, chan, nick, message); + default: { // Post-registration. + std::string_view command_token = getLineToken(1); + if (jessilib::equalsi(command_token, "PRIVMSG"sv)) { + std::string_view channel_name = getLineToken(2); + if (!channel_name.empty()) { + std::string_view nick = getSender(line); + if (!nick.empty()) { + std::string_view message_view = line.substr(std::min(line.find(':', 1), line.size())); + if (!message_view.empty()) { + message_view.remove_prefix(1); + } + if (!message_view.empty() && message_view.front() == Jupiter::IRC::CTCP) { //CTCP (ACTIONs are included) + // Strip leading & trailing CTCP tokens + message_view.remove_prefix(1); + message_view = message_view.substr(0, message_view.find(Jupiter::IRC::CTCP)); + + auto split_message = jessilib::split_once_view(message_view, WHITESPACE_SV); + std::string_view ctcp_command = split_message.first; + std::string_view ctcp_parameters = split_message.second; + if (ctcp_command == "ACTION"sv) { + this->OnAction(channel_name, nick, ctcp_parameters); + for (auto& plugin: Jupiter::plugins) { + plugin->OnAction(this, channel_name, nick, ctcp_parameters); } } - else - { - Jupiter::StringL response = "NOTICE "; + else { + std::string response = "NOTICE "s; response += nick; response += " :" IRCCTCP; - response += command; + response += ctcp_command; response += ' '; - if (command == "PING"sv) response += message; - else if (command == "VERSION"sv) response += Jupiter::version; - else if (command == "FINGER"sv) response += "Oh, yeah, a little to the left."; - else if (command == "SOURCE"sv) response += "https://github.com/JAJames/Jupiter"; - else if (command == "USERINFO"sv) response += "Hey, I'm Jupiter! If you have questions, ask Agent! (GitHub: JAJames; Discord: Agent#0001)"; - else if (command == "CLIENTINFO"sv) response += "I'll tell you what I don't know: This command!"; - else if (command == "TIME"sv) response += getTime(); - else if (command == "ERRMSG"sv) response += message; - else - { + if (ctcp_command == "PING"sv) + response += ctcp_parameters; + else if (ctcp_command == "VERSION"sv) + response += Jupiter::version; + else if (ctcp_command == "FINGER"sv) + response += Jupiter::version; + else if (ctcp_command == "SOURCE"sv) + response += "https://github.com/JAJames/Jupiter"; + else if (ctcp_command == "USERINFO"sv) + response += "Hey, I'm Jupiter! If you have questions, ask Agent! (GitHub: JAJames; Discord: Agent#0001)"; + else if (ctcp_command == "CLIENTINFO"sv) + response += "I'll tell you what I don't know: This command!"; + else if (ctcp_command == "TIME"sv) + response += getTime(); + else if (ctcp_command == "ERRMSG"sv) + response += ctcp_parameters; + else { response = "NOTICE "; response += nick; response += " :" IRCCTCP "ERRMSG "; - response += command; + response += ctcp_command; response += " :Query is unknown"; } response += IRCCTCP ENDL; m_socket->send(response); - this->OnCTCP(chan, nick, command, message); - for (auto& plugin : Jupiter::plugins) { - plugin->OnCTCP(this, chan, nick, message); + + this->OnCTCP(channel_name, nick, ctcp_command, ctcp_parameters); + for (auto& plugin: Jupiter::plugins) { + plugin->OnCTCP(this, channel_name, nick, message_view); } } } - else - { - Jupiter::ReferenceString message = premessage; - this->OnChat(chan, nick, message); - for (auto& plugin : Jupiter::plugins) { - plugin->OnChat(this, chan, nick, message); + else { + Jupiter::ReferenceString message = message_view; + this->OnChat(channel_name, nick, message); + for (auto& plugin: Jupiter::plugins) { + plugin->OnChat(this, channel_name, nick, message); } } } } } - else if (w2.equalsi("NOTICE")) - { - Jupiter::ReferenceString chan = Jupiter::ReferenceString::getWord(line, 2, WHITESPACE); - if (chan.isNotEmpty()) - { + else if (jessilib::equalsi(command_token, "NOTICE"sv)) { + std::string_view channel_name = getLineToken(2); + if (!channel_name.empty()) { size_t pos = line.find('!', 0); - auto message = Jupiter::ReferenceString::substring(line, line.find(':', 1) + 1, line.size()); - if (pos < line.find(' ')) - { + auto message = Jupiter::ReferenceString::substring(line, line.find(':', 1) + 1, + line.size()); + if (pos < line.find(' ')) { auto nick = Jupiter::ReferenceString::substring(line, 1, pos); - this->OnNotice(chan, nick, message); - for (auto& plugin : Jupiter::plugins) { - plugin->OnNotice(this, chan, nick, message); + this->OnNotice(channel_name, nick, message); + for (auto& plugin: Jupiter::plugins) { + plugin->OnNotice(this, channel_name, nick, message); } } - else - { + else { auto sender = getSender(line); - if (sender.isNotEmpty()) - { - this->OnServerNotice(chan, sender, message); - for (auto& plugin : Jupiter::plugins) { - plugin->OnServerNotice(this, chan, sender, message); + if (sender.isNotEmpty()) { + this->OnServerNotice(channel_name, sender, message); + for (auto& plugin: Jupiter::plugins) { + plugin->OnServerNotice(this, channel_name, sender, message); } } } } } - else if (w2.equalsi("NICK")) - { + else if (jessilib::equalsi(command_token, "NICK"sv)) { auto nick = getSender(line); - Jupiter::ReferenceString newnick = Jupiter::ReferenceString::substring(line, line.find(' ', 1) + 1); - if (newnick.isNotEmpty() && newnick[0] == ':') newnick.shiftRight(1); - if (nick.equalsi(m_nickname)) - { + Jupiter::ReferenceString newnick = Jupiter::ReferenceString::substring(line, + line.find(' ', 1) + 1); + if (newnick.isNotEmpty() && newnick[0] == ':') + newnick.shiftRight(1); + if (nick.equalsi(m_nickname)) { m_nickname = newnick; } auto user = Client::findUser(nick); - if (user != nullptr) - { + if (user != nullptr) { user->m_nickname = newnick; this->OnNick(nick, newnick); } - for (auto& plugin : Jupiter::plugins) { + for (auto& plugin: Jupiter::plugins) { plugin->OnNick(this, nick, newnick); } } - else if (w2.equalsi("JOIN")) - { + else if (jessilib::equalsi(command_token, "JOIN"sv)) { auto nick = getSender(line); - Jupiter::ReferenceString chan = Jupiter::ReferenceString::getWord(line, 2, WHITESPACE); - - if (chan[0] == ':') - chan.shiftRight(1); - - auto channel = getChannel(chan); + std::string_view channel_name = getLineToken(2); + if (!channel_name.empty() && channel_name.front() == ':') { + channel_name.remove_prefix(1); + } - if (jessilib::equalsi(m_nickname, nick)) - { + auto channel = getChannel(channel_name); + if (jessilib::equalsi(m_nickname, nick)) { // TODO: Optimize by simply wiping channel data, rather than removing and re-adding if (channel != nullptr) - Client::delChannel(channel->getName()); + delChannel(channel->getName()); - Client::addChannel(chan); - channel = getChannel(chan); + addChannel(channel_name); + channel = getChannel(channel_name); channel->m_adding_names = true; - if (channel->getType() < 0) - { + if (channel->getType() < 0) { if (!m_auto_part_message.empty()) - Jupiter::IRC::Client::partChannel(chan, m_auto_part_message); + partChannel(channel_name, m_auto_part_message); else - Jupiter::IRC::Client::partChannel(chan); + partChannel(channel_name); } } else if (channel != nullptr) channel->addUser(Client::findUserOrAdd(nick)); - this->OnJoin(chan, nick); + this->OnJoin(channel_name, nick); - for (auto& plugin : Jupiter::plugins) { - plugin->OnJoin(this, chan, nick); + for (auto& plugin: Jupiter::plugins) { + plugin->OnJoin(this, channel_name, nick); } } - else if (w2.equalsi("PART")) - { + else if (jessilib::equalsi(command_token, "PART"sv)) { auto nick = getSender(line); - if (nick.isNotEmpty()) - { - Jupiter::ReferenceString chan = Jupiter::ReferenceString::getWord(line, 2, WHITESPACE); - if (chan.isNotEmpty()) - { - Channel *channel = getChannel(chan); - if (channel != nullptr) - { + if (nick.isNotEmpty()) { + std::string_view channel_name = getLineToken(2); + if (!channel_name.empty()) { + Channel* channel = getChannel(channel_name); + if (channel != nullptr) { auto user = getUser(nick); - if (user != nullptr) - { + if (user != nullptr) { channel->delUser(nick); Jupiter::ReferenceString reason; size_t pos = line.find(':', 1); @@ -989,15 +879,15 @@ int Jupiter::IRC::Client::process_line(std::string_view in_line) { if (pos != Jupiter::INVALID_INDEX) reason = Jupiter::ReferenceString::substring(line, pos + 1); - this->OnPart(chan, nick, reason); + this->OnPart(channel_name, nick, reason); - for (auto& plugin : Jupiter::plugins) { - plugin->OnPart(this, chan, nick, reason); + for (auto& plugin: Jupiter::plugins) { + plugin->OnPart(this, channel_name, nick, reason); } - + if (nick.equalsi(m_nickname)) - Client::delChannel(chan); - + Client::delChannel(channel_name); + if (user->getChannelCount() == 0) m_users.erase(nick); } @@ -1005,162 +895,160 @@ int Jupiter::IRC::Client::process_line(std::string_view in_line) { } } } - else if (w2.equalsi("KICK")) - { - Jupiter::ReferenceString chan = Jupiter::ReferenceString::getWord(line, 2, WHITESPACE); - if (chan.isNotEmpty()) - { + else if (jessilib::equalsi(command_token, "KICK"sv)) { + std::string_view channel_name = getLineToken(2); + if (!channel_name.empty()) { Jupiter::ReferenceString kicker = getSender(line); - if (kicker.isNotEmpty()) - { - Jupiter::ReferenceString kicked = Jupiter::ReferenceString::getWord(line, 3, WHITESPACE); - if (kicked.isNotEmpty()) - { - Channel *channel = getChannel(chan); - if (channel != nullptr) - { - auto user = getUser(kicked); - if (user != nullptr) - { - channel->delUser(kicked); + if (kicker.isNotEmpty()) { + std::string_view kicked_nickname = getLineToken(3); + if (!kicked_nickname.empty()) { + Channel* channel = getChannel(channel_name); + if (channel != nullptr) { + auto user = getUser(kicked_nickname); + if (user != nullptr) { + channel->delUser(kicked_nickname); size_t pos = line.find(':', 1); Jupiter::ReferenceString reason; if (pos != Jupiter::INVALID_INDEX) reason = Jupiter::ReferenceString::substring(line, pos + 1); - this->OnKick(chan, kicker, kicked, reason); + this->OnKick(channel_name, kicker, kicked_nickname, reason); - for (auto& plugin : Jupiter::plugins) { - plugin->OnKick(this, chan, kicker, kicked, reason); + for (auto& plugin: Jupiter::plugins) { + plugin->OnKick(this, channel_name, kicker, kicked_nickname, reason); } - if (kicked.equalsi(m_nickname)) - { - Client::delChannel(chan); - if (m_join_on_kick) - Jupiter::IRC::Client::joinChannel(chan); + if (jessilib::equalsi(kicked_nickname, m_nickname)) { + Client::delChannel(channel_name); + if (m_join_on_kick) { + Jupiter::IRC::Client::joinChannel(channel_name); + } } - if (user->getChannelCount() == 0) - m_users.erase(kicked); + if (user->getChannelCount() == 0) { + m_users.erase(Jupiter::ReferenceString{kicked_nickname}); + } } } } } } } - else if (w2.equalsi("QUIT")) - { + else if (jessilib::equalsi(command_token, "QUIT"sv)) { Jupiter::ReferenceString nick = getSender(line); - Jupiter::ReferenceString message = Jupiter::ReferenceString::substring(line, line.find(':', 1) + 1); + Jupiter::ReferenceString message = Jupiter::ReferenceString::substring(line, + line.find(':', 1) + 1); auto user = getUser(nick); - if (user != nullptr) - { - for (auto& channel : m_channels) { + if (user != nullptr) { + for (auto& channel: m_channels) { channel.second.delUser(nick); } this->OnQuit(nick, message); - for (auto& plugin : Jupiter::plugins) { + for (auto& plugin: Jupiter::plugins) { plugin->OnQuit(this, nick, message); } m_users.erase(nick); } } - else if (w2.equalsi("INVITE")) - { + else if (jessilib::equalsi(command_token, "INVITE"sv)) { Jupiter::ReferenceString inviter = getSender(line); - Jupiter::ReferenceString invited = Jupiter::ReferenceString::getWord(line, 2, WHITESPACE); - Jupiter::ReferenceString chan = Jupiter::ReferenceString::substring(line, line.find(':', 1) + 1); - this->OnInvite(chan, inviter, invited); - for (auto& plugin : Jupiter::plugins) { - plugin->OnInvite(this, chan, inviter, invited); + std::string_view invited_nickname = getLineToken(2); + Jupiter::ReferenceString chan = Jupiter::ReferenceString::substring(line, + line.find(':', 1) + 1); + this->OnInvite(chan, inviter, invited_nickname); + for (auto& plugin: Jupiter::plugins) { + plugin->OnInvite(this, chan, inviter, invited_nickname); } } - else if (w2.equalsi("MODE")) - { - Jupiter::ReferenceString chan = Jupiter::ReferenceString::getWord(line, 2, WHITESPACE); - if (chan.isNotEmpty()) - { - if (m_chan_types.contains(chan[0])) - { + else if (jessilib::equalsi(command_token, "MODE"sv)) { + std::string_view channel_name = getLineToken(2); + if (!channel_name.empty()) { + if (m_chan_types.find(channel_name[0]) != std::string::npos) { auto nick = getSender(line); - if (nick.isNotEmpty()) - { - Jupiter::ReferenceString modestring = Jupiter::ReferenceString::substring(line, line.find(' ', 2) + 1); - if (modestring.wordCount(" ") > 1) - { - Jupiter::ReferenceString modes = modestring.getWord(0, " "); - if (modes.isNotEmpty()) - { - Jupiter::ReferenceString modeParameters = modestring.gotoWord(1, " "); - Jupiter::ReferenceString tword; - unsigned char word_index = 0; - char symb = 0; - for (uint8_t mode_index = 0; mode_index != modes.size(); mode_index++) - { - if (modes[mode_index] == '+' || modes[mode_index] == '-') - symb = modes[mode_index]; - else if (m_prefix_modes.contains(modes[mode_index])) // user prefix mode - { - tword = modeParameters.getWord(word_index, " "); - if (tword.isNotEmpty() && tword[0] == ':') { - // Sttrip leading ':' - tword.shiftRight(1); - - // erase modeParameters - modeParameters.erase(); - } + if (nick.isNotEmpty()) { + std::string_view mode_line = line.substr(std::min(line.find(' ', 2), line.size())); + if (!mode_line.empty()) { + mode_line.remove_prefix(1); + } + + auto split_mode_line = jessilib::word_split_once_view(mode_line, ' '); + std::string_view modes = split_mode_line.first; + std::vector mode_parameters = jessilib::word_split_view(split_mode_line.second, ' '); + + size_t word_index = 0; + char sign = 0; + for (auto mode : modes) { + if (mode == '+' || mode == '-') { + sign = mode; + } + else if (m_prefix_modes.find(mode) != std::string::npos) { // user prefix mode + std::string_view mode_parameter; + if (word_index < mode_parameters.size()) { + mode_parameter = mode_parameters[word_index]; + } + + if (!mode_parameter.empty() && mode_parameter.front() == ':') { + // Strip leading ':' + // TODO: actually populate mode_parameter correctly with the full parameter string + mode_parameter.remove_prefix(1); + + // erase modeParameters; all further parameters are a part of this one + mode_parameters.clear(); + } - if (tword.isNotEmpty()) - { - Jupiter::IRC::Client::Channel *channel = getChannel(chan); - if (channel != nullptr) - { - if (symb == '+') - channel->addUserPrefix(tword, m_prefixes[m_prefix_modes.find(modes[mode_index])]); - else - channel->delUserPrefix(tword, m_prefixes[m_prefix_modes.find(modes[mode_index])]); - } + if (!mode_parameter.empty()) { + Jupiter::IRC::Client::Channel* channel = getChannel(channel_name); + if (channel != nullptr) { + if (sign == '+') { + channel->addUserPrefix(mode_parameter, + m_prefixes[m_prefix_modes.find(mode)]); + } + else { + channel->delUserPrefix(mode_parameter, + m_prefixes[m_prefix_modes.find(mode)]); } - ++word_index; } - else if (m_modeA.contains(modes[mode_index])) // mode type A - ++word_index; - else if (m_modeB.contains(modes[mode_index])) // mode type B - ++word_index; - else if (m_modeC.contains(modes[mode_index]) && symb == '+') // mode type C (with parameter) - ++word_index; - // else; // mode type D } + ++word_index; + } + else if (m_modeA.find(mode) != std::string::npos) { // mode type A + ++word_index; + } + else if (m_modeB.find(mode) != std::string::npos) { // mode type B + ++word_index; + } + else if (m_modeC.find(mode) != std::string::npos + && sign == '+') { // mode type C (with parameter) + ++word_index; } + // else; // mode type D } - this->OnMode(chan, nick, modestring); - for (auto& plugin : Jupiter::plugins) { - plugin->OnMode(this, chan, nick, modestring); + + this->OnMode(channel_name, nick, mode_line); + for (auto& plugin: Jupiter::plugins) { + plugin->OnMode(this, channel_name, nick, mode_line); } } } } } - // else if ACCOUNT - // else if CHGHOST - else if (numeric == Reply::NAMREPLY) // Some names. - { - Jupiter::ReferenceString chan = Jupiter::ReferenceString::getWord(line, 4, " "); - Jupiter::ReferenceString names = Jupiter::ReferenceString::substring(line, line.find(':', 1) + 1); - - Channel *channel = getChannel(chan); - if (channel != nullptr) - { - if (channel->m_adding_names == false) - { - Client::delChannel(chan); - Client::addChannel(chan); - channel = Jupiter::IRC::Client::getChannel(chan); + // else if ACCOUNT + // else if CHGHOST + else if (numeric == Reply::NAMREPLY) { // Some names. + std::string_view channel_name = getLineToken(4); + Jupiter::ReferenceString names = Jupiter::ReferenceString::substring(line, + line.find(':', 1) + 1); + + Channel* channel = getChannel(channel_name); + if (channel != nullptr) { + if (channel->m_adding_names == false) { + Client::delChannel(channel_name); + Client::addChannel(channel_name); + channel = Jupiter::IRC::Client::getChannel(channel_name); channel->m_adding_names = true; } @@ -1168,28 +1056,29 @@ int Jupiter::IRC::Client::process_line(std::string_view in_line) { Client::addNamesToChannel(*channel, names); } } - else if (numeric == Reply::ENDOFNAMES) // We're done here. - { - Jupiter::ReferenceString chan = Jupiter::ReferenceString::getWord(line, 3, " "); - Channel *channel = getChannel(chan); - - if (channel != nullptr) + else if (numeric == Reply::ENDOFNAMES) { // We're done here. + std::string_view channel_name = getLineToken(3); + Channel* channel = getChannel(channel_name); + if (channel != nullptr) { channel->m_adding_names = false; + } } break; } + } } } else { if (w1 == "PING"sv) { - m_socket->send(Jupiter::StringS::Format("PONG %.*s" ENDL, w2.size(), w2.ptr())); + m_socket->send(Jupiter::StringS::Format("PONG %.*s" ENDL, first_split.second.size(), first_split.second.data())); } else if (w1 == "NICK"sv) { - if (w2.isNotEmpty()) - m_nickname = w2; + // TODO: How should we be handling whitespaces in first_split.second? + if (!first_split.second.empty()) + m_nickname = first_split.second; } else if (w1 == "ERROR"sv) { @@ -1368,61 +1257,60 @@ int Jupiter::IRC::Client::think() return handle_error(tmp); } -std::string_view Jupiter::IRC::Client::readConfigValue(const Jupiter::ReadableString &key, const Jupiter::ReadableString &defaultValue) const -{ - if (m_primary_section != nullptr) - { +std::string_view Jupiter::IRC::Client::readConfigValue(const Jupiter::ReadableString &key, std::string_view defaultValue) const { + if (m_primary_section != nullptr) { std::string_view val = m_primary_section->get(key); - if (!val.empty()) + if (!val.empty()) { return val; + } } - if (m_secondary_section != nullptr) + if (m_secondary_section != nullptr) { return m_secondary_section->get(key, defaultValue); + } return defaultValue; } -bool Jupiter::IRC::Client::readConfigBool(const Jupiter::ReadableString &key, bool defaultValue) const -{ - if (m_primary_section != nullptr) - { +bool Jupiter::IRC::Client::readConfigBool(const Jupiter::ReadableString &key, bool defaultValue) const { + if (m_primary_section != nullptr) { std::string_view val = m_primary_section->get(key); - if (!val.empty()) - return Jupiter::ReferenceString{val}.asBool(); + if (!val.empty()) { + return Jupiter::from_string(val); + } } - if (m_secondary_section != nullptr) + if (m_secondary_section != nullptr) { return m_secondary_section->get(key, defaultValue); + } return defaultValue; } -int Jupiter::IRC::Client::readConfigInt(const Jupiter::ReadableString &key, int defaultValue) const -{ - if (m_primary_section != nullptr) - { +int Jupiter::IRC::Client::readConfigInt(const Jupiter::ReadableString &key, int defaultValue) const { + if (m_primary_section != nullptr) { std::string_view val = m_primary_section->get(key); - if (!val.empty()) - return Jupiter::ReferenceString{val}.asInt(); + if (!val.empty()) { + return Jupiter::from_string(val); + } } - if (m_secondary_section != nullptr) + if (m_secondary_section != nullptr) { return m_secondary_section->get(key, defaultValue); + } return defaultValue; } -long Jupiter::IRC::Client::readConfigLong(const Jupiter::ReadableString &key, long defaultValue) const -{ - if (m_primary_section != nullptr) - { +long Jupiter::IRC::Client::readConfigLong(const Jupiter::ReadableString &key, long defaultValue) const { + if (m_primary_section != nullptr) { std::string_view val = m_primary_section->get(key); - if (!val.empty()) - return Jupiter::ReferenceString{val}.asInt(); + if (!val.empty()) { + return Jupiter::from_string(val); + } } if (m_secondary_section != nullptr) @@ -1431,27 +1319,26 @@ long Jupiter::IRC::Client::readConfigLong(const Jupiter::ReadableString &key, lo return defaultValue; } -double Jupiter::IRC::Client::readConfigDouble(const Jupiter::ReadableString &key, double defaultValue) const -{ - if (m_primary_section != nullptr) - { +double Jupiter::IRC::Client::readConfigDouble(const Jupiter::ReadableString &key, double defaultValue) const { + if (m_primary_section != nullptr) { std::string_view val = m_primary_section->get(key); - if (!val.empty()) - return Jupiter::ReferenceString{val}.asDouble(); + if (!val.empty()) { + return Jupiter::from_string(val); + } } - if (m_secondary_section != nullptr) + if (m_secondary_section != nullptr) { return m_secondary_section->get(key, defaultValue); + } return defaultValue; } -void Jupiter::IRC::Client::writeToLogs(const Jupiter::ReadableString &message) -{ - if (m_log_file != nullptr) - { - message.println(m_log_file); +void Jupiter::IRC::Client::writeToLogs(const Jupiter::ReadableString &message) { + if (m_log_file != nullptr) { + fwrite(message.ptr(), sizeof(char), message.size(), m_log_file); + fputs("\r\n", m_log_file); fflush(m_log_file); } } @@ -1460,74 +1347,73 @@ void Jupiter::IRC::Client::writeToLogs(const Jupiter::ReadableString &message) * @brief IRC Client Private Function Implementations */ -void Jupiter::IRC::Client::delChannel(const Jupiter::ReadableString &in_channel) -{ - m_channels.erase(in_channel); +void Jupiter::IRC::Client::delChannel(std::string_view in_channel) { + m_channels.erase(Jupiter::ReferenceString{in_channel}); } -std::shared_ptr Jupiter::IRC::Client::findUser(const Jupiter::ReadableString &in_nickname) const -{ +std::shared_ptr Jupiter::IRC::Client::findUser(const Jupiter::ReadableString &in_nickname) const { return getUser(in_nickname); } -std::shared_ptr Jupiter::IRC::Client::findUserOrAdd(const Jupiter::ReadableString &name) -{ - Jupiter::ReferenceString nick = Jupiter::ReferenceString::getWord(name, 0, "!"); - auto result = Jupiter::IRC::Client::findUser(nick); +std::shared_ptr Jupiter::IRC::Client::findUserOrAdd(std::string_view in_name) { + // in_name: Nickname!Username@Hostname + // Assume: Nickname can contain anything but ! + // Assume: Username can contain anything but @ + // Assume: Hostname can contain anything + auto name_split = jessilib::split_once(in_name, '!'); - if (result != nullptr) + auto result = getUser(name_split.first); + if (result != nullptr) { return result; + } + + auto username_hostname = jessilib::split_once(name_split.second, '@'); auto user = std::make_shared(); - user->m_nickname = nick; - user->m_username = Jupiter::ReferenceString::getWord(name, 1, "!@"); - user->m_hostname = Jupiter::ReferenceString::getWord(name, 2, "!@"); - return m_users.emplace(nick, user).first->second; + user->m_nickname = name_split.first; + user->m_username = username_hostname.first; + user->m_hostname = username_hostname.second; + return m_users.emplace(user->m_nickname, user).first->second; } -void Jupiter::IRC::Client::addNamesToChannel(Channel &in_channel, Jupiter::ReadableString &in_names) -{ - Jupiter::ReferenceString tmp; +void Jupiter::IRC::Client::addNamesToChannel(Channel &in_channel, std::string_view in_names) { size_t offset; - unsigned int nameLen = in_names.wordCount(" "); - - for (size_t i = 0; i != nameLen; i++) - { - tmp = Jupiter::ReferenceString::getWord(in_names, i, " "); - - if (tmp.isNotEmpty()) - { - offset = tmp.span(m_prefixes); - tmp.shiftRight(offset); - - auto user = Client::findUserOrAdd(tmp); - tmp.shiftLeft(offset); + std::vector names = jessilib::split_view(in_names, ' '); + for (auto name : names) { + if (name.empty()) { + // Good candidate for a warning log + continue; + } - in_channel.addUser(user); + offset = name.find_first_not_of(m_prefixes); + if (offset == std::string_view::npos) { + // The user's name is nothing but prefixes!? Good candidate for an error log + continue; + } - while (offset > 0) - { - --offset; + // Add the user + auto user = Client::findUserOrAdd(name.substr(offset)); + in_channel.addUser(user); - in_channel.addUserPrefix(user->getNickname(), tmp[offset]); - } + // Add any prefixes we received to the user + while (offset > 0) { + --offset; + in_channel.addUserPrefix(user->getNickname(), name[offset]); } } } -void Jupiter::IRC::Client::addChannel(const Jupiter::ReadableString &in_channel) -{ - m_channels.emplace(in_channel, Channel(in_channel, this)); +void Jupiter::IRC::Client::addChannel(std::string_view in_channel) { + Jupiter::ReferenceString channel_name = in_channel; + m_channels.emplace(channel_name, Channel(channel_name, this)); } -bool Jupiter::IRC::Client::startCAP() -{ +bool Jupiter::IRC::Client::startCAP() { m_connection_status = 2; - return m_socket->send("CAP LS"_jrs ENDL) > 0; + return m_socket->send("CAP LS"sv ENDL) > 0; } -bool Jupiter::IRC::Client::registerClient() -{ +bool Jupiter::IRC::Client::registerClient() { bool result = true; const char *localHostname = Jupiter::Socket::getLocalHostname(); Jupiter::StringS messageToSend; @@ -1550,23 +1436,19 @@ bool Jupiter::IRC::Client::registerClient() * User Implementation */ -const Jupiter::ReadableString &Jupiter::IRC::Client::User::getNickname() const -{ +const Jupiter::ReadableString &Jupiter::IRC::Client::User::getNickname() const { return m_nickname; } -const Jupiter::ReadableString &Jupiter::IRC::Client::User::getUsername() const -{ +const Jupiter::ReadableString &Jupiter::IRC::Client::User::getUsername() const { return m_username; } -const Jupiter::ReadableString &Jupiter::IRC::Client::User::getHostname() const -{ +const Jupiter::ReadableString &Jupiter::IRC::Client::User::getHostname() const { return m_hostname; } -size_t Jupiter::IRC::Client::User::getChannelCount() const -{ +size_t Jupiter::IRC::Client::User::getChannelCount() const { return m_channel_count; } @@ -1574,10 +1456,8 @@ size_t Jupiter::IRC::Client::User::getChannelCount() const * Channel Implementation */ -Jupiter::IRC::Client::Channel::Channel(const Jupiter::ReadableString &in_name, Jupiter::IRC::Client *in_parent) -{ - auto to_lower = [&in_name]() - { +Jupiter::IRC::Client::Channel::Channel(const Jupiter::ReadableString &in_name, Jupiter::IRC::Client *in_parent) { + auto to_lower = [&in_name]() { Jupiter::String result(in_name.size()); const char *itr = in_name.ptr(); const char *end = itr + in_name.size(); @@ -1597,8 +1477,7 @@ Jupiter::IRC::Client::Channel::Channel(const Jupiter::ReadableString &in_name, J Jupiter::String name = to_lower(); - auto read_type = [&name](Jupiter::Config &in_config, int default_type) - { + auto read_type = [&name](Jupiter::Config &in_config, int default_type) { return in_config["Channels"_jrs][name].get("Type"_jrs, default_type); }; @@ -1609,8 +1488,7 @@ Jupiter::IRC::Client::Channel::Channel(const Jupiter::ReadableString &in_name, J m_type = read_type(*m_parent->getPrimaryConfigSection(), m_type); } -std::shared_ptr Jupiter::IRC::Client::Channel::addUser(std::shared_ptr user) -{ +std::shared_ptr Jupiter::IRC::Client::Channel::addUser(std::shared_ptr user) { auto channel_user = std::make_shared(); channel_user->m_user = user; @@ -1620,8 +1498,7 @@ std::shared_ptr Jupiter::IRC::Client::Chann return channel_user; } -std::shared_ptr Jupiter::IRC::Client::Channel::addUser(std::shared_ptr user, const char prefix) -{ +std::shared_ptr Jupiter::IRC::Client::Channel::addUser(std::shared_ptr user, const char prefix) { auto channel_user = std::make_shared(); channel_user->m_user = user; channel_user->m_prefixes = prefix; @@ -1632,35 +1509,30 @@ std::shared_ptr Jupiter::IRC::Client::Chann return channel_user; } -void Jupiter::IRC::Client::Channel::delUser(const Jupiter::ReadableString &in_nickname) -{ - m_users.erase(in_nickname); +void Jupiter::IRC::Client::Channel::delUser(std::string_view in_nickname) { + m_users.erase(Jupiter::ReferenceString{in_nickname}); } -void Jupiter::IRC::Client::Channel::addUserPrefix(const Jupiter::ReadableString &in_nickname, char prefix) -{ +void Jupiter::IRC::Client::Channel::addUserPrefix(std::string_view in_nickname, char prefix) { auto user = getUser(in_nickname); - if (user != nullptr && user->m_prefixes.contains(prefix) == false) + if (user != nullptr && user->m_prefixes.find(prefix) == std::string_view::npos) user->m_prefixes += prefix; } -void Jupiter::IRC::Client::Channel::delUserPrefix(const Jupiter::ReadableString &in_nickname, char prefix) -{ +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); } -const Jupiter::ReadableString &Jupiter::IRC::Client::Channel::getName() const -{ +const Jupiter::ReadableString &Jupiter::IRC::Client::Channel::getName() const { return m_name; } -std::shared_ptr Jupiter::IRC::Client::Channel::getUser(const Jupiter::ReadableString &in_nickname) const -{ - auto user = m_users.find(in_nickname); +std::shared_ptr Jupiter::IRC::Client::Channel::getUser(std::string_view in_nickname) const { + auto user = m_users.find(Jupiter::ReferenceString{in_nickname}); if (user != m_users.end()) { return user->second; } @@ -1668,19 +1540,20 @@ std::shared_ptr Jupiter::IRC::Client::Chann return nullptr; } -char Jupiter::IRC::Client::Channel::getUserPrefix(const Channel::User &in_user) const -{ - const Jupiter::ReadableString &prefixes = m_parent->getPrefixes(); +static_assert(Jupiter::INVALID_INDEX == std::string_view::npos); - for (size_t index = 0; index != prefixes.size(); ++index) - if (in_user.m_prefixes.contains(prefixes[index])) - return prefixes[index]; +char Jupiter::IRC::Client::Channel::getUserPrefix(const Channel::User& in_user) const { + std::string_view prefixes = m_parent->getPrefixes(); + for (auto prefix : prefixes) { + if (in_user.m_prefixes.find(prefix) != std::string_view::npos) { + return prefix; + } + } return 0; } -char Jupiter::IRC::Client::Channel::getUserPrefix(const Jupiter::ReadableString &in_nickname) const -{ +char Jupiter::IRC::Client::Channel::getUserPrefix(std::string_view in_nickname) const { auto user = getUser(in_nickname); if (user != nullptr) @@ -1689,23 +1562,19 @@ char Jupiter::IRC::Client::Channel::getUserPrefix(const Jupiter::ReadableString return 0; } -const Jupiter::IRC::Client::Channel::UserTableType &Jupiter::IRC::Client::Channel::getUsers() const -{ +const Jupiter::IRC::Client::Channel::UserTableType &Jupiter::IRC::Client::Channel::getUsers() const { return m_users; } -size_t Jupiter::IRC::Client::Channel::getUserCount() const -{ +size_t Jupiter::IRC::Client::Channel::getUserCount() const { return m_users.size(); } -int Jupiter::IRC::Client::Channel::getType() const -{ +int Jupiter::IRC::Client::Channel::getType() const { return m_type; } -void Jupiter::IRC::Client::Channel::setType(int in_type) -{ +void Jupiter::IRC::Client::Channel::setType(int in_type) { m_type = in_type; } @@ -1713,38 +1582,31 @@ void Jupiter::IRC::Client::Channel::setType(int in_type) * Channel User Implementation */ -Jupiter::IRC::Client::Channel::User::~User() -{ +Jupiter::IRC::Client::Channel::User::~User() { if (m_user != nullptr) --m_user->m_channel_count; } -Jupiter::IRC::Client::User *Jupiter::IRC::Client::Channel::User::getUser() const -{ +Jupiter::IRC::Client::User *Jupiter::IRC::Client::Channel::User::getUser() const { return m_user.get(); } -const Jupiter::ReadableString &Jupiter::IRC::Client::Channel::User::getPrefixes() const -{ +const Jupiter::ReadableString &Jupiter::IRC::Client::Channel::User::getPrefixes() const { return m_prefixes; } -const Jupiter::ReadableString &Jupiter::IRC::Client::Channel::User::getNickname() const -{ +const Jupiter::ReadableString &Jupiter::IRC::Client::Channel::User::getNickname() const { return m_user->getNickname(); } -const Jupiter::ReadableString &Jupiter::IRC::Client::Channel::User::getUsername() const -{ +const Jupiter::ReadableString &Jupiter::IRC::Client::Channel::User::getUsername() const { return m_user->getUsername(); } -const Jupiter::ReadableString &Jupiter::IRC::Client::Channel::User::getHostname() const -{ +const Jupiter::ReadableString &Jupiter::IRC::Client::Channel::User::getHostname() const { return m_user->getHostname(); } -size_t Jupiter::IRC::Client::Channel::User::getChannelCount() const -{ +size_t Jupiter::IRC::Client::Channel::User::getChannelCount() const { return m_user->getChannelCount(); } \ No newline at end of file diff --git a/src/common/Plugin.cpp b/src/common/Plugin.cpp index ac54592..20e50ac 100644 --- a/src/common/Plugin.cpp +++ b/src/common/Plugin.cpp @@ -281,63 +281,63 @@ void Jupiter::Plugin::OnReconnectAttempt(Jupiter::IRC::Client *, bool) { return; } -void Jupiter::Plugin::OnRaw(Jupiter::IRC::Client *, const Jupiter::ReadableString &) { +void Jupiter::Plugin::OnRaw(Jupiter::IRC::Client *, std::string_view) { return; } -void Jupiter::Plugin::OnNumeric(Jupiter::IRC::Client *, long int, const Jupiter::ReadableString &) { +void Jupiter::Plugin::OnNumeric(Jupiter::IRC::Client *, long int, std::string_view) { return; } -void Jupiter::Plugin::OnError(Jupiter::IRC::Client *, const Jupiter::ReadableString &) { +void Jupiter::Plugin::OnError(Jupiter::IRC::Client *, std::string_view) { return; } -void Jupiter::Plugin::OnChat(Jupiter::IRC::Client *, const Jupiter::ReadableString &, const Jupiter::ReadableString &, const Jupiter::ReadableString &) { +void Jupiter::Plugin::OnChat(Jupiter::IRC::Client *, std::string_view, std::string_view, std::string_view) { return; } -void Jupiter::Plugin::OnNotice(Jupiter::IRC::Client *, const Jupiter::ReadableString &, const Jupiter::ReadableString &, const Jupiter::ReadableString &) { +void Jupiter::Plugin::OnNotice(Jupiter::IRC::Client *, std::string_view, std::string_view, std::string_view) { return; } -void Jupiter::Plugin::OnServerNotice(Jupiter::IRC::Client *, const Jupiter::ReadableString &, const Jupiter::ReadableString &, const Jupiter::ReadableString &) { +void Jupiter::Plugin::OnServerNotice(Jupiter::IRC::Client *, std::string_view, std::string_view, std::string_view) { return; } -void Jupiter::Plugin::OnCTCP(Jupiter::IRC::Client *, const Jupiter::ReadableString &, const Jupiter::ReadableString &, const Jupiter::ReadableString &) { +void Jupiter::Plugin::OnCTCP(Jupiter::IRC::Client *, std::string_view, std::string_view, std::string_view) { return; } -void Jupiter::Plugin::OnAction(Jupiter::IRC::Client *, const Jupiter::ReadableString &, const Jupiter::ReadableString &, const Jupiter::ReadableString &) { +void Jupiter::Plugin::OnAction(Jupiter::IRC::Client *, std::string_view, std::string_view, std::string_view) { return; } -void Jupiter::Plugin::OnInvite(Jupiter::IRC::Client *, const Jupiter::ReadableString &, const Jupiter::ReadableString &, const Jupiter::ReadableString &) { +void Jupiter::Plugin::OnInvite(Jupiter::IRC::Client *, std::string_view, std::string_view, std::string_view) { return; } -void Jupiter::Plugin::OnJoin(Jupiter::IRC::Client *, const Jupiter::ReadableString &, const Jupiter::ReadableString &) { +void Jupiter::Plugin::OnJoin(Jupiter::IRC::Client *, std::string_view, std::string_view) { return; } -void Jupiter::Plugin::OnPart(Jupiter::IRC::Client *, const Jupiter::ReadableString &, const Jupiter::ReadableString &, const Jupiter::ReadableString &) { +void Jupiter::Plugin::OnPart(Jupiter::IRC::Client *, std::string_view, std::string_view, std::string_view) { return; } -void Jupiter::Plugin::OnNick(Jupiter::IRC::Client *, const Jupiter::ReadableString &, const Jupiter::ReadableString &) { +void Jupiter::Plugin::OnNick(Jupiter::IRC::Client *, std::string_view, std::string_view) { return; } -void Jupiter::Plugin::OnKick(Jupiter::IRC::Client *, const Jupiter::ReadableString &, const Jupiter::ReadableString &, const Jupiter::ReadableString &, const Jupiter::ReadableString &) { +void Jupiter::Plugin::OnKick(Jupiter::IRC::Client *, std::string_view, std::string_view, std::string_view, std::string_view) { return; } -void Jupiter::Plugin::OnQuit(Jupiter::IRC::Client *, const Jupiter::ReadableString &, const Jupiter::ReadableString &) { +void Jupiter::Plugin::OnQuit(Jupiter::IRC::Client *, std::string_view, std::string_view) { return; } -void Jupiter::Plugin::OnMode(Jupiter::IRC::Client *, const Jupiter::ReadableString &, const Jupiter::ReadableString &, const Jupiter::ReadableString &) { +void Jupiter::Plugin::OnMode(Jupiter::IRC::Client *, std::string_view, std::string_view, std::string_view) { return; } diff --git a/src/common/Socket.cpp b/src/common/Socket.cpp index 720c95c..61d5596 100644 --- a/src/common/Socket.cpp +++ b/src/common/Socket.cpp @@ -346,15 +346,17 @@ in_addr6 Jupiter::Socket::pton6(const char *str) { Jupiter::StringS Jupiter::Socket::ntop4(uint32_t ip) { static char buf[16]; - if (inet_ntop(AF_INET, &ip, buf, sizeof(buf)) == nullptr) - return Jupiter::StringS::empty; + if (inet_ntop(AF_INET, &ip, buf, sizeof(buf)) == nullptr) { + return {}; + } return Jupiter::String(buf); } Jupiter::StringS Jupiter::Socket::ntop6(in_addr6 ip) { static char buf[46]; - if (inet_ntop(AF_INET6, &ip, buf, sizeof(buf)) == nullptr) - return Jupiter::StringS::empty; + if (inet_ntop(AF_INET6, &ip, buf, sizeof(buf)) == nullptr) { + return {}; + } return Jupiter::String(buf); } @@ -365,7 +367,7 @@ Jupiter::StringS Jupiter::Socket::ntop(void *ip, size_t size) { case 16: return ntop6(*reinterpret_cast(ip)); default: - return Jupiter::StringS::empty; + return {}; } } @@ -503,6 +505,10 @@ int Jupiter::Socket::send(const Jupiter::ReadableString &str) { return this->send(str.ptr(), str.size()); } +int Jupiter::Socket::send(std::string_view str) { + return this->send(str.data(), str.size()); +} + int Jupiter::Socket::send(const char *msg) { return this->send(msg, strlen(msg)); } diff --git a/src/include/Jupiter/Command.h b/src/include/Jupiter/Command.h index 0a235f2..97d29c9 100644 --- a/src/include/Jupiter/Command.h +++ b/src/include/Jupiter/Command.h @@ -73,7 +73,7 @@ namespace Jupiter * @param parameters Optional string containing any data to be passed to the help command. * @return Brief description of command and syntax. */ - virtual const Jupiter::ReadableString &getHelp(const Jupiter::ReadableString ¶meters = Jupiter::ReferenceString::empty) = 0; + virtual const Jupiter::ReadableString &getHelp(const Jupiter::ReadableString ¶meters = Jupiter::ReferenceString{}) = 0; /** * @brief Default constructor for command class. diff --git a/src/include/Jupiter/Config.h b/src/include/Jupiter/Config.h index 098b98b..302bac8 100644 --- a/src/include/Jupiter/Config.h +++ b/src/include/Jupiter/Config.h @@ -253,14 +253,15 @@ namespace Jupiter /** Template function implementations */ -template inline T Jupiter::Config::get(std::string_view in_key, T in_default_value) const -{ +template +inline T Jupiter::Config::get(std::string_view in_key, T in_default_value) const { auto result = m_table.find(JUPITER_WRAP_CONFIG_KEY(in_key)); - if (result == m_table.end()) + if (result == m_table.end()) { return in_default_value; + } - return static_cast(Jupiter::ReferenceString{result->second}); + return from_string(result->second); } /** Re-enable warnings */ diff --git a/src/include/Jupiter/File.h b/src/include/Jupiter/File.h index d98a913..320cc30 100644 --- a/src/include/Jupiter/File.h +++ b/src/include/Jupiter/File.h @@ -48,14 +48,14 @@ namespace Jupiter * @param line Index of the line to fetch. * @return Line of text at the specified index. */ - const Jupiter::ReadableString &getLine(size_t line) const; + const std::string& getLine(size_t line) const; /** * @brief Returns the name of the first raw file originally loaded. * * @return String containing the name of the first file loaded into this file. */ - const std::string &getFileName() const; + const std::string& getFileName() const; /** * @brief Adds data to a file, which may consist of one or more lines. @@ -63,7 +63,7 @@ namespace Jupiter * @param data Data to add to the file. * @param True if data was added to the file, false otherwise. */ - bool addData(const Jupiter::ReadableString &data); + bool addData(std::string_view data); /** * @brief Loads a file from the drive into this file. @@ -72,7 +72,7 @@ namespace Jupiter * @return True if a file was successfully loaded from the drive, false otherwise. */ bool load(const char *file); - bool load(const Jupiter::ReadableString &file); + bool load(std::string file); /** * @brief Loads a file from the drive into this file. @@ -101,7 +101,7 @@ namespace Jupiter * @return True if a file was successfully loaded from the drive, false otherwise. */ bool reload(const char *file); - bool reload(const Jupiter::ReadableString &file); + bool reload(std::string file); /** * @brief Unloads all of a file's contents, and attempts to load from a specified file stream. @@ -125,7 +125,7 @@ namespace Jupiter * @return True if the file was successfully written to the drive, false otherwise. */ bool sync(const char *file); - bool sync(const Jupiter::ReadableString &file); + bool sync(const std::string& file); /** * @brief Syncs data from the file to the drive. @@ -133,7 +133,7 @@ namespace Jupiter * @param file C FILE stream to output to. * @return True if the file was successfully written to the drive, false otherwise. */ - bool sync(FILE *file); + bool sync(FILE *file); // Why are we working with FILE instead of istream? /** * @brief Default constructor for File class. diff --git a/src/include/Jupiter/GenericCommand.h b/src/include/Jupiter/GenericCommand.h index c515a1b..e65c729 100644 --- a/src/include/Jupiter/GenericCommand.h +++ b/src/include/Jupiter/GenericCommand.h @@ -57,7 +57,7 @@ namespace Jupiter /** Data entry returned by trigger() */ struct JUPITER_API ResponseLine { - Jupiter::StringS response; + std::string response; GenericCommand::DisplayType type; ResponseLine *next = nullptr; @@ -71,6 +71,7 @@ namespace Jupiter ResponseLine *set(const Jupiter::ReadableString &response, GenericCommand::DisplayType type); ResponseLine() = default; ResponseLine(const Jupiter::ReadableString &response, GenericCommand::DisplayType type); + ResponseLine(std::string response, GenericCommand::DisplayType type); }; /** @@ -140,11 +141,11 @@ namespace Jupiter void setUsing(bool in_value); std::vector getCommands() const; // differs from m_commands in that it checks if it's using a namespace - GenericCommand *getCommand(const Jupiter::ReadableString &in_command) const; + GenericCommand *getCommand(std::string_view in_command) const; void addCommand(GenericCommand &in_command); void removeCommand(GenericCommand &in_command); - void removeCommand(const Jupiter::ReadableString &in_command); + void removeCommand(std::string_view in_command); void updateHelp(); diff --git a/src/include/Jupiter/IRC_Client.h b/src/include/Jupiter/IRC_Client.h index 1335578..cd7890e 100644 --- a/src/include/Jupiter/IRC_Client.h +++ b/src/include/Jupiter/IRC_Client.h @@ -77,7 +77,7 @@ namespace Jupiter * * @param in_message The raw message. */ - virtual void OnRaw(const Jupiter::ReadableString &in_message); + virtual void OnRaw(std::string_view in_message); /** * @brief This is called after an IRC numeric has been processed. @@ -85,7 +85,7 @@ namespace Jupiter * @param in_numerouic The numeroic of the message * @param in_message The raw message. */ - virtual void OnNumeric(long int in_numeric, const Jupiter::ReadableString &in_message); + virtual void OnNumeric(long int in_numeric, std::string_view in_message); /** * @brief This is called when an ERROR is received. @@ -93,7 +93,7 @@ namespace Jupiter * * @param message Message sent by the server. */ - virtual void OnError(const Jupiter::ReadableString &in_message); + virtual void OnError(std::string_view in_message); /** * @brief This is called when a chat message is received. @@ -102,7 +102,7 @@ namespace Jupiter * @param nick String containing the nickname of the sender. * @param message String containing the message sent. */ - virtual void OnChat(const Jupiter::ReadableString &in_channel, const Jupiter::ReadableString &in_nickname, const Jupiter::ReadableString &in_message); + virtual void OnChat(std::string_view in_channel, std::string_view in_nickname, std::string_view in_message); /** * @brief This is called when a notice is received. @@ -111,7 +111,7 @@ namespace Jupiter * @param nick String containing the nickname of the sender. * @param message String containing the message sent. */ - virtual void OnNotice(const Jupiter::ReadableString &in_channel, const Jupiter::ReadableString &in_sender, const Jupiter::ReadableString &in_message); + virtual void OnNotice(std::string_view in_channel, std::string_view in_sender, std::string_view in_message); /** * @brief This is called when a server notice is received. @@ -120,7 +120,7 @@ namespace Jupiter * @param nick String containing the sender. * @param message String containing the message sent. */ - virtual void OnServerNotice(const Jupiter::ReadableString &in_channel, const Jupiter::ReadableString &in_sender, const Jupiter::ReadableString &in_message); + virtual void OnServerNotice(std::string_view in_channel, std::string_view in_sender, std::string_view in_message); /** * @brief This is called when a CTCP message is received. @@ -129,7 +129,7 @@ namespace Jupiter * @param nick String containing the nickname of the sender. * @param message String containing the message sent. */ - virtual void OnCTCP(const Jupiter::ReadableString &in_channel, const Jupiter::ReadableString &in_nickname, const Jupiter::ReadableString &in_command, const Jupiter::ReadableString &in_message); + virtual void OnCTCP(std::string_view in_channel, std::string_view in_nickname, std::string_view in_command, std::string_view in_message); /** * @brief This is called when an action message is received. @@ -138,7 +138,7 @@ namespace Jupiter * @param nick String containing the nickname of the sender. * @param message String containing the message sent. */ - virtual void OnAction(const Jupiter::ReadableString &in_channel, const Jupiter::ReadableString &in_nickname, const Jupiter::ReadableString &in_message); + virtual void OnAction(std::string_view in_channel, std::string_view in_nickname, std::string_view in_message); /** * @brief This is called when an invite is received. @@ -147,7 +147,7 @@ namespace Jupiter * @param inviter String containing the nickname of the inviter. * @param invited String containing the nickname of the user invited. */ - virtual void OnInvite(const Jupiter::ReadableString &in_channel, const Jupiter::ReadableString &in_inviter, const Jupiter::ReadableString &in_invited); + virtual void OnInvite(std::string_view in_channel, std::string_view in_inviter, std::string_view in_invited); /** * @brief This is called when a chat message is received. @@ -156,7 +156,7 @@ namespace Jupiter * @param nick String containing the nickname of the sender. * @param message String containing the message sent. */ - virtual void OnJoin(const Jupiter::ReadableString &in_channel, const Jupiter::ReadableString &in_nickname); + virtual void OnJoin(std::string_view in_channel, std::string_view in_nickname); /** * @brief This is called when a user parts a channel. @@ -165,7 +165,7 @@ namespace Jupiter * @param nick String containing the nickname of the user. * @param reason String containing the reason for parting, or nullptr if none is specified. */ - virtual void OnPart(const Jupiter::ReadableString &in_channel, const Jupiter::ReadableString &in_nickname, const Jupiter::ReadableString &in_reason); + virtual void OnPart(std::string_view in_channel, std::string_view in_nickname, std::string_view in_reason); /** * @brief This is called when a user changes their nickname. @@ -173,7 +173,7 @@ namespace Jupiter * @param oldnick String containing the old nickname of the user. * @param newnick String containing the new nickname of the user. */ - virtual void OnNick(const Jupiter::ReadableString &in_old_nick, const Jupiter::ReadableString &in_new_nick); + virtual void OnNick(std::string_view in_old_nick, std::string_view in_new_nick); /** * @brief This is called when a user is kicked from a channel. @@ -183,7 +183,7 @@ namespace Jupiter * @param kicked String containing the nickname of the user kicked. * @param reason String containing the reason for the kick, or nullptr if none is specified. */ - virtual void OnKick(const Jupiter::ReadableString &in_channel, const Jupiter::ReadableString &in_kicker, const Jupiter::ReadableString &in_kicked, const Jupiter::ReadableString &in_reason); + virtual void OnKick(std::string_view in_channel, std::string_view in_kicker, std::string_view in_kicked, std::string_view in_reason); /** * @brief This is called when a user quits the server. @@ -191,7 +191,7 @@ namespace Jupiter * @param nick String containing the nickname of the user. * @param message String containing the reason for quiting. */ - virtual void OnQuit(const Jupiter::ReadableString &in_nickname, const Jupiter::ReadableString &in_message); + virtual void OnQuit(std::string_view in_nickname, std::string_view in_message); /** * @brief This is called when a channel mode is changed. @@ -200,7 +200,7 @@ namespace Jupiter * @param nick String containing the nickname of the user. * @param modeString String containing the modes changed. */ - virtual void OnMode(const Jupiter::ReadableString &in_channel, const Jupiter::ReadableString &in_nickname, const Jupiter::ReadableString &in_mode_string); + virtual void OnMode(std::string_view in_channel, std::string_view in_nickname, std::string_view in_mode_string); public: class Channel; @@ -330,7 +330,7 @@ namespace Jupiter * @param nickname String containing the nickname of the user to find. * @return A user if a match is found, nullptr otherwise. */ - std::shared_ptr getUser(const Jupiter::ReadableString &in_nickname) const; + std::shared_ptr getUser(std::string_view in_nickname) const; /** * @brief Adds a user to the channel @@ -354,7 +354,7 @@ namespace Jupiter * * @param nickname String containing the nickname of the user. */ - void delUser(const Jupiter::ReadableString &in_nickname); + void delUser(std::string_view in_nickname); /** * @brief Adds a prefix to a user. @@ -362,7 +362,7 @@ namespace Jupiter * @param user String containing the nickname of the user. * @param prefix Prefix to add to the user. */ - void addUserPrefix(const Jupiter::ReadableString &in_nickname, char in_prefix); + void addUserPrefix(std::string_view in_nickname, char in_prefix); /** * @brief Removes a prefix from a user. @@ -370,7 +370,7 @@ namespace Jupiter * @param user String containing the nickname of a user. * @param prefix Prefix to remove from the user. */ - void delUserPrefix(const Jupiter::ReadableString &in_nickname, char in_prefix); + void delUserPrefix(std::string_view in_nickname, char in_prefix); /** * @brief Returns a user's most significant prefix. @@ -379,7 +379,7 @@ namespace Jupiter * @return User's most significant prefix. */ char getUserPrefix(const Channel::User &in_user) const; - char getUserPrefix(const Jupiter::ReadableString &in_nickname) const; + char getUserPrefix(std::string_view in_nickname) const; /** * @brief Fetches the channel's user table @@ -437,7 +437,7 @@ namespace Jupiter * * @return String containing a config section's name. */ - const Jupiter::ReadableString &getConfigSection() const; + std::string_view getConfigSection() const; /** * @brief Fetches the primary config section @@ -481,14 +481,14 @@ namespace Jupiter * * @return String containing nickname prefixes. */ - const Jupiter::ReadableString &getPrefixes() const; + std::string_view getPrefixes() const; /** * @brief Returns mode symbols for nickname prefixes supported by the connected server. * * @return String containing mode symbols for nickname prefixes. */ - const Jupiter::ReadableString &getPrefixModes() const; + std::string_view getPrefixModes() const; /** * @brief Returns the client's current nickname. @@ -598,7 +598,7 @@ namespace Jupiter * @param nickname String containing the nickname of the user to fetch. * @return A User if a match is found, nullptr otherwise. */ - std::shared_ptr getUser(const Jupiter::ReadableString &in_nickname) const; + std::shared_ptr getUser(std::string_view in_nickname) const; /** * @brief Fetches the channel table @@ -620,7 +620,7 @@ namespace Jupiter * @param chanName String containing the name of a channel. * @return A channel with the specified name if it exists, nullptr otherwise. */ - Channel *getChannel(const Jupiter::ReadableString &in_channel) const; + Channel *getChannel(std::string_view in_channel) const; /** * @brief Sends a join request. @@ -635,14 +635,14 @@ namespace Jupiter * @param channel Channel to join. * @param password Password to use. */ - void joinChannel(const Jupiter::ReadableString &in_channel, const Jupiter::ReadableString &in_password); + void joinChannel(std::string_view in_channel, std::string_view in_password); /** * @brief Parts a channel. * * @param channel Channel to part. */ - void partChannel(const Jupiter::ReadableString &in_channel); + void partChannel(std::string_view in_channel); /** * @brief Parts a channel. @@ -650,7 +650,7 @@ namespace Jupiter * @param channel Channel to part. * @param message Reason for parting. */ - void partChannel(const Jupiter::ReadableString &in_channel, std::string_view in_message); + void partChannel(std::string_view in_channel, std::string_view in_message); /** * @brief Gets the access level of a user. @@ -659,8 +659,8 @@ namespace Jupiter * @param nick String containing the nickname of the user. * @return Access level of the user. */ - int getAccessLevel(const Channel &in_channel, const Jupiter::ReadableString &in_nickname) const; - int getAccessLevel(const Jupiter::ReadableString &in_channel, const Jupiter::ReadableString &in_nickname) const; + int getAccessLevel(const Channel &in_channel, std::string_view in_nickname) const; + int getAccessLevel(std::string_view in_channel, std::string_view in_nickname) const; /** * @brief Sends a message. @@ -668,7 +668,7 @@ namespace Jupiter * @param dest String containing the destination of the message (nickname or channel). * @param message String containing the message to send. */ - void sendMessage(const Jupiter::ReadableString &in_destination, const Jupiter::ReadableString &in_message); + void sendMessage(std::string_view in_destination, std::string_view in_message); /** * @brief Sends a notice. @@ -676,7 +676,7 @@ namespace Jupiter * @param dest String containing the destination of the message (nickname or channel). * @param message String containing the message to send. */ - void sendNotice(const Jupiter::ReadableString &in_destination, const Jupiter::ReadableString &in_message); + void sendNotice(std::string_view in_destination, std::string_view in_message); /** * @brief Sends a message to all channels of a given type. @@ -685,7 +685,7 @@ namespace Jupiter * @param message String containing the message to send. * @return Number of messages sent. */ - size_t messageChannels(int type, const Jupiter::ReadableString &in_message); + size_t messageChannels(int type, std::string_view in_message); /** * @brief Sends a message to all channels with a type of at least 0. @@ -732,7 +732,7 @@ namespace Jupiter * @param in_default_value Optional parameter specifying the default value to return if none is found. * @return String containing the key value if it exists, in_default_value otherwise. */ - std::string_view readConfigValue(const Jupiter::ReadableString &key, const Jupiter::ReadableString &in_default_value = Jupiter::ReferenceString::empty) const; + std::string_view readConfigValue(const Jupiter::ReadableString &key, std::string_view in_default_value = std::string_view{}) const; /** * @brief Returns a key's value as a boolean. @@ -849,13 +849,13 @@ namespace Jupiter std::string m_nickname; std::string m_realname; - Jupiter::StringS m_prefix_modes = "ov"; - Jupiter::StringS m_prefixes = "@+"; - Jupiter::StringS m_chan_types = "#"; - Jupiter::StringS m_modeA = "b"; - Jupiter::StringS m_modeB = "k"; - Jupiter::StringS m_modeC = "l"; - Jupiter::StringS m_modeD = "psitnm"; + std::string m_prefix_modes = "ov"; + std::string m_prefixes = "@+"; + std::string m_chan_types = "#"; + std::string m_modeA = "b"; + std::string m_modeB = "k"; + std::string m_modeC = "l"; + std::string m_modeD = "psitnm"; UserTableType m_users; ChannelTableType m_channels; @@ -871,14 +871,14 @@ namespace Jupiter int m_default_chan_type; bool m_dead = false; - void delChannel(const Jupiter::ReadableString &in_channel); - void addNamesToChannel(Channel &in_channel, Jupiter::ReadableString &in_names); - void addChannel(const Jupiter::ReadableString &in_channel); + void delChannel(std::string_view in_channel); + void addNamesToChannel(Channel &in_channel, std::string_view in_names); + void addChannel(std::string_view in_channel); bool startCAP(); bool registerClient(); std::shared_ptr findUser(const Jupiter::ReadableString &in_nickname) const; - std::shared_ptr findUserOrAdd(const Jupiter::ReadableString &in_nickname); + std::shared_ptr findUserOrAdd(std::string_view in_nickname); }; // Jupiter::IRC::Client class } // Jupiter::IRC namespace diff --git a/src/include/Jupiter/IRC_Numerics.h b/src/include/Jupiter/IRC_Numerics.h index c425652..6e0e01c 100644 --- a/src/include/Jupiter/IRC_Numerics.h +++ b/src/include/Jupiter/IRC_Numerics.h @@ -38,7 +38,6 @@ namespace Jupiter constexpr NumericType YOURHOST = 2; /** RFC2812: Post-registration. Your host is this server and I am running some daemon */ constexpr NumericType CREATED = 3; /** RFC2812: Post-registration. This was was created at some point in time */ constexpr NumericType MYINFO = 4; /** RFC2812: Post-registration. */ - constexpr NumericType BOUNCEOLD = 5; /** RFC2812/DEPRECATED: 005 is rarely used as a bounce indicator, but was defined in RFC2812. */ constexpr NumericType ISUPPORT = 5; /** Used to indicate what a server supports. Does not appear in any RFC, but was drafted in 2004. */ constexpr NumericType SNOMASK = 8; /** Server notice mask */ constexpr NumericType STATMEMTOT = 9; /** I have no idea what this does. */ diff --git a/src/include/Jupiter/Plugin.h b/src/include/Jupiter/Plugin.h index 9442f2f..bf15908 100644 --- a/src/include/Jupiter/Plugin.h +++ b/src/include/Jupiter/Plugin.h @@ -139,14 +139,14 @@ namespace Jupiter * * @param raw The raw message. */ - virtual void OnRaw(Jupiter::IRC::Client *server, const Jupiter::ReadableString &raw); + virtual void OnRaw(Jupiter::IRC::Client *server, std::string_view raw); /** * @brief This is called after an IRC numeric has been processed. * * @param raw The raw message. */ - virtual void OnNumeric(Jupiter::IRC::Client *server, long int numeric, const Jupiter::ReadableString &raw); + virtual void OnNumeric(Jupiter::IRC::Client *server, long int numeric, std::string_view raw); /** * @brief This is called when an ERROR is received. @@ -154,7 +154,7 @@ namespace Jupiter * * @param message Message sent by the server. */ - virtual void OnError(Jupiter::IRC::Client *server, const Jupiter::ReadableString &message); + virtual void OnError(Jupiter::IRC::Client *server, std::string_view message); /** * @brief This is called when a chat message is received. @@ -163,7 +163,7 @@ namespace Jupiter * @param nick String containing the nickname of the sender. * @param message String containing the message sent. */ - virtual void OnChat(Jupiter::IRC::Client *server, const Jupiter::ReadableString &channel, const Jupiter::ReadableString &nick, const Jupiter::ReadableString &message); + virtual void OnChat(Jupiter::IRC::Client *server, std::string_view channel, std::string_view nick, std::string_view message); /** * @brief This is called when a notice is received. @@ -172,7 +172,7 @@ namespace Jupiter * @param nick String containing the nickname of the sender. * @param message String containing the message sent. */ - virtual void OnNotice(Jupiter::IRC::Client *server, const Jupiter::ReadableString &chan, const Jupiter::ReadableString &sender, const Jupiter::ReadableString &message); + virtual void OnNotice(Jupiter::IRC::Client *server, std::string_view chan, std::string_view sender, std::string_view message); /** * @brief This is called when a server notice is received. @@ -181,7 +181,7 @@ namespace Jupiter * @param nick String containing the sender. * @param message String containing the message sent. */ - virtual void OnServerNotice(Jupiter::IRC::Client *server, const Jupiter::ReadableString &chan, const Jupiter::ReadableString &sender, const Jupiter::ReadableString &message); + virtual void OnServerNotice(Jupiter::IRC::Client *server, std::string_view chan, std::string_view sender, std::string_view message); /** * @brief This is called when a CTCP message is received. @@ -190,7 +190,7 @@ namespace Jupiter * @param nick String containing the nickname of the sender. * @param message String containing the message sent. */ - virtual void OnCTCP(Jupiter::IRC::Client *server, const Jupiter::ReadableString &channel, const Jupiter::ReadableString &nick, const Jupiter::ReadableString &message); + virtual void OnCTCP(Jupiter::IRC::Client *server, std::string_view channel, std::string_view nick, std::string_view message); /** * @brief This is called when an action message is received. @@ -199,7 +199,7 @@ namespace Jupiter * @param nick String containing the nickname of the sender. * @param message String containing the message sent. */ - virtual void OnAction(Jupiter::IRC::Client *server, const Jupiter::ReadableString &chan, const Jupiter::ReadableString &nick, const Jupiter::ReadableString &message); + virtual void OnAction(Jupiter::IRC::Client *server, std::string_view chan, std::string_view nick, std::string_view message); /** * @brief This is called when an invite is received. @@ -208,7 +208,7 @@ namespace Jupiter * @param inviter String containing the nickname of the inviter. * @param invited String containing the nickname of the user invited. */ - virtual void OnInvite(Jupiter::IRC::Client *server, const Jupiter::ReadableString &chan, const Jupiter::ReadableString &inviter, const Jupiter::ReadableString &invited); + virtual void OnInvite(Jupiter::IRC::Client *server, std::string_view chan, std::string_view inviter, std::string_view invited); /** * @brief This is called when a chat message is received. @@ -217,7 +217,7 @@ namespace Jupiter * @param nick String containing the nickname of the sender. * @param message String containing the message sent. */ - virtual void OnJoin(Jupiter::IRC::Client *server, const Jupiter::ReadableString &chan, const Jupiter::ReadableString &nick); + virtual void OnJoin(Jupiter::IRC::Client *server, std::string_view chan, std::string_view nick); /** * @brief This is called when a user parts a channel. @@ -226,7 +226,7 @@ namespace Jupiter * @param nick String containing the nickname of the user. * @param reason String containing the reason for parting, or nullptr if none is specified. */ - virtual void OnPart(Jupiter::IRC::Client *server, const Jupiter::ReadableString &chan, const Jupiter::ReadableString &nick, const Jupiter::ReadableString &reason); + virtual void OnPart(Jupiter::IRC::Client *server, std::string_view chan, std::string_view nick, std::string_view reason); /** * @brief This is called when a user changes their nickname. @@ -234,7 +234,7 @@ namespace Jupiter * @param oldnick String containing the old nickname of the user. * @param newnick String containing the new nickname of the user. */ - virtual void OnNick(Jupiter::IRC::Client *server, const Jupiter::ReadableString &oldnick, const Jupiter::ReadableString &newnick); + virtual void OnNick(Jupiter::IRC::Client *server, std::string_view oldnick, std::string_view newnick); /** * @brief This is called when a user is kicked from a channel. @@ -244,7 +244,7 @@ namespace Jupiter * @param kicked String containing the nickname of the user kicked. * @param reason String containing the reason for the kick, or nullptr if none is specified. */ - virtual void OnKick(Jupiter::IRC::Client *server, const Jupiter::ReadableString &chan, const Jupiter::ReadableString &kicker, const Jupiter::ReadableString &kicked, const Jupiter::ReadableString &reason); + virtual void OnKick(Jupiter::IRC::Client *server, std::string_view chan, std::string_view kicker, std::string_view kicked, std::string_view reason); /** * @brief This is called when a user quits the server. @@ -252,7 +252,7 @@ namespace Jupiter * @param nick String containing the nickname of the user. * @param message String containing the reason for quiting. */ - virtual void OnQuit(Jupiter::IRC::Client *server, const Jupiter::ReadableString &nick, const Jupiter::ReadableString &message); + virtual void OnQuit(Jupiter::IRC::Client *server, std::string_view nick, std::string_view message); /** * @brief This is called when a channel mode is changed. @@ -261,7 +261,7 @@ namespace Jupiter * @param nick String containing the nickname of the user. * @param modeString String containing the modes changed. */ - virtual void OnMode(Jupiter::IRC::Client *server, const Jupiter::ReadableString &chan, const Jupiter::ReadableString &nick, const Jupiter::ReadableString &modeString); + virtual void OnMode(Jupiter::IRC::Client *server, std::string_view chan, std::string_view nick, std::string_view modeString); /** * @brief This is called when a server "thinks". diff --git a/src/include/Jupiter/Readable_String.h b/src/include/Jupiter/Readable_String.h index 11eb82e..4ab4d65 100644 --- a/src/include/Jupiter/Readable_String.h +++ b/src/include/Jupiter/Readable_String.h @@ -31,6 +31,7 @@ #include // std::endl #include "InvalidIndex.h" #include "DataBuffer.h" +#include "Functions.h" namespace Jupiter { @@ -45,50 +46,33 @@ namespace Jupiter { public: - /** - * @brief Fetches an element from the string. - * - * @param index Index of the element to return. - * @return The element located at the specified index. - */ - virtual const T &get(size_t index) const = 0; - /** * @brief Returns a pointer to the underlying string of elements. * * @return Pointer to the underlying string of elements. */ - virtual const T *ptr() const = 0; + virtual const T *ptr() const = 0; // RENAME; 'data' /** * @brief Returns the number of elements in the String. * * @return Number of elements in the string. */ - virtual size_t size() const = 0; + virtual size_t size() const = 0; // KEEP /** * @brief Checks if the String is empty. * * @return True if the String is empty, false otherwise. */ - bool isEmpty() const { return size() == 0; }; - bool empty() const { return size() == 0; }; + bool empty() const { return size() == 0; }; // KEEP /** * @brief Checks if the String is not empty. * * @return True if the String is not empty, false otherwise. */ - virtual bool isNotEmpty() const { return !empty(); }; - - /** - * @brief Checks if the string contains an element with the specified value. - * - * @param value Value of the element to search for. - * @return True if a match is found, false otherwise. - */ - bool contains(const T &value) const; + virtual bool isNotEmpty() const { return !empty(); }; // REMOVE /** * @brief Returns the index of the first element in the string with the specified value. @@ -97,18 +81,8 @@ namespace Jupiter * @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; - size_t find(const Readable_String &in) const; - - /** - * @brief Returns the number of elements of the string which match the input string. - * - * @param in Character set to match against. - * @return Number of elements at the start of the string that match the character set. - */ - size_t span(const Readable_String &in) const; - size_t span(const T *str) const; - size_t span(const T &in) const; + 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. @@ -117,114 +91,10 @@ namespace Jupiter * @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; - bool equalsi(const std::basic_string &in) const; - bool equalsi(const T *in, size_t len) const; - bool equalsi(const T &in) const; - - /** - * @brief Checks if the String matches a wildcard format. - * Note: Case sensitive. - * - * @param format Format that the string is compared against. - * @return True if the String matches the wildcard format, false otherwise. - */ - bool match(const Readable_String &format) const; - bool match(const std::basic_string &format) const; - bool match(const T *format, size_t formatSize) const; - bool match(const T *format) const; - - /** - * @brief Checks if the CString matches a wildcard format. - * Note: Case insensitive. Returns false for any type other than char and wchar_t. - * - * @param format Format that the string is compared against. - * @return True if the CString matches the wildcard format, false otherwise. - */ - bool matchi(const Readable_String &format) const; - bool matchi(const std::basic_string &format) const; - bool matchi(const T *format, size_t formatSize) const; - bool matchi(const T *format) const; - - /** - * @brief Counts the number of token deliminated words. - * - * @param whitespace A string of tokens used to deliminate words. - * @return Number of words found. - */ - unsigned int wordCount(const T *whitespace) const; - - /** - * @brief Interprets the string as a bool. - * - * @return Bool interpretation of the string. - */ - bool asBool() const; - - /** - * @brief Interprets the string as an integer. - * Note: This returns 0 on any value string type other than char. - * - * @param base Base of the string representation. - * @return Integer representation of the string. - */ - int asInt(int base = 0) const; - long long asLongLong(int base = 0) const; - unsigned int asUnsignedInt(int base = 0) const; - unsigned long long asUnsignedLongLong(int base = 0) const; - - /** - * @brief Interprets the string as a floating-point decimal number. - * Note: This returns 0 on any value string type other than char. - * - * @param base Base of the string representation. - * @return Integer representation of the string. - */ - double asDouble() const; - - /** - * @brief Outputs the string to a FILE stream. - * - * @param out Stream to output to. - * @return Number of elements written successfully. - */ - size_t print(FILE *out) const; - size_t print(std::basic_ostream &out) const; - - /** - * @brief Outputs the string and a newline to a FILE stream - * - * @param out Stream to output to. - * @param Number of elements written successfully. - */ - size_t println(FILE *out) const; - size_t println(std::basic_ostream &out) const; - - /** - * @brief Copies a "word" from an input string and returns it in an output type. - * - * @param R Type to return. Must be a subclass of String_Type. - * - * @param in String to get a partial copy of. - * @param pos Index of the word to copy. - * @param whitespace String of characters that are to be considered whitespace. - * @return Copy of the word at the specified index on success, an empty string otherwise. - */ - template class R> static R getWord(const Jupiter::Readable_String &in, size_t pos, const T *whitespace); - template class R> static R getWord(const T *in, size_t pos, const T *whitespace); - - /** - * @brief Copies a part of an input string starting at a specified "word" and returns it in an output type. - * - * @param R Type to return. Must be a subclass of String_Type. - * - * @param in String to get a partial copy of. - * @param pos Index of the word to start copying from. - * @param whitespace String of characters that are to be considered whitespace. - * @return Copy of the string starting at the specified word on success, an empty string otherwise. - */ - template class R> static R gotoWord(const Jupiter::Readable_String &in, size_t pos, const T *whitespace); - template class R> static R gotoWord(const T *in, size_t pos, const T *whitespace); + 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. @@ -232,7 +102,7 @@ namespace Jupiter virtual ~Readable_String() = default; /** Access operator */ - inline const T &operator[](size_t index) const { return this->get(index); }; + inline const T &operator[](size_t index) const { return this->ptr()[index]; }; /** Comparative operators */ inline bool operator==(const Readable_String& right)const{ return operator==(std::basic_string_view{right}); } @@ -258,26 +128,131 @@ namespace Jupiter inline bool operator>=(const T right)const{ return !operator<(right); } /** Conversion operators */ - explicit inline operator bool() const { return this->asBool(); } - explicit inline operator signed char() const { return static_cast(this->asInt()); } - explicit inline operator unsigned char() const { return static_cast(this->asUnsignedInt()); } - explicit inline operator short() const { return static_cast(this->asInt()); } - explicit inline operator unsigned short() const { return static_cast(this->asUnsignedInt()); } - explicit inline operator int() const { return this->asInt(); } - explicit inline operator unsigned int() const { return this->asUnsignedInt(); } - explicit inline operator long() const { return static_cast(this->asLongLong()); } - explicit inline operator unsigned long() const { return static_cast(this->asLongLong()); } - explicit inline operator long long() const { return this->asLongLong(); } - explicit inline operator unsigned long long() const { return this->asUnsignedLongLong(); } - explicit inline operator float() const { return static_cast(this->asDouble()); } - explicit inline operator double() const { return this->asDouble(); } - explicit inline operator long double() const { return this->asDouble(); } // NEEDS TO NOT CAST FROM DOUBLE explicit inline operator std::basic_string() const { return std::basic_string(this->ptr(), this->size()); } inline operator std::basic_string_view() const { return std::basic_string_view(this->ptr(), this->size()); } }; /** Generic Readable String Type */ typedef Readable_String ReadableString; + + // TEMP; create some equivalent in jessilib + template + bool asBool(std::basic_string_view in_string) { + using namespace std::literals; + + // Invert the logic here, so that we return true for numerics and "true"/"on"/"+", false otherwise + // Note: temporarily removed case insensitivity, solely to avoid including unicode.hpp here + if (in_string == "FALSE"sv + || in_string == "false"sv + || in_string == "0"sv + || in_string == "OFF"sv + || in_string == "off"sv + || in_string == "-"sv) { + return false; + } + + return true; + } + + template + int asInt(std::basic_string_view in_string, int base = 0) { + return Jupiter_strtoi_s(in_string.data(), in_string.size(), base); + } + + template + long long asLongLong(std::basic_string_view in_string, int base = 0) { + return Jupiter_strtoll_s(in_string.data(), in_string.size(), base); + } + + template + unsigned int asUnsignedInt(std::basic_string_view in_string, int base = 0) { + return Jupiter_strtoui_s(in_string.data(), in_string.size(), base); + } + + template + unsigned long long asUnsignedLongLong(std::basic_string_view in_string, int base = 0) { + return Jupiter_strtoull_s(in_string.data(), in_string.size(), base); + } + + template + double asDouble(std::basic_string_view in_string) { + return Jupiter_strtod_s(in_string.data(), in_string.size()); + } + + template + OutT from_string(std::string_view in_string) { + return static_cast(in_string); + } + + template<> + inline bool from_string(std::string_view in_string) { + return asBool(in_string); + } + + template<> + inline signed char from_string(std::string_view in_string) { + return static_cast(asInt(in_string)); + } + + template<> + inline unsigned char from_string(std::string_view in_string) { + return static_cast(asUnsignedInt(in_string)); + } + + template<> + inline short from_string(std::string_view in_string) { + return static_cast(asInt(in_string)); + } + + template<> + inline unsigned short from_string(std::string_view in_string) { + return static_cast(asUnsignedInt(in_string)); + } + + template<> + inline int from_string(std::string_view in_string) { + return asInt(in_string); + } + + template<> + inline unsigned int from_string(std::string_view in_string) { + return asUnsignedInt(in_string); + } + + template<> + inline long from_string(std::string_view in_string) { + return asInt(in_string); + } + + template<> + inline unsigned long from_string(std::string_view in_string) { + return asUnsignedInt(in_string); + } + + template<> + inline long long from_string(std::string_view in_string) { + return asLongLong(in_string); + } + + template<> + inline unsigned long long from_string(std::string_view in_string) { + return asUnsignedLongLong(in_string); + } + + template<> + inline float from_string(std::string_view in_string) { + return static_cast(asDouble(in_string)); + } + + template<> + inline double from_string(std::string_view in_string) { + return asDouble(in_string); + } + + template<> + inline long double from_string(std::string_view in_string) { + return asDouble(in_string); + } } #include "Readable_String_Imp.h" diff --git a/src/include/Jupiter/Readable_String_Imp.h b/src/include/Jupiter/Readable_String_Imp.h index 3766505..fccac2b 100644 --- a/src/include/Jupiter/Readable_String_Imp.h +++ b/src/include/Jupiter/Readable_String_Imp.h @@ -26,28 +26,19 @@ */ #include "Readable_String.h" -#include "Functions.h" /** * IMPLEMENTATION: * Readable_String */ -// contains - -template bool Jupiter::Readable_String::contains(const T &value) const -{ - for (size_t i = 0; i != this->size(); i++) if (this->get(i) == value) return true; - return false; -} - // 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 (this->get(i) == value) + if (operator[](i) == value) { if (index == 0) return i; else index--; @@ -62,44 +53,18 @@ template size_t Jupiter::Readable_String::find(const Jupiter::Rea return Jupiter::INVALID_INDEX; if (in.size() == this->size()) return (*this == in) ? 0 : Jupiter::INVALID_INDEX; - if (in.isEmpty()) + if (in.empty()) return 0; for (size_t i = 0, j; i != this->size() - in.size() + 1; i++) { j = 0; - while (this->get(i + j) == in.get(j)) + while (operator[](i + j) == in[j]) if (++j == in.size()) return i; } return Jupiter::INVALID_INDEX; } -// span() - -template size_t Jupiter::Readable_String::span(const Jupiter::Readable_String &in) const -{ - size_t index = 0; - while (index != this->size() && in.contains(this->get(index))) - ++index; - return index; -} - -template size_t Jupiter::Readable_String::span(const T *in) const -{ - size_t index = 0; - while (index != this->size() && containsSymbol(in, this->get(index))) - ++index; - return index; -} - -template size_t Jupiter::Readable_String::span(const T &in) const -{ - size_t index = 0; - while (index != this->size() && this->get(index) == in) - ++index; - return index; -} - // equalsi() template bool Jupiter::Readable_String::equalsi(const Jupiter::Readable_String &in) const @@ -119,7 +84,7 @@ template<> bool inline Jupiter::Readable_String::equalsi(const char *in, s in += len; while (len != 0) - if (toupper(this->get(--len)) != toupper(*(--in))) + if (toupper(operator[](--len)) != toupper(*(--in))) return false; return true; @@ -131,7 +96,7 @@ template<> bool inline Jupiter::Readable_String::equalsi(const wchar_t return false; while (len != 0) - if (towupper(this->get(--len)) != towupper(*(--in))) + if (towupper(operator[](--len)) != towupper(*(--in))) return false; return true; @@ -144,12 +109,12 @@ template bool Jupiter::Readable_String::equalsi(const T *in, size template<> bool inline Jupiter::Readable_String::equalsi(const char &in) const { - return this->size() == 1 && toupper(this->get(0)) == toupper(in); + 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(this->get(0)) == towupper(in); + return this->size() == 1 && towupper(operator[](0)) == towupper(in); } template bool Jupiter::Readable_String::equalsi(const T &in) const @@ -157,744 +122,6 @@ template bool Jupiter::Readable_String::equalsi(const T &in) cons return *this == in; // Concept of "case" not supported for type. } -// match() - -template bool Jupiter::Readable_String::match(const Jupiter::Readable_String &format) const -{ - return this->match(format.ptr(), format.size()); -} - -template bool Jupiter::Readable_String::match(const std::basic_string &format) const -{ - return this->match(format.data(), format.size()); -} - -template<> bool inline Jupiter::Readable_String::match(const char *format, size_t formatSize) const -{ - if (this->size() == 0) - { - if (formatSize == 0) - return true; - while (format[--formatSize] == '*') - if (formatSize == 0) - return *format == '*'; - return false; - } - size_t index = 0; - size_t formatIndex = 0; - while (formatIndex != formatSize) - { - if (format[formatIndex] == '*') - { - if (++formatIndex == formatSize) - return true; - - while (format[formatIndex] == '?') - { - if (++formatIndex == formatSize) - return true; - if (++index == this->size()) - { - while (format[formatIndex++] == '*') - if (formatIndex == formatSize) - return true; - return false; - } - } - if (format[formatIndex] == '*') continue; - - while (format[formatIndex] != this->get(index)) - { - if (++index == this->size()) - { - while (format[formatIndex] == '*') - if (++formatIndex == formatSize) - return true; - return false; - } - } - } - else if (format[formatIndex] != '?' && format[formatIndex] != this->get(index)) return false; - formatIndex++; - if (++index == this->size()) - { - if (formatIndex == formatSize) - return true; - while (format[formatIndex] == '*') - if (++formatIndex == formatSize) - return true; - return false; - } - } - return false; -} - -template<> bool inline Jupiter::Readable_String::match(const wchar_t *format, size_t formatSize) const -{ - if (this->size() == 0) - { - if (formatSize == 0) - return true; - while (format[--formatSize] == L'*') - if (formatSize == 0) - return *format == L'*'; - return false; - } - size_t index = 0; - size_t formatIndex = 0; - while (formatIndex != formatSize) - { - if (format[formatIndex] == L'*') - { - if (++formatIndex == formatSize) - return true; - - while (format[formatIndex] == L'?') - { - if (++formatIndex == formatSize) - return index + 1 == this->size(); - if (++index == this->size()) - { - while (format[formatIndex++] == L'*') - if (formatIndex == formatSize) - return true; - return false; - } - } - if (format[formatIndex] == L'*') continue; - - while (format[formatIndex] != this->get(index)) - { - if (++index == this->size()) - { - while (format[formatIndex] == L'*') - if (++formatIndex == formatSize) - return true; - return false; - } - } - } - else if (format[formatIndex] != L'?' && format[formatIndex] != this->get(index)) return false; - formatIndex++; - if (++index == this->size()) - { - while (format[formatIndex] == L'*') - if (++formatIndex == formatSize) - return true; - return false; - } - } - return false; -} - -template bool Jupiter::Readable_String::match(const T *, size_t) const -{ - return false; // Wildcard matching not supported for type. -} - -template<> bool inline Jupiter::Readable_String::match(const char *format) const -{ - if (this->size() == 0) - { - while (*format == '*') - format++; - return *format == 0; - } - size_t index = 0; - while (*format != 0) - { - if (*format == '*') - { - format++; - while (*format == '?') - { - format++; - if (++index == this->size()) - { - while (*format == '*') - format++; - return *format == 0; - } - } - if (*format == 0) return true; - if (*format == '*') continue; - - while (*format != this->get(index)) - { - if (++index == this->size()) - { - while (*format == '*') - format++; - return *format == 0; - } - } - } - else if (*format != '?' && *format != this->get(index)) return false; - format++; - if (++index == this->size()) - { - while (*format == '*') - format++; - return *format == 0; - } - } - return false; -} - -template<> bool inline Jupiter::Readable_String::match(const wchar_t *format) const -{ - if (this->size() == 0) - { - while (*format == L'*') - format++; - return *format == 0; - } - size_t index = 0; - while (*format != 0) - { - if (*format == L'*') - { - format++; - while (*format == L'?') - { - format++; - if (this->size() == 0) - { - while (*format == L'*') - format++; - return *format == 0; - } - } - if (*format == 0) return true; - if (*format == '*') continue; - - while (*format != this->get(index)) - { - if (this->size() == 0) - { - while (*format == L'*') - format++; - return *format == 0; - } - } - } - else if (*format != L'?' && *format != this->get(index)) return false; - format++; - if (this->size() == 0) - { - while (*format == L'*') - format++; - return *format == 0; - } - } - return false; -} - -template bool Jupiter::Readable_String::match(const T *format) const -{ - return false; // Wildcard matching not supported for type. -} - -// matchi() - -template bool Jupiter::Readable_String::matchi(const Jupiter::Readable_String &format) const -{ - return this->matchi(format.ptr(), format.size()); -} - -template bool Jupiter::Readable_String::matchi(const std::basic_string &format) const -{ - return this->matchi(format.data(), format.size()); -} - -template<> bool inline Jupiter::Readable_String::matchi(const char *format, size_t formatSize) const -{ - if (this->size() == 0) - { - if (formatSize == 0) - return true; - while (format[--formatSize] == '*') - if (formatSize == 0) - return *format == '*'; - return false; - } - size_t index = 0; - size_t formatIndex = 0; - while (formatIndex != formatSize) - { - if (format[formatIndex] == '*') - { - if (++formatIndex == formatSize) - return true; - - while (format[formatIndex] == '?') - { - if (++formatIndex == formatSize) - return true; - if (++index == this->size()) - { - while (format[formatIndex++] == '*') - if (formatIndex == formatSize) - return true; - return false; - } - } - if (format[formatIndex] == '*') continue; - - while (toupper(format[formatIndex]) != toupper(this->get(index))) - { - if (++index == this->size()) - { - while (format[formatIndex] == '*') - if (++formatIndex == formatSize) - return true; - return false; - } - } - } - else if (format[formatIndex] != '?' && toupper(format[formatIndex]) != toupper(this->get(index))) return false; - formatIndex++; - if (++index == this->size()) - { - if (formatIndex == formatSize) - return true; - while (format[formatIndex] == '*') - if (++formatIndex == formatSize) - return true; - return false; - } - } - return false; -} - -template<> bool inline Jupiter::Readable_String::matchi(const wchar_t *format, size_t formatSize) const -{ - if (this->size() == 0) - { - if (formatSize == 0) - return true; - while (format[--formatSize] == L'*') - if (formatSize == 0) - return *format == L'*'; - return false; - } - size_t index = 0; - size_t formatIndex = 0; - while (formatIndex != formatSize) - { - if (format[formatIndex] == L'*') - { - if (++formatIndex == formatSize) - return true; - - while (format[formatIndex] == L'?') - { - if (++formatIndex == formatSize) - return index + 1 == this->size(); - if (++index == this->size()) - { - while (format[formatIndex++] == L'*') - if (formatIndex == formatSize) - return true; - return false; - } - } - if (format[formatIndex] == L'*') continue; - - while (towupper(format[formatIndex]) != towupper(this->get(index))) - { - if (++index == this->size()) - { - while (format[formatIndex] == L'*') - if (++formatIndex == formatSize) - return true; - return false; - } - } - } - else if (format[formatIndex] != L'?' && towupper(format[formatIndex]) != towupper(this->get(index))) return false; - formatIndex++; - if (++index == this->size()) - { - while (format[formatIndex] == L'*') - if (++formatIndex == formatSize) - return true; - return false; - } - } - return false; -} - -template bool Jupiter::Readable_String::matchi(const T *, size_t) const -{ - return false; // Wildcard matching not supported for type. Concept of "case" not supported for type. -} - -template<> bool inline Jupiter::Readable_String::matchi(const char *format) const -{ - if (this->size() == 0) - { - while (*format == '*') - format++; - return *format == 0; - } - size_t index = 0; - while (*format != 0) - { - if (*format == '*') - { - format++; - while (*format == '?') - { - format++; - if (++index == this->size()) - { - while (*format == '*') - format++; - return *format == 0; - } - } - if (*format == 0) return true; - if (*format == '*') continue; - - while (toupper(*format) != toupper(this->get(index))) - { - if (++index == this->size()) - { - while (*format == '*') - format++; - return *format == 0; - } - } - } - else if (*format != '?' && toupper(*format) != toupper(this->get(index))) return false; - format++; - if (++index == this->size()) - { - while (*format == '*') - format++; - return *format == 0; - } - } - return false; -} - -template<> bool inline Jupiter::Readable_String::matchi(const wchar_t *format) const -{ - if (this->size() == 0) - { - while (*format == L'*') - format++; - return *format == 0; - } - size_t index = 0; - while (*format != 0) - { - if (*format == L'*') - { - format++; - while (*format == L'?') - { - format++; - if (this->size() == 0) - { - while (*format == L'*') - format++; - return *format == 0; - } - } - if (*format == 0) return true; - if (*format == '*') continue; - - while (towupper(*format) != towupper(this->get(index))) - { - if (this->size() == 0) - { - while (*format == L'*') - format++; - return *format == 0; - } - } - } - else if (*format != L'?' && towupper(*format) != towupper(this->get(index))) return false; - format++; - if (this->size() == 0) - { - while (*format == L'*') - format++; - return *format == 0; - } - } - return false; -} - -template bool Jupiter::Readable_String::matchi(const T *format) const -{ - return false; // Wildcard matching not supported for type. Concept of "case" not supported for type. -} - -// wordCount() - -template unsigned int Jupiter::Readable_String::wordCount(const T *whitespace) const -{ - unsigned int result = 0; - size_t i = 0; - bool prev = true; - while (i != this->size()) - { - if (Jupiter::strpbrk(whitespace, this->get(i)) == nullptr) // This isn't whitespace! - { - if (prev == true) // We just left whitespace! - { - prev = false; - result++; - } - } - else prev = true; // This is whitespace! - i++; - } - return result; -} - -// as - -template<> bool inline Jupiter::Readable_String::asBool() const -{ - if (this->equalsi("FALSE")) return false; - if (this->equalsi('0')) return false; - if (this->equalsi("OFF")) return false; - if (this->equalsi('-')) return false; - return true; -} - -template<> bool inline Jupiter::Readable_String::asBool() const -{ - if (this->equalsi(L"FALSE")) return false; - if (this->equalsi(L'0')) return false; - if (this->equalsi(L"OFF")) return false; - if (this->equalsi(L'-')) return false; - return true; -} - -template bool Jupiter::Readable_String::asBool() const -{ - return false; -} - -template<> int inline Jupiter::Readable_String::asInt(int base) const -{ - return Jupiter_strtoi_s(this->ptr(), this->size(), base); -} - -template int Jupiter::Readable_String::asInt(int base) const -{ - return 0; -} - -template<> long long inline Jupiter::Readable_String::asLongLong(int base) const -{ - return Jupiter_strtoll_s(this->ptr(), this->size(), base); -} - -template long long Jupiter::Readable_String::asLongLong(int base) const -{ - return 0; -} - -template<> unsigned int inline Jupiter::Readable_String::asUnsignedInt(int base) const -{ - return Jupiter_strtoui_s(this->ptr(), this->size(), base); -} - -template unsigned int Jupiter::Readable_String::asUnsignedInt(int base) const -{ - return 0; -} - -template<> unsigned long long inline Jupiter::Readable_String::asUnsignedLongLong(int base) const -{ - return Jupiter_strtoull_s(this->ptr(), this->size(), base); -} - -template unsigned long long Jupiter::Readable_String::asUnsignedLongLong(int base) const -{ - return 0; -} - -template<> double inline Jupiter::Readable_String::asDouble() const -{ - return Jupiter_strtod_s(this->ptr(), this->size()); -} - -template double Jupiter::Readable_String::asDouble() const -{ - return 0; -} - -// Stream output - -template size_t Jupiter::Readable_String::print(FILE *out) const -{ - return fwrite(this->ptr(), sizeof(T), this->size(), out); -} - -template<> size_t inline Jupiter::Readable_String::print(FILE *out) const -{ - size_t index = 0; - while (index != this->size() && fputwc(this->get(index), out) != WEOF) index++; - return index; -} - -template size_t Jupiter::Readable_String::print(std::basic_ostream &out) const -{ - size_t index = 0; - while (index != this->size()) - { - out << this->get(index); - index++; - } - return index; -} - -template size_t Jupiter::Readable_String::println(FILE *out) const -{ - size_t r = this->print(out); - if (r != this->size()) return r; - if (fputs("\r\n", out) != EOF) r += 2; - return r; -} - -template size_t Jupiter::Readable_String::println(std::basic_ostream &out) const -{ - size_t r = this->print(out); - if (r != this->size()) return r; - out << std::endl; - return r; -} - -// getWord - -template template class R> R Jupiter::Readable_String::getWord(const Jupiter::Readable_String &in, size_t pos, const T *whitespace) -{ - unsigned int x = 0; - unsigned int y = 1; - for (unsigned int i = 0; i < pos || y == 1; x++) - { - if (x == in.size()) return R(); - if (Jupiter::strpbrk(whitespace, in.get(x)) != nullptr) - { - if (y != 1) - { - y = 1; - i++; - } - } - else - { - if (i >= pos) break; - y = 0; - } - } - for (y = x; y != in.size() && Jupiter::strpbrk(whitespace, in.get(y)) == nullptr; y++); - return R::substring(in, x, y - x); -} - -template template class R> R Jupiter::Readable_String::getWord(const T *in, size_t pos, const T *whitespace) -{ - if (*in == 0) - return R(); - - while (Jupiter::strpbrk(whitespace, *in) != nullptr) - if (*++in == 0) - return R(); - - if (pos == 0) - { - do - ++in, ++pos; - while (*in != 0 && Jupiter::strpbrk(whitespace, *in) == nullptr); - - in -= pos; - return R::substring(in, 0, pos); - } - - loop_start: - { - if (Jupiter::strpbrk(whitespace, *in) != nullptr) - { - do - if (*++in == 0) - return R(); - while (Jupiter::strpbrk(whitespace, *in) != nullptr); - - if (--pos == 0) - goto loop_end; - } - - if (*++in == 0) - return R(); - goto loop_start; - } - loop_end: - - do - ++in, ++pos; - while (*in != 0 && Jupiter::strpbrk(whitespace, *in) == nullptr); - - in -= pos; - return R::substring(in, 0, pos); -} - -// gotoWord - -template template class R> R Jupiter::Readable_String::gotoWord(const Jupiter::Readable_String &in, size_t pos, const T *whitespace) -{ - unsigned int x = 0; - bool y = true; - for (unsigned int i = 0; i < pos || y == true; x++) - { - if (x == in.size()) return R(); - if (Jupiter::strpbrk(whitespace, in.get(x)) != nullptr) - { - if (y != true) - { - y = true; - i++; - } - } - else - { - if (i >= pos) break; - y = false; - } - } - return R::substring(in, x); -} - -template template class R> R Jupiter::Readable_String::gotoWord(const T *in, size_t pos, const T *whitespace) -{ - unsigned int x = 0; - bool y = true; - for (unsigned int i = 0; i < pos || y == true; x++) - { - if (in[x] == 0) return R(); - if (Jupiter::strpbrk(whitespace, in[x]) != nullptr) - { - if (y != true) - { - y = true; - i++; - } - } - else - { - if (i >= pos) break; - y = false; - } - } - return R::substring(in, x); -} - // Jupiter::DataBuffer specialization template<> struct _Jupiter_DataBuffer_partial_specialization_impl diff --git a/src/include/Jupiter/Reference_String.h b/src/include/Jupiter/Reference_String.h index 513654f..1509d28 100644 --- a/src/include/Jupiter/Reference_String.h +++ b/src/include/Jupiter/Reference_String.h @@ -41,14 +41,6 @@ namespace Jupiter /** DEPRECATED IN FAVOR OF std::string_view */ public: - /** - * @brief Fetches an element from the string. - * - * @param index Index of the element to return. - * @return The element located at the specified index. - */ - const T &get(size_t index) const override; - /** * @brief Returns the number of elements in the String. * @@ -141,64 +133,6 @@ namespace Jupiter static Reference_String substring(const Jupiter::Readable_String &in, size_t pos, size_t length); static Reference_String substring(const T *in, size_t pos, size_t length); - /** - * @brief Creates a partial copy of the string, based on a set of tokens. - * - * @param pos Position of word in the string to start copying from. - * @param whitespace A string of tokens used to deliminate words. - * @return String containing a partial copy of the original string. - */ - Reference_String getWord(size_t pos, const T *whitespace) const; - - /** - * @brief Creates a partial copy of an input string, based on a set of tokens. - * - * @param in String to get a partial copy of. - * @param pos Position of word in the string to start copying from. - * @param whitespace A string of tokens used to deliminate words. - * @return String containing a partial copy of the original string. - */ - static Reference_String getWord(const Jupiter::Readable_String &in, size_t pos, const T *whitespace); - - /** - * @brief Creates a partial copy of an input string, based on a set of tokens. - * - * @param in C-Style string to get a partial copy of. - * @param pos Position of word in the string to start copying from. - * @param whitespace A string of tokens used to deliminate words. - * @return String containing a partial copy of the original string. - */ - static Reference_String getWord(const T *in, size_t pos, const T *whitespace); - - /** - * @brief Creates a partial copy of the string, based on a set of tokens. - * - * @param pos Position in the string to start copying from. - * @param whitespace A string of tokens used to deliminate words. - * @return String containing a partial copy of the original string. - */ - Reference_String gotoWord(size_t pos, const T *whitespace) const; - - /** - * @brief Creates a partial copy of the string, based on a set of tokens. - * - * @param in String to get a partial copy of. - * @param pos Position in the string to start copying from. - * @param whitespace A string of tokens used to deliminate words. - * @return String containing a partial copy of the original string. - */ - static Reference_String gotoWord(const Jupiter::Readable_String &in, size_t pos, const T *whitespace); - - /** - * @brief Creates a partial copy of the string, based on a set of tokens. - * - * @param in C-Style string to get a partial copy of. - * @param pos Position in the string to start copying from. - * @param whitespace A string of tokens used to deliminate words. - * @return String containing a partial copy of the original string. - */ - static Reference_String gotoWord(const T *in, size_t pos, const T *whitespace); - /** Mutative operators */ inline Reference_String& operator-=(size_t right) { this->truncate(right); return *this; }; inline Reference_String& operator=(const Readable_String &right) { std::basic_string_view::operator=({right.ptr(), right.size()}); return *this; }; @@ -239,11 +173,10 @@ namespace Jupiter // Bring in constructors from basic_string_view using std::basic_string_view::basic_string_view; - static const Jupiter::Reference_String empty; /** Empty instantiation of Reference_String */ - /** 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 */ diff --git a/src/include/Jupiter/Reference_String_Imp.h b/src/include/Jupiter/Reference_String_Imp.h index 4220b70..a42862a 100644 --- a/src/include/Jupiter/Reference_String_Imp.h +++ b/src/include/Jupiter/Reference_String_Imp.h @@ -51,10 +51,6 @@ template Jupiter::Reference_String::Reference_String(const Jupite : Reference_String(in.ptr(), in.size()) { } -template const T &Jupiter::Reference_String::get(size_t index) const { - return operator[](index); -} - template size_t Jupiter::Reference_String::size() const { return std::basic_string_view::size(); } @@ -142,32 +138,6 @@ template Jupiter::Reference_String Jupiter::Reference_String:: return Jupiter::Reference_String(in + pos, len); } -template Jupiter::Reference_String Jupiter::Reference_String::getWord(size_t pos, const T *whitespace) const { - return Jupiter::Reference_String::getWord(*this, pos, whitespace); -} - -template Jupiter::Reference_String Jupiter::Reference_String::getWord(const Jupiter::Readable_String &in, size_t pos, const T *whitespace) { - return Jupiter::Readable_String::template getWord(in, pos, whitespace); -} - -template Jupiter::Reference_String Jupiter::Reference_String::getWord(const T *in, size_t pos, const T *whitespace) { - return Jupiter::Readable_String::template getWord(in, pos, whitespace); -} - -template Jupiter::Reference_String Jupiter::Reference_String::gotoWord(size_t pos, const T *whitespace) const { - return Jupiter::Reference_String::gotoWord(*this, pos, whitespace); -} - -template Jupiter::Reference_String Jupiter::Reference_String::gotoWord(const Jupiter::Readable_String &in, size_t pos, const T *whitespace) { - return Jupiter::Readable_String::template gotoWord(in, pos, whitespace); -} - -template Jupiter::Reference_String Jupiter::Reference_String::gotoWord(const T *in, size_t pos, const T *whitespace) { - return Jupiter::Readable_String::template gotoWord(in, pos, whitespace); -} - -template const Jupiter::Reference_String Jupiter::Reference_String::empty = Jupiter::Reference_String(); - // Jupiter::DataBuffer specialization template<> struct _Jupiter_DataBuffer_partial_specialization_impl { diff --git a/src/include/Jupiter/Socket.h b/src/include/Jupiter/Socket.h index 756dc1d..7e412fa 100644 --- a/src/include/Jupiter/Socket.h +++ b/src/include/Jupiter/Socket.h @@ -438,6 +438,7 @@ namespace Jupiter * 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); /** * @brief Sends a null-terminated string of data across the socket. diff --git a/src/include/Jupiter/String.hpp b/src/include/Jupiter/String.hpp index f46909b..8485acb 100644 --- a/src/include/Jupiter/String.hpp +++ b/src/include/Jupiter/String.hpp @@ -115,54 +115,6 @@ namespace Jupiter static typename Jupiter::template String_Strict substring(const Jupiter::Readable_String &in, size_t pos, size_t length); static typename Jupiter::template String_Strict substring(const T *in, size_t pos, size_t length); - /** - * @brief Creates a partial copy of the string, based on a set of tokens. - * - * @param pos Position of word in the string to start copying from. - * @param whitespace A string of tokens used to deliminate words. - * @return String containing a partial copy of the original string. - */ - String_Strict getWord(size_t pos, const T *whitespace) const; - - /** - * @brief Creates a partial copy of an input string, based on a set of tokens. - * - * @param in String to get a partial copy of. - * @param pos Position of word in the string to start copying from. - * @param whitespace A string of tokens used to deliminate words. - * @return String containing a partial copy of the original string. - */ - static String_Strict getWord(const Jupiter::Readable_String &in, size_t pos, const T *whitespace); - - /** - * @brief Creates a partial copy of an input string, based on a set of tokens. - * - * @param in C-Style string to get a partial copy of. - * @param pos Position of word in the string to start copying from. - * @param whitespace A string of tokens used to deliminate words. - * @return String containing a partial copy of the original string. - */ - static String_Strict getWord(const T *in, size_t pos, const T *whitespace); - - /** - * @brief Creates a partial copy of the string, based on a set of tokens. - * - * @param pos Position in the string to start copying from. - * @param whitespace A string of tokens used to deliminate words. - * @return String containing a partial copy of the original string. - */ - String_Strict gotoWord(size_t pos, const T *whitespace) const; - - /** - * @brief Creates a partial copy of the string, based on a set of tokens. - * - * @param in String to get a partial copy of. - * @param pos Position in the string to start copying from. - * @param whitespace A string of tokens used to deliminate words. - * @return String containing a partial copy of the original string. - */ - static String_Strict gotoWord(const Jupiter::Readable_String &in, size_t pos, const T *whitespace); - /** Default Constructor */ String_Strict(); @@ -181,7 +133,7 @@ namespace Jupiter String_Strict(const String_Strict &in); String_Strict(const Readable_String &in); String_Strict(const std::basic_string &in); - //String_Strict(const std::basic_string_view &in) : String_Strict(in.data(), in.size()){}; + 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); @@ -206,18 +158,14 @@ namespace Jupiter inline String_Strict &operator=(const std::basic_string &right) { this->set(right); return *this; }; inline String_Strict &operator=(const T *right) { this->set(right); return *this; }; inline String_Strict &operator=(const T right) { this->set(right); return *this; }; - - static const Jupiter::String_Strict empty; /** Empty instantiation of String_Strict */ }; -#if defined JUPITER_STRING_STRICT_OPERATOR_PLUS /** 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); -#endif // JUPITER_STRING_STRICT_OPERATOR_PLUS /** * @brief Provides a "loose" String implementation that's more optimized for repeated concatenations. @@ -306,56 +254,6 @@ namespace Jupiter static typename Jupiter::template String_Loose substring(const Jupiter::Readable_String &in, size_t pos, size_t length); static typename Jupiter::template String_Loose substring(const T *in, size_t pos, size_t length); - /** - * @brief Creates a partial copy of the string, based on a set of tokens. - * - * @param pos Position in the string to start copying from. - * @param whitespace A string of tokens used to deliminate words. - * @return String containing a partial copy of the original string. - */ - String_Loose getWord(size_t pos, const T *whitespace) const; - - /** - * @brief Creates a partial copy of an input string, based on a set of tokens. - * Useful when the input string's type isn't known. - * - * @param in String to get a partial copy of. - * @param pos Position of word in the string to start copying from. - * @param whitespace A string of tokens used to deliminate words. - * @return String containing a partial copy of the original string. - */ - static String_Loose getWord(const Jupiter::Readable_String &in, size_t pos, const T *whitespace); - - /** - * @brief Creates a partial copy of an input string, based on a set of tokens. - * Useful when the input string's type isn't known. - * - * @param in C-Style string to get a partial copy of. - * @param pos Position of word in the string to start copying from. - * @param whitespace A string of tokens used to deliminate words. - * @return String containing a partial copy of the original string. - */ - static String_Loose getWord(const T *in, size_t pos, const T *whitespace); - - /** - * @brief Creates a partial copy of the string, based on a set of tokens. - * - * @param pos Position in the string to start copying from. - * @param whitespace A string of tokens used to deliminate words. - * @return String containing a partial copy of the original string. - */ - String_Loose gotoWord(size_t pos, const T *whitespace) const; - - /** - * @brief Creates a partial copy of the string, based on a set of tokens. - * - * @param in String to get a partial copy of. - * @param pos Position in the string to start copying from. - * @param whitespace A string of tokens used to deliminate words. - * @return String containing a partial copy of the original string. - */ - static String_Loose gotoWord(const Jupiter::Readable_String &in, size_t pos, const T *whitespace); - /** * @brief Sets the internal buffer to be at least large enough to old a specified number of elements. * Note: This does nothing if len is less than the string's current length. @@ -416,23 +314,12 @@ namespace Jupiter inline String_Loose &operator=(const T *right) { this->set(right); return *this; }; inline String_Loose &operator=(const T right) { this->set(right); return *this; }; - static const Jupiter::String_Loose empty; /** Empty instantiation of String_Loose */ static const size_t start_size = 8; /** Starting size for loose Strings. */ protected: size_t strSize; /** Size of underlying string buffer */ }; -#if !defined JUPITER_STRING_STRICT_OPERATOR_PLUS -#if !defined DISABLE_DEFAULT_JUPITER_STRING_OPERATOR_PLUS - /** String_Loose Addition Operator */ - template static inline Jupiter::String_Loose operator+(const Jupiter::Readable_String &lhs, const T &rhs); - template static inline Jupiter::String_Loose operator+(const Jupiter::Readable_String &lhs, const Jupiter::Readable_String &rhs); - template static inline Jupiter::String_Loose operator+(const Jupiter::Readable_String &lhs, const std::basic_string &rhs); - template static inline Jupiter::String_Loose operator+(const Jupiter::Readable_String &lhs, const T *rhs); -#endif // DISABLE_DEFAULT_JUPITER_STRING_OPERATOR_PLUS -#endif // JUPITER_STRING_STRICT_OPERATOR_PLUS - /** Definition of a Strict String. */ typedef String_Strict StringS; diff --git a/src/include/Jupiter/String_Imp.h b/src/include/Jupiter/String_Imp.h index 3422751..9cf2166 100644 --- a/src/include/Jupiter/String_Imp.h +++ b/src/include/Jupiter/String_Imp.h @@ -283,31 +283,6 @@ template typename Jupiter::String_Strict Jupiter::String_Strict::template substring(in, pos, len); } -template Jupiter::String_Strict Jupiter::String_Strict::getWord(size_t pos, const T *whitespace) const -{ - return Jupiter::String_Strict::getWord(*this, pos, whitespace); -} - -template Jupiter::String_Strict Jupiter::String_Strict::getWord(const Jupiter::Readable_String &in, size_t pos, const T *whitespace) -{ - return Jupiter::Readable_String::template getWord(in, pos, whitespace); -} - -template Jupiter::String_Strict Jupiter::String_Strict::getWord(const T *in, size_t pos, const T *whitespace) -{ - return Jupiter::Readable_String::template getWord(in, pos, whitespace); -} - -template Jupiter::String_Strict Jupiter::String_Strict::gotoWord(size_t pos, const T *whitespace) const -{ - return Jupiter::String_Strict::gotoWord(*this, pos, whitespace); -} - -template Jupiter::String_Strict Jupiter::String_Strict::gotoWord(const Jupiter::Readable_String &in, size_t pos, const T *whitespace) -{ - return Jupiter::Readable_String::template gotoWord(in, pos, whitespace); -} - // Operators template inline Jupiter::String_Strict Jupiter::String_Strict::operator+(const T &rhs) const @@ -335,7 +310,6 @@ template inline Jupiter::String_Strict Jupiter::String_Strict: return Jupiter::operator+(*this, rhs); } -#if defined JUPITER_STRING_STRICT_OPERATOR_PLUS template static inline Jupiter::String_Strict Jupiter::operator+(const Jupiter::Readable_String &lhs, const T &rhs) { return Jupiter::String_Strict(lhs, rhs); @@ -355,9 +329,6 @@ template static inline Jupiter::String_Strict Jupiter::operator+( { return Jupiter::String_Strict(lhs, rhs); } -#endif // JUPITER_STRING_STRICT_OPERATOR_PLUS - -template const Jupiter::String_Strict Jupiter::String_Strict::empty = Jupiter::String_Strict(); // Jupiter::DataBuffer specialization @@ -697,31 +668,6 @@ template typename Jupiter::template String_Loose Jupiter::String_ return Jupiter::String_Type::template substring(in, pos, len); } -template Jupiter::String_Loose Jupiter::String_Loose::getWord(size_t pos, const T *whitespace) const -{ - return Jupiter::String_Loose::getWord(*this, pos, whitespace); -} - -template Jupiter::String_Loose Jupiter::String_Loose::getWord(const Jupiter::Readable_String &in, size_t pos, const T *whitespace) -{ - return Jupiter::Readable_String::template getWord(in, pos, whitespace); -} - -template Jupiter::String_Loose Jupiter::String_Loose::getWord(const T *in, size_t pos, const T *whitespace) -{ - return Jupiter::Readable_String::template getWord(in, pos, whitespace); -} - -template Jupiter::String_Loose Jupiter::String_Loose::gotoWord(size_t pos, const T *whitespace) const -{ - return Jupiter::String_Loose::gotoWord(*this, pos, whitespace); -} - -template Jupiter::String_Loose Jupiter::String_Loose::gotoWord(const Jupiter::Readable_String &in, size_t pos, const T *whitespace) -{ - return Jupiter::Readable_String::template gotoWord(in, pos, whitespace); -} - // Operators template inline Jupiter::String_Loose Jupiter::String_Loose::operator+(const T &rhs) const @@ -749,32 +695,6 @@ template inline Jupiter::String_Loose Jupiter::String_Loose::o return Jupiter::operator+(*this, rhs); } -#if !defined JUPITER_STRING_STRICT_OPERATOR_PLUS -#if !defined DISABLE_DEFAULT_JUPITER_STRING_OPERATOR_PLUS -template static inline Jupiter::String_Loose Jupiter::operator+(const Jupiter::Readable_String &lhs, const T &rhs) -{ - return Jupiter::String_Loose(lhs, rhs); -} - -template static inline Jupiter::String_Loose Jupiter::operator+(const Jupiter::Readable_String &lhs, const Jupiter::Readable_String &rhs) -{ - return Jupiter::String_Loose(lhs, rhs); -} - -template static inline Jupiter::String_Loose Jupiter::operator+(const Jupiter::Readable_String &lhs, const std::basic_string &rhs) -{ - return Jupiter::String_Loose(lhs, rhs); -} - -template static inline Jupiter::String_Loose Jupiter::operator+(const Jupiter::Readable_String &lhs, const T *rhs) -{ - return Jupiter::String_Loose(lhs, rhs); -} -#endif // DISABLE_DEFAULT_JUPITER_STRING_OPERATOR_PLUS -#endif // JUPITER_STRING_STRICT_OPERATOR_PLUS - -template const Jupiter::String_Loose Jupiter::String_Loose::empty = Jupiter::String_Loose(); - // Jupiter::DataBuffer specialization template<> struct _Jupiter_DataBuffer_partial_specialization_impl diff --git a/src/include/Jupiter/String_Type_Imp.h b/src/include/Jupiter/String_Type_Imp.h index 2895848..5f53119 100644 --- a/src/include/Jupiter/String_Type_Imp.h +++ b/src/include/Jupiter/String_Type_Imp.h @@ -442,11 +442,11 @@ template size_t Jupiter::String_Type::insert(size_t index, const if (index >= Jupiter::String_Type::length) return this->concat(value); - if (value.isEmpty()) + if (value.empty()) return Jupiter::String_Type::length; if (value.size() == 1) - return this->insert(index, value.get(0)); + return this->insert(index, value[0]); this->setBufferSize(Jupiter::String_Type::length + value.size()); size_t i; @@ -455,10 +455,10 @@ template size_t Jupiter::String_Type::insert(size_t index, const while (i != index) { - Jupiter::String_Type::str[i] = value.get(i - index); + Jupiter::String_Type::str[i] = value[i - index]; i--; } - Jupiter::String_Type::str[index] = value.get(0); + Jupiter::String_Type::str[index] = value[0]; return Jupiter::String_Type::length += value.size(); } @@ -774,7 +774,7 @@ template template class R> R Jupiter::String_T { if (pos >= in.size()) return R(); R r = R(in.size() - pos); - for (r.length = 0; pos + r.length != in.size(); r.length++) r.str[r.length] = in.get(pos + r.length); + for (r.length = 0; pos + r.length != in.size(); r.length++) r.str[r.length] = in[pos + r.length]; return r; } @@ -782,7 +782,7 @@ template template class R> R Jupiter::String_T { if (pos + len >= in.size()) return R::substring(in, pos); R r = R(len); - for (r.length = 0; r.length != len; r.length++) r.str[r.length] = in.get(pos + r.length); + for (r.length = 0; r.length != len; r.length++) r.str[r.length] = in[pos + r.length]; return r; }