1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 06:07:34 +00:00

find: Add the -regex and -iregex options

These options match a given regular expression against the whole file
path. `-iregex` is identical to `-regex` except it is case-insensitive.
This commit is contained in:
Tim Ledbetter 2023-08-27 16:46:31 +01:00 committed by Ali Mohammad Pur
parent 2a86bd8c91
commit 0a02c7a926
2 changed files with 34 additions and 0 deletions

View file

@ -12,6 +12,7 @@
#include <AK/Vector.h>
#include <LibCore/System.h>
#include <LibMain/Main.h>
#include <LibRegex/Regex.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
@ -267,6 +268,24 @@ private:
CaseSensitivity m_case_sensitivity { CaseSensitivity::CaseSensitive };
};
class RegexCommand : public Command {
public:
RegexCommand(Regex<PosixBasic>&& regex)
: m_regex(move(regex))
{
}
private:
virtual bool evaluate(FileData& file_data) const override
{
auto haystack = file_data.full_path.string();
auto match_result = m_regex.match(haystack);
return match_result.success;
}
Regex<PosixBasic> m_regex;
};
class AccessCommand final : public Command {
public:
AccessCommand(mode_t mode)
@ -426,6 +445,20 @@ static OwnPtr<Command> parse_simple_command(Vector<char*>& args)
if (args.is_empty())
fatal_error("-iname: requires additional arguments");
return make<NameCommand>(args.take_first(), CaseSensitivity::CaseInsensitive);
} else if (arg == "-regex") {
if (args.is_empty())
fatal_error("-regex: requires additional arguments");
Regex<PosixBasic> regex { args.take_first(), PosixFlags::Default };
if (regex.parser_result.error != regex::Error::NoError)
fatal_error("{}", regex.error_string());
return make<RegexCommand>(move(regex));
} else if (arg == "-iregex") {
if (args.is_empty())
fatal_error("-iregex: requires additional arguments");
Regex<PosixBasic> regex { args.take_first(), PosixFlags::Insensitive };
if (regex.parser_result.error != regex::Error::NoError)
fatal_error("{}", regex.error_string());
return make<RegexCommand>(move(regex));
} else if (arg == "-readable") {
return make<AccessCommand>(R_OK);
} else if (arg == "-writable") {