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 <titovartem@webrtc.org>
Commit-Queue: Danil Chapovalov <danilchap@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#36505}
diff --git a/test/BUILD.gn b/test/BUILD.gn
index e2c9050..1d6b1b8 100644
--- a/test/BUILD.gn
+++ b/test/BUILD.gn
@@ -222,6 +222,7 @@
"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 a080a0c..c9e5ac1 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 @@
<< "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 @@
<< "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 @@
}
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 355f01d..5685c13 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 <functional>
#include <map>
#include <string>
@@ -22,11 +23,13 @@
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<std::string, std::string> key_value_map_;
+ // Unlike std::less<std::string>, std::less<> is transparent and allows
+ // heterogeneous lookup directly with absl::string_view.
+ std::map<std::string, std::string, std::less<>> key_value_map_;
};
} // namespace test
diff --git a/test/field_trial.cc b/test/field_trial.cc
index ae61e0a..3d6c6ac 100644
--- a/test/field_trial.cc
+++ b/test/field_trial.cc
@@ -10,23 +10,21 @@
#include "test/field_trial.h"
-#include <algorithm>
-#include <cstdio>
-#include <cstdlib>
-#include <map>
#include <string>
+#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 076470e..516faa0 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 <map>
#include <string>
+#include "absl/strings/string_view.h"
+
namespace webrtc {
namespace test {
@@ -21,7 +22,7 @@
// 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 3b35c3d..449d5f0 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<std::string, std::string>& key_value_map,
- const std::string& s) {
+void InsertIntoMap(
+ std::map<std::string, std::string, std::less<>>& 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 @@
<< "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 @@
<< "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 @@
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::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 0ecbddc..db90ca3 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 <functional>
#include <map>
#include <memory>
#include <string>
@@ -26,13 +27,13 @@
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 @@
// Only set on root (e.g with parent_ == nullptr).
const ScopedKeyValueConfig* leaf_;
- std::map<std::string, std::string> key_value_map_;
+ // Unlike std::less<std::string>, std::less<> is transparent and allows
+ // heterogeneous lookup directly with absl::string_view.
+ std::map<std::string, std::string, std::less<>> key_value_map_;
std::unique_ptr<ScopedFieldTrials> scoped_field_trials_;
};