Skip to content

Commit

Permalink
Explicit collection type for commasplit and non-static for older clang
Browse files Browse the repository at this point in the history
  • Loading branch information
black-sliver committed Sep 7, 2024
1 parent 879275d commit 958df39
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 15 deletions.
12 changes: 6 additions & 6 deletions src/core/jsonutil.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ static nlohmann::json parse_jsonc(std::string& s)
return j;
}

template <template <class> class T>
static void commasplit(const std::string& s, T<std::string>& l)
template <template <typename...> class T>
void commasplit(const std::string& s, T<std::string>& l)
{
auto sta = s.find_first_not_of(' '); // ltrim
auto end = s.find(',');
Expand All @@ -56,16 +56,16 @@ static void commasplit(const std::string& s, T<std::string>& l)
l.push_back(s.substr(sta, end - sta));
}

template <template <class> class T = std::list>
static T<std::string> commasplit(const std::string& s)
template <template <typename...> class T>
T<std::string> commasplit(const std::string& s)
{
T<std::string> lst;
commasplit<T>(s, lst);
return lst;
}

template <template <class> class T = std::list>
static T<std::string> commasplit(std::string&& s)
template <template <typename...> class T>
T<std::string> commasplit(std::string&& s)
{
std::string tmp = s;
return commasplit<T>(tmp);
Expand Down
4 changes: 2 additions & 2 deletions src/core/luaitem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ bool LuaItem::Lua_NewIndex(lua_State *L, const char *key) {
} else if (strcmp(key,"IconMods")==0) {
// NOTE: these are applied on top of .Icon ImageReference
std::string s = luaL_checkstring(L,-1);
auto mods = commasplit(s);
auto mods = commasplit<std::list>(s);
if (_extraImgMods != mods) {
_extraImgMods = mods;
parseFullImg();
Expand Down Expand Up @@ -353,7 +353,7 @@ void LuaItem::parseFullImg()
_imgMods = _extraImgMods;
} else {
_img = _fullImg.substr(0, pos);
_imgMods = commasplit(_fullImg.substr(pos + 1));
_imgMods = commasplit<std::list>(_fullImg.substr(pos + 1));
_imgMods.insert(_imgMods.end(), _extraImgMods.begin(), _extraImgMods.end());
}
}
4 changes: 2 additions & 2 deletions src/ui/trackerview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ Item* TrackerView::makeItem(int x, int y, int width, int height, const ::BaseIte
f = img;
} else {
f = img.substr(0, p);
filters = imageModsToFilters(_tracker, commasplit(img.substr(p + 1)));
filters = imageModsToFilters(_tracker, commasplit<std::list>(img.substr(p + 1)));
}
std::string s;
_tracker->getPack()->ReadFile(f, s);
Expand Down Expand Up @@ -448,7 +448,7 @@ void TrackerView::updateDisplay(const std::string& itemid)
f = img;
} else {
f = img.substr(0, p);
filters = imageModsToFilters(_tracker, commasplit(img.substr(p + 1)));
filters = imageModsToFilters(_tracker, commasplit<std::list>(img.substr(p + 1)));
}
std::string s;
_tracker->getPack()->ReadFile(f, s);
Expand Down
10 changes: 5 additions & 5 deletions test/jsonutil/test_commasplit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,30 @@ using namespace std;
// only trailing empty fields should be dropped

TEST(CommaSplitTest, Empty) {
EXPECT_EQ(commasplit(""),
EXPECT_EQ(commasplit<list>(""),
list<string>({}));
}

TEST(CommaSplitTest, TrailingComma) {
// Trailing empty values should be dropped
EXPECT_EQ(commasplit("a,"),
EXPECT_EQ(commasplit<list>("a,"),
list<string>({"a"}));
}

TEST(CommaSplitTest, LeadingEmpty) {
// Leading empty values should be kept
EXPECT_EQ(commasplit(",b"),
EXPECT_EQ(commasplit<list>(",b"),
list<string>({"", "b"}));
}

TEST(CommaSplitTest, MiddleEmpty) {
// Empty values in the middle should be kept
EXPECT_EQ(commasplit("a,,b"),
EXPECT_EQ(commasplit<list>("a,,b"),
list<string>({"a", "", "b"}));
}

TEST(CommaSplitTest, TrailingWhitespace) {
// If trailing whitespace results in trailing comma, it should behave the same as TrailingComma
EXPECT_EQ(commasplit("a, "),
EXPECT_EQ(commasplit<list>("a, "),
list<string>({"a"}));
}

0 comments on commit 958df39

Please sign in to comment.