File indexing completed on 2024-12-29 04:50:10

0001 /*
0002     SPDX-FileCopyrightText: 2021 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "airportnametokenizer_p.h"
0008 
0009 using namespace KItinerary;
0010 
0011 AirportNameTokenizer::AirportNameTokenizer(QStringView text)
0012     : m_text(text)
0013 {
0014     advance();
0015 }
0016 
0017 AirportNameTokenizer::~AirportNameTokenizer() = default;
0018 
0019 bool AirportNameTokenizer::hasNext()
0020 {
0021     return m_end > m_begin && m_begin >= 0 && m_end >= 0 && m_end <= m_text.size();
0022 }
0023 
0024 QStringView AirportNameTokenizer::next()
0025 {
0026     Q_ASSERT(hasNext());
0027     const auto s = m_text.mid(m_begin, m_end - m_begin);
0028     advance();
0029     return s;
0030 }
0031 
0032 void AirportNameTokenizer::advance()
0033 {
0034     m_begin = m_end;
0035     for (;m_begin < m_text.size(); ++m_begin) {
0036         if (!isSeparator(m_text.at(m_begin))) {
0037             break;
0038         }
0039     }
0040 
0041     m_end = m_begin + 1;
0042     for (;m_end < m_text.size(); ++m_end) {
0043         if (isSeparator(m_text.at(m_end))) {
0044             break;
0045         }
0046     }
0047 
0048     if ((m_end - m_begin) < MIN_LENGTH) {
0049         m_begin = m_end;
0050     }
0051     if (!hasNext() && m_end < m_text.size()) {
0052         advance();
0053     }
0054 }
0055 
0056 bool AirportNameTokenizer::isSeparator(QChar c) const
0057 {
0058     return c.isSpace() || c.isNumber() || c.isPunct() || !c.isPrint();
0059 }
0060 
0061 QStringList AirportNameTokenizer::toStringList()
0062 {
0063     QStringList l;
0064     while (hasNext()) {
0065         l.push_back(next().toString());
0066     }
0067     return l;
0068 }