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

Code before:
      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;
        lastNonSpaceInLine = wordEnd; // exclude trailing space
        current = wordEnd;
        if (hasSpace)
          ++current;
        continue;
      }

      // Try to include word without trailing space
      if (currentWidth + wordWidth <= maxWidth)
      {
        m_lines.emplace_back(currentStart, wordEnd, false);
        if (m_lines.size() >= nMaxLines)
          return;

        current = hasSpace ? (wordEnd + 1) : wordEnd;
        skipLeadingSpaces(current);

Review comment (suggestion):
This loop as well:

```suggestion
      const auto wordEnd = std::find_if(current, line.m_text.end(),
                                        std::bind_front(&CGUITextLayout::CanWrapAtLetter, this));
```

Code after:
                       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;
        lastNonSpaceInLine = wordEnd; // exclude trailing space
        current = wordEnd;
        if (hasSpace)
          ++current;
        continue;
      }

      // Try to include word without trailing space
      if (currentWidth + wordWidth <= maxWidth)
      {
        m_lines.emplace_back(currentStart, wordEnd, false);
        if (m_lines.size() >= nMaxLines)
          return;

        current = wordEnd;
        skipLeadingSpaces(current);