From d48a479a39b4c330e9437414520666279c9f5d71 Mon Sep 17 00:00:00 2001 From: noavarice Date: Sat, 8 Jul 2017 17:20:15 +0300 Subject: [PATCH] refactor: applyMarkdown() function logic is more rigorous Brief list of changes: - changed way of getting count of formatting symbols for captured string: patternSignsCount() function replaced with constant index which points to regex group that captures formatting symbols. Regexes updated accordingly; - updated docs for applyMarkdown() function; - applyMarkdown() function refactored: * changed var names; * logic is more strict: get captured string -> crop formatting symbols if necessary -> wrap captured string into HTML and replace in result. --- src/chatlog/textformatter.cpp | 65 ++++++++++++++--------------------- 1 file changed, 26 insertions(+), 39 deletions(-) diff --git a/src/chatlog/textformatter.cpp b/src/chatlog/textformatter.cpp index 0a8b7df8a..48a2aa0a8 100644 --- a/src/chatlog/textformatter.cpp +++ b/src/chatlog/textformatter.cpp @@ -22,8 +22,14 @@ #include // clang-format off + +/* Easy way to get count of markdown symbols - through length of substring, captured by regex group. + * If you suppose to change regexes, assure that this const points to right group. + */ +static constexpr uint8_t MARKDOWN_SYMBOLS_GROUP_INDEX = 1; + static const QString SINGLE_SIGN_PATTERN = QStringLiteral("(?<=^|\\s|\\n)" - "[%1]" + "([%1])" "(?!\\s)" "[^%1\\n]+" "(? pair : REGEX_TO_WRAPPER) { - QRegularExpressionMatchIterator matchesIterator = pair.first.globalMatch(result); - int insertedTagSymbolsCount = 0; - - while (matchesIterator.hasNext()) { - QRegularExpressionMatch match = matchesIterator.next(); - if (isTagIntersection(match.captured())) { + for (const QPair& pair : REGEX_TO_WRAPPER) { + QRegularExpressionMatchIterator iter = pair.first.globalMatch(result); + int offset = 0; + while (iter.hasNext()) { + const QRegularExpressionMatch match = iter.next(); + QString captured = match.captured(); + if (isTagIntersection(captured)) { continue; } - int capturedStart = match.capturedStart() + insertedTagSymbolsCount; - int capturedLength = match.capturedLength(); + const int length = match.capturedLength(); + if (!showFormattingSymbols) { + const int choppingSignsCount = match.captured(MARKDOWN_SYMBOLS_GROUP_INDEX).length(); + captured = captured.mid(choppingSignsCount, length - choppingSignsCount * 2); + } - QString stylingText = result.mid(capturedStart, capturedLength); - int choppingSignsCount = showFormattingSymbols ? 0 : patternSignsCount(stylingText); - int textStart = capturedStart + choppingSignsCount; - int textLength = capturedLength - 2 * choppingSignsCount; - - QString styledText = pair.second.arg(result.mid(textStart, textLength)); - - result.replace(capturedStart, capturedLength, styledText); - // Subtracting length of "%1" - insertedTagSymbolsCount += pair.second.length() - 2 - 2 * choppingSignsCount; + const QString wrappedText = pair.second.arg(captured); + const int startPos = match.capturedStart() + offset; + result.replace(startPos, length, wrappedText); + offset += wrappedText.length() - length; } } return result;