From c83d95c8786b9933603f7c2face0c70623065b9c Mon Sep 17 00:00:00 2001 From: Chris Phlipot Date: Sat, 14 Dec 2024 11:33:52 -0800 Subject: [PATCH] tp: use new NextToken() API for battery stats checkin parsing Replace the `splitter.Next() ? splitter.cur_token() : ""` pattern with `splitter.NextToken()` to improve readability without introducing any functional or other changes. Change-Id: Ica3d5d3753339aad680d427fc4396b8690b420c2 --- .../android_battery_stats_reader.cc | 30 ++++++++----------- .../android_dumpstate_event_parser_impl.cc | 4 +-- 2 files changed, 14 insertions(+), 20 deletions(-) diff --git a/src/trace_processor/importers/android_bugreport/android_battery_stats_reader.cc b/src/trace_processor/importers/android_bugreport/android_battery_stats_reader.cc index 7ec0d6dace..a719a15f50 100644 --- a/src/trace_processor/importers/android_bugreport/android_battery_stats_reader.cc +++ b/src/trace_processor/importers/android_bugreport/android_battery_stats_reader.cc @@ -63,19 +63,17 @@ util::Status AndroidBatteryStatsReader::ParseLine(base::StringView line) { // consume the legacy version number which we expect to be at the start of // every line. - if (!splitter.Next() || splitter.cur_token() != "9") { + if (splitter.NextToken() != "9") { return base::ErrStatus("Unexpected start of battery stats checkin line"); } - const base::StringView possible_event_type = - splitter.Next() ? splitter.cur_token() : ""; + const base::StringView possible_event_type = splitter.NextToken(); if (possible_event_type == "hsp") { - ASSIGN_OR_RETURN( - int64_t index, - StringToStatusOrInt64(splitter.Next() ? splitter.cur_token() : "")); + ASSIGN_OR_RETURN(int64_t index, + StringToStatusOrInt64(splitter.NextToken())); const std::optional possible_uid = - base::StringViewToInt32(splitter.Next() ? splitter.cur_token() : ""); + base::StringViewToInt32(splitter.NextToken()); // the next element is quoted and can contain commas. Instead of // implementing general logic to parse quoted CSV elements just grab the // rest of the line, which is possible since this element should be the @@ -91,8 +89,7 @@ util::Status AndroidBatteryStatsReader::ParseLine(base::StringView line) { hsp_string.ToStdString()); } else if (possible_event_type == "h") { const base::StringView time_adjustment_marker = ":TIME:"; - const base::StringView possible_timestamp = - splitter.Next() ? splitter.cur_token() : ""; + const base::StringView possible_timestamp = splitter.NextToken(); size_t time_marker_index = possible_timestamp.find(time_adjustment_marker); if (time_marker_index != base::StringView::npos) { // Special case timestamp adjustment event. @@ -110,21 +107,18 @@ util::Status AndroidBatteryStatsReader::ParseLine(base::StringView line) { ASSIGN_OR_RETURN(int64_t parsed_timestamp_delta, StringToStatusOrInt64(possible_timestamp)); current_timestamp_ms_ += parsed_timestamp_delta; - for (base::StringView item = splitter.Next() ? splitter.cur_token() : ""; - !item.empty(); item = splitter.Next() ? splitter.cur_token() : "") { + for (base::StringView item = splitter.NextToken(); !item.empty(); + item = splitter.NextToken()) { RETURN_IF_ERROR(ProcessBatteryStatsHistoryEvent(item)); } } } else if (possible_event_type == "0") { - const base::StringView metadata_type = - (splitter.Next() ? base::StringView(splitter.cur_token()) : ""); + const base::StringView metadata_type = splitter.NextToken(); if (metadata_type == "i") { - const base::StringView info_type = - (splitter.Next() ? base::StringView(splitter.cur_token()) : ""); + const base::StringView info_type = splitter.NextToken(); if (info_type == "vers") { - ASSIGN_OR_RETURN( - int64_t battery_stats_version, - StringToStatusOrInt64(splitter.Next() ? splitter.cur_token() : "")); + ASSIGN_OR_RETURN(int64_t battery_stats_version, + StringToStatusOrInt64(splitter.NextToken())); AndroidBatteryStatsHistoryStringTracker::GetOrCreate(context_) ->battery_stats_version( static_cast(battery_stats_version)); diff --git a/src/trace_processor/importers/android_bugreport/android_dumpstate_event_parser_impl.cc b/src/trace_processor/importers/android_bugreport/android_dumpstate_event_parser_impl.cc index 85752c4839..22ed8e675a 100644 --- a/src/trace_processor/importers/android_bugreport/android_dumpstate_event_parser_impl.cc +++ b/src/trace_processor/importers/android_bugreport/android_dumpstate_event_parser_impl.cc @@ -227,8 +227,8 @@ base::Status AndroidDumpstateEventParserImpl::ProcessBatteryStatsHistoryItem( base::StringViewSplitter splitter(base::StringView(raw_event), '='); TokenizedBatteryStatsHistoryItem item; item.ts = ts; - item.key = splitter.Next() ? splitter.cur_token() : ""; - item.value = splitter.Next() ? splitter.cur_token() : ""; + item.key = splitter.NextToken(); + item.value = splitter.NextToken(); item.prefix = ""; if (item.key.size() > 0 && (item.key.at(0) == '+' || item.key.at(0) == '-')) { item.prefix = item.key.substr(0, 1);