/** * @brief prepend symbol on nth match * @param input input string * @param number_length the length of the number, will prepend symbol * if the length smaller than this number * @param nth the number of match * @param reg the pattern want to find * @param symbol symbol prepand before the number * @return the input after symbol prepend if match the condition; if the reg * found the match pattern but the target length less than number_length, do nothing; * if reg do not find any match, return "the number of match is out of range * or found no match in this string" */ QString prepend_symbol_on_nth_match(QString const &input, int number_length, int nth, const QRegularExpression ®, QChar symbol) { auto it = reg.globalMatch(input); QRegularExpressionMatch match; int count = 0; while (it.hasNext()){ match = it.next(); if(count == nth){ if(match.hasMatch()){ QString new_file_name = input; if(!match.captured(1).isEmpty()){ if(match.capturedLength(0) < number_length){ new_file_name.insert(match.capturedStart(0), QString(number_length - match.capturedLength(0), symbol)); } } return new_file_name; }else{ break; } } ++count; } return "the number of match is out of range or found no match in this string"; }
// The actual generation engine QString ShopTemplateManager::Generate(const Items &items) { QString temp = shopTemplate; { QRegularExpression expr("{(?<key>.+?)(?<options>(\\|(.+?))*?)}"); QRegularExpressionMatchIterator matcher = expr.globalMatch(shopTemplate); int offset = 0; while (matcher.hasNext()) { QRegularExpressionMatch match = matcher.next(); QString key = match.captured("key").toLower(); int startPos = offset + match.capturedStart(); int len = match.capturedLength(); QStringList optionsAndData = match.captured("options").split("|", QString::SkipEmptyParts); QHash<QString, QString> options; for (QString optionAndData : optionsAndData) { int split = optionAndData.indexOf(":"); if (split == -1) { options.insert(optionAndData.toLower(), ""); } else { QString option = optionAndData.left(split).toLower(); QString data = optionAndData.mid(split + 1); options.insert(option, data); } } QString replacement = FetchFromKey(key, items, &options); temp.replace(startPos, len, replacement); offset += replacement.length() - len; } } // Now clean up empty spoiler tags! int matches = -1; while (matches != 0){ QRegularExpression expr("\\[spoiler=\\\"(?>.*?\\\"\\])(?>\\s*?\\[\\/spoiler\\]\\n)"); QRegularExpressionMatchIterator matcher = expr.globalMatch(temp); int offset = 0; matches = 0; while (matcher.hasNext()) { QRegularExpressionMatch match = matcher.next(); int startPos = match.capturedStart() + offset; int length = match.capturedLength(); temp.remove(startPos, length); offset -= length; matches++; } } return temp; }
void Template::translate(ITemplateTranslationProvider &provider) { //This regex captures expressions of the form //<?= tr("This is a test") ?> and <?= tr("optional %1 parameters %2","bla","blu") ?> //The first capture group is the key (untranslated string), the second the optional list of parameters const QRegularExpression regexp = QRegularExpression("<\\?=\\s*tr\\(\"([^\"\\\\]*(?:\\\\.[^\"\\\\]*)*)\"((?:,\"[^\"\\\\]*(?:\\\\.[^\"\\\\]*)*\")*)\\s*\\)\\?>"); //This one is used to extract the parameters using global matching const QRegularExpression paramExp = QRegularExpression(",\"([^\"\\\\]*(?:\\\\.[^\"\\\\]*)*)\""); int offset = 0; QRegularExpressionMatch match; do { match = regexp.match(*this,offset); if(match.hasMatch()) { int start = match.capturedStart(0); int len = match.capturedLength(0); QString key = match.captured(1); //replace escaped double and single quotes key.replace("\\\"","\""); key.replace("\\'", "'"); QString translation = provider.getTranslation(key); //find out if we have optional parameters if(match.capturedLength(2)>0) { QString params = match.captured(2); //extract each optional parameter QRegularExpressionMatchIterator it = paramExp.globalMatch(params); while(it.hasNext()) { QRegularExpressionMatch paramMatch = it.next(); QString param = paramMatch.captured(1); //replace escaped quotes param.replace("\\\"","\""); param.replace("\\'", "'"); //apply the param translation = translation.arg(param); } } this->replace(start,len,translation); offset = start+translation.length(); } }while(match.hasMatch()); }
// Handle the general case QString GumboInterface::fix_self_closing_tags(const QString &source) { QString newsource = source; QRegularExpression selfclosed("<\\s*([a-zA-Z]+)(\\s*[^>/]*)/\\s*>"); QRegularExpressionMatch match = selfclosed.match(newsource, 0); while (match.hasMatch()) { if (match.capturedStart() == -1) { break; } QString tag = match.captured(0); int sp = match.capturedStart(0); int n = match.capturedLength(0); QString name = match.captured(1); QString atts = match.captured(2);; atts = atts.trimmed(); if (!atts.isEmpty()) { atts = " " + atts; } int nsp = sp + n; if (!allowed_void_tags.contains(name)) { QString newtag = "<" + name + atts + "></" + name + ">"; newsource = newsource.replace(sp,n,newtag); nsp = sp + newtag.length(); } match = selfclosed.match(newsource, nsp); } return newsource; }
void SyntaxHighlighter::highlightBlock(const QString &text) { for (std::size_t i = 0; i < _keywords.size(); i++) { const KeywordRule &rule = _keywords.at(i); QRegularExpressionMatchIterator it = rule.rulePattern.globalMatch(text); while (it.hasNext()) { QRegularExpressionMatch match = it.next(); setFormat( match.capturedStart(), match.capturedLength(), rule.ruleTextFormat); } } }
void Highlighter::highlightBlock(const QString &text) { foreach (const HighlightingRule &rule, highlightingRules) { QRegularExpressionMatchIterator matchIterator = rule.pattern.globalMatch(text); while (matchIterator.hasNext()) { QRegularExpressionMatch match = matchIterator.next(); setFormat(match.capturedStart(), match.capturedLength(), rule.format); } }
QString StringUtils::htmlEncode( QString text, bool urlAsLinks, bool newlineAsBr) { QString s; QHash<int,int> linksIndexes; // start of link index -> length of link if (urlAsLinks) { QRegularExpressionMatchIterator it = linkRe.globalMatch(text); while (it.hasNext()) { QRegularExpressionMatch match = it.next(); linksIndexes.insert(match.capturedStart(0), match.capturedLength(0)); } } for (int i = 0; i < text.length(); ) { if (urlAsLinks && linksIndexes.contains(i)) { int l = linksIndexes.value(i); QString html = htmlEncode(text.mid(i, l), false), href = html; href.replace("\"", "%22"); s.append("<a href=\"").append(href).append("\">").append(html) .append("</a>"); i += l; } else { const QChar c = text.at(i); switch(c.toLatin1()) { case '<': s.append("<"); break; case '>': s.append(">"); break; case '&': s.append("&"); break; case '"': s.append("""); break; case '\'': s.append("'"); break; case '\n': if (newlineAsBr) s.append("<br/>\n"); else s.append(c); break; default: s.append(c); } ++i; } } return s; }
foreach (OnePartRule rule, onepartrules) { QRegularExpressionMatchIterator i = rule.pattern.globalMatch(text); while (i.hasNext()) { QRegularExpressionMatch match = i.next(); int length = match.capturedLength(); if (length == 0) continue; int start = match.capturedStart(); setFormat(start, length, rule.format); } }
bool check_links(QString text) { QRegularExpression link_regex("http://restbase.wikitolearn.org/en.wikitolearn.org/v1/media/math/render/svg"); QRegularExpressionMatch contain = link_regex.match(text); qDebug() << contain; QRegularExpression png_regex("http://pool.wikitolearn.org"); QRegularExpressionMatch png = png_regex.match(text); qDebug() << png; if(contain.capturedLength() > 0 && png.capturedLength() > 0) { // change the status to true , so that i can download dependencies math_svg = true; image_png = true; return true; } else if(contain.capturedLength() > 0) { // change the status to true , to download the dependencies math_svg = true; return true; } else if(png.capturedLength() > 0) { // change the status to true , to download the images image_png = true; return true; } else { return false; } }
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(); }
std::string GumboInterface::update_style_urls(const std::string &source) { QString result = QString::fromStdString(source); // Now parse the text once looking urls and replacing them where needed QRegularExpression reference( "(?:(?:src|background|background-image|list-style|list-style-image|border-image|border-image-source|content)\\s*:|@import)\\s*" "[^;\\}\\(\"']*" "(?:" "url\\([\"']?([^\\(\\)\"']*)[\"']?\\)" "|" "[\"']([^\\(\\)\"']*)[\"']" ")"); int start_index = 0; QRegularExpressionMatch mo = reference.match(result, start_index); do { for (int i = 1; i < reference.captureCount(); ++i) { if (mo.captured(i).trimmed().isEmpty()) { continue; } QString apath = Utility::URLDecodePath(mo.captured(i)); QString search_key = QDir::cleanPath(m_currentdir + FORWARD_SLASH + apath); QString new_href; if (m_sourceupdates.contains(search_key)) { new_href = m_sourceupdates.value(search_key); } if (!new_href.isEmpty()) { new_href = Utility::URLEncodePath(new_href); result.replace(mo.capturedStart(i), mo.capturedLength(i), new_href); } } start_index += mo.capturedLength(); mo = reference.match(result, start_index); } while (mo.hasMatch()); return result.toStdString(); }
bool HRProcessor::test(const Element &, const QString &block) { //! No atomic grouping in python so we simulate it here for performance. //! The regex only matches what would be in the atomic group - the HR. //! Then check if we are at end of block or if next char is a newline. QRegularExpressionMatch m = this->SEARCH_RE.match(block); if ( m.hasMatch() && ( m.capturedEnd() == block.size() || block.at(m.capturedStart()+m.capturedLength()) == '\n' ) ) { //! Save match object on class instance so we can use it later. this->match = m; return true; } return false; }
static QString replaceInString (const QRegularExpression &rx, QString string, T func) { QRegularExpressionMatchIterator it = rx.globalMatch (string); int offset = 0; while (it.hasNext ()) { QRegularExpressionMatch match = it.next (); QString replaceWith = func (match); int length = match.capturedLength (); int begin = match.capturedStart () + offset; string.replace (begin, length, replaceWith); offset += replaceWith.length () - length; } return string; }
bool QuickFindPattern::matchLine( const QString& line, QList<QuickFindMatch>& matches ) const { matches.clear(); if ( active_ ) { QRegularExpressionMatchIterator matchIterator = regexp_.globalMatch(line); while( matchIterator.hasNext() ) { QRegularExpressionMatch match = matchIterator.next(); matches << QuickFindMatch ( match.capturedStart(), match.capturedLength() ); } } return ( matches.count() > 0 ); }
QString Toolbox::incrementNumberInString(QString string) noexcept { QRegularExpression regex("([0-9]+)(?!.*[0-9]+)"); QRegularExpressionMatch match = regex.match(string); if (match.hasMatch()) { // string contains numbers -> increment last number bool ok = false; uint number = match.captured().toUInt(&ok); if (ok) { string.replace(match.capturedStart(), match.capturedLength(), QString::number(number + 1U)); return string; } } // fallback: just add a "1" at the end return string % "1"; }
void WebApplication::translateDocument(QString &data) { const QRegularExpression regex("QBT_TR\\((([^\\)]|\\)(?!QBT_TR))+)\\)QBT_TR\\[CONTEXT=([a-zA-Z_][a-zA-Z0-9_]*)\\]"); const bool isTranslationNeeded = !m_currentLocale.startsWith("en") || m_currentLocale.startsWith("en_AU") || m_currentLocale.startsWith("en_GB") || !m_translator.isEmpty(); int i = 0; bool found = true; while (i < data.size() && found) { QRegularExpressionMatch regexMatch; i = data.indexOf(regex, i, ®exMatch); if (i >= 0) { const QString sourceText = regexMatch.captured(1); const QString context = regexMatch.captured(3); QString translation = sourceText; if (isTranslationNeeded) { const QString loadedText = m_translator.translate(context.toUtf8().constData(), sourceText.toUtf8().constData(), nullptr, 1); if (!loadedText.isEmpty()) translation = loadedText; } // Use HTML code for quotes to prevent issues with JS translation.replace('\'', "'"); translation.replace('\"', """); data.replace(i, regexMatch.capturedLength(), translation); i += translation.length(); } else { found = false; // no more translatable strings } data.replace(QLatin1String("${LANG}"), m_currentLocale.left(2)); data.replace(QLatin1String("${VERSION}"), QBT_VERSION); } }
// Returns the largest index of all the Sigil CSS classes int CleanSource::MaxSigilCSSClassIndex(const QStringList &css_style_tags) { int max_class_index = 0; foreach(QString style_tag, css_style_tags) { QRegularExpression sigil_class(SIGIL_CLASS_NAME_REG); int main_index = 0; while (true) { QRegularExpressionMatch match = sigil_class.match(style_tag, main_index); main_index = match.capturedStart(); if (main_index == -1) { break; } main_index += match.capturedLength(); int class_index = match.captured(1).toInt(); if (class_index > max_class_index) { max_class_index = class_index; } } }
// neither svg nor math tags need a namespace prefix defined // especially as epub3 now includes them into the html5 spec // So we need to remove the svg prefix from the tags before // processing them with gumbo QString CleanSource::PreprocessSpecialCases(const QString &source) { QString newsource = source; // remove prefix from root tag and add unprefixed svg namespace to it QRegularExpression root_svg_tag_with_prefix("<\\s*svg\\s*:\\s*svg"); QString root_svg_embeddedNS = "<svg xmlns=\"http://www.w3.org/2000/svg\""; newsource.replace(root_svg_tag_with_prefix, root_svg_embeddedNS); // search for any prefixed svg namespace in that root tag and remove it QRegularExpression svg_nsprefix(SVG_NAMESPACE_PREFIX); QRegularExpressionMatch mo = svg_nsprefix.match(newsource); if (mo.hasMatch()) { newsource.replace(mo.capturedStart(1), mo.capturedLength(1), ""); } // now strip the prefix from all child starting tags QRegularExpression starting_child_svg_tag_with_prefix("<\\s*svg\\s*:"); QString starting_child_tag_no_prefix = "<"; newsource.replace(starting_child_svg_tag_with_prefix, starting_child_tag_no_prefix); // do the same for any child ending tags QRegularExpression ending_child_svg_tag_with_prefix("<\\s*/\\s*svg\\s*:"); QString ending_child_tag_no_prefix = "</"; newsource.replace(ending_child_svg_tag_with_prefix, ending_child_tag_no_prefix); return newsource; }
// Handle the specific problem of iframe being self-closed QString GumboInterface::fix_self_closing_tags(const QString &source) { QString newsource = source; QRegularExpression selfclosed("<\\s*iframe(\\s*[^>/]*)/\\s*>"); QRegularExpressionMatch match = selfclosed.match(newsource, 0); while (match.hasMatch()) { if (match.capturedStart() == -1) { break; } QString tag = match.captured(0); int sp = match.capturedStart(0); int n = match.capturedLength(0); QString atts = match.captured(1);; atts = atts.trimmed(); if (!atts.isEmpty()) { atts = " " + atts; } QString newtag = "<iframe" + atts + "></iframe>"; newsource = newsource.replace(sp,n,newtag); int nsp = sp + newtag.length(); match = selfclosed.match(newsource, nsp); } return newsource; }
void Query::buildG2Keywords( QString strPhrase ) { QStringList lPositive, lNegative; strPhrase = strPhrase.trimmed().replace( "_", " " ).normalized( QString::NormalizationForm_KC ).toLower().append( " " ); QStringList lWords; { QRegularExpression re( "(-?\\\".*\\\"|-?\\w+)\\W+", QRegularExpression::InvertedGreedinessOption ); int nPos = 0, nOldPos = 0; bool bHasDash = false; QRegularExpressionMatch oMatch; while ( ( oMatch = re.match( strPhrase, nPos ) ).hasMatch() ) { nPos = re.match( strPhrase, nPos ).capturedStart(); QString sWord = oMatch.captured( 1 ); if ( bHasDash && nPos - oMatch.capturedLength() - nOldPos == 0 && lWords.last().size() < 4 && sWord.size() < 4 ) { lWords.last().append( "-" ).append( sWord ); } else { lWords << sWord; } nOldPos = nPos; nPos += oMatch.capturedLength(); if ( strPhrase.mid( nPos - 1, 1 ) == "-" ) { bHasDash = true; } else { bHasDash = false; } } } lWords.removeDuplicates(); for ( QStringList::iterator itWord = lWords.begin(); itWord != lWords.end(); ) { if ( ( *itWord ).size() < 4 ) { itWord = lWords.erase( itWord ); } else { itWord++; } } QRegularExpression rx( "\\w+" ); foreach ( const QString& sWord, lWords ) { if ( sWord.at( 0 ) == '-' && sWord.at( 1 ) != '"' ) { // plain negative word m_sG2NegativeWords.append( sWord.mid( 1 ) ).append( "," ); lNegative.append( sWord.mid( 1 ) ); } else if ( sWord.at( 0 ) == '"' ) { // positive quoted phrase m_sG2PositiveWords.append( sWord ).append( "," ); // extract words int p = 0; QRegularExpressionMatch oMatch; while ( ( oMatch = rx.match( sWord, p ) ).hasMatch() ) { p = oMatch.capturedStart() + oMatch.capturedLength(); lPositive.append( oMatch.captured() ); } } else if ( sWord.at( 0 ) == '-' && sWord.at( 1 ) == '"' ) { // negative quoted phrase m_sG2NegativeWords.append( sWord ).append( "," ); // extract words int p = 0; QRegularExpressionMatch oMatch; while ( ( oMatch = rx.match( sWord, p ) ).hasMatch() ) { p = oMatch.capturedStart() + oMatch.capturedLength(); lNegative.append( oMatch.captured() ); } } else { // plain positive word m_sG2PositiveWords.append( sWord ).append( "," ); lPositive.append( sWord ); } } m_sG2PositiveWords.chop( 1 ); m_sG2NegativeWords.chop( 1 ); foreach( const QString& sWord, lNegative ) { lPositive.removeAll( sWord ); }
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; };
void SignalSyntax::highlightBlock(const QString& text) { QTextCharFormat pointFormat; pointFormat.setFontWeight(QFont::Normal); pointFormat.setForeground(QBrush(QColor(180,182,182))); pointFormat.setFont(CodeFont{}); QTextCharFormat ppointFormat; ppointFormat.setFontWeight(QFont::Normal); ppointFormat.setForeground(QBrush(QColor(232,117,15))); ppointFormat.setFont(CodeFont{}); QTextCharFormat commaFormat; commaFormat.setFontWeight(QFont::Normal); commaFormat.setForeground(QBrush(QColor(239,93,61))); commaFormat.setFont(CodeFont{}); QRegularExpression countReg("^[0-9]*|\\[[ 0-9]*\\]"); QRegularExpressionMatchIterator itr = countReg.globalMatch(text); while(itr.hasNext()) { QRegularExpressionMatch match = itr.next(); setFormat(match.capturedStart(), match.capturedLength(), ppointFormat); } QRegularExpression pointReg("[A-Za-z,\\.:() <>\\[\\]\\_$0-9]*"); itr = pointReg.globalMatch(text); while(itr.hasNext()) { QRegularExpressionMatch match = itr.next(); setFormat(match.capturedStart(), match.capturedLength(), pointFormat); } QRegularExpression ppointReg("::[A-Za-z0-9_]*:"); itr = ppointReg.globalMatch(text); while(itr.hasNext()) { QRegularExpressionMatch match = itr.next(); setFormat(match.capturedStart(), match.capturedLength(), ppointFormat); } QRegularExpression ppointReg4("[ ]+[A-Za-z0-9_]*:"); itr = ppointReg4.globalMatch(text); while(itr.hasNext()) { QRegularExpressionMatch match = itr.next(); setFormat(match.capturedStart(), match.capturedLength(), ppointFormat); } QRegularExpression commaReg("[\\,]*"); itr = commaReg.globalMatch(text); while(itr.hasNext()) { QRegularExpressionMatch match = itr.next(); setFormat(match.capturedStart(), match.capturedLength(), commaFormat); } QRegularExpression pointReg2("[:]*"); itr = pointReg2.globalMatch(text); while(itr.hasNext()) { QRegularExpressionMatch match = itr.next(); setFormat(match.capturedStart(), match.capturedLength(), pointFormat); } }
void GccParser::stdError(const QString &line) { QString lne = rightTrimmed(line); // Blacklist some lines to not handle them: if (lne.startsWith(QLatin1String("TeamBuilder ")) || lne.startsWith(QLatin1String("distcc["))) { IOutputParser::stdError(line); return; } // Handle misc issues: if (lne.startsWith(QLatin1String("ERROR:")) || lne == QLatin1String("* cpp failed")) { newTask(Task(Task::Error, lne /* description */, Utils::FileName() /* filename */, -1 /* linenumber */, Constants::TASK_CATEGORY_COMPILE)); return; } QRegularExpressionMatch match = m_regExpGccNames.match(lne); if (match.hasMatch()) { QString description = lne.mid(match.capturedLength()); Task::TaskType type = Task::Error; if (description.startsWith(QLatin1String("warning: "))) { type = Task::Warning; description = description.mid(9); } else if (description.startsWith(QLatin1String("fatal: "))) { description = description.mid(7); } Task task(type, description, Utils::FileName(), /* filename */ -1, /* line */ Constants::TASK_CATEGORY_COMPILE); newTask(task); return; } match = m_regExp.match(lne); if (match.hasMatch()) { Utils::FileName filename = Utils::FileName::fromUserInput(match.captured(1)); int lineno = match.captured(3).toInt(); Task::TaskType type = Task::Unknown; QString description = match.captured(8); if (match.captured(7) == QLatin1String("warning")) type = Task::Warning; else if (match.captured(7) == QLatin1String("error") || description.startsWith(QLatin1String("undefined reference to")) || description.startsWith(QLatin1String("multiple definition of"))) type = Task::Error; // Prepend "#warning" or "#error" if that triggered the match on (warning|error) // We want those to show how the warning was triggered if (match.captured(5).startsWith(QLatin1Char('#'))) description = match.captured(5) + description; Task task(type, description, filename, lineno, Constants::TASK_CATEGORY_COMPILE); newTask(task); return; } match = m_regExpIncluded.match(lne); if (match.hasMatch()) { newTask(Task(Task::Unknown, lne.trimmed() /* description */, Utils::FileName::fromUserInput(match.captured(1)) /* filename */, match.captured(3).toInt() /* linenumber */, Constants::TASK_CATEGORY_COMPILE)); return; } else if (lne.startsWith(QLatin1Char(' '))) { amendDescription(lne, true); return; } doFlush(); IOutputParser::stdError(line); }
void readInlineText(const QString &data) { QString line = data; // remove all but one space on the front { bool startedWithSpace = line.startsWith(' '); line = line.remove(QRegularExpression("^ *")); if (startedWithSpace) { line = " " + line; } } // inline code { QRegularExpression exp("`(.*)`"); QRegularExpressionMatch match = exp.match(line); while (match.hasMatch()) { codeSections.append(match.captured(1)); line.replace(match.capturedStart(), match.capturedLength(), QString("$${{%1}}$$").arg(codeSections.size() - 1)); match = exp.match(line); } } // link and image { QRegularExpression exp("(\\!?)\\[(.*)\\]\\((.*)\\)"); QRegularExpressionMatch match = exp.match(line); while (match.hasMatch()) { if (match.captured(1) == "!") { // FIXME this no work htmlSections.append(QString("<img src=\"%1\" alt=\"%2\"/>").arg(match.captured(3), match.captured(2))); } else { htmlSections.append(QString("<a href=\"%1\">%2</a>").arg(match.captured(3), match.captured(2))); } line.replace(match.capturedStart(), match.capturedLength(), QString("$$[[%1]]$$").arg(htmlSections.size() - 1)); match = exp.match(line); } } QTextCharFormat fmt; int numStarsOrUnderscores = 0; QChar last = 0; for (const QChar &c : line) { if (last != c) { // bold if (numStarsOrUnderscores == 2) { if (fmt.fontWeight() == QFont::Normal) { fmt.setFontWeight(QFont::Bold); } else { fmt.setFontWeight(QFont::Normal); } } // italic else if (numStarsOrUnderscores == 1) { fmt.setFontItalic(!fmt.fontItalic()); } numStarsOrUnderscores = 0; cursor.setCharFormat(fmt); } last = c; if (c == '*' || c == '_') { numStarsOrUnderscores++; } else { cursor.insertText(c); } } }
void MarkupHighlighter::highlightBlock(const QString &text) { if (FORMATTING.empty()){ qWarning() << "Not given any formatting, so not highlighting."; return; } if (text.isEmpty()) return; int start=0, end=0, highlightLength = 0; QRegularExpression re; QTextCharFormat textFormat; for (QString exp : TOKENS.keys()){ setCurrentBlockState(0); start = 0, end = 0; re = QRegularExpression(exp); if (!re.isValid()){ QString message = "Invalid regular expression \"" + re.pattern() + "\" :" + re.errorString(); qFatal("%s", message.toStdString().data()); return; } if (previousBlockState() != 1) start = re.match(text).capturedStart(); while (start >= 0){ QRegularExpressionMatch match = re.match(text, start); end = match.capturedEnd(); if (end == -1 || (end == start && end != 0)){ setCurrentBlockState(1); highlightLength = text.length(); } else { highlightLength = match.capturedLength(); } QTextCharFormat baseFormat = currentBlock().blockFormat().toCharFormat(); MarkupHighlighter::MarkupToken tok = TOKENS[exp]; if (!FORMATTING.contains(tok)){ qWarning() << "Could not find" << tok; break; } const StyleProxy *styleProxy = FORMATTING[tok]; textFormat = styleProxy->toFormat(baseFormat); setFormat(start, highlightLength, textFormat); qDebug() << "highlightBlock"; qDebug() << "Formatting" << "token" << tok << "with regex=" << exp << ", string=" << text.mid(start, highlightLength) << "\n" << " Bold?" << textFormat.font().bold() << "\n" << " Italic?" << textFormat.font().italic() << "\n" << " Size:" << textFormat.font().pointSize() << "\n" << " Background:" << textFormat.background().color(); start = re.match(text, end).capturedStart(); // This should not be 0 again. If it is, that means our search has // come up empty. if (start == 0) start = -1; } } }
void syntax_highlighter::highlightBlock(const QString &text) { m_current_block = text; for (const auto& rule : m_multi_rules) { // Search for all the matching strings QRegularExpressionMatchIterator iter = rule.expression.globalMatch(m_current_block); // Iterate through the matching strings while (iter.hasNext()) { // Apply formats to their respective found groups QRegularExpressionMatch match = iter.next(); for (int i = 0; i <= match.lastCapturedIndex(); i++) { setFormat(match.capturedStart(i), match.capturedLength(i), rule.formats[i]); } } } for (const auto& rule : m_rules) { for (const auto& expression : rule.expressions) { // Search for all the matching strings QRegularExpressionMatchIterator iter = expression.globalMatch(m_current_block); bool contains_group = expression.captureCount() > 0; // Iterate through the matching strings while (iter.hasNext()) { // Apply format to the matching string QRegularExpressionMatch match = iter.next(); if (contains_group) { setFormat(match.capturedStart(1), match.capturedLength(1), rule.format); } else { setFormat(match.capturedStart(), match.capturedLength(), rule.format); } } } } for (const auto& rule : m_comment_rules) { int comment_start = 0; // Current comment's start position in the text block int comment_end = 0; // Current comment's end position in the text block int comment_length = 0; // Current comment length // We assume we end outside a comment until we know better setCurrentBlockState(block_state::ended_outside_comment); // Search for the first comment in this block if we start outside or don't want to search for multiline comments if (!rule.multi_line || previousBlockState() != block_state::ended_inside_comment) { comment_start = m_current_block.indexOf(rule.start_expression); } // Set format for the rest of this block/line if (!rule.multi_line) { comment_length = m_current_block.length() - comment_start; setFormat(comment_start, comment_length, rule.format); break; } // Format all comments in this block (if they exist) while (comment_start >= 0) { // Search for end of comment in the remaining text QRegularExpressionMatch match = rule.end_expression.match(m_current_block, comment_start); comment_end = match.capturedStart(); match.captured(1); if (comment_end == -1) { // We end inside a comment and want to format the entire remaining text setCurrentBlockState(block_state::ended_inside_comment); comment_length = m_current_block.length() - comment_start; } else { // We found the end of the comment so we need to go another round comment_length = comment_end - comment_start + match.capturedLength(); } // Set format for this text segment setFormat(comment_start, comment_length, rule.format); // Search for the next comment comment_start = m_current_block.indexOf(rule.start_expression, comment_start + comment_length); } } }
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; }
// The actual generation engine QStringList ShopTemplateManager::Generate(const Items &items) { QStringList result; QString temp = shopTemplate; // Records extractor QList<ShopTemplateRecord> records; { QRegularExpression expr("{(?<key>.+?)(?<options>(\\|(.+?))*?)}"); QRegularExpressionMatchIterator matcher = expr.globalMatch(shopTemplate); int offset = 0; while (matcher.hasNext()) { QRegularExpressionMatch match = matcher.next(); QString key = match.captured("key").toLower(); int startPos = offset + match.capturedStart(); const int len = match.capturedLength(); QStringList optionsAndData = match.captured("options").split("|", QString::SkipEmptyParts); QHash<QString, QString> options; for (QString optionAndData : optionsAndData) { int split = optionAndData.indexOf(":"); if (split == -1) { options.insert(optionAndData.toLower(), ""); } else { QString option = optionAndData.left(split).toLower(); QString data = optionAndData.mid(split + 1); options.insert(option, data); } } temp.remove(startPos, len); ShopTemplateRecord record = {key, options, startPos}; records << record; offset -= len; } } const QString cleanTemplate = temp; // Injector { int indexOffset = 0; for (ShopTemplateRecord record : records){ QList<ShopTemplateSection> sections = FetchFromKey(record.key, items, &record.options); int templateOffset = record.templateIndex; while (!sections.isEmpty()) { ShopTemplateSection replacement = sections.takeFirst(); int itemStart = 0; int itemCount = replacement.items(); while (itemCount > 0) { QString text = replacement.generate(itemStart, itemCount); if (temp.size() + text.size() >= POE_MAX_CHAR_IN_POST) { itemCount--; continue; } temp.insert(templateOffset + indexOffset, text); templateOffset += text.size(); // If we ended up ignoring some items if (itemStart + itemCount < replacement.items()) { // We couldn't fit in all the items from last time into the chunk... result << temp; // New temp starts from the old templates end temp = cleanTemplate.mid(record.templateIndex); indexOffset = -record.templateIndex; templateOffset = 0; itemStart += itemCount; itemCount = replacement.items() - itemStart; } else { break; } } } indexOffset += (templateOffset - record.templateIndex); } if (!temp.isEmpty()) result << temp; } return result; }