From 5ba2dba392966449ac980eadf3b1dadedaa4487b Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sun, 26 May 2019 22:33:30 +0200 Subject: [PATCH] FileSystemPath: Add a has_extension() helper. This code: if (path.string().to_lowercase().ends_with(".foo")) Can now be written as: if (path.has_extension(".foo")) --- AK/FileSystemPath.cpp | 6 ++++++ AK/FileSystemPath.h | 2 ++ 2 files changed, 8 insertions(+) diff --git a/AK/FileSystemPath.cpp b/AK/FileSystemPath.cpp index 971bbf52bd..e094c1de3d 100644 --- a/AK/FileSystemPath.cpp +++ b/AK/FileSystemPath.cpp @@ -45,5 +45,11 @@ bool FileSystemPath::canonicalize(bool resolve_symbolic_links) return true; } +bool FileSystemPath::has_extension(StringView extension) const +{ + // FIXME: This is inefficient, expand StringView with enough functionality that we don't need to copy strings here. + String extension_string = extension; + return m_string.to_lowercase().ends_with(extension_string.to_lowercase()); } +} diff --git a/AK/FileSystemPath.h b/AK/FileSystemPath.h index 64827157a7..3e56523269 100644 --- a/AK/FileSystemPath.h +++ b/AK/FileSystemPath.h @@ -16,6 +16,8 @@ public: const Vector& parts() const { return m_parts; } + bool has_extension(StringView) const; + private: bool canonicalize(bool resolve_symbolic_links = false);