From 385b6c54603510c2f1339ec7a8e3882bba6112df Mon Sep 17 00:00:00 2001 From: Danil Chapovalov Date: Fri, 8 Apr 2022 16:01:50 +0200 Subject: [PATCH] Adopt absl::string_view in field trial test helpers Bug: webrtc:13579 Change-Id: Ie16b2f1cf5288cf795ea6d40f4b3a37f76f00f76 Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/258422 Reviewed-by: Artem Titov Commit-Queue: Danil Chapovalov Cr-Commit-Position: refs/heads/main@{#36505} --- test/BUILD.gn | 1 + test/explicit_key_value_config.cc | 9 +++++---- test/explicit_key_value_config.h | 7 +++++-- test/field_trial.cc | 16 +++++++--------- test/field_trial.h | 5 +++-- test/scoped_key_value_config.cc | 17 +++++++++-------- test/scoped_key_value_config.h | 11 +++++++---- 7 files changed, 37 insertions(+), 29 deletions(-) diff --git a/test/BUILD.gn b/test/BUILD.gn index e2c9050703..1d6b1b8738 100644 --- a/test/BUILD.gn +++ b/test/BUILD.gn @@ -222,6 +222,7 @@ rtc_library("field_trial") { "field_trial.h", ] + absl_deps = [ "//third_party/abseil-cpp/absl/strings" ] deps = [ "../rtc_base:checks", "../system_wrappers:field_trial", diff --git a/test/explicit_key_value_config.cc b/test/explicit_key_value_config.cc index a080a0c592..c9e5ac1c28 100644 --- a/test/explicit_key_value_config.cc +++ b/test/explicit_key_value_config.cc @@ -10,13 +10,14 @@ #include "test/explicit_key_value_config.h" +#include "absl/strings/string_view.h" #include "api/field_trials_view.h" #include "rtc_base/checks.h" namespace webrtc { namespace test { -ExplicitKeyValueConfig::ExplicitKeyValueConfig(const std::string& s) { +ExplicitKeyValueConfig::ExplicitKeyValueConfig(absl::string_view s) { std::string::size_type field_start = 0; while (field_start < s.size()) { std::string::size_type separator_pos = s.find('/', field_start); @@ -24,7 +25,7 @@ ExplicitKeyValueConfig::ExplicitKeyValueConfig(const std::string& s) { << "Missing separator '/' after field trial key."; RTC_CHECK_GT(separator_pos, field_start) << "Field trial key cannot be empty."; - std::string key = s.substr(field_start, separator_pos - field_start); + std::string key(s.substr(field_start, separator_pos - field_start)); field_start = separator_pos + 1; RTC_CHECK_LT(field_start, s.size()) @@ -34,7 +35,7 @@ ExplicitKeyValueConfig::ExplicitKeyValueConfig(const std::string& s) { << "Missing terminating '/' in field trial string."; RTC_CHECK_GT(separator_pos, field_start) << "Field trial value cannot be empty."; - std::string value = s.substr(field_start, separator_pos - field_start); + std::string value(s.substr(field_start, separator_pos - field_start)); field_start = separator_pos + 1; key_value_map_[key] = value; @@ -46,7 +47,7 @@ ExplicitKeyValueConfig::ExplicitKeyValueConfig(const std::string& s) { } std::string ExplicitKeyValueConfig::Lookup(absl::string_view key) const { - auto it = key_value_map_.find(std::string(key)); + auto it = key_value_map_.find(key); if (it != key_value_map_.end()) return it->second; return ""; diff --git a/test/explicit_key_value_config.h b/test/explicit_key_value_config.h index 355f01d48f..5685c13604 100644 --- a/test/explicit_key_value_config.h +++ b/test/explicit_key_value_config.h @@ -11,6 +11,7 @@ #ifndef TEST_EXPLICIT_KEY_VALUE_CONFIG_H_ #define TEST_EXPLICIT_KEY_VALUE_CONFIG_H_ +#include #include #include @@ -22,11 +23,13 @@ namespace test { class ExplicitKeyValueConfig : public FieldTrialsView { public: - explicit ExplicitKeyValueConfig(const std::string& s); + explicit ExplicitKeyValueConfig(absl::string_view s); std::string Lookup(absl::string_view key) const override; private: - std::map key_value_map_; + // Unlike std::less, std::less<> is transparent and allows + // heterogeneous lookup directly with absl::string_view. + std::map> key_value_map_; }; } // namespace test diff --git a/test/field_trial.cc b/test/field_trial.cc index ae61e0a055..3d6c6ac617 100644 --- a/test/field_trial.cc +++ b/test/field_trial.cc @@ -10,23 +10,21 @@ #include "test/field_trial.h" -#include -#include -#include -#include #include +#include "absl/strings/string_view.h" #include "rtc_base/checks.h" #include "system_wrappers/include/field_trial.h" namespace webrtc { namespace test { -ScopedFieldTrials::ScopedFieldTrials(const std::string& config) - : previous_field_trials_(webrtc::field_trial::GetFieldTrialString()) { - RTC_CHECK(webrtc::field_trial::FieldTrialsStringIsValid(config.c_str())) - << "Invalid field trials string: " << config; - current_field_trials_ = config; +ScopedFieldTrials::ScopedFieldTrials(absl::string_view config) + : current_field_trials_(config), + previous_field_trials_(webrtc::field_trial::GetFieldTrialString()) { + RTC_CHECK(webrtc::field_trial::FieldTrialsStringIsValid( + current_field_trials_.c_str())) + << "Invalid field trials string: " << current_field_trials_; webrtc::field_trial::InitFieldTrialsFromString(current_field_trials_.c_str()); } diff --git a/test/field_trial.h b/test/field_trial.h index 076470e8cc..516faa0513 100644 --- a/test/field_trial.h +++ b/test/field_trial.h @@ -11,9 +11,10 @@ #ifndef TEST_FIELD_TRIAL_H_ #define TEST_FIELD_TRIAL_H_ -#include #include +#include "absl/strings/string_view.h" + namespace webrtc { namespace test { @@ -21,7 +22,7 @@ namespace test { // After this class goes out of scope previous field trials will be restored. class ScopedFieldTrials { public: - explicit ScopedFieldTrials(const std::string& config); + explicit ScopedFieldTrials(absl::string_view config); ScopedFieldTrials(const ScopedFieldTrials&) = delete; ScopedFieldTrials& operator=(const ScopedFieldTrials&) = delete; ~ScopedFieldTrials(); diff --git a/test/scoped_key_value_config.cc b/test/scoped_key_value_config.cc index 3b35c3d92b..449d5f0722 100644 --- a/test/scoped_key_value_config.cc +++ b/test/scoped_key_value_config.cc @@ -18,8 +18,9 @@ namespace { // This part is copied from system_wrappers/field_trial.cc. -void InsertIntoMap(std::map& key_value_map, - const std::string& s) { +void InsertIntoMap( + std::map>& key_value_map, + absl::string_view s) { std::string::size_type field_start = 0; while (field_start < s.size()) { std::string::size_type separator_pos = s.find('/', field_start); @@ -27,7 +28,7 @@ void InsertIntoMap(std::map& key_value_map, << "Missing separator '/' after field trial key."; RTC_CHECK_GT(separator_pos, field_start) << "Field trial key cannot be empty."; - std::string key = s.substr(field_start, separator_pos - field_start); + std::string key(s.substr(field_start, separator_pos - field_start)); field_start = separator_pos + 1; RTC_CHECK_LT(field_start, s.size()) @@ -37,7 +38,7 @@ void InsertIntoMap(std::map& key_value_map, << "Missing terminating '/' in field trial string."; RTC_CHECK_GT(separator_pos, field_start) << "Field trial value cannot be empty."; - std::string value = s.substr(field_start, separator_pos - field_start); + std::string value(s.substr(field_start, separator_pos - field_start)); field_start = separator_pos + 1; key_value_map[key] = value; @@ -56,15 +57,15 @@ namespace test { ScopedKeyValueConfig::ScopedKeyValueConfig() : ScopedKeyValueConfig(nullptr, "") {} -ScopedKeyValueConfig::ScopedKeyValueConfig(const std::string& s) +ScopedKeyValueConfig::ScopedKeyValueConfig(absl::string_view s) : ScopedKeyValueConfig(nullptr, s) {} ScopedKeyValueConfig::ScopedKeyValueConfig(ScopedKeyValueConfig& parent, - const std::string& s) + absl::string_view s) : ScopedKeyValueConfig(&parent, s) {} ScopedKeyValueConfig::ScopedKeyValueConfig(ScopedKeyValueConfig* parent, - const std::string& s) + absl::string_view s) : parent_(parent), leaf_(nullptr) { InsertIntoMap(key_value_map_, s); @@ -105,7 +106,7 @@ std::string ScopedKeyValueConfig::Lookup(absl::string_view key) const { } std::string ScopedKeyValueConfig::LookupRecurse(absl::string_view key) const { - auto it = key_value_map_.find(std::string(key)); + auto it = key_value_map_.find(key); if (it != key_value_map_.end()) return it->second; diff --git a/test/scoped_key_value_config.h b/test/scoped_key_value_config.h index 0ecbddcfbf..db90ca3533 100644 --- a/test/scoped_key_value_config.h +++ b/test/scoped_key_value_config.h @@ -11,6 +11,7 @@ #ifndef TEST_SCOPED_KEY_VALUE_CONFIG_H_ #define TEST_SCOPED_KEY_VALUE_CONFIG_H_ +#include #include #include #include @@ -26,13 +27,13 @@ class ScopedKeyValueConfig : public FieldTrialsView { public: virtual ~ScopedKeyValueConfig(); ScopedKeyValueConfig(); - explicit ScopedKeyValueConfig(const std::string& s); - ScopedKeyValueConfig(ScopedKeyValueConfig& parent, const std::string& s); + explicit ScopedKeyValueConfig(absl::string_view s); + ScopedKeyValueConfig(ScopedKeyValueConfig& parent, absl::string_view s); std::string Lookup(absl::string_view key) const override; private: - ScopedKeyValueConfig(ScopedKeyValueConfig* parent, const std::string& s); + ScopedKeyValueConfig(ScopedKeyValueConfig* parent, absl::string_view s); ScopedKeyValueConfig* GetRoot(ScopedKeyValueConfig* n); std::string LookupRecurse(absl::string_view key) const; @@ -42,7 +43,9 @@ class ScopedKeyValueConfig : public FieldTrialsView { // Only set on root (e.g with parent_ == nullptr). const ScopedKeyValueConfig* leaf_; - std::map key_value_map_; + // Unlike std::less, std::less<> is transparent and allows + // heterogeneous lookup directly with absl::string_view. + std::map> key_value_map_; std::unique_ptr scoped_field_trials_; };