From a534ab83ec18f3edf2a2236675b3749deb2c1a00 Mon Sep 17 00:00:00 2001 From: Uleat Date: Sat, 31 Aug 2019 20:55:46 -0400 Subject: [PATCH] Converted new implode and join_pair functions to template functions --- common/shareddb.cpp | 13 ++++++++-- common/string_util.cpp | 47 ----------------------------------- common/string_util.h | 56 +++++++++++++++++++++++++++++++++++++++--- zone/bot_database.cpp | 13 ++++++++-- 4 files changed, 74 insertions(+), 55 deletions(-) diff --git a/common/shareddb.cpp b/common/shareddb.cpp index c07869ec8..e2d482cd7 100644 --- a/common/shareddb.cpp +++ b/common/shareddb.cpp @@ -1478,7 +1478,16 @@ bool SharedDatabase::UpdateCommandSettings(const std::vector('(', ')'), + join_pair( + ",", + std::pair('\'', '\''), + std::pair('\'', '\''), + injected + ) + ) ); if (!QueryDatabase(query).Success()) { @@ -1490,7 +1499,7 @@ bool SharedDatabase::UpdateCommandSettings(const std::vector('\'', '\''), orphaned) ); if (!QueryDatabase(query).Success()) { diff --git a/common/string_util.cpp b/common/string_util.cpp index a3c775777..fc171cdc2 100644 --- a/common/string_util.cpp +++ b/common/string_util.cpp @@ -16,7 +16,6 @@ #include "string_util.h" #include -#include #ifdef _WINDOWS #include @@ -145,52 +144,6 @@ std::string implode(std::string glue, std::vector src) return final_output; } -std::string implode(std::string glue, string_string encapsulation, std::vector src) -{ - if (src.empty()) { - return {}; - } - - std::ostringstream output; - std::vector::iterator src_iter; - - for (src_iter = src.begin(); src_iter != src.end(); src_iter++) { - output << encapsulation.first << *src_iter << encapsulation.second << glue; - } - - std::string final_output = output.str(); - final_output.resize(output.str().size() - glue.size()); - - return final_output; -} - -std::vector join_pair(string_string outer_encap, std::string joiner, string_string inner_encap, std::vector> src) -{ - if (src.empty()) { - return {}; - } - - std::vector output; - - for (auto src_iter : src) { - output.push_back( - fmt::format( - "{}{}{}{}{}{}{}{}{}", - outer_encap.first, - inner_encap.first, - src_iter.first, - inner_encap.second, - joiner, - inner_encap.first, - src_iter.second, - inner_encap.second, - outer_encap.second - ) - ); - } - - return output; -} std::string EscapeString(const std::string &s) { std::string ret; diff --git a/common/string_util.h b/common/string_util.h index 6b0ac8bbc..b3301ab90 100644 --- a/common/string_util.h +++ b/common/string_util.h @@ -31,10 +31,58 @@ std::vector split(std::string str_to_split, char delimiter); const std::string StringFormat(const char* format, ...); const std::string vStringFormat(const char* format, va_list args); std::string implode(std::string glue, std::vector src); -typedef std::pair string_string; -std::string implode(std::string glue, string_string encapsulation, std::vector src); -// wanted to make 'join_pair' a template function..this will do for now -std::vector join_pair(string_string outer_encap, std::string joiner, string_string inner_encap, std::vector> src); + +template +std::string implode(std::string glue, std::pair encapsulation, std::vector src) +{ + if (src.empty()) { + return {}; + } + + std::ostringstream output; + + for (const T &src_iter : src) { + output << encapsulation.first << src_iter << encapsulation.second << glue; + } + + std::string final_output = output.str(); + final_output.resize(output.str().size() - glue.size()); + + return final_output; +} + +// this requires that #include be included in whatever file the invocation is made from +template +std::vector join_pair(std::string glue, std::pair first_encap, std::pair second_encap, std::vector> src) +{ + if (src.empty()) { + return {}; + } + + std::vector output; + + for (const std::pair &src_iter : src) { + output.push_back( + // There are issues with including in a header file that result in compile + // failure. I'm not sure if this applies only within the same project or across projects. + // Since templates act similar to macros in regards to initialization, this call should be + // safe so long as the '#include' rule above is observed. + fmt::format( + "{}{}{}{}{}{}{}", + first_encap.first, + src_iter.first, + first_encap.second, + glue, + second_encap.first, + src_iter.second, + second_encap.second + ) + ); + } + + return output; +} + std::vector SplitString(const std::string &s, char delim); std::string EscapeString(const char *src, size_t sz); std::string EscapeString(const std::string &s); diff --git a/zone/bot_database.cpp b/zone/bot_database.cpp index b87ff2c00..e5511776a 100644 --- a/zone/bot_database.cpp +++ b/zone/bot_database.cpp @@ -62,7 +62,16 @@ bool BotDatabase::UpdateBotCommandSettings(const std::vector('(', ')'), + join_pair( + ",", + std::pair('\'', '\''), + std::pair('\'', '\''), + injected + ) + ) ); if (!database.QueryDatabase(query).Success()) { @@ -74,7 +83,7 @@ bool BotDatabase::UpdateBotCommandSettings(const std::vector('\'', '\''), orphaned) ); if (!database.QueryDatabase(query).Success()) {