mirror of
https://github.com/EQEmu/Server.git
synced 2025-12-14 15:41:30 +00:00
Converted new implode and join_pair functions to template functions
This commit is contained in:
parent
3092a8ba3b
commit
a534ab83ec
@ -1478,7 +1478,16 @@ bool SharedDatabase::UpdateCommandSettings(const std::vector<std::pair<std::stri
|
||||
|
||||
std::string query = fmt::format(
|
||||
"REPLACE INTO `command_settings`(`command`, `access`) VALUES {}",
|
||||
implode(",", string_string("(", ")"), join_pair(string_string(), ",", string_string("'", "'"), injected))
|
||||
implode(
|
||||
",",
|
||||
std::pair<char, char>('(', ')'),
|
||||
join_pair(
|
||||
",",
|
||||
std::pair<char, char>('\'', '\''),
|
||||
std::pair<char, char>('\'', '\''),
|
||||
injected
|
||||
)
|
||||
)
|
||||
);
|
||||
|
||||
if (!QueryDatabase(query).Success()) {
|
||||
@ -1490,7 +1499,7 @@ bool SharedDatabase::UpdateCommandSettings(const std::vector<std::pair<std::stri
|
||||
|
||||
std::string query = fmt::format(
|
||||
"DELETE FROM `command_settings` WHERE `command` IN ({})",
|
||||
implode(",", string_string("'", "'"), orphaned)
|
||||
implode(",", std::pair<char, char>('\'', '\''), orphaned)
|
||||
);
|
||||
|
||||
if (!QueryDatabase(query).Success()) {
|
||||
|
||||
@ -16,7 +16,6 @@
|
||||
|
||||
#include "string_util.h"
|
||||
#include <algorithm>
|
||||
#include <fmt/format.h>
|
||||
|
||||
#ifdef _WINDOWS
|
||||
#include <windows.h>
|
||||
@ -145,52 +144,6 @@ std::string implode(std::string glue, std::vector<std::string> src)
|
||||
return final_output;
|
||||
}
|
||||
|
||||
std::string implode(std::string glue, string_string encapsulation, std::vector<std::string> src)
|
||||
{
|
||||
if (src.empty()) {
|
||||
return {};
|
||||
}
|
||||
|
||||
std::ostringstream output;
|
||||
std::vector<std::string>::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<std::string> join_pair(string_string outer_encap, std::string joiner, string_string inner_encap, std::vector<std::pair<std::string, uint8>> src)
|
||||
{
|
||||
if (src.empty()) {
|
||||
return {};
|
||||
}
|
||||
|
||||
std::vector<std::string> 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;
|
||||
|
||||
@ -31,10 +31,58 @@ std::vector<std::string> 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<std::string> src);
|
||||
typedef std::pair<std::string, std::string> string_string;
|
||||
std::string implode(std::string glue, string_string encapsulation, std::vector<std::string> src);
|
||||
// wanted to make 'join_pair' a template function..this will do for now
|
||||
std::vector<std::string> join_pair(string_string outer_encap, std::string joiner, string_string inner_encap, std::vector<std::pair<std::string, uint8>> src);
|
||||
|
||||
template <typename T>
|
||||
std::string implode(std::string glue, std::pair<char, char> encapsulation, std::vector<T> 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<fmt/format.h> be included in whatever file the invocation is made from
|
||||
template <typename T1, typename T2>
|
||||
std::vector<std::string> join_pair(std::string glue, std::pair<char, char> first_encap, std::pair<char, char> second_encap, std::vector<std::pair<T1, T2>> src)
|
||||
{
|
||||
if (src.empty()) {
|
||||
return {};
|
||||
}
|
||||
|
||||
std::vector<std::string> output;
|
||||
|
||||
for (const std::pair<T1, T2> &src_iter : src) {
|
||||
output.push_back(
|
||||
// There are issues with including <fmt/format.h> 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<fmt/format.h>' 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<std::string> SplitString(const std::string &s, char delim);
|
||||
std::string EscapeString(const char *src, size_t sz);
|
||||
std::string EscapeString(const std::string &s);
|
||||
|
||||
@ -62,7 +62,16 @@ bool BotDatabase::UpdateBotCommandSettings(const std::vector<std::pair<std::stri
|
||||
|
||||
query = fmt::format(
|
||||
"REPLACE INTO `bot_command_settings`(`bot_command`, `access`) VALUES {}",
|
||||
implode(",", string_string("(", ")"), join_pair(string_string(), ",", string_string("'", "'"), injected))
|
||||
implode(
|
||||
",",
|
||||
std::pair<char, char>('(', ')'),
|
||||
join_pair(
|
||||
",",
|
||||
std::pair<char, char>('\'', '\''),
|
||||
std::pair<char, char>('\'', '\''),
|
||||
injected
|
||||
)
|
||||
)
|
||||
);
|
||||
|
||||
if (!database.QueryDatabase(query).Success()) {
|
||||
@ -74,7 +83,7 @@ bool BotDatabase::UpdateBotCommandSettings(const std::vector<std::pair<std::stri
|
||||
|
||||
query = fmt::format(
|
||||
"DELETE FROM `bot_command_settings` WHERE `bot_command` IN ({})",
|
||||
implode(",", string_string("'", "'"), orphaned)
|
||||
implode(",", std::pair<char, char>('\'', '\''), orphaned)
|
||||
);
|
||||
|
||||
if (!database.QueryDatabase(query).Success()) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user