Albert
Loading...
Searching...
No Matches
matcher.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2025 Manuel Schneider
2// SPDX-License-Identifier: MIT
3
4#pragma once
5#include <QRegularExpression>
6#include <QString>
7#include <albert/export.h>
9#include <ranges>
10class MatcherPrivate;
11
12namespace albert
13{
14
26class ALBERT_EXPORT Match final
27{
28public:
29 using Score = double;
30
34 Match() : score_(-1.) {}
35
39 Match(const Score score) : score_(score) {}
40
44 Match(const Match &o) = default;
45
49 Match &operator=(const Match &o) = default;
50
54 inline bool isMatch() const { return score_ >= 0.0; }
55
59 inline bool isEmptyMatch() const { return qFuzzyCompare(score_, 0.0); }
60
64 inline bool isExactMatch() const { return qFuzzyCompare(score_, 1.0); }
65
69 inline Score score() const { return score_; }
70
74 inline explicit operator bool() const { return isMatch(); }
75
79 inline operator Score() const { return score_; }
80
81private:
82
83 Score score_;
84};
85
86
94class ALBERT_EXPORT Matcher final
95{
96public:
102 Matcher(const QString &string, MatchConfig config = {});
103
108
113
118
122 const QString &string() const;
123
127 Match match(const QString &string) const;
128
132 Match match(QString first, auto... remainder) const
133 { return std::max(match(first), match(remainder...)); }
134
138 Match match(std::ranges::range auto &&strings) const
139 requires std::same_as<std::ranges::range_value_t<decltype(strings)>, QString>
140 {
141 if (strings.empty())
142 return Match();
143 return std::ranges::max(
144 strings | std::views::transform([this](const QString &s) { return this->match(s); }));
145 }
146
147
148private:
149
150 class Private;
151 std::unique_ptr<Private> d;
152
153};
154
155}
Configuration for string matching.
Definition matchconfig.h:20
Augmented match score.
Definition matcher.h:27
Match()
Constructs an invalid match.
Definition matcher.h:34
bool isEmptyMatch() const
Returns true if this is a zero score match, otherwise returns false.
Definition matcher.h:59
Match(const Match &o)=default
Constructs a Match with the score of other.
bool isMatch() const
Returns true if this is a match, otherwise returns false.
Definition matcher.h:54
Match & operator=(const Match &o)=default
Replaces the score with that of other.
bool isExactMatch() const
Returns true if this is a perfect match, otherwise returns false.
Definition matcher.h:64
Match(const Score score)
Constructs a match with the given score.
Definition matcher.h:39
Score score() const
Returns the score.
Definition matcher.h:69
double Score
Definition matcher.h:29
Configurable string matcher.
Definition matcher.h:95
Matcher(const QString &string, MatchConfig config={})
Constructs a Matcher with the given string and match config.
Match match(std::ranges::range auto &&strings) const
Returns the max Match in the range of strings.
Definition matcher.h:138
const QString & string() const
Returns the string matched against.
~Matcher()
Destructs the Matcher.
Match match(const QString &string) const
Returns a Match for string.
Matcher(Matcher &&o)
Constructs a Matcher with the contents of other using move semantics.
Match match(QString first, auto... remainder) const
Returns the max Match for the strings first and remainder.
Definition matcher.h:132
Matcher & operator=(Matcher &&o)
Replaces the contents with those of other using move semantics.
Definition app.h:56