AK: Add String::is_one_of(...)

This allows you to compare a string against an arbitrary number of
other strings with a single call.
This commit is contained in:
Andreas Kling 2020-05-25 12:36:41 +02:00
parent 21a574f6d2
commit 5e77517e6e
Notes: sideshowbarker 2024-07-19 06:09:11 +09:00
2 changed files with 21 additions and 2 deletions

View file

@ -88,6 +88,16 @@ public:
static void did_destroy_impl(Badge<StringImpl>, StringImpl&); static void did_destroy_impl(Badge<StringImpl>, StringImpl&);
bool is_one_of() { return false; }
template<typename T, typename... Rest>
bool is_one_of(const T& string, Rest... rest)
{
if (string == *this)
return true;
return is_one_of(rest...);
}
private: private:
RefPtr<StringImpl> m_impl; RefPtr<StringImpl> m_impl;
}; };

View file

@ -58,9 +58,9 @@ class String {
public: public:
using ConstIterator = const char*; using ConstIterator = const char*;
~String() {} ~String() { }
String() {} String() { }
String(const StringView&); String(const StringView&);
String(const String& other) String(const String& other)
@ -224,6 +224,15 @@ public:
int replace(const String& needle, const String& replacement, bool all_occurences = false); int replace(const String& needle, const String& replacement, bool all_occurences = false);
bool is_one_of() { return false; }
template<typename T, typename... Rest>
bool is_one_of(const T& string, Rest... rest)
{
if (string == *this)
return true;
return is_one_of(rest...);
}
private: private:
RefPtr<StringImpl> m_impl; RefPtr<StringImpl> m_impl;
}; };