diff --git a/include/znc/ZNCString.h b/include/znc/ZNCString.h index 8b853002..749489b7 100644 --- a/include/znc/ZNCString.h +++ b/include/znc/ZNCString.h @@ -24,6 +24,7 @@ #include #include #include +#include #define _SQL(s) CString("'" + CString(s).Escape_n(CString::ESQL) + "'") #define _URL(s) CString(s).Escape_n(CString::EURL) @@ -104,6 +105,7 @@ public: CString(const char* c, size_t l) : std::string(c, l) {} CString(const std::string& s) : std::string(s) {} CString(size_t n, char c) : std::string(n, c) {} + CString(std::initializer_list list) : std::string(list) {} ~CString() {} /** @@ -569,6 +571,7 @@ protected: /** * @brief A dictionary for strings. + * @todo Replace with "using MCString = std::map;" in ZNC 2.0 * * This class maps strings to other strings. */ @@ -576,6 +579,8 @@ class MCString : public std::map { public: /** Construct an empty MCString. */ MCString() : std::map() {} + /** Construct a MCString using an initializer list eg. MCString m = { {"key1", "val1"}, {"key2", "val2"} }; */ + MCString(std::initializer_list> list) : std::map(list) {} /** Destruct this MCString. */ virtual ~MCString() { clear(); } diff --git a/test/StringTest.cpp b/test/StringTest.cpp index 06ad8cda..cd8d441d 100644 --- a/test/StringTest.cpp +++ b/test/StringTest.cpp @@ -161,18 +161,15 @@ TEST(StringTest, Split) { CS("a b c").Split(" ", vresult); EXPECT_EQ(vexpected, vresult); - MCString mexpected; + MCString mexpected = { {"a","b"}, {"c","d"} }; MCString mresult; - mexpected["a"] = "b"; - mexpected["c"] = "d"; CS("a=x&c=d&a=b").URLSplit(mresult); EXPECT_EQ(mexpected, mresult) << "URLSplit"; } TEST(StringTest, NamedFormat) { - MCString m; - m["a"] = "b"; + MCString m = { {"a","b"} }; EXPECT_EQ("{xbyb", CString::NamedFormat(CS("\\{x{a}y{a}"), m)); } diff --git a/test/UtilsTest.cpp b/test/UtilsTest.cpp index c7e73500..ea5bf21a 100644 --- a/test/UtilsTest.cpp +++ b/test/UtilsTest.cpp @@ -22,38 +22,27 @@ TEST(IRC32, GetMessageTags) { EXPECT_EQ(MCString(), CUtils::GetMessageTags("")); EXPECT_EQ(MCString(), CUtils::GetMessageTags(":nick!ident@host PRIVMSG #chan :hello world")); - MCString exp; - exp["a"] = "b"; + MCString exp = { {"a","b"} }; EXPECT_EQ(exp, CUtils::GetMessageTags("@a=b")); EXPECT_EQ(exp, CUtils::GetMessageTags("@a=b :nick!ident@host PRIVMSG #chan :hello world")); EXPECT_EQ(exp, CUtils::GetMessageTags("@a=b :rest")); exp.clear(); - exp["ab"] = "cdef"; - exp["znc.in/gh-ij"] = "klmn,op"; + exp = { {"ab","cdef"}, {"znc.in/gh-ij","klmn,op"} }; EXPECT_EQ(exp, CUtils::GetMessageTags("@ab=cdef;znc.in/gh-ij=klmn,op :rest")); - exp.clear(); - exp["a"] = "==b=="; + exp = { {"a","==b=="} }; EXPECT_EQ(exp, CUtils::GetMessageTags("@a===b== :rest")); exp.clear(); - exp["a"] = ""; - exp["b"] = "c"; - exp["d"] = ""; + exp = { {"a",""}, {"b","c"}, {"d",""} }; EXPECT_EQ(exp, CUtils::GetMessageTags("@a;b=c;d :rest")); - exp.clear(); - exp["semi-colon"] += ';'; - exp["space"] += ' '; - exp["NUL"] += '\0'; - exp["backslash"] += '\\'; - exp["CR"] += '\r'; - exp["LF"] += '\n'; + exp = { {"semi-colon",";"}, {"space"," "}, {"NUL",{'\0'}}, {"backslash","\\"}, {"CR",{'\r'}}, {"LF",{'\n'}} }; EXPECT_EQ(exp, CUtils::GetMessageTags(R"(@semi-colon=\:;space=\s;NUL=\0;backslash=\\;CR=\r;LF=\n :rest)")); exp.clear(); - exp["a"] = "; \\\r\n"; + exp = { {"a","; \\\r\n"} }; EXPECT_EQ(exp, CUtils::GetMessageTags(R"(@a=\:\s\\\r\n :rest)")); exp.clear(); } @@ -65,31 +54,23 @@ TEST(IRC32, SetMessageTags) { CUtils::SetMessageTags(sLine, MCString()); EXPECT_EQ(":rest", sLine); - MCString tags; - tags["a"] = "b"; + MCString tags = { {"a","b"} }; CUtils::SetMessageTags(sLine, tags); EXPECT_EQ("@a=b :rest", sLine); - tags["c"] = "d"; + tags = { {"a","b"}, {"c","d"} }; CUtils::SetMessageTags(sLine, tags); EXPECT_EQ("@a=b;c=d :rest", sLine); - tags["e"] = ""; + tags = { {"a","b"}, {"c","d"}, {"e",""} }; CUtils::SetMessageTags(sLine, tags); EXPECT_EQ("@a=b;c=d;e :rest", sLine); - tags.clear(); - tags["semi-colon"] += ';'; - tags["space"] += ' '; - tags["NUL"] += '\0'; - tags["backslash"] += '\\'; - tags["CR"] += '\r'; - tags["LF"] += '\n'; + tags = { {"semi-colon",";"}, {"space"," "}, {"NUL",{'\0'}}, {"backslash","\\"}, {"CR",{'\r'}}, {"LF",{'\n'}} }; CUtils::SetMessageTags(sLine, tags); EXPECT_EQ(R"(@CR=\r;LF=\n;NUL=\0;backslash=\\;semi-colon=\:;space=\s :rest)", sLine); - tags.clear(); - tags["a"] = "; \\\r\n"; + tags = { {"a","; \\\r\n"} }; CUtils::SetMessageTags(sLine, tags); EXPECT_EQ(R"(@a=\:\s\\\r\n :rest)", sLine); }