From 5e77517e6e71df80e8a1f7ad0cba359e7bc7e299 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Mon, 25 May 2020 12:36:41 +0200 Subject: [PATCH] AK: Add String::is_one_of(...) This allows you to compare a string against an arbitrary number of other strings with a single call. --- AK/FlyString.h | 10 ++++++++++ AK/String.h | 13 +++++++++++-- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/AK/FlyString.h b/AK/FlyString.h index 8f92396928..d3423522c5 100644 --- a/AK/FlyString.h +++ b/AK/FlyString.h @@ -88,6 +88,16 @@ public: static void did_destroy_impl(Badge, StringImpl&); + bool is_one_of() { return false; } + template + bool is_one_of(const T& string, Rest... rest) + { + if (string == *this) + return true; + return is_one_of(rest...); + } + + private: RefPtr m_impl; }; diff --git a/AK/String.h b/AK/String.h index 1f06490696..e882b8919f 100644 --- a/AK/String.h +++ b/AK/String.h @@ -58,9 +58,9 @@ class String { public: using ConstIterator = const char*; - ~String() {} + ~String() { } - String() {} + String() { } String(const StringView&); String(const String& other) @@ -224,6 +224,15 @@ public: int replace(const String& needle, const String& replacement, bool all_occurences = false); + bool is_one_of() { return false; } + template + bool is_one_of(const T& string, Rest... rest) + { + if (string == *this) + return true; + return is_one_of(rest...); + } + private: RefPtr m_impl; };