Albert
Loading...
Searching...
No Matches
matcher.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2024 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
24class ALBERT_EXPORT Match final
25{
26public:
27 using Score = double;
28
32 Match() : score_(-1.) {}
33
37 Match(const Score score) : score_(score) {}
38
42 Match(const Match &o) = default;
43
47 Match &operator=(const Match &o) = default;
48
52 inline bool isMatch() const { return score_ >= 0.0; }
53
57 inline bool isEmptyMatch() const { return qFuzzyCompare(score_, 0.0); }
58
62 inline bool isExactMatch() const { return qFuzzyCompare(score_, 1.0); }
63
67 inline Score score() const { return score_; }
68
72 inline explicit operator bool() const { return isMatch(); }
73
77 inline operator Score() const { return score_; }
78
79private:
80
81 Score score_;
82};
83
84
90class ALBERT_EXPORT Matcher final
91{
92public:
98 Matcher(const QString &string, MatchConfig config = {});
99
104
109
114
118 const QString &string() const;
119
123 Match match(const QString &string) const;
124
128 template<typename... Args>
129 Match match(QString first, Args... args) const {
130 return std::max(match(first), match(args...));
131 }
132
136 Match match(std::ranges::range auto &&strings) const
137 requires std::same_as<std::ranges::range_value_t<decltype(strings)>, QString>
138 {
139 if (strings.empty())
140 return Match();
141 return std::ranges::max(
142 strings | std::views::transform([this](const QString &s) { return this->match(s); }));
143 }
144
145
146private:
147 class Private;
148 std::unique_ptr<Private> d;
149
150};
151
152}
Configuration for string matching.
Definition matchconfig.h:17
Augmented match score.
Definition matcher.h:25
Match()
Constructs an invalid match.
Definition matcher.h:32
bool isEmptyMatch() const
Returns true if this is a zero score match, otherwise returns false.
Definition matcher.h:57
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:52
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:62
Match(const Score score)
Constructs a match with the given score.
Definition matcher.h:37
Score score() const
Returns the score.
Definition matcher.h:67
double Score
Definition matcher.h:27
Configurable string matcher.
Definition matcher.h:91
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 for strings.
Definition matcher.h:136
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, Args... args) const
Returns a Match for the given strings.
Definition matcher.h:129
Matcher & operator=(Matcher &&o)
Replaces the contents with those of other using move semantics.
Definition action.h:10