summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/utils/stringutils.cpp41
-rw-r--r--src/utils/stringutils_unittest.cc32
2 files changed, 67 insertions, 6 deletions
diff --git a/src/utils/stringutils.cpp b/src/utils/stringutils.cpp
index 830b0346a..dadfd35f5 100644
--- a/src/utils/stringutils.cpp
+++ b/src/utils/stringutils.cpp
@@ -970,9 +970,11 @@ std::string timeDiffToString(int timeDiff)
return str;
}
+#include "logger.h"
+
+#ifndef DYECMD
void replaceItemLinks(std::string &msg)
{
-#ifndef DYECMD
// Check for item link
size_t start2 = msg.find('[');
size_t sz = msg.size();
@@ -998,11 +1000,34 @@ void replaceItemLinks(std::string &msg)
{
std::string itemStr = msg.substr(start2 + 1, end - start2 - 1);
- const ItemInfo &itemInfo = ItemDB::get(itemStr);
- if (itemInfo.getId() != 0)
+ StringVect parts;
+ splitToStringVector(parts, itemStr, ',');
+ if (parts.empty())
+ return;
+
+ const ItemInfo &itemInfo = ItemDB::get(parts[0]);
+ const int itemId = itemInfo.getId();
+ if (itemId != 0)
{
- std::string temp = strprintf("@@%d|", itemInfo.getId());
- msg.insert(end, "@@");
+ std::string temp = strprintf("@@%d", itemId);
+ std::string name = parts[0];
+ msg.erase(start2 + 1, end - start2 - 1);
+ parts.erase(parts.begin());
+ if (!parts.empty())
+ name.clear();
+
+ FOR_EACH (StringVectCIter, it, parts)
+ {
+ std:: string str = *it;
+ trim(str);
+ const ItemInfo &itemInfo2 = ItemDB::get(str);
+ const int cardId = itemInfo2.getId();
+ if (cardId != 0)
+ temp.append(strprintf(",%d", cardId));
+ }
+ temp.append("|");
+ temp.append(name);
+ temp.append("@@");
msg.insert(start2 + 1, temp);
sz = msg.size();
}
@@ -1010,5 +1035,9 @@ void replaceItemLinks(std::string &msg)
}
start2 = msg.find('[', start2 + 1);
}
-#endif
}
+#else
+void replaceItemLinks(std::string &msg A_UNUSED)
+{
+}
+#endif
diff --git a/src/utils/stringutils_unittest.cc b/src/utils/stringutils_unittest.cc
index e2db8eed1..d2dfcdf62 100644
--- a/src/utils/stringutils_unittest.cc
+++ b/src/utils/stringutils_unittest.cc
@@ -625,6 +625,14 @@ TEST_CASE("stringuntils replaceItemLinks")
str = "[qqq]";
replaceItemLinks(str);
REQUIRE(str == "[qqq]");
+
+ str = "[,]";
+ replaceItemLinks(str);
+ REQUIRE(str == "[,]");
+
+ str = "[, ]";
+ replaceItemLinks(str);
+ REQUIRE(str == "[, ]");
}
SECTION("simple")
@@ -653,6 +661,14 @@ TEST_CASE("stringuntils replaceItemLinks")
str = "[test name 1] [no link]";
replaceItemLinks(str);
REQUIRE(str == "[@@123456|test name 1@@] [no link]");
+
+ str = "[test name 1,test name 2]";
+ replaceItemLinks(str);
+ REQUIRE(str == "[@@123456,123|test name 1@@]");
+
+ str = "[test name 1, test name 2 ]";
+ replaceItemLinks(str);
+ REQUIRE(str == "[@@123456,123|test name 1@@]");
}
SECTION("broken")
@@ -770,5 +786,21 @@ TEST_CASE("stringuntils replaceItemLinks")
str = "[[test name 1[]test name 1]";
replaceItemLinks(str);
REQUIRE(str == "[[test name 1[]test name 1]");
+
+ str = "[[test name 1],test name2,test name 1]";
+ replaceItemLinks(str);
+ REQUIRE(str == "[[@@123456|test name 1@@],test name2,test name 1]");
+
+ str = "[[ test name 1], test name2,test name 1 ]";
+ replaceItemLinks(str);
+ REQUIRE(str == "[[@@123456|test name 1@@], test name2,test name 1 ]");
+
+ str = "[[test name 1,test name2[]test name 1]";
+ replaceItemLinks(str);
+ REQUIRE(str == "[[test name 1,test name2[]test name 1]");
+
+ str = "[[test name 1 ,test name2[] test name 1]";
+ replaceItemLinks(str);
+ REQUIRE(str == "[[test name 1 ,test name2[] test name 1]");
}
}