int QzRegExp::indexIn(const QString &str, int offset) const { QzRegExp* that = const_cast<QzRegExp*>(this); QRegularExpressionMatch m = match(str, offset); if (!m.hasMatch()) { that->m_matchedLength = -1; that->m_capturedTexts.clear(); return -1; } that->m_matchedLength = m.capturedLength(); that->m_capturedTexts = m.capturedTexts(); return m.capturedStart(); }
QStringList QgsFileUtils::extensionsFromFilter( const QString &filter ) { const QRegularExpression rx( QStringLiteral( "\\*\\.([a-zA-Z0-9]+)" ) ); QStringList extensions; QRegularExpressionMatchIterator matches = rx.globalMatch( filter ); while ( matches.hasNext() ) { const QRegularExpressionMatch match = matches.next(); if ( match.hasMatch() ) { QStringList newExtensions = match.capturedTexts(); newExtensions.pop_front(); // remove whole match extensions.append( newExtensions ); } } return extensions; }
QStringList readQmlTypes(const QString &filename) { QRegularExpression re("import QtQuick.tooling 1.2.*Module {\\s*dependencies:\\[([^\\]]*)\\](.*)}", QRegularExpression::DotMatchesEverythingOption); if (!QFileInfo(filename).exists()) { std::cerr << "Non existing file: " << filename.toStdString() << std::endl; return QStringList(); } QFile f(filename); if (!f.open(QFileDevice::ReadOnly)) { std::cerr << "Error in opening file " << filename.toStdString() << " : " << f.errorString().toStdString() << std::endl; return QStringList(); } QByteArray fileData = f.readAll(); QString data(fileData); QRegularExpressionMatch m = re.match(data); if (m.lastCapturedIndex() != 2) { std::cerr << "Malformed file: " << filename.toStdString() << std::endl; return QStringList(); } return m.capturedTexts(); }
/*! \internal */ void QWebSocketPrivate::processHandshake(QTcpSocket *pSocket) { Q_Q(QWebSocket); if (Q_UNLIKELY(!pSocket)) return; bool ok = false; QString errorDescription; const QString regExpStatusLine(QStringLiteral("^(HTTP/[0-9]+\\.[0-9]+)\\s([0-9]+)\\s(.*)")); const QRegularExpression regExp(regExpStatusLine); const QString statusLine = readLine(pSocket); QString httpProtocol; int httpStatusCode; QString httpStatusMessage; const QRegularExpressionMatch match = regExp.match(statusLine); if (Q_LIKELY(match.hasMatch())) { QStringList tokens = match.capturedTexts(); tokens.removeFirst(); //remove the search string if (tokens.length() == 3) { httpProtocol = tokens[0]; httpStatusCode = tokens[1].toInt(); httpStatusMessage = tokens[2].trimmed(); ok = true; } } if (Q_UNLIKELY(!ok)) { errorDescription = QWebSocket::tr("Invalid statusline in response: %1.").arg(statusLine); } else { QString headerLine = readLine(pSocket); QMap<QString, QString> headers; while (!headerLine.isEmpty()) { const QStringList headerField = headerLine.split(QStringLiteral(": "), QString::SkipEmptyParts); if (headerField.size() == 2) { headers.insertMulti(headerField[0].toLower(), headerField[1]); } headerLine = readLine(pSocket); } const QString acceptKey = headers.value(QStringLiteral("sec-websocket-accept"), QString()); const QString upgrade = headers.value(QStringLiteral("upgrade"), QString()); const QString connection = headers.value(QStringLiteral("connection"), QString()); // unused for the moment // const QString extensions = headers.value(QStringLiteral("sec-websocket-extensions"), // QString()); // const QString protocol = headers.value(QStringLiteral("sec-websocket-protocol"), // QString()); const QString version = headers.value(QStringLiteral("sec-websocket-version"), QString()); if (Q_LIKELY(httpStatusCode == 101)) { //HTTP/x.y 101 Switching Protocols bool conversionOk = false; const float version = httpProtocol.midRef(5).toFloat(&conversionOk); //TODO: do not check the httpStatusText right now ok = !(acceptKey.isEmpty() || (!conversionOk || (version < 1.1f)) || (upgrade.toLower() != QStringLiteral("websocket")) || (connection.toLower() != QStringLiteral("upgrade"))); if (ok) { const QString accept = calculateAcceptKey(m_key); ok = (accept == acceptKey); if (!ok) errorDescription = QWebSocket::tr("Accept-Key received from server %1 does not match the client key %2.") .arg(acceptKey).arg(accept); } else { errorDescription = QWebSocket::tr("QWebSocketPrivate::processHandshake: Invalid statusline in response: %1.") .arg(statusLine); } } else if (httpStatusCode == 400) { //HTTP/1.1 400 Bad Request if (!version.isEmpty()) { const QStringList versions = version.split(QStringLiteral(", "), QString::SkipEmptyParts); if (!versions.contains(QString::number(QWebSocketProtocol::currentVersion()))) { //if needed to switch protocol version, then we are finished here //because we cannot handle other protocols than the RFC one (v13) errorDescription = QWebSocket::tr("Handshake: Server requests a version that we don't support: %1.") .arg(versions.join(QStringLiteral(", "))); ok = false; } else { //we tried v13, but something different went wrong errorDescription = QWebSocket::tr("QWebSocketPrivate::processHandshake: Unknown error condition encountered. Aborting connection."); ok = false; } } } else { errorDescription = QWebSocket::tr("QWebSocketPrivate::processHandshake: Unhandled http status code: %1 (%2).") .arg(httpStatusCode).arg(httpStatusMessage); ok = false; } if (!ok) { setErrorString(errorDescription); Q_EMIT q->error(QAbstractSocket::ConnectionRefusedError); } else { //handshake succeeded setSocketState(QAbstractSocket::ConnectedState); Q_EMIT q->connected(); } } }
QStringList CSDDataBase::decode_pinyin(const QString& data) { QStringList words = data.toLower().split(' '); QStringList accented; QString temp = ""; for (QString& pinyin : words) { for (QChar& c : pinyin) { if (c >= 'a' && c <= 'z') temp += c; else if (c == ':') { if (temp[temp.length() - 1] == 'u') { temp = temp.left(temp.length() - 1) + "ü"; } } else //handle tone { if (c >= '0' && c <= '4') // 5 is the null tone (NO MARK) { int tone = c.digitValue(); if (tone != 0) { QRegularExpression regex("[aeiouvü]+"); QRegularExpressionMatch match = regex.match(temp); if (!match.hasMatch()) temp += c; else if (match.capturedTexts()[0].length() == 1) { int char_index = m_tones[0].first.indexOf(match.capturedTexts()[0]); temp = temp.mid(0, match.capturedStart()) + m_tones[tone].first[char_index] + temp.mid(match.capturedStart() + match.capturedLength()); } else { if (temp.contains("a")) temp = temp.replace("a", QString(m_tones[tone].first[0])); else if(temp.contains("e")) temp = temp.replace("e", QString(m_tones[tone].first[1])); else if (temp.contains("i")) temp = temp.replace("i", QString(m_tones[tone].first[2])); else if (temp.contains("o")) temp = temp.replace("o", QString(m_tones[tone].first[3])); else if (temp.contains("ui")) temp = temp.replace("i", QString(m_tones[tone].first[4])); else if (temp.contains("iu")) temp = temp.replace("u", QString(m_tones[tone].first[5])); else temp += "!"; } } } } } accented.append(temp); temp = ""; } return accented; }
void playground(QApplication& a) { std::vector<int> l1 = { 0,1,3,6,9,11 }; auto fromItem = std::upper_bound(l1.begin(), l1.end(), 2 , [](const auto &a, const auto &b) { return a > b; }); return; TextColorize::List _coloredTextParts; auto match = [&_coloredTextParts](const QString& text, Part::List& parts) { QRegularExpression::PatternOptions options; /*if (tc.caseSensitive == false) options |= QRegularExpression::PatternOption::CaseInsensitiveOption; */ QString rePattern; foreach(TextColorize ctp, _coloredTextParts) { QString pattern; if (ctp.caseSensitive == false) pattern += "(?i)"; pattern += ctp.text; pattern.prepend("(").append(")"); if(ctp.wordOnly) pattern.prepend("(?:^|[^\\w])").append("(?:[^\\w]|$)"); rePattern += "|" + pattern; } rePattern = rePattern.mid(1); QRegularExpression re(rePattern, options); QRegularExpressionMatchIterator it = re.globalMatch(text); int nonMatchedStart = 0; bool hasMatches = it.hasNext(); while (it.hasNext()) { QRegularExpressionMatch match = it.next(); int matchedGroup = match.lastCapturedIndex(); while (match.capturedTexts().at(matchedGroup).length() && --matchedGroup); qDebug() << match.capturedTexts() << " - " << match.capturedView() << " - " << match.hasPartialMatch() << " - " << matchedGroup; int nonMatechedEnd = match.capturedStart(0); int nonMatchedLength = nonMatechedEnd - nonMatchedStart; //auto& ct = _coloredTextParts[match.lastCapturedIndex() - 1]; auto& ct = _coloredTextParts[0]; if (nonMatchedLength) ;// parts.push_back({ text.mid(nonMatchedStart, nonMatchedLength), nullptr }); parts.push_back({ text.mid(match.capturedStart(0), match.capturedLength(0)), match.lastCapturedIndex() }); nonMatchedStart = match.capturedEnd(0); } if (nonMatchedStart < text.length()) parts.push_back({ text.mid(nonMatchedStart), false }); return hasMatches; };