Skip to content

Commit af4ae1a

Browse files
committed
Move restoreCmdFromArgv to StringUtils
I'll make a structure for chars later, as well as bigger changes that I planned to do but didn't want to confuse them together
1 parent e0ae06d commit af4ae1a

File tree

4 files changed

+34
-19
lines changed

4 files changed

+34
-19
lines changed

src/network/protocols/command_manager.cpp

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -124,13 +124,13 @@ namespace
124124
} // forAllQueuesInMask
125125
//-------------------------------------------------------------------------
126126

127-
void restoreCmdByArgv(std::string& cmd,
127+
void restoreCmdFromArgv(std::string& cmd,
128128
std::vector<std::string>& argv, char c, char d, char e, char f,
129129
int from = 0)
130130
{
131131
cmd = StringUtils::quoteEscapeArray(argv.begin() + from, argv.end(),
132132
c, d, e, f);
133-
} // restoreCmdByArgv
133+
} // restoreCmdFromArgv
134134
//-------------------------------------------------------------------------
135135

136136

@@ -531,7 +531,7 @@ void CommandManager::handleCommand(Event* event, std::shared_ptr<STKPeer> peer)
531531
argv = StringUtils::splitQuoted(cmd, ' ', '"', '"', '\\');
532532
if (argv.empty())
533533
return;
534-
restoreCmdByArgv(cmd, argv, ' ', '"', '"', '\\');
534+
StringUtils::restoreCmdFromArgv(cmd, argv, ' ', '"', '"', '\\');
535535

