From 40340324d31b2927bf16519c2ac58765d382312a Mon Sep 17 00:00:00 2001 From: Samy BEN TAYEB Date: Sun, 8 Jun 2025 23:43:30 +0200 Subject: [PATCH] split fixed --- sources/commands/commands.cpp | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/sources/commands/commands.cpp b/sources/commands/commands.cpp index c11b7e5..882bf07 100644 --- a/sources/commands/commands.cpp +++ b/sources/commands/commands.cpp @@ -27,26 +27,26 @@ * @param channel channel where the command is sent * @param line line send by the user */ + std::vector cmd::split(std::string &line) { std::vector args; - std::string arg; - if (line.empty()) - return args; - size_t pos = line.find(' '); - size_t old_pos = 0; - while (pos != std::string::npos) { - arg = line.substr(old_pos, pos); - if (arg.empty()) { - break; + size_t start = 0; + size_t end; + + while ((end = line.find(' ', start)) != std::string::npos) { + std::string arg = line.substr(start, end - start); + if (!arg.empty()) { + for (size_t i = 0; i < arg.length(); ++i) + arg[i] = std::tolower(arg[i]); + args.push_back(arg); } - for (size_t i = 0; i < arg.length(); i++) - arg = std::tolower(line[i]); - args.push_back(arg); - old_pos = pos; - pos = line.find(' ', old_pos + 1); + start = end + 1; } - if (!line.empty()) { - return args; + if (start < line.length()) { + std::string arg = line.substr(start); + for (size_t i = 0; i < arg.length(); ++i) + arg[i] = std::tolower(arg[i]); + args.push_back(arg); } return args; }