Guest User

Untitled

a guest
Feb 18th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. struct Bound
  2. {
  3. int position;
  4. bool isBegining;
  5. };
  6.  
  7. // bounds должен быть отсортирован по position
  8. QString getFormatedText(const QString & text, const QVector<Bound> & bounds)
  9. {
  10. UNI_CHECK_RETURN(!bounds.isEmpty() && !text.isEmpty(), text);
  11.  
  12. const QString startSelectionText = QStringLiteral("<b>");
  13. const QString endSelectionText = QStringLiteral("</b>");
  14. const int selectionSize = startSelectionText.size() + endSelectionText.size();
  15.  
  16. QString result;
  17. result.reserve(text.size() + bounds.size() / 2 * selectionSize);
  18.  
  19. int position = 0;
  20. int boundIndex = 0;
  21.  
  22. for (QChar c : text)
  23. {
  24. // Пробуем вставить начало выделения
  25. Bound * bound = bounds.value(boundIndex);
  26. if (bound && bound->isBegining && bound->position == position)
  27. {
  28. result.append(startSelectionText);
  29. boundIndex++;
  30. }
  31.  
  32. // Вставляем символ исходной строки
  33. if (c.isLowSurrogate())
  34. {
  35. result.append(c);
  36. continue;
  37. }
  38. result.append(c);
  39.  
  40. // Пробуем вставить конец выделения
  41. bound = bounds.value(boundIndex);
  42. if (bound && !bound->isBegining && bound->position == position)
  43. {
  44. result.append(endSelectionText);
  45. boundIndex++;
  46. }
  47.  
  48. position++;
  49. }
  50.  
  51. return result;
  52. }
Add Comment
Please, Sign In to add comment