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

Code before:
    if (use_call_id) {
      json.attribute("id", ++lsp_call_id);
    }
    if (parsed_extra_content != nullptr) {
      if (!extra_content_label.empty()) {
        json.attribute(extra_content_label, parsed_extra_content);
      } else {
        for (const auto& [key, value] : *parsed_extra_content.getAsObject()) {
          json.attribute(key, value);
        }
      }
    }
  });

  // Add the Content-Length header. The `2` accounts for extra newlines.
  int content_length = buffer.size() + 2;
  auto json_with_header = llvm::formatv("Content-Length: {0}\n\n{1}\n",
                                        content_length, buffer.TakeStr())
                              .str();
  int keyword_len =
      (body_start.data() + body_end + LspEnd.size()) - keyword.data();
  content.replace(keyword_pos, keyword_len, json_with_header);
  return keyword_pos + json_with_header.size();
}

// Replaces `[[@0xAB]]` with the raw byte with value 0xAB.
static auto ReplaceRawByteKeywordAt(std::string& content, size_t keyword_pos)
    -> ErrorOr<size_t> {
  llvm::StringRef content_at_keyword =
      llvm::StringRef(content).substr(keyword_pos);
  auto [keyword, rest] = content_at_keyword.split("]]");
  if (keyword.empty()) {
    return ErrorBuilder() << "Missing `]]` after " << keyword.take_front(10)
                          << "`";
  }

  unsigned char byte_value;
  if (keyword.substr(std::size("[[@0x") - 1).getAsInteger(16, byte_value)) {
    return ErrorBuilder() << "Invalid raw byte specifier `"
                          << keyword.take_front(10) << "`";
  }

  content.replace(keyword_pos, keyword.size() + 2, 1, byte_value);
  return keyword_pos + 1;
}

// Replaces the keyword at the given position. Returns the position to start a
// find for the next keyword.
static auto ReplaceContentKeywordAt(std::string& content, size_t keyword_pos,
                                    llvm::StringRef test_name, int& lsp_call_id,
                                    llvm::ArrayRef<TestFile::Split> splits)

Review comment (suggestion):
```suggestion
// Replaces `[[@0xAB]]` with the raw byte with value 0xAB. Returns the
// position to start a find for the next keyword.
```

Code after:
    if (use_call_id) {
      json.attribute("id", ++lsp_call_id);
    }
    if (parsed_extra_content != nullptr) {
      if (!extra_content_label.empty()) {
        json.attribute(extra_content_label, parsed_extra_content);
      } else {
        for (const auto& [key, value] : *parsed_extra_content.getAsObject()) {
          json.attribute(key, value);
        }
      }
    }
  });

  // Add the Content-Length header. The `2` accounts for extra newlines.
  int content_length = buffer.size() + 2;
  auto json_with_header = llvm::formatv("Content-Length: {0}\n\n{1}\n",
                                        content_length, buffer.TakeStr())
                              .str();
  size_t keyword_len = rest.data() - keyword.data();
  content.replace(keyword_pos, keyword_len, json_with_header);
  return keyword_pos + json_with_header.size();
}

// Replaces `[[@0xAB]]` with the raw byte with value 0xAB. Returns the position
// to start a find for the next keyword.
static auto ReplaceRawByteKeywordAt(std::string& content, size_t keyword_pos)
    -> ErrorOr<size_t> {
  llvm::StringRef content_at_keyword =
      llvm::StringRef(content).substr(keyword_pos);
  auto [keyword, rest] = content_at_keyword.split("]]");
  if (keyword.size() == content_at_keyword.size()) {
    return ErrorBuilder() << "Missing `]]` after " << keyword.take_front(10)
                          << "`";
  }

  unsigned char byte_value;
  if (keyword.substr(std::size("[[@0x") - 1).getAsInteger(16, byte_value)) {
    return ErrorBuilder() << "Invalid raw byte specifier `"
                          << keyword.take_front(10) << "`";
  }

  content.replace(keyword_pos, keyword.size() + 2, 1, byte_value);
  return keyword_pos + 1;
}

// Replaces the keyword at the given position. Returns the position to start a
// find for the next keyword.
static auto ReplaceContentKeywordAt(std::string& content, size_t keyword_pos,
                                    llvm::StringRef test_name, int& lsp_call_id,
                                    llvm::ArrayRef<TestFile::Split> splits)