Commit 52eecb8e authored by Ahmad Samir's avatar Ahmad Samir
Browse files

Port QRegExp to QRegularExpression

In Speech class, macroExpander has been defined for the past 16 years, so I
assume it works. And QRegExp::search doesn't exist in Qt5 at all
https://doc.qt.io/archives/qt-4.8/qregexp-compat.html#search
parent 07999489
......@@ -26,7 +26,6 @@
#include <QFileDialog>
#include <QFontDatabase>
#include <QPainter>
#include <QRegExp>
#include <QStack>
#include <QUrl>
#include <QXmlSimpleReader>
......@@ -231,8 +230,7 @@ QStringList PhraseBook::toStringList()
bool PhraseBook::save(const QUrl &url)
{
QRegExp pattern(QStringLiteral("*.phrasebook"), Qt::CaseSensitive, QRegExp::Wildcard);
return save(url, pattern.exactMatch(url.fileName()));
return save(url, url.fileName().endsWith(QLatin1String(".phrasebook")));
}
......
......@@ -21,7 +21,6 @@
#include "speech.h"
#include <QHash>
#include <QRegExp>
#include <QStack>
#include <QTextCodec>
#include <QTextStream>
......@@ -40,123 +39,11 @@ Speech::~Speech()
QString Speech::prepareCommand(const QString &command, const QString &text,
const QString &filename, const QString &language)
{
#ifdef macroExpander
QHash<QChar, QString> map;
map[QLatin1Char('t')] = text;
map[QLatin1Char('f')] = filename;
map[QLatin1Char('l')] = language;
return KMacroExpander::expandMacrosShellQuote(command, map);
#else
QStack<bool> stack; // saved isdoublequote values during parsing of braces
bool issinglequote = false; // inside '...' ?
bool isdoublequote = false; // inside "..." ?
int noreplace = 0; // nested braces when within ${...}
QString escText = K3ShellProcess::quote(text);
// character sequences that change the state or need to be otherwise processed
QRegExp re_singlequote("('|%%|%t|%f|%l)");
QRegExp re_doublequote("(\"|\\\\|`|\\$\\(|\\$\\{|%%|%t|%f|%l)");
QRegExp re_noquote("('|\"|\\\\|`|\\$\\(|\\$\\{|\\(|\\{|\\)|\\}|%%|%t|%f|%l)");
// parse the command:
for (int i = re_noquote.search(command);
i != -1;
i = (issinglequote ? re_singlequote.search(command, i)
: isdoublequote ? re_doublequote.search(command, i)
: re_noquote.search(command, i))
)
// while there are character sequences that need to be processed
{
if ((command[i] == '(') || (command[i] == '{')) { // (...) or {...}
// assert(isdoublequote == false)
stack.push(isdoublequote);
if (noreplace > 0)
// count nested braces when within ${...}
noreplace++;
i++;
} else if (command[i] == '$') { // $(...) or ${...}
stack.push(isdoublequote);
isdoublequote = false;
if ((noreplace > 0) || (command[i + 1] == '{'))
// count nested braces when within ${...}
noreplace++;
i += 2;
} else if ((command[i] == ')') || (command[i] == '}')) {
// $(...) or (...) or ${...} or {...}
if (!stack.isEmpty())
isdoublequote = stack.pop();
else
qWarning("Parse error.");
if (noreplace > 0)
// count nested braces when within ${...}
noreplace--;
i++;
} else if (command[i] == '\'') {
issinglequote = !issinglequote;
i++;
} else if (command[i] == '"') {
isdoublequote = !isdoublequote;
i++;
} else if (command[i] == '\\')
i += 2;
else if (command[i] == '`') {
// Replace all `...` with safer $(...)
command.replace(i, 1, "$(");
QRegExp re_backticks("(`|\\\\`|\\\\\\\\|\\\\\\$)");
for (int i2 = re_backticks.search(command, i + 2);
i2 != -1;
i2 = re_backticks.search(command, i2)
) {
if (command[i2] == '`') {
command.replace(i2, 1, ")");
i2 = command.length(); // leave loop
} else {
// remove backslash and ignore following character
command.remove(i2, 1);
i2++;
}
}
// Leave i unchanged! We need to process "$("
} else if (noreplace > 0) { // do not replace macros within ${...}
if (issinglequote)
i += re_singlequote.matchedLength();
else if (isdoublequote)
i += re_doublequote.matchedLength();
else
i += re_noquote.matchedLength();
} else { // replace macro
QString match, v;
// get match
if (issinglequote)
match = re_singlequote.cap();
else if (isdoublequote)
match = re_doublequote.cap();
else
match = re_noquote.cap();
// substitute %variables
if (match == "%t")
v = escText;
else if (match == "%f")
v = filename;
else if (match == "%%")
v = "%";
else if (match == "%l")
v = language;
// %variable inside of a quote?
if (isdoublequote)
v = '"' + v + '"';
else if (issinglequote)
v = '\'' + v + '\'';
command.replace(i, match.length(), v);
i += v.length();
}
}
return command;
#endif
}
void Speech::speak(QString command, bool stdIn, const QString &text, const QString &language, int encoding, QTextCodec *codec)
......
......@@ -22,7 +22,7 @@
#include <QFile>
#include <QList>
#include <QRegExp>
#include <QRegularExpression>
#include <QStandardPaths>
#include <QTextStream>
......@@ -72,7 +72,7 @@ QString WordCompletion::makeCompletion(const QString &text)
d->lastText = text;
KCompletion::clear();
int border = text.lastIndexOf(QRegExp(QStringLiteral("\\W")));
int border = text.lastIndexOf(QRegularExpression(QStringLiteral("\\W")));
QString suffix = text.right(text.length() - border - 1).toLower();
QString prefix = text.left(border + 1);
......@@ -205,11 +205,11 @@ bool WordCompletion::setWordList(const QString &wordlist)
void WordCompletion::addSentence(const QString &sentence)
{
const QStringList words = sentence.split(QRegExp(QStringLiteral("\\W")));
const QStringList words = sentence.split(QRegularExpression(QStringLiteral("\\W")));
QStringList::ConstIterator it;
for (it = words.constBegin(); it != words.constEnd(); ++it) {
if (!(*it).contains(QRegExp(QStringLiteral("\\d|_")))) {
if (!(*it).contains(QRegularExpression(QStringLiteral("\\d|_")))) {
QString key = (*it).toLower();
if (d->map.contains(key))
d->map[key] += 1;
......
......@@ -24,7 +24,7 @@
#include <QDir>
#include <QList>
#include <QProgressDialog>
#include <QRegExp>
#include <QRegularExpression>
#include <QStandardPaths>
#include <QTextCodec>
#include <QTextStream>
......@@ -153,11 +153,11 @@ bool saveWordList(const WordMap &map, const QString &filename)
void addWords(WordMap &map, const QString &line)
{
const QStringList words = line.split(QRegExp(QStringLiteral("\\W")));
const QStringList words = line.split(QRegularExpression(QStringLiteral("\\W")));
QStringList::ConstIterator it;
for (it = words.constBegin(); it != words.constEnd(); ++it) {
if (!(*it).contains(QRegExp(QStringLiteral("\\d|_")))) {
if (!(*it).contains(QRegularExpression(QStringLiteral("\\d|_")))) {
QString key = (*it).toLower();
if (map.contains(key))
map[key] += 1;
......@@ -385,7 +385,7 @@ void loadAffFile(const QString &filename, AffMap &prefixes, AffMap &suffixes)
QTextStream stream(&afile);
while (!stream.atEnd()) {
QString s = stream.readLine();
QStringList fields = s.split(QRegExp(QStringLiteral("\\s")));
QStringList fields = s.split(QRegularExpression(QStringLiteral("\\s")));
if (fields.count() == 4) {
cross = (fields[2] == QLatin1String("Y"));
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment