AK: Add String::contains

This commit is contained in:
Timothy Flynn 2023-01-14 10:17:32 -05:00 committed by Linus Groh
parent bd9b65e82f
commit cf0899f440
Notes: sideshowbarker 2024-07-17 01:41:46 +09:00
2 changed files with 14 additions and 0 deletions

View file

@ -339,6 +339,16 @@ ErrorOr<String> String::reverse() const
return builder.to_string();
}
bool String::contains(StringView needle, CaseSensitivity case_sensitivity) const
{
return StringUtils::contains(bytes_as_string_view(), needle, case_sensitivity);
}
bool String::contains(char needle, CaseSensitivity case_sensitivity) const
{
return contains(StringView { &needle, 1 }, case_sensitivity);
}
bool String::is_short_string() const
{
return has_short_string_bit(reinterpret_cast<uintptr_t>(m_data));

View file

@ -12,6 +12,7 @@
#include <AK/Optional.h>
#include <AK/RefCounted.h>
#include <AK/Span.h>
#include <AK/StringUtils.h>
#include <AK/StringView.h>
#include <AK/Traits.h>
#include <AK/Types.h>
@ -92,6 +93,9 @@ public:
return (this->operator==(forward<Ts>(strings)) || ...);
}
[[nodiscard]] bool contains(StringView, CaseSensitivity = CaseSensitivity::CaseSensitive) const;
[[nodiscard]] bool contains(char, CaseSensitivity = CaseSensitivity::CaseSensitive) const;
[[nodiscard]] u32 hash() const;
template<Arithmetic T>