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 Match match(QString first, auto... args) const
129 { return std::max(match(first), match(args...)); }
130
134 Match match(std::ranges::range auto &&strings) const
135 requires std::same_as<std::ranges::range_value_t<decltype(strings)>, QString>
136 {
137 if (strings.empty())
138 return Match();
139 return std::ranges::max(
140 strings | std::views::transform([this](const QString &s) { return this->match(s); }));
141 }
142
143
144private:
145 class Private;
146 std::unique_ptr<Private> d;
147
148};
149
150}
Configuration for string matching.
Definition matchconfig.h:23
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 in the range of strings.
Definition matcher.h:134
const QString & string() const
Returns the string matched against.
~Matcher()
Destructs the Matcher.
Match match(QString first, auto... args) const
Returns the max Match for the given strings.
Definition matcher.h:128
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.
Matcher & operator=(Matcher &&o)
Replaces the contents with those of other using move semantics.
Definition action.h:10