536536
permissions = getLobby()->getPermissions(peer);
537537
voting = false;
@@ -554,7 +554,7 @@ void CommandManager::handleCommand(Event* event, std::shared_ptr<STKPeer> peer)
554554
argv = StringUtils::splitQuoted(cmd, ' ', '"', '"', '\\');
555555
if (argv.empty())
556556
return;
557-
restoreCmdByArgv(cmd, argv, ' ', '"', '"', '\\');
557+
StringUtils::restoreCmdFromArgv(cmd, argv, ' ', '"', '"', '\\');
558558
voting = m_user_saved_voting[username];
559559
target_peer = m_user_saved_acting_peer[username];
560560
target_peer_strong = target_peer.lock();
@@ -728,7 +728,7 @@ void CommandManager::handleCommand(Event* event, std::shared_ptr<STKPeer> peer)
728728
{
729729
std::string new_cmd = p.first + " " + p.second;
730730
auto new_argv = StringUtils::splitQuoted(new_cmd, ' ', '"', '"', '\\');
731-
restoreCmdByArgv(new_cmd, new_argv, ' ', '"', '"', '\\');
731+
StringUtils::restoreCmdFromArgv(new_cmd, new_argv, ' ', '"', '"', '\\');
732732
std::string msg2 = StringUtils::insertValues(
733733
"Command \"/%s\" has been successfully voted",
734734
new_cmd.c_str());
@@ -789,7 +789,7 @@ void CommandManager::update()
789789
{
790790
std::string new_cmd = p.first + " " + p.second;
791791
auto new_argv = StringUtils::splitQuoted(new_cmd, ' ', '"', '"', '\\');
792-
restoreCmdByArgv(new_cmd, new_argv, ' ', '"', '"', '\\');
792+
StringUtils::restoreCmdFromArgv(new_cmd, new_argv, ' ', '"', '"', '\\');
793793
std::string msg = StringUtils::insertValues(
794794
"Command \"/%s\" has been successfully voted",
795795
new_cmd.c_str());
@@ -2216,7 +2216,7 @@ void CommandManager::process_queue_push(Context& context)
22162216
argv[2] = asset_manager->getRandomAddonMap();
22172217

22182218
std::string filter_text = "";
2219-
restoreCmdByArgv(filter_text, argv, ' ', '"', '"', '\\', 2);
2219+
restoreCmdFromArgv(filter_text, argv, ' ', '"', '"', '\\', 2);
22202220

22212221
// Fix typos only if track queues are used (majority of cases anyway)
22222222
// TODO: I don't know how to fix typos for both karts and tracks
@@ -2837,7 +2837,7 @@ void CommandManager::process_scoring_assign(Context& context)
28372837
auto& argv = context.m_argv;
28382838

28392839
std::string cmd2;
2840-
restoreCmdByArgv(cmd2, argv, ' ', '"', '"', '\\', 1);
2840+
StringUtils::restoreCmdFromArgv(cmd2, argv, ' ', '"', '"', '\\', 1);
28412841
if (getGPManager()->trySettingGPScoring(cmd2))
28422842
Comm::sendStringToAllPeers("Scoring set to \"" + cmd2 + "\"");
28432843
else
@@ -3734,13 +3734,16 @@ bool CommandManager::hasTypo(std::shared_ptr<STKPeer> acting_peer, std::shared_p
37343734
{
37353735
if (!acting_peer.get()) // voted
37363736
return false;
3737+
37373738
std::string username = "";
37383739
if (peer->hasPlayerProfiles())
37393740
username = peer->getMainName();
3741+
37403742
auto it = m_user_last_correct_argument.find(username);
37413743
if (it != m_user_last_correct_argument.end() &&
37423744
std::make_pair(idx, subidx) <= it->second)
37433745
return false;
3746+
37443747
std::string text = argv[idx];
37453748
std::string prefix = "";
37463749
std::string suffix = "";
@@ -3750,6 +3753,7 @@ bool CommandManager::hasTypo(std::shared_ptr<STKPeer> acting_peer, std::shared_p
37503753
suffix = text.substr(substr_r);
37513754
text = text.substr(substr_l, substr_r - substr_l);
37523755
}
3756+
37533757
auto closest_commands = stf.getClosest(text, top, case_sensitive);
37543758
if (closest_commands.empty())
37553759
{
@@ -3786,20 +3790,20 @@ bool CommandManager::hasTypo(std::shared_ptr<STKPeer> acting_peer, std::shared_p
37863790
}
37873791
for (unsigned i = 0; i < closest_commands.size(); ++i) {
37883792
argv[idx] = prefix + closest_commands[i].first + suffix;
3789-
restoreCmdByArgv(cmd, argv, ' ', '"', '"', '\\');
3793+
StringUtils::restoreCmdFromArgv(cmd, argv, ' ', '"', '"', '\\');
37903794
m_user_command_replacements[username].push_back(cmd);
37913795
response += "\ntype /" + std::to_string(i + 1) + " to choose \"" + closest_commands[i].first + "\"";
37923796
}
37933797
argv[idx] = initial_argument;
3794-
restoreCmdByArgv(cmd, argv, ' ', '"', '"', '\\');
3798+
StringUtils::restoreCmdFromArgv(cmd, argv, ' ', '"', '"', '\\');
37953799
Comm::sendStringToPeer(peer, response);
37963800
return true;
37973801
}
37983802

37993803
if (!dont_replace)
38003804
{
38013805
argv[idx] = prefix + closest_commands[0].first + suffix; // converts case or regex
3802-
restoreCmdByArgv(cmd, argv, ' ', '"', '"', '\\');
3806+
StringUtils::restoreCmdFromArgv(cmd, argv, ' ', '"', '"', '\\');
38033807
}
38043808
return false;
38053809
} // hasTypo
@@ -3891,7 +3895,7 @@ void CommandManager::shift(std::string& cmd, std::vector<std::string>& argv,
38913895

38923896
m_user_last_correct_argument[username].first -= count;
38933897

3894-
restoreCmdByArgv(cmd, argv, ' ', '"', '"', '\\');
3898+
StringUtils::restoreCmdFromArgv(cmd, argv, ' ', '"', '"', '\\');
38953899
} // shift
38963900
//-----------------------------------------------------------------------------
38973901

src/utils/lobby_queues.hpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,8 @@ class LobbyQueues: public LobbyContextComponent
4949

5050
private:
5151
Queue m_onetime_tracks_queue;
52-
5352
Queue m_cyclic_tracks_queue;
54-
5553
Queue m_onetime_karts_queue;
56-
5754
Queue m_cyclic_karts_queue;
5855

5956
// Temporary reference getters

src/utils/string_utils.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,8 +256,8 @@ namespace StringUtils
256256
} // quoteEscape
257257

258258

259-
std::string quoteEscapeArray(const std::vector<std::string>::iterator begin,
260-
const std::vector<std::string>::iterator end,
259+
std::string quoteEscapeArray(const std::vector<std::string>::const_iterator begin,
260+
const std::vector<std::string>::const_iterator end,
261261
char c, char d, char e, char f)
262262
{
263263
std::string res = "";
@@ -271,6 +271,16 @@ namespace StringUtils
271271
return res;
272272
} // quoteEscapeArray
273273
//-------------------------------------------------------------------------
274+
275+
void restoreCmdFromArgv(std::string& cmd,
276+
const std::vector<std::string>& argv,
277+
char c, char d, char e, char f,
278+
int from)
279+
{
280+
cmd = quoteEscapeArray(argv.begin() + from, argv.end(), c, d, e, f);
281+
} // restoreCmdFromArgv
282+
//-------------------------------------------------------------------------
283+
274284
/** Splits a string into substrings separated by a certain character, and
275285
* returns a std::vector of all those substring. E.g.:
276286
* split("a b=c d=e",' ') --> ["a", "b=c", "d=e"]

src/utils/string_utils.hpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,13 @@ namespace StringUtils
5858
char d, char e, char f);
5959
std::string quoteEscape(const std::string& s, char c,
6060
char d, char e, char f);
61-
std::string quoteEscapeArray(const std::vector<std::string>::iterator begin,
62-
const std::vector<std::string>::iterator end,
61+
std::string quoteEscapeArray(const std::vector<std::string>::const_iterator begin,
62+
const std::vector<std::string>::const_iterator end,
6363
char c, char d, char e, char f);
64+
void restoreCmdFromArgv(std::string& cmd,
65+
const std::vector<std::string>& argv,
66+
char c, char d, char e, char f,
67+
int from = 0);
6468
std::vector<std::string> split(const std::string& s, char c,
6569
bool keepSplitChar=false);
6670
std::vector<std::u32string> split(const std::u32string& s, char32_t c,

0 commit comments

Comments
 (0)