From 4e8c4d56630c2bb1f603733abbcb126f0588fbb0 Mon Sep 17 00:00:00 2001 From: black-sliver <59490463+black-sliver@users.noreply.github.com> Date: Wed, 21 Feb 2024 22:05:41 +0100 Subject: [PATCH] Fix comma splitting with leading comma this is used for codes and mods --- src/core/jsonutil.h | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/core/jsonutil.h b/src/core/jsonutil.h index da9f5a36..08de17f7 100644 --- a/src/core/jsonutil.h +++ b/src/core/jsonutil.h @@ -41,24 +41,31 @@ static void commasplit(const std::string& s, std::list& l) auto sta = s.find_first_not_of(' '); // ltrim auto end = s.find(','); while (end != std::string::npos) { - auto p = s.find_last_not_of(' ', end-1)+1; // rtrim - l.push_back(s.substr(sta,p-sta)); + if (sta == end) { + l.push_back(""); + } else { + auto p = s.find_last_not_of(' ', end - 1) + 1; // rtrim + l.push_back(s.substr(sta, p - sta)); + } sta = s.find_first_not_of(' ', end + 1); // ltrim - end = s.find(',',sta); + end = s.find(',', sta); } - end = s.find_last_not_of(' ')+1; // rtrim - if (sta < end) l.push_back(s.substr(sta,end-sta)); + end = s.find_last_not_of(' ') + 1; // rtrim + if (sta < end) // ignore trailing comma + l.push_back(s.substr(sta, end - sta)); } + static std::list commasplit(const std::string& s) { std::list lst; commasplit(s, lst); return lst; } + static std::list commasplit(std::string&& s) { std::string tmp = s; - return commasplit(s); + return commasplit(tmp); }