Skip to content

fix(generator): preserve comment introducers across embedded newlines - #16332

Open
colinmoy wants to merge 2 commits into
googleapis:mainfrom
colinmoy:fix-format-comment-block
Open

fix(generator): preserve comment introducers across embedded newlines#16332
colinmoy wants to merge 2 commits into
googleapis:mainfrom
colinmoy:fix-format-comment-block

Conversation

@colinmoy

@colinmoy colinmoy commented Aug 7, 2026

Copy link
Copy Markdown
Contributor

No description provided.

@colinmoy
colinmoy requested a review from a team as a code owner August 7, 2026 21:00

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request updates FormatCommentBlock in codegen_utils.cc to correctly handle multi-line comments and paragraph splitting, and adds corresponding unit tests. The review feedback recommends optimizing memory usage by using absl::string_view instead of std::string to avoid unnecessary heap allocations, and refactoring the final formatting loop to eliminate an unnecessary else block, aligning with the repository's style guide.

Comment thread generator/internal/codegen_utils.cc Outdated
Comment on lines 334 to 357
std::vector<std::string> lines;
std::size_t start_pos = 0;
while (start_pos != std::string::npos) {
std::size_t boundary = start_pos + comment_width;
std::size_t end_pos = boundary;
if (boundary < comment.length()) {
// Look backward from the boundary for the last word
end_pos = comment.rfind(' ', boundary);
// If there is only one word, find and use its boundary
if (end_pos == std::string::npos || end_pos < start_pos) {
end_pos = comment.find(' ', boundary);
std::vector<absl::string_view> paragraphs = absl::StrSplit(comment, '\n');
for (auto const& paragraph : paragraphs) {
if (paragraph.empty()) {
lines.emplace_back("");
continue;
}
std::size_t start_pos = 0;
while (start_pos != absl::string_view::npos) {
std::size_t boundary = start_pos + comment_width;
std::size_t end_pos = boundary;
if (boundary < paragraph.length()) {
// Look backward from the boundary for the last word
end_pos = paragraph.rfind(' ', boundary);
// If there is only one word, find and use its boundary
if (end_pos == absl::string_view::npos || end_pos < start_pos) {
end_pos = paragraph.find(' ', boundary);
}
}
lines.push_back(
std::string(paragraph.substr(start_pos, end_pos - start_pos)));
start_pos = paragraph.find_first_not_of(' ', end_pos);
}
lines.push_back(comment.substr(start_pos, end_pos - start_pos));
start_pos = comment.find_first_not_of(' ', end_pos);
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Using std::vector<std::string> causes unnecessary memory allocations and copies for each line of the comment block. Since the comment parameter is passed as a const std::string& and its lifetime outlives this function, we can use std::vector<absl::string_view> instead to avoid these heap allocations.

  std::vector<absl::string_view> lines;
  std::vector<absl::string_view> paragraphs = absl::StrSplit(comment, '\n');
  for (auto const& paragraph : paragraphs) {
    if (paragraph.empty()) {
      lines.emplace_back();
      continue;
    }
    std::size_t start_pos = 0;
    while (start_pos != absl::string_view::npos) {
      std::size_t boundary = start_pos + comment_width;
      std::size_t end_pos = boundary;
      if (boundary < paragraph.length()) {
        // Look backward from the boundary for the last word
        end_pos = paragraph.rfind(' ', boundary);
        // If there is only one word, find and use its boundary
        if (end_pos == absl::string_view::npos || end_pos < start_pos) {
          end_pos = paragraph.find(' ', boundary);
        }
      }
      lines.push_back(paragraph.substr(start_pos, end_pos - start_pos));
      start_pos = paragraph.find_first_not_of(' ', end_pos);
    }
  }
References
  1. Scrutinize copies of non-fundamental C++ types. Is it necessary to copy the data? (link)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use std::string_view instead of absl::string_view

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Comment thread generator/internal/codegen_utils.cc
@colinmoy
colinmoy force-pushed the fix-format-comment-block branch from f41554a to 2235e98 Compare August 7, 2026 22:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants