Repository: xbmc/xbmc
File: xbmc/guilib/GUITextLayout.cpp (C++)

Code before:
  for (const CGUIString& line : lines)
  {
    if (m_lines.size() >= nMaxLines)
      return;

    if (line.m_text.empty()) // Blank line
    {
      m_lines.emplace_back(line);
      continue;
    }

    std::vector<character_t> tmp;
    tmp.reserve(line.m_text.size());
    auto widthOf = [&](const std::vector<character_t>::const_iterator start,
                       const std::vector<character_t>::const_iterator end) -> float
    {
      if (start == end)
        return 0.0f;
      tmp.assign(start, end);
      return m_font->GetTextWidth(tmp);
    };

    auto skipLeadingSpaces = [&](vecText::const_iterator& it)
    {
      while (it != line.m_text.end() && CanWrapAtLetter(*it))
        ++it;
    };

    auto current = line.m_text.begin();
    skipLeadingSpaces(current);

    float currentWidth = 0.0f;
    std::vector<character_t>::const_iterator currentStart = current;
    // track end without trailing spaces
    std::vector<character_t>::const_iterator lastNonSpaceInLine = currentStart;

    while (current != line.m_text.end())
    {
      // Find next candidate wrap position
      std::vector<character_t>::const_iterator wordEnd = current;
      while (wordEnd != line.m_text.end() && !CanWrapAtLetter(*wordEnd))
        ++wordEnd;

      const bool hasSpace = (wordEnd != line.m_text.end());
      const float wordWidth = widthOf(current, wordEnd);
      const float spaceWidth = hasSpace ? widthOf(wordEnd, wordEnd + 1) : 0.0f;

      // Try to include word + trailing space
      if (currentWidth + wordWidth + spaceWidth <= maxWidth)
      {
        currentWidth += wordWidth + spaceWidth;

Review comment (suggestion):
This loop could be replaced with an algorithm:
```suggestion
      it = std::find_if_not(it, line.m_text.end(),
                            std::bind_front(&CGUITextLayout::CanWrapAtLetter, this));
```

Code after:
  // lines can be split by last space char or if there are no spaces by character
  // to mimics the line behavior of word processors
  for (const CGUIString& line : lines)
  {
    if (m_lines.size() >= nMaxLines)
      return;

    if (line.m_text.empty()) // Blank line
    {
      m_lines.emplace_back(line);
      continue;
    }

    std::vector<character_t> tmp;
    tmp.reserve(line.m_text.size());
    auto widthOf = [&](const std::vector<character_t>::const_iterator start,
                       const std::vector<character_t>::const_iterator end) -> float
    {
      if (start == end)
        return 0.0f;
      tmp.assign(start, end);
      return m_font->GetTextWidth(tmp);
    };

    auto skipLeadingSpaces = [&](vecText::const_iterator& it)
    {
      it = std::find_if_not(it, line.m_text.end(),
                            std::bind_front(&CGUITextLayout::CanWrapAtLetter, this));
    };

    auto current = line.m_text.begin();
    skipLeadingSpaces(current);

    float currentWidth = 0.0f;
    std::vector<character_t>::const_iterator currentStart = current;
    // track end without trailing spaces
    std::vector<character_t>::const_iterator lastNonSpaceInLine = currentStart;

    while (current != line.m_text.end())
    {
      // Find next candidate wrap position
      auto wordEnd = std::find_if(current, line.m_text.end(),
                                  std::bind_front(&CGUITextLayout::CanWrapAtLetter, this));
      const bool hasSpace = (wordEnd != line.m_text.end());
      const float wordWidth = widthOf(current, wordEnd);
      const float spaceWidth = hasSpace ? widthOf(wordEnd, wordEnd + 1) : 0.0f;

      // Try to include word + trailing space
      if (currentWidth + wordWidth + spaceWidth <= maxWidth)
      {
        currentWidth += wordWidth + spaceWidth;