Skip to content

Commit

Permalink
Make StringPrintf() calls use constexpr format strings: chrome/
Browse files Browse the repository at this point in the history
Bug: 1371963
Change-Id: I5149a83c19377a4845a0759b632810251d53c74a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4950852
Auto-Submit: Peter Kasting <pkasting@chromium.org>
Commit-Queue: Marc Treib <treib@chromium.org>
Code-Coverage: findit-for-me@appspot.gserviceaccount.com <findit-for-me@appspot.gserviceaccount.com>
Reviewed-by: Marc Treib <treib@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1211371}
  • Loading branch information
pkasting authored and Chromium LUCI CQ committed Oct 18, 2023
1 parent b82305f commit 1a3e2d2
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 51 deletions.
40 changes: 18 additions & 22 deletions chrome/browser/ash/power/cpu_data_collector.cc
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,8 @@ size_t EnsureInVector(const std::string& str,

// Returns true if the |i|-th CPU is online; false otherwise.
bool CpuIsOnline(const int i) {
const std::string online_file_format = base::StringPrintf(
"%s%s", kCpuDataPathBase, kCpuOnlinePathSuffixFormat);
const std::string cpu_online_file = base::StringPrintf(
online_file_format.c_str(), i);
const std::string cpu_online_file =
kCpuDataPathBase + base::StringPrintf(kCpuOnlinePathSuffixFormat, i);
if (!base::PathExists(base::FilePath(cpu_online_file))) {
// If the 'online' status file is missing, then it means that the CPU is
// not hot-pluggable and hence is always online.
Expand Down Expand Up @@ -133,26 +131,26 @@ void SampleCpuIdleData(
} else {
idle_sample.cpu_online = true;

const std::string idle_state_dir_format = base::StringPrintf(
"%s%s", kCpuDataPathBase, kCpuIdleStateDirPathSuffixFormat);
for (int state_count = 0; ; ++state_count) {
std::string idle_state_dir = base::StringPrintf(
idle_state_dir_format.c_str(), cpu, state_count);
std::string idle_state_dir =
kCpuDataPathBase +
base::StringPrintf(kCpuIdleStateDirPathSuffixFormat, cpu,
state_count);
// This insures us from the unlikely case wherein the 'cpuidle_stats'
// kernel module is not loaded. This could happen on a VM.
if (!base::DirectoryExists(base::FilePath(idle_state_dir)))
break;

const std::string name_file_format = base::StringPrintf(
"%s%s", kCpuDataPathBase, kCpuIdleStateNamePathSuffixFormat);
const std::string name_file_path = base::StringPrintf(
name_file_format.c_str(), cpu, state_count);
const std::string name_file_path =
kCpuDataPathBase +
base::StringPrintf(kCpuIdleStateNamePathSuffixFormat, cpu,
state_count);
DCHECK(base::PathExists(base::FilePath(name_file_path)));

const std::string time_file_format = base::StringPrintf(
"%s%s", kCpuDataPathBase, kCpuIdleStateTimePathSuffixFormat);
const std::string time_file_path = base::StringPrintf(
time_file_format.c_str(), cpu, state_count);
const std::string time_file_path =
kCpuDataPathBase +
base::StringPrintf(kCpuIdleStateTimePathSuffixFormat, cpu,
state_count);
DCHECK(base::PathExists(base::FilePath(time_file_path)));

std::string state_name, occupancy_time_string;
Expand Down Expand Up @@ -230,10 +228,9 @@ void SampleCpuFreqData(
} else {
for (int cpu = 0; cpu < cpu_count; ++cpu) {
if ((*freq_samples)[cpu].cpu_online) {
const std::string time_in_state_path_format = base::StringPrintf(
"%s%s", kCpuDataPathBase, kCpuFreqTimeInStatePathSuffixFormat);
const base::FilePath time_in_state_path(
base::StringPrintf(time_in_state_path_format.c_str(), cpu));
kCpuDataPathBase +
base::StringPrintf(kCpuFreqTimeInStatePathSuffixFormat, cpu));
if (base::PathExists(time_in_state_path)) {
if (!CpuDataCollector::ReadCpuFreqTimeInState(
time_in_state_path, cpu_freq_state_names,
Expand All @@ -243,10 +240,9 @@ void SampleCpuFreqData(
}
} else {
freq_samples->clear();
const std::string cpu_freq_stats_path_format = base::StringPrintf(
"%s%s", kCpuDataPathBase, kCpuFreqStatsPathSuffixFormat);
const base::FilePath cpu_freq_stats_path(
base::StringPrintf(cpu_freq_stats_path_format.c_str(), cpu));
kCpuDataPathBase +
base::StringPrintf(kCpuFreqStatsPathSuffixFormat, cpu));
if (!base::PathExists(cpu_freq_stats_path)) {
// If the path to 'stats' folder for a single CPU is missing, then
// current platform does not produce discrete CPU frequency data.
Expand Down
4 changes: 2 additions & 2 deletions chrome/browser/ash/system/timezone_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,8 @@ std::u16string GetTimezoneName(const icu::TimeZone& timezone) {
int min_remainder = minute_offset % 60;
// Some timezones have a non-integral hour offset. So, we need to use hh:mm
// form.
std::string offset_str = base::StringPrintf(offset >= 0 ?
"UTC+%d:%02d" : "UTC-%d:%02d", hour_offset, min_remainder);
std::string offset_str = base::StringPrintf(
"UTC%c%d:%02d", offset >= 0 ? '+' : '-', hour_offset, min_remainder);

// TODO(jungshik): When coming up with a better list of timezones, we also
// have to come up with better 'display' names. One possibility is to list
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,15 +110,15 @@ TEST_F(CrashReporterBreadcrumbObserverTest, MAYBE_ProductDataOverflow) {
// Linux uses Breakpad, which breaks the crash key value up into chunks of 127
// characters each, named <crash key>__1, <crash key>__2, etc. These must be
// summed to determine the total length of the breadcrumbs crash string.
static const std::string kBreakpadNameFormat =
std::string(breadcrumbs::kBreadcrumbsProductDataKey) + "__%d";
int chunk = 1;
size_t chunk_length = 0;
size_t breadcrumbs_crash_string_length = 0;
do {
chunk_length = crash_reporter::GetCrashKeyValue(
base::StringPrintf(kBreakpadNameFormat.c_str(), chunk))
.length();
chunk_length =
crash_reporter::GetCrashKeyValue(
base::StringPrintf("%s__%d",
breadcrumbs::kBreadcrumbsProductDataKey, chunk))
.length();
breadcrumbs_crash_string_length += chunk_length;
chunk++;
} while (chunk_length > 0);
Expand Down
8 changes: 4 additions & 4 deletions chrome/browser/download/download_frame_policy_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,10 @@ class DownloadFramePolicyBrowserTest
)";
static constexpr char kNavDownloadScript[] = "window.location = '%s'";

std::string script = base::StringPrintf(
source == DownloadSource::kAnchorAttribute ? kADownloadScript
: kNavDownloadScript,
file_name.c_str());
std::string script =
source == DownloadSource::kAnchorAttribute
? base::StringPrintf(kADownloadScript, file_name.c_str())
: base::StringPrintf(kNavDownloadScript, file_name.c_str());

if (initiate_with_gesture) {
EXPECT_TRUE(ExecJs(adapter, script));
Expand Down
6 changes: 2 additions & 4 deletions chrome/browser/download/download_target_determiner.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1088,12 +1088,10 @@ DownloadTargetDeterminer::Result
static constexpr char kUnconfirmedFormatSuffix[] = " %d.crdownload";
// Range of the <random> uniquifier.
constexpr int kUnconfirmedUniquifierRange = 1000000;
std::string unconfirmed_format =
l10n_util::GetStringUTF8(IDS_DOWNLOAD_UNCONFIRMED_PREFIX);
unconfirmed_format.append(kUnconfirmedFormatSuffix);

std::string file_name =
base::StringPrintf(unconfirmed_format.c_str(),
l10n_util::GetStringUTF8(IDS_DOWNLOAD_UNCONFIRMED_PREFIX) +
base::StringPrintf(kUnconfirmedFormatSuffix,
base::RandInt(0, kUnconfirmedUniquifierRange));
intermediate_path_ =
local_path_.DirName().Append(base::FilePath::FromUTF8Unsafe(file_name));
Expand Down
7 changes: 4 additions & 3 deletions chrome/browser/policy/extension_policy_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include "base/memory/raw_ptr.h"
#include "base/path_service.h"
#include "base/run_loop.h"
#include "base/strings/stringprintf.h"
#include "base/strings/strcat.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/bind.h"
#include "base/test/gtest_tags.h"
Expand Down Expand Up @@ -322,8 +322,9 @@ class ExtensionPolicyTest : public ExtensionPolicyTestBase {
// Setting the forcelist extension should install extension with ExtensionId
// equal to id.
base::Value::List forcelist;
forcelist.Append(base::StringPrintf(update_url.is_empty() ? "%s" : "%s;%s",
id.c_str(), update_url.spec().c_str()));
forcelist.Append(update_url.is_empty()
? id
: base::StrCat({id, ";", update_url.spec()}));
policies->Set(key::kExtensionInstallForcelist, POLICY_LEVEL_MANDATORY,
POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD,
base::Value(std::move(forcelist)), nullptr);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "chrome/browser/ui/quick_answers/quick_answers_browsertest_base.h"

#include "base/strings/strcat.h"
#include "base/strings/stringprintf.h"
#include "chrome/test/base/chrome_test_utils.h"
#include "chromeos/components/quick_answers/public/cpp/quick_answers_state.h"
Expand All @@ -19,10 +20,6 @@ constexpr char kDataUrlTemplate[] =
"data:text/html,<html><body><span style=\"position: absolute; left: %ipx; "
"top: %ipx;\">%s</body></html>";

constexpr char kDataUrlPasswordFieldTemplate[] =
"data:text/html,<html><body><span style=\"position: absolute; left: %ipx; "
"top: %ipx;\"><input type=\"password\">%s</input></body></html>";

} // namespace

QuickAnswersBrowserTestBase::QuickAnswersBrowserTestBase() = default;
Expand All @@ -39,10 +36,13 @@ void QuickAnswersBrowserTestBase::ShowMenu(
content::WebContents* web_contents =
chrome_test_utils::GetActiveWebContents(this);

std::string data_url = base::StringPrintf(
params.is_password_field ? kDataUrlPasswordFieldTemplate
: kDataUrlTemplate,
params.x, params.y, params.selected_text.c_str());
const std::string text =
params.is_password_field
? base::StrCat(
{"<input type=\"password\">", params.selected_text, "</input>"})
: params.selected_text;
const std::string data_url =
base::StringPrintf(kDataUrlTemplate, params.x, params.y, text.c_str());
ASSERT_TRUE(content::NavigateToURL(web_contents, GURL(data_url)));

content::RenderFrameHost* main_frame = web_contents->GetPrimaryMainFrame();
Expand Down
9 changes: 6 additions & 3 deletions chrome/browser/ui/views/sad_tab_view.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include <string>

#include "base/strings/string_number_conversions.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "build/build_config.h"
Expand Down Expand Up @@ -457,9 +458,11 @@ std::u16string ErrorToString(int error_code) {
break;
default:
// Render small error values as integers, and larger values as hex.
error_string = base::StringPrintf(
(error_code >= 0 && error_code < 65536) ? "%lu" : "0x%08lX",
static_cast<unsigned long>(error_code));
error_string =
(error_code >= 0 && error_code < 65536)
? base::NumberToString(error_code)
: base::StringPrintf("0x%08lX",
static_cast<unsigned long>(error_code));
}

return base::UTF8ToUTF16(error_string);
Expand Down

0 comments on commit 1a3e2d2

Please sign in to comment.