Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- struct Bound
- {
- int position;
- bool isBegining;
- };
- // bounds должен быть отсортирован по position
- QString getFormatedText(const QString & text, const QVector<Bound> & bounds)
- {
- UNI_CHECK_RETURN(!bounds.isEmpty() && !text.isEmpty(), text);
- const QString startSelectionText = QStringLiteral("<b>");
- const QString endSelectionText = QStringLiteral("</b>");
- const int selectionSize = startSelectionText.size() + endSelectionText.size();
- QString result;
- result.reserve(text.size() + bounds.size() / 2 * selectionSize);
- int position = 0;
- int boundIndex = 0;
- for (QChar c : text)
- {
- // Пробуем вставить начало выделения
- Bound * bound = bounds.value(boundIndex);
- if (bound && bound->isBegining && bound->position == position)
- {
- result.append(startSelectionText);
- boundIndex++;
- }
- // Вставляем символ исходной строки
- if (c.isLowSurrogate())
- {
- result.append(c);
- continue;
- }
- result.append(c);
- // Пробуем вставить конец выделения
- bound = bounds.value(boundIndex);
- if (bound && !bound->isBegining && bound->position == position)
- {
- result.append(endSelectionText);
- boundIndex++;
- }
- position++;
- }
- return result;
- }
Add Comment
Please, Sign In to add comment