Repository: carbon-language/carbon-lang
File: testing/file_test/file_test_base.cpp (C++)

Code before:
// Reformats `[[@LSP:` and `[[LSP-NOTIFY:` as an LSP call with headers. For
// notifications, `lsp_call_id` is null.
static auto ReplaceLspKeywordAt(std::string* content, size_t keyword_pos,
                                int* lsp_call_id, llvm::StringLiteral keyword,
                                llvm::StringLiteral method_or_id_label)
    -> ErrorOr<size_t> {
  auto method_or_id_start = keyword_pos + keyword.size();

  static constexpr llvm::StringLiteral LspEnd = "]]";
  auto keyword_end = content->find("]]", method_or_id_start);
  if (keyword_end == std::string::npos) {
    return ErrorBuilder() << "Missing `" << LspEnd << "` after `" << keyword
                          << "`";
  }

  auto method_or_id_end = content->find(":", method_or_id_start);
  auto extra_content_start = method_or_id_end + 1;
  if (method_or_id_end == std::string::npos || method_or_id_end > keyword_end) {
    method_or_id_end = keyword_end;
    extra_content_start = keyword_end;
  }
  auto method_or_id = content->substr(method_or_id_start,
                                      method_or_id_end - method_or_id_start);

  auto extra_content =
      content->substr(extra_content_start, keyword_end - extra_content_start);
  std::string extra_content_sep;
  if (!extra_content.empty()) {
    extra_content_sep = ",";
    if (!extra_content.starts_with("\n")) {
      extra_content_sep += " ";
    }
  }

  // Form the JSON.
  std::string json = R"({"jsonrpc": "2.0", )";
  if (lsp_call_id) {
    json += llvm::formatv(R"("id": "{0}", )", ++(*lsp_call_id));
  }
  json += llvm::formatv(R"("{0}": "{1}"{2}{3}})", method_or_id_label,
                        method_or_id, extra_content_sep, extra_content);

  // Add the Content-Length header. The `2` accounts for extra newlines.
  auto json_with_header =
      llvm::formatv("Content-Length: {0}\n\n{1}\n", json.size() + 2, json)
          .str();
  // Insert the content.
  content->replace(keyword_pos, keyword_end + 2 - keyword_pos,
                   json_with_header);
  return keyword_pos + json_with_header.size();
}

Review comment (suggestion):
```suggestion
  auto extra_content = content->slice(extra_content_start, keyword_end);
```

Code after:
// Reformats `[[@LSP:` and `[[LSP-NOTIFY:` as an LSP call with headers. For
// notifications, `lsp_call_id` is null.
static auto ReplaceLspKeywordAt(std::string* content, size_t keyword_pos,
                                int* lsp_call_id, llvm::StringLiteral keyword,
                                llvm::StringLiteral method_or_id_label)
    -> ErrorOr<size_t> {
  auto method_or_id_start = keyword_pos + keyword.size();

  static constexpr llvm::StringLiteral LspEnd = "]]";
  auto keyword_end = content->find("]]", method_or_id_start);
  if (keyword_end == std::string::npos) {
    return ErrorBuilder() << "Missing `" << LspEnd << "` after `" << keyword
                          << "`";
  }

  auto method_or_id_end = content->find(":", method_or_id_start);
  auto extra_content_start = method_or_id_end + 1;
  if (method_or_id_end == std::string::npos || method_or_id_end > keyword_end) {
    method_or_id_end = keyword_end;
    extra_content_start = keyword_end;
  }
  auto method_or_id =
      llvm::StringRef(*content).slice(method_or_id_start, method_or_id_end);

  auto extra_content =
      llvm::StringRef(*content).slice(extra_content_start, keyword_end);
  std::string extra_content_sep;
  if (!extra_content.empty()) {
    extra_content_sep = ",";
    if (!extra_content.starts_with("\n")) {
      extra_content_sep += " ";
    }
  }

  // Form the JSON.
  std::string json = R"({"jsonrpc": "2.0", )";
  if (lsp_call_id) {
    json += llvm::formatv(R"("id": "{0}", )", ++(*lsp_call_id));
  }
  json += llvm::formatv(R"("{0}": "{1}"{2}{3}})", method_or_id_label,
                        method_or_id, extra_content_sep, extra_content);

  // Add the Content-Length header. The `2` accounts for extra newlines.
  auto json_with_header =
      llvm::formatv("Content-Length: {0}\n\n{1}\n", json.size() + 2, json)
          .str();
  // Insert the content.
  content->replace(keyword_pos, keyword_end + 2 - keyword_pos,
                   json_with_header);
  return keyword_pos + json_with_header.size();
}