Repository: llvm/llvm-project
File: lldb/include/lldb/Utility/AnsiTerminal.h (C/C++)

Code before:
  // result.size(), so we track it separately.
  size_t result_visible_length = 0;

  // The loop below makes many adjustments, and we never know which will be the
  // last. This tracks whether the most recent adjustment put us at a word
  // boundary and is checked after the main loop.
  bool at_word_boundary = false;

  // Trim the string to the given visible length.
  while (!str.empty() && result_visible_length < visible_length) {
    auto [left, escape, right] = FindNextAnsiSequence(str);
    str = right;

    // We know that left does not include ANSI codes. Compute its visible length
    // and if it fits, append it together with the invisible escape code.
    size_t column_width = llvm::sys::locale::columnWidth(left);
    if (result_visible_length + column_width <= visible_length) {
      result.append(left).append(escape);
      result_visible_length += column_width;
      at_word_boundary = right.empty() || std::isspace(right[0]);

      continue;
    }

    // The string might contain unicode which means it's not safe to truncate.
    // Repeatedly trim the string until it its valid unicode and fits.
    llvm::StringRef trimmed = left;

    // A word break can happen at the character we trim to, or the one we
    // trimmed before that (we are going backwards, so before in the loop is
    // after in the string).

    // A word break can happen at the point we trim, or just beyond that point.
    // In other words: at the current back of trimmed, or what was the back last
    // time around. following_char records the character popped in the previous
    // loop iteration.
    std::optional<char> following_char = std::nullopt;
    while (!trimmed.empty()) {
      int trimmed_width = llvm::sys::locale::columnWidth(trimmed);
      if (
          // If we have a partial Unicode character, keep trimming.
          trimmed_width !=
              llvm::sys::unicode::ColumnWidthErrors::ErrorInvalidUTF8 &&
          // If the trimmed string fits in the column limit, stop trimming.
          (result_visible_length + static_cast<size_t>(trimmed_width) <=
           visible_length)) {
        result.append(trimmed);
        result_visible_length += trimmed_width;
        at_word_boundary = std::isspace(trimmed.back()) ||
                           (following_char && std::isspace(*following_char));


Review comment (suggestion):
```suggestion
    // Repeatedly trim the string until it is valid unicode and fits.
```

Code after:
  size_t result_visible_length = 0;

  // The loop below makes many adjustments, and we never know which will be the
  // last. This tracks whether the most recent adjustment put us at a word
  // boundary and is checked after the main loop.
  bool at_word_boundary = false;

  // Trim the string to the given visible length.
  while (!str.empty()) {
    auto [left, escape, right] = FindNextAnsiSequence(str);
    str = right;

    // We know that left does not include ANSI codes. Compute its visible length
    // and if it fits, append it together with the invisible escape code.
    size_t column_width = llvm::sys::locale::columnWidth(left);
    if (result_visible_length + column_width <= visible_length) {
      result.append(left).append(escape);
      result_visible_length += column_width;
      at_word_boundary = right.empty() || std::isspace(right[0]);

      continue;
    }

    // The string might contain unicode which means it's not safe to truncate.
    // Repeatedly trim the string until it is valid unicode and fits.
    llvm::StringRef trimmed = left;

    // A word break can happen at the character we trim to, or the one we
    // trimmed before that (we are going backwards, so before in the loop is
    // after in the string).

    // A word break can happen at the point we trim, or just beyond that point.
    // In other words: at the current back of trimmed, or what was the back last
    // time around. following_char records the character popped in the previous
    // loop iteration.
    std::optional<char> following_char = std::nullopt;
    while (!trimmed.empty()) {
      int trimmed_width = llvm::sys::locale::columnWidth(trimmed);
      if (
          // If we have a partial Unicode character, keep trimming.
          trimmed_width !=
              llvm::sys::unicode::ColumnWidthErrors::ErrorInvalidUTF8 &&
          // If the trimmed string fits in the column limit, stop trimming.
          (result_visible_length + static_cast<size_t>(trimmed_width) <=
           visible_length)) {
        result.append(trimmed);
        result_visible_length += trimmed_width;
        at_word_boundary = std::isspace(trimmed.back()) ||
                           (following_char && std::isspace(*following_char));

        break;