KItinerary

terminalfinder.cpp
1/*
2 SPDX-FileCopyrightText: 2019-2022 Volker Krause <vkrause@kde.org>
3 SPDX-License-Identifier: LGPL-2.0-or-later
4*/
5
6#include "terminalfinder_p.h"
7
8#include <QDebug>
9
10using namespace KItinerary;
11
12struct {
13 const char *pattern;
15} static constexpr const terminal_patterns[] = {
16 // use a named capture group for the actual name part of the terminal
17 { " ?\\((?:terminal|aerogare) (?<name>\\w.*)\\)", QRegularExpression::CaseInsensitiveOption },
18 { " ?\\((?<name>\\w.*) (?:terminal|aerogare)\\)", QRegularExpression::CaseInsensitiveOption },
19 { "(?:, | ?- ?| )(?:terminal|aerogare) (?<name>\\w.*?)", QRegularExpression::CaseInsensitiveOption },
20 { " T-?(?<name>\\d[A-Z]?)", QRegularExpression::NoPatternOption },
21 { " ?\\(T(?<name>\\d[A-Z]?)\\)", QRegularExpression::NoPatternOption },
22};
23
24
25TerminalFinder::TerminalFinder(QStringView frontAnchor, QStringView backAnchor)
26{
27 static_assert(std::tuple_size_v<decltype(m_patterns)> == std::size(terminal_patterns));
28
29 int i = 0;
30 for (const auto &pattern : terminal_patterns) {
31 m_patterns[i++] = QRegularExpression(
32 frontAnchor + QLatin1StringView("(?<terminal>") +
33 QLatin1StringView(pattern.pattern) + QLatin1StringView(")") + backAnchor,
34 pattern.options);
35 }
36}
37
38TerminalFinder::~TerminalFinder() = default;
39
40TerminalFinder::Result TerminalFinder::find(QStringView s) const
41{
42 for (const auto &re: m_patterns) {
43 const auto match = re.matchView(s);
44 if (match.hasMatch()) {
45 Result res;
46 res.start = match.capturedStart(u"terminal");
47 res.end = match.capturedEnd(u"terminal");
48 res.name = match.captured(u"name"); // ### can't be a QStringView, that becomes invalid as soon as match goes out of scope (not when s becomes invalid!)
49 return res;
50 }
51 }
52 return {};
53}
54
KCOREADDONS_EXPORT Result match(QStringView pattern, QStringView str)
Classes for reservation/travel data models, data extraction and data augmentation.
Definition berelement.h:17
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 4 2024 16:28:48 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.