diff --git a/tdutils/td/utils/misc.h b/tdutils/td/utils/misc.h index b7a1584d..614b5aa1 100644 --- a/tdutils/td/utils/misc.h +++ b/tdutils/td/utils/misc.h @@ -102,6 +102,16 @@ void remove_if(T &v, const Func &f) { v.erase(v.begin() + j, v.end()); } +template +bool contains(const V &v, const T &elem) { + for (auto &x : v) { + if (x == elem) { + return true; + } + } + return false; +} + template void reset_to_empty(T &value) { using std::swap; diff --git a/tdutils/test/misc.cpp b/tdutils/test/misc.cpp index cd10b396..0f2e30e0 100644 --- a/tdutils/test/misc.cpp +++ b/tdutils/test/misc.cpp @@ -211,7 +211,7 @@ TEST(Misc, base64) { template static void test_remove_if(vector v, const T &func, vector expected) { - remove_if(v, func); + td::remove_if(v, func); if (expected != v) { LOG(FATAL) << "Receive " << v << ", expected " << expected << " in remove_if"; } @@ -268,6 +268,25 @@ TEST(Misc, remove_if) { test_remove_if(v, none, v); } +TEST(Misc, contains) { + td::vector v{1, 3, 5, 7, 4, 2}; + for (int i = -10; i < 20; i++) { + ASSERT_EQ(td::contains(v, i), (1 <= i && i <= 5) || i == 7); + } + + v.clear(); + ASSERT_TRUE(!td::contains(v, 0)); + ASSERT_TRUE(!td::contains(v, 1)); + + td::string str = "abacaba"; + ASSERT_TRUE(!td::contains(str, '0')); + ASSERT_TRUE(!td::contains(str, 0)); + ASSERT_TRUE(!td::contains(str, 'd')); + ASSERT_TRUE(td::contains(str, 'a')); + ASSERT_TRUE(td::contains(str, 'b')); + ASSERT_TRUE(td::contains(str, 'c')); +} + TEST(Misc, to_integer) { ASSERT_EQ(to_integer("-1234567"), -1234567); ASSERT_EQ(to_integer("-1234567"), -1234567);