source: ogBrowser-Git/qtermwidget/lib/HistorySearch.cpp @ c0cec9d

jenkinsmain
Last change on this file since c0cec9d was fedf2a2, checked in by Vadim Troshchinskiy Shmelev <vtroshchinskiy@…>, 18 months ago

Update Qtermwidget to Qt6 version
Remove build files

  • Property mode set to 100644
File size: 5.7 KB
Line 
1/*
2    Copyright 2013 Christian Surlykke
3
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 2 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program; if not, write to the Free Software
16    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17    02110-1301  USA.
18*/
19#include <QApplication>
20#include <QTextStream>
21#include <QDebug>
22
23#include "TerminalCharacterDecoder.h"
24#include "Emulation.h"
25#include "HistorySearch.h"
26
27HistorySearch::HistorySearch(EmulationPtr emulation, const RegExp& regExp,
28        bool forwards, int startColumn, int startLine,
29        QObject* parent) :
30QObject(parent),
31m_emulation(emulation),
32m_regExp(regExp),
33m_forwards(forwards),
34m_startColumn(startColumn),
35m_startLine(startLine) {
36}
37
38HistorySearch::~HistorySearch() {
39}
40
41void HistorySearch::search() {
42    bool found = false;
43
44#if QT_VERSION >= 0x060000
45    if( ! m_regExp.isValid())
46#else
47    if (! m_regExp.isEmpty())
48#endif
49    {
50        if (m_forwards) {
51            found = search(m_startColumn, m_startLine, -1, m_emulation->lineCount()) || search(0, 0, m_startColumn, m_startLine);
52        } else {
53            found = search(0, 0, m_startColumn, m_startLine) || search(m_startColumn, m_startLine, -1, m_emulation->lineCount());
54        }
55
56        if (found) {
57            emit matchFound(m_foundStartColumn, m_foundStartLine, m_foundEndColumn, m_foundEndLine);
58        }
59        else {
60            emit noMatchFound();
61        }
62    }
63
64    deleteLater();
65}
66
67bool HistorySearch::search(int startColumn, int startLine, int endColumn, int endLine) {
68    qDebug() << "search from" << startColumn << "," << startLine
69            <<  "to" << endColumn << "," << endLine;
70
71    int linesRead = 0;
72    int linesToRead = endLine - startLine + 1;
73
74    qDebug() << "linesToRead:" << linesToRead;
75
76    // We read process history from (and including) startLine to (and including) endLine in
77    // blocks of at most 10K lines so that we do not use unhealthy amounts of memory
78    int blockSize;
79    while ((blockSize = qMin(10000, linesToRead - linesRead)) > 0) {
80
81        QString string;
82        QTextStream searchStream(&string);
83        PlainTextDecoder decoder;
84        decoder.begin(&searchStream);
85        decoder.setRecordLinePositions(true);
86
87        // Calculate lines to read and read them
88        int blockStartLine = m_forwards ? startLine + linesRead : endLine - linesRead - blockSize + 1;
89        int chunkEndLine = blockStartLine + blockSize - 1;
90        m_emulation->writeToStream(&decoder, blockStartLine, chunkEndLine);
91
92        // We search between startColumn in the first line of the string and endColumn in the last
93        // line of the string. First we calculate the position (in the string) of endColumn in the
94        // last line of the string
95        int endPosition;
96
97        // The String that Emulator.writeToStream produces has a newline at the end, and so ends with an
98        // empty line - we ignore that.
99        int numberOfLinesInString = decoder.linePositions().size() - 1;
100        if (numberOfLinesInString > 0 && endColumn > -1 )
101        {
102            endPosition = decoder.linePositions().at(numberOfLinesInString - 1) + endColumn;
103        }
104        else
105        {
106            endPosition = string.size();
107        }
108
109        // So now we can log for m_regExp in the string between startColumn and endPosition
110        int matchStart;
111        if (m_forwards)
112        {
113            matchStart = string.indexOf(m_regExp, startColumn);
114            if (matchStart >= endPosition)
115                matchStart = -1;
116        }
117        else
118        {
119            matchStart = string.lastIndexOf(m_regExp, endPosition - 1);
120            if (matchStart < startColumn)
121                matchStart = -1;
122        }
123
124        if (matchStart > -1)
125        {
126#if QT_VERSION >= 0x060000
127            auto match = m_regExp.match(string);
128            int matchEnd = matchStart + match.capturedLength() - 1;
129#else
130            int matchEnd = matchStart + m_regExp.matchedLength() - 1;
131#endif
132            qDebug() << "Found in string from" << matchStart << "to" << matchEnd;
133
134            // Translate startPos and endPos to startColum, startLine, endColumn and endLine in history.
135            int startLineNumberInString = findLineNumberInString(decoder.linePositions(), matchStart);
136            m_foundStartColumn = matchStart - decoder.linePositions().at(startLineNumberInString);
137            m_foundStartLine = startLineNumberInString + startLine + linesRead;
138
139            int endLineNumberInString = findLineNumberInString(decoder.linePositions(), matchEnd);
140            m_foundEndColumn = matchEnd - decoder.linePositions().at(endLineNumberInString);
141            m_foundEndLine = endLineNumberInString + startLine + linesRead;
142
143            qDebug() << "m_foundStartColumn" << m_foundStartColumn
144                    << "m_foundStartLine" << m_foundEndLine
145                    << "m_foundEndColumn" << m_foundEndColumn
146                    << "m_foundEndLine" << m_foundEndLine;
147
148            return true;
149        }
150
151
152        linesRead += blockSize;
153    }
154
155    qDebug() << "Not found";
156    return false;
157}
158
159
160int HistorySearch::findLineNumberInString(QList<int> linePositions, int position) {
161    int lineNum = 0;
162    while (lineNum + 1 < linePositions.size() && linePositions[lineNum + 1] <= position)
163        lineNum++;
164
165    return lineNum;
166}
Note: See TracBrowser for help on using the repository browser.