File indexing completed on 2024-04-21 04:00:55

0001 /*  This file is part of the KDE libraries
0002     SPDX-FileCopyrightText: 2006 Jacob R Rideout <kde@jacobrideout.net>
0003     SPDX-FileCopyrightText: 2006 Martin Sandsmark <martin.sandsmark@kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #include <QHash>
0009 #include <QString>
0010 #include <QTextBoundaryFinder>
0011 
0012 #include "textbreaks_p.h"
0013 
0014 namespace Sonnet
0015 {
0016 class TextBreaksPrivate
0017 {
0018 public:
0019     TextBreaksPrivate()
0020     {
0021     }
0022 
0023     QString text;
0024 };
0025 
0026 TextBreaks::TextBreaks(const QString &text)
0027     : d(new TextBreaksPrivate())
0028 {
0029     setText(text);
0030 }
0031 
0032 TextBreaks::~TextBreaks() = default;
0033 
0034 QString TextBreaks::text() const
0035 {
0036     return d->text;
0037 }
0038 
0039 void TextBreaks::setText(const QString &text)
0040 {
0041     d->text = text;
0042 }
0043 
0044 TextBreaks::Positions TextBreaks::wordBreaks(const QString &text)
0045 {
0046     Positions breaks;
0047 
0048     if (text.isEmpty()) {
0049         return breaks;
0050     }
0051 
0052     QTextBoundaryFinder boundaryFinder(QTextBoundaryFinder::Word, text);
0053 
0054     while (boundaryFinder.position() < text.length()) {
0055         if (!(boundaryFinder.boundaryReasons().testFlag(QTextBoundaryFinder::StartOfItem))) {
0056             if (boundaryFinder.toNextBoundary() == -1) {
0057                 break;
0058             }
0059             continue;
0060         }
0061 
0062         Position pos;
0063         pos.start = boundaryFinder.position();
0064         int end = boundaryFinder.toNextBoundary();
0065         if (end == -1) {
0066             break;
0067         }
0068         pos.length = end - pos.start;
0069         if (pos.length < 1) {
0070             continue;
0071         }
0072         breaks.append(pos);
0073 
0074         if (boundaryFinder.toNextBoundary() == -1) {
0075             break;
0076         }
0077     }
0078     return breaks;
0079 }
0080 
0081 TextBreaks::Positions TextBreaks::sentenceBreaks(const QString &text)
0082 {
0083     Positions breaks;
0084 
0085     if (text.isEmpty()) {
0086         return breaks;
0087     }
0088 
0089     QTextBoundaryFinder boundaryFinder(QTextBoundaryFinder::Sentence, text);
0090 
0091     while (boundaryFinder.position() < text.length()) {
0092         Position pos;
0093         pos.start = boundaryFinder.position();
0094         int end = boundaryFinder.toNextBoundary();
0095         if (end == -1) {
0096             break;
0097         }
0098         pos.length = end - pos.start;
0099         if (pos.length < 1) {
0100             continue;
0101         }
0102         breaks.append(pos);
0103     }
0104     return breaks;
0105 }
0106 
0107 TextBreaks::Positions TextBreaks::wordBreaks() const
0108 {
0109     return wordBreaks(d->text);
0110 }
0111 
0112 TextBreaks::Positions TextBreaks::sentenceBreaks() const
0113 {
0114     return sentenceBreaks(d->text);
0115 }
0116 